Bitcoin

A Developer’s Guide to Same-Origin Policy (SOP) and Cross-Origin Resource Sharing (CORS)

If you’ve spent enough time developing web applications, you’ve certainly come across this error:

Access to fetch at 'https://api.example.com' from origin 'https://app.yoursaas.com' has been blocked by CORS

Sound familiar?

Most likely, you’ve encountered it when your backend and frontend are hosted on separate servers, and the frontend tries to communicate with the backend.

This error occurs because of a browser-enforced security mechanism called the Same-Origin Policy (SOP), which blocks JavaScript access to the response body of cross-origin HTTP requests by default.

To bypass that restriction, your server needs to explicitly allow such access by responding with the appropriate Cross-Origin Resource Sharing (CORS) headers.

Both SOP and CORS are designed to protect users by preventing unauthorized access to resources and sensitive data between different origins. As a developer, understanding how they work together is essential to building secure and reliable web applications. But first:

What Is an “Origin”?

An origin is defined by three things:

  • Protocol (http, https)
  • Domain name (example.com)
  • Port (:80, :443, etc.)

The combination of these three: protocol, domain, and port, defines an origin.

Diagram explaining the concept of origin in web security. Shows how protocol (https), domain (example.com), and port (443) combine to form an origin, with examples of different origins being blocked due to changes in protocol or subdomain.Diagram explaining the concept of origin in web security. Shows how protocol (https), domain (example.com), and port (443) combine to form an origin, with examples of different origins being blocked due to changes in protocol or subdomain.

If any of those differ, the origin is not the same:

https://app.example.com  ≠  http://app.example.com
https://app.example.com  ≠  https://api.example.com
https://example.com:3000 ≠  https://example.com

So, now that we made it clear what an origin is, we can get to the meat of this post.

What Is the Same-Origin Policy (SOP)?

According to MDN:

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.

Source: MDN Web Docs, licensed under CC-BY-SA 2.5.

Same-Origin Policy is one of the cornerstones of web security. It protects your users by stopping potentially malicious websites from interacting with your site’s data behind their backs.

In plain English: browsers allow web pages to send requests to different origins, but block JavaScript from accessing the response, unless the server explicitly allows it via Cross-Origin Resource Sharing.

Many developers mistakenly believe that the Same-Origin Policy blocks cross-origin requests. In truth, it doesn’t prevent the requests themselves, they’re still sent and can be seen as successful in the browser’s network tab. What SOP actually restricts is JavaScript’s ability to read the response content from those requests.

This brings up another common point of confusion when learning about SOP and CORS:

Some cross-origin requests are allowed by design. Here are the main exceptions:

  • : Linking to other sites is allowed.
  • : Images can be loaded cross-origin.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button