Much of this is taken from the
same origin and
CORS
articles at Mozilla, which provide much more information.
- A web page is built from many resources of different types: HTML,
images, CSS, Javascript, etc. These are loaded from various locations.
- The base page loads these resources by URL, which means they could be
anywhere.
- In classic web, this no big deal. But as the web is used for more
things, this starts to create problems. One in particular is
called a Cross-Site Request Forgery (CSRF).
- When a browser sends a request, all the cookies go along, so the
the request essentially carries all current credentials.
- When a user is logged in to an account, any request from the browser
will be treated as authorized.
- If Joe, while logged into site bank, visits some other site,
not-bank, the relevant cookies are not sent to not-bank.
- But some Javascript on the non-bank page
could build and send a request to bank.
- Bill, who either works at non-bank, or just hacked it, could then
use Javascript to construct and send a request to bank that
transfers $1000 from Joe to Bill.
- Since it's going to bank, it brings all the cookies and bank will treat it
as authorized.
- To help limit this, browsers implement a Same-Origin Policy.
- A page (or other resource) is limited in its access to resources
from different origins.
- The Javascript at origin not-bank would be forbidden
to fetch a page with origin bank.
- An origin is the triple of protocol, port and host. (But not path.)
- Not as simple as it sounds.
- Pages created by Javascript generally inherit the origin of the script.
- Special URLs like about:, and file: URLs present some subtleties.
- Javascript can adjust its own origin by removing subdomains (so
foobar.fred.com can become fred.com. (Apparently,
this capability is deprecated.)
- Different types of requests are restricted differently.
- Cross-origin linking, including form submission,
and embedding (e.g., images)
is generally allowed.
- XMLHttpRequest (and the newer Fetch) are restricted. (The origin
of the request is the origin of the Javascript, wherever it's data
actually came from.)
- Full rules are complicated.
- Sometimes you want to allow a cross-origin transaction which the
policy would forbid.
Third-party services: authentication, payment, etc.
- Cross-Origin Resource Sharing (CORS) is an HTTP extension to control
such access.
- In the simplest form, the foreign server sends the header
Access-Control-Allow-Origin: * to permit the loading by the
foreign site.
- Alternatively, the header may give a list of allowed origins.
- There is much more to this, which I mostly don't understand just
now.