Same-origin policy

Same-origin policy (SOP) — security policy governing such as Javascript downloaded from one origin can integrate with resources from another origin.


User of web browser uses his Gmail mail in one tab of web browser, and opens another website in the second tab. If the Same-origin policy did not exist, than then website could send Ajax request to read mail to Gmail and receive the contents of the letters from the response, since the user's Cookie from Gmail is sent along with the request.

Same-origin policy restricts Javascript access to one downloaded from one origin to:

  • Reading Ajax responses via XMLHttpRequest and fetch from another origin
  • Reading and writing the Document Object Model (DOM) of another origin
  • Reading and writing stored data (Cookie, session & local storage) of another origin


Origin is the triple

  • protocol
  • domain
  • port
Origin = schema://host:port

The same origins are origins which coincide protocol, domain and port.

An example for determination of identical origins with
URL Result Reason Same origins Same origins Different origins Different protocols Different origins Different ports Different origins Different domains

Reading Ajax responses via XMLHttpRequest and fetch

The user entered Facebook on one tab of the web browser, and opened another site on the second tab This website is trying to steal personal correspondence from Facebook by making the following Ajax request for reading private messages

    url: "",
    type: "GET",
    xhrFields: { withCredentials: true }
}, function( data ) {

If you copy this code into the web browser console, the following error occurs XMLHttpRequest cannot load No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin '' is therefore not allowed access.

This is Same-origin policy in action. The browser does not allow to read the request response to another origin. But the request was successfully sent to Facebook from the user's Cookie, and Facebook sent back private messages in response.

Reading and writing Document Object Model

The web browser user is authorized on the Wikipedia site and switches to another website The attacker builds on his site iframe on Wikipedia and tries to read the contents of the DOM from the iframe.

<iframe src="" id="victimIframe"></iframe>
    var iframeContent = document.getElementById("victimIframe").contentDocument;

This code will cause an error Uncaught DOMException: Failed to read the 'contentDocument' property from 'HTMLIFrameElement': Blocked a frame with origin "" from accessing a cross-origin frame.

Since web browser does not allow DOM reading and writing to an origin other than origin iframe (windows).

Reading and writing stored data from another origin

Each origin gets its own instance of localStorage and IndexedDB. Javascript from one origin can not read or write to the repository of another origin. Cookies use a different origin definition. A domain can set a cookie for its domain and any subdomain, the protocol and port do not matter.


Same-origin policy does not forbid you to connect from other origins for:

  • Javascript across <script src=""></script>. Connected Javascript will have a origin equal to an origin of the page that connects it. It means that any library connected through the <script> tag gains access to the resources of the current origin, including the content of the page and Cookie.
  • CSS across <link rel="stylesheet" href="">.
  • The content of the tags <img>, <video>, <audio>, <object>, <embed>, <applet> can also be from another origin.

  Bypass methods →