The concept of CORS and should I enforce an Origin header?

api, cors, javascript, rest, web-services

Solution

The point of CORS is to prevent (or allow) Javascript running on a different domain from sending AJAX requests to your API and using the user's authenticated session cookie.

CORS cannot replace proper authentication; all does is prevent the browser from acting as a confused deputy against your existing authentication scheme.

Problem

As far as I understand CORS cannot exactly protect you in the way that you can really be sure who the caller is. Because the caller can send any ORIGIN header he wants. Actually I read somewhere you cannot set the origin header via javascript as it is a restricted header - but I'm not quite sure of that. Anyway.. if you were to implement your own HttpClient you could easily forge your origin header and therefore consume services which you are not supposed to consume. Secondly if no Origin header is specified the request works as well. For example I use Google Chrome's Postman Extension and it doesn't send any origin headers. In fact if you try to add one manually it doesn't send it over the wire. Therefore... - ...question 1 is: Should my application deny requests without any Origin header? And... - ...question 2: How exactly does make CORS my REST service more secure?

Original source