What Is the Same-Origin Policy?

The same-origin policy is a browser security mechanism that restricts how a script from one origin can interact with resources from a different origin, preve...

Published November 16, 2026

The same-origin policy is a browser security rule that stops a page from reading data from a different origin (scheme + host + port). It prevents a malicious site from reading your email or bank pages in other tabs.

The concept

Two URLs share an origin only if protocol, domain and port all match. https://a.com and https://a.com:8443 are different origins.

Pages can still send cross-origin requests (forms, images, scripts), but scripts cannot read the responses unless the server opts in with CORS.

Same or different origin?

https://example.com/a vs https://example.com/b same
https://example.com vs http://example.com different (scheme)
https://example.com vs https://api.example.com different (host)
https://example.com vs https://example.com:8443 different (port)

Practical examples

Blocked read

fetch('https://other.com/data').then(r => r.json())  // blocked unless other.com sends CORS headers

Allow with CORS

Access-Control-Allow-Origin: https://example.com

Cross-window messaging

otherWindow.postMessage('hi', 'https://other.com')
window.addEventListener('message', e => { if (e.origin !== 'https://other.com') return })

Common mistakes

  • Thinking the policy blocks sending requests (it blocks reading responses).
  • Allowing * origins on authenticated APIs.
  • Not checking event.origin in postMessage handlers.

Key takeaways

  • Origin = scheme + host + port.
  • Blocks cross-origin reads.
  • CORS is the controlled exception.

FAQ

Does it apply to images and scripts?

They can be loaded cross-origin but their contents are not readable by scripts.

Is it the same as CORS?

CORS is the mechanism for relaxing it.

More General articles