The Same-Origin Policy (SOP) is a security measure implemented by web browsers to restrict web pages from making requests to a different domain than the one that served the original web page. This policy helps to prevent malicious websites from making unauthorized requests to another site on behalf of the user.
So in summary SOP Is a rule that is enforced by the browsers to control access to data between web apps
The policy is based on the concept of the "same origin," which is defined by the combination of the protocol (http or https), domain, and port of a URL.
"origin" refers to the combination of three components that uniquely identifies a web page's source:
The following table gives examples of origin comparisons with the URL http://store.company.com/dir/page.html
:
URL | Outcome | Reason |
---|---|---|
http://store.company.com/dir2/other.html |
Same origin | Only the path differs |
http://store.company.com/dir/inner/another.html |
Same origin | Only the path differs |
https://store.company.com/page.html |
Failure | Different protocol |
http://store.company.com:81/dir/page.html |
Failure | Different port (http:// is port 80 by default) |
http://news.company.com/dir/page.html |
Failure | Different host |
The Same-Origin Policy (SOP) is a critical security measure on the web that helps protect users from various types of attacks by preventing unauthorized access to sensitive data and resources. Here are some key reasons why the Same-Origin Policy is necessary:
The same-origin policy generally controls the access that JavaScript code has to content that is loaded cross-domain. Cross-origin loading of page resources is generally permitted. For example, the SOP allows embedding of images via the <img>
tag, media via the <video>
tag and JavaScript includes with the <script>
tag. However, while these external resources can be loaded by the page, any JavaScript on the page won't be able to read the contents of these resources.