Same-Origin Policy Fundamentals
You'll learn how the browser's same-origin policy acts as a fundamental security boundary controlling how web pages interact with each other.
What is the Same-Origin Policy?
The Same-Origin Policy (SOP) is a critical browser security mechanism that restricts how documents and scripts loaded from one origin can interact with resources from a different origin. Think of it as a firewall between websites: your bank's website shouldn't be able to read data from your email, even if both are open in your browser at the same time.
What Defines an "Origin"?
An origin consists of three components:
- Protocol (scheme):
httpvshttps - Domain (host):
example.comvssubdomain.example.com - Port:
:80vs:443vs:8080
All three must match exactly for two URLs to be considered "same-origin."
Examples:
https://example.com/page1andhttps://example.com/page2→ Same origin ✓https://example.comandhttp://example.com→ Different origin (protocol differs) ✗https://example.comandhttps://api.example.com→ Different origin (subdomain differs) ✗https://example.com:443andhttps://example.com:8443→ Different origin (port differs) ✗
Why Does This Matter for CSRF?
The Same-Origin Policy prevents malicious websites from directly reading responses from other sites. However, it doesn't prevent requests from being sent—only from reading the response. This is why CSRF attacks work: an attacker's page can trigger a request to your bank (with your cookies), but it can't read the response to steal data.
Key Takeaway: The Same-Origin Policy is the browser's fundamental trust boundary that prevents websites from accessing each other's data, but it doesn't prevent cross-origin requests from being sent—which is exactly what makes CSRF possible.