I've been reading up on CORS
and how it works, but I'm finding a lot of things confusing. For example, there are lots of details about things like
User
Joe
is using browserBrowserX
to get data fromsite.com
, which in turn sends a request tospot.com
. To allow this,spot
has special headers... yada yada yada
Without much background, I don't understand why websites wouldn't let requests from some places. I mean, they exist to serve responses to requests, don't they? Why would certain people's of requests not be allowed?
It would really appreciate a nice explanation (or a link to one) of the problem that CORS
is made to solve.
So the question is,
What is the problem CORS
is solving?
The default behavior of web browsers that initiate requests from a page via JavaScript (AKA AJAX) is that they follow the same-origin policy. This means that requests can only be made via AJAX to the same domain (or sub domain). Requests to an entirely different domain will fail.
This restriction exists because requests made at other domains by your browser would carry along your cookies which often means you'd be logged in to the other site. So, without same-origin, any site could host JavaScript that called logout on stackoverflow.com for example, and it would log you out. Now imagine the complications when we talk about social networks, banking sites, etc.
So, all browsers simply restrict script-based network calls to their own domain to make it simple and safe.
There are some known work-arounds in place (such as JSONP which doesn't include cookies in the request), but these are not a permanent solution.
CORS allows these cross-domain requests to happen, but only when each side opts into CORS support.
There are security and privacy reasons for not allowing requests from anywhere. If you visited my website, you wouldn't want my code to make requests to Facebook, reddit, your bank, eBay, etc. from your browser using your cookies, right? My site would then be able to make posts, read information, place orders, etc. on your behalf. Or on my behalf with your accounts.
First, let's talk about the same origin policy. I'll quote from a previous answer of mine:
(Note that I've said "credential-restricted content", but it can also be topology-restricted content when a website is only visible to certain IP addresses.)
Sometimes, however, it's not
evil.com
trying to peek into your inbox. Sometimes, it's just a helpful website (say,http://goodsite.foo
) trying to use a public API from another origin (say,http://api.example.com
). The programmers who worked hard onapi.example.com
want all origins to access their site's contents freely. In that case, the API server atapi.example.com
can use CORS headers to allowgoodsite.foo
(or any other requesting origin) to access its API responses.So, in sum, we assume by default that cross-origin access is a bad thing (think of someone trying to read your inbox), but there are cases where it's a good thing (think of a website trying to access a public API). CORS allows the good case to happen when the requested site wants it to happen.