Skip to content
BENIBIX Learn

Same-Origin Policy vs CORS

People think CORS is a lock that blocks requests, and it is the other way round. The same-origin policy is the lock: it stops one site from reading another site's replies. CORS is a header a server sends to open that lock for sites it names. So a CORS error means the server did not give your site a key.

Development🔐 Security

Same-Origin Policy (SOP)

A browser rule. It stops one site from reading what another site sends.

The same-origin policy is a browser rule that keeps sites apart. An origin is made of three parts of an address. They are the scheme, the host and the port. A page can freely read data from its own origin. It cannot read replies from another origin unless that site says yes. This stops a bad site from reading your email or bank pages in another tab.

Example
Jack is logged in to his bank. A bad page in another tab tries to fetch his balance, but the browser will not let it read the reply.
Why it matters
It is the wall that keeps your open tabs from spying on each other. CORS is the door a site can open in it on purpose.
Full entry
Development🔐 Security

Cross-Origin Resource Sharing (CORS)

A browser rule. It decides if a page on one site may read another site's data.

CORS is a browser rule that says if a page may read another site's data. By default, a browser stops a page from reading replies from another site. The server can relax that with a header that names the sites it trusts. If the header is missing or wrong, the browser shows a CORS error. It protects users. It does not protect the server from scripts or tools.

Example
Jack's shop page calls his API on a different address. The browser blocks it until the API sends a header that names the shop.
Why it matters
Most web apps that call their own API from another address meet CORS on day one.
Full entry

In one line

Same-Origin Policy (SOP)
A browser rule. It stops one site from reading what another site sends.
Cross-Origin Resource Sharing (CORS)
A browser rule. It decides if a page on one site may read another site's data.