Written by: Mio Kanzaki(Security Analyst (AI))Reviewed by: 千葉圭介Updated
The same-origin policy is a browser rule that stops one site from reading data from another. A browser limits how far a page from one site can touch the content of another site, and that limit keeps damage small. RFC 6454 from the IETF says that browsers and similar software restrict the ways content from one origin may interact with content from another origin.
How does the same-origin policy work?
Think of apartments where each unit has its own key. Even in the same building, your key does not open another unit. The browser tells these units apart by "origin".
RFC 6454 defines an origin as a triple of scheme (http or https), host name and port number. If all three match, the origins are the same. If even one differs, they are different. The RFC states that treating a scheme difference as a difference is essential for security.
Reading the information of another origin is generally forbidden. Running a script from another origin, displaying its images and applying its style sheets are possible. MDN likewise explains that writing to and embedding another origin is generally allowed, while reading is generally not.
CORS is the mechanism by which a server declares permission and relaxes this limit. If you are unsure how permission is granted, read the page on CORS misconfiguration. How your pages appear from outside can be viewed in the free URL check.
Why does the same-origin policy matter?
If a malicious page could read the content of another site where you are logged in, member information would leak. The same-origin policy is the first wall that the browser raises by default.
This rule does not stop the sending of data itself. Writing and embedding are allowed, so protection against unwanted submissions is handled separately as CSRF defense. What the site operator can do is avoid widening CORS permission too far, and grant it only to the pages that need it.
Related terms
- CORS: a mechanism that permits data use from another site
- CSRF: an attack that makes a logged-in user perform an action they did not intend
- XSS: an attack in which a script planted in a site runs in a visitor's browser
- SameSite attribute: a setting that limits cookie sending from other sites
Frequently asked questions
Is a request from an https page to the same host over http the same origin?
No, it is a different origin. RFC 6454 treats two origins as the same only when scheme, host and port all match, and it calls treating a scheme difference as a difference essential to security.
With the same-origin policy in place, can CSRF still happen?
It can. MDN explains that writing to another origin is generally allowed. Protection against unwanted submissions is done separately, as CSRF defense such as checking a token.
Next step
Examples of permission granted too widely are collected on the page about CORS misconfiguration. What your own site shows from outside can be seen in the free URL check. To review your own configuration with us, contact us.
Frequently asked questions
Is a request from an https page to the same host over http the same origin?
No, it is a different origin. RFC 6454 treats two origins as the same only when scheme, host and port all match, and it calls treating a scheme difference as a difference essential to security.
With the same-origin policy in place, can CSRF still happen?
It can. MDN explains that writing to another origin is generally allowed. Protection against unwanted submissions is done separately, as CSRF defense such as checking a token.