Skip to content
IT glossary
SecurityUpdated December 3, 2026

Same-origin policy

Browsers use this rule to stop a script on one page from reading another origin’s data by default. An origin is defined by the URL’s scheme, hostname, and port.

Also known as: политика одного источника

Why browsers need it

Being signed in to your email in one tab should not let a script in another tab read your inbox. The same-origin policy (SOP) sets a boundary between pages: a script can normally inspect documents and access browser storage belonging to its own origin, but it cannot freely access those belonging to another origin.

What counts as the same origin?

For HTTP and HTTPS URLs, compare three things: the scheme, the hostname, and the effective port. All three must match. Paths, query strings, and fragments do not matter. Take https://app.example.com/dashboard as the starting point:

  • https://app.example.com:443/settings is same-origin. The explicit port matches the default HTTPS port.
  • https://app.example.com:8443/settings is cross-origin because the port differs.
  • https://api.example.com/settings is cross-origin because the hostname differs. Sharing a parent domain does not make two URLs same-origin.
  • http://app.example.com/dashboard is cross-origin because the scheme differs.

Loading something is not the same as reading it

A page can often display a cross-origin image or embed a cross-origin document in an iframe. That does not mean its JavaScript can inspect the frame’s DOM or read pixel data from the image through a canvas. The frame and its parent can deliberately exchange messages with postMessage; the receiver should check where a message came from before trusting it.

Example: a frontend calling a separate API

Suppose a page at https://app.example.com uses fetch to load https://api.example.com/profile. The browser treats this as cross-origin. The API can use CORS to allow the page to read the response, for example by returning Access-Control-Allow-Origin: https://app.example.com and satisfying any other applicable CORS checks. Without permission, an ordinary GET can still reach the server even though the calling script cannot read the response. Other requests require a preflight: if that check fails, the browser does not send the actual request.

How it comes up in interviews

A common debugging prompt is: “Why does the API work in curl but fail in the browser?” The command-line client does not enforce the browser’s same-origin policy. Another common question is whether SOP prevents CSRF. It does not do so on its own: a browser can send certain cross-origin requests without letting the initiating page read the response. Keep the responsibilities separate: the browser controls what the script can read, the server authorizes requests, and cookie-authenticated state changes need their own CSRF defenses.