Course contentsShow
← Security
Lesson 855 of 3,05218. Web Security - CSRF and Request ForgeryFree lesson

Same-Origin Policy Fundamentals

Understanding the browser security model that restricts how documents or scripts from one origin interact with resources from another origin.

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): http vs https
  • Domain (host): example.com vs subdomain.example.com
  • Port: :80 vs :443 vs :8080

All three must match exactly for two URLs to be considered "same-origin."

Examples:

  • https://example.com/page1 and https://example.com/page2 → Same origin ✓
  • https://example.com and http://example.com → Different origin (protocol differs) ✗
  • https://example.com and https://api.example.com → Different origin (subdomain differs) ✗
  • https://example.com:443 and https://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.