The same-origin policy is a critical security mechanism that restricts how a document or script loaded by one origin can interact with a resource from another origin.
It helps isolate potentially malicious documents, reducing possible attack vectors. For example, it prevents a malicious website on the Internet from running JS in a browser to read data from a third-party webmail service (which the user is signed into) or a company intranet (which is protected from direct access by the attacker by not having a public IP address) and relaying that data to the attacker.
Two URLs have the same origin if the protocol, port (if specified), and host are the same for both. You may see this referenced as the "scheme/host/port tuple", or just "tuple". (A "tuple" is a set of items that together comprise a whole — a generic form for double/triple/quadruple/quintuple/etc.)
The following table gives examples of origin comparisons with the URL
|Only the path differs
|Only the path differs
|Different port (
http:// is port 80 by default)
Scripts executed from pages with an
about:blank is often used as a URL of new, empty popup windows into which the parent script writes content (e.g. via the
data: URLs get a new, empty, security context.
Modern browsers usually treat the origin of files loaded using the
file:/// schema as opaque origins.
What this means is that if a file includes other files from the same folder (say), they are not assumed to come from the same origin, and may trigger CORS errors.
Note that the URL specification states that the origin of files is implementation-dependent, and some browsers may treat files in the same directory or subdirectory as same-origin even though this has security implications.
Warning: The approach described here (using the
document.domain setter) is deprecated because it undermines the security protections provided by the same origin policy, and complicates the origin model in browsers, leading to interoperability problems and security bugs.
A page may change its own origin, with some limitations. A script can set the value of
document.domain to its current domain or a superdomain of its current domain. If set to a superdomain of the current domain, the shorter superdomain is used for same-origin checks.
For example, assume a script from the document at
http://store.company.com/dir/other.html executes the following:
document.domain = "company.com";
Afterward, the page can pass the same-origin check with
http://company.com/dir/page.html sets its
document.domain to "
company.com" to indicate that it wishes to allow that - see
document.domain for more). However,
company.com could not set
othercompany.com, since that is not a superdomain of
The port number is checked separately by the browser. Any call to
document.domain = document.domain, causes the port number to be overwritten with
null. Therefore, one cannot make
company.com:8080 talk to
company.com by only setting
document.domain = "company.com" in the first. It has to be set in both so their port numbers are both
The mechanism has some limitations. For example, it will throw a "
DOMException if the
Permissions-Policy is enabled or the document is in a sandboxed
<iframe>, and changing the origin in this way does not affect the origin checks used by many Web APIs (e.g.
SharedWorker). A more exhaustive list of failure cases can be found in Document.domain > Failures.
Note: When using
document.domain to allow a subdomain to access its parent, you need to set
document.domain to the same value in both the parent domain and the subdomain. This is necessary even if doing so is setting the parent domain back to its original value. Failure to do this may result in permission errors.
- Cross-origin writes are typically allowed. Examples are links, redirects, and form submissions. Some HTTP requests require preflight.
- Cross-origin embedding is typically allowed. (Examples are listed below.)
- Cross-origin reads are typically disallowed, but read access is often leaked by embedding. For example, you can read the dimensions of an embedded image, the actions of an embedded script, or the availability of an embedded resource.
Here are some examples of resources which may be embedded cross-origin:
<script src="…"></script>. Error details for syntax errors are only available for same-origin scripts.
- CSS applied with
<link rel="stylesheet" href="…">. Due to the relaxed syntax rules of CSS, cross-origin CSS requires a correct
Content-Typeheader. Browsers block stylesheet loads if it is a cross-origin load where the MIME type is incorrect and the resource does not start with a valid CSS construct.
- Images displayed by
- Media played by
- External resources embedded with
- Fonts applied with
@font-face. Some browsers allow cross-origin fonts, others require same-origin.
- Anything embedded by
<iframe>. Sites can use the
X-Frame-Optionsheader to prevent cross-origin framing.
- To prevent cross-origin writes, check an unguessable token in the request — known as a Cross-Site Request Forgery (CSRF) token. You must prevent cross-origin reads of pages that require this token.
- To prevent cross-origin reads of a resource, ensure that it is not embeddable. It is often necessary to prevent embedding because embedding a resource always leaks some information about it.
- To prevent cross-origin embeds, ensure that your resource cannot be interpreted as one of the embeddable formats listed above. Browsers may not respect the
Content-Typeheader. For example, if you point a
window.opener allow documents to directly reference each other. When two documents do not have the same origin, these references provide very limited access to
Location objects, as described in the next two sections.
To communicate between documents from different origins, use
Specification: HTML Living Standard § Cross-origin objects.
The following cross-origin access to these
Window properties is allowed:
Some browsers allow access to more properties than the above.
Cookies use a separate definition of origins. A page can set a cookie for its own domain or any parent domain, as long as the parent domain is not a public suffix. Firefox and Chrome use the Public Suffix List to determine if a domain is a public suffix. When you set a cookie, you can limit its availability using the
HttpOnly flags. When you read a cookie, you cannot see from where it was set. Even if you use only secure https connections, any cookie you see may have been set using an insecure connection.