SOP, CORS, and CSP Explained
Understanding Web Security Policies: SOP, CORS, and CSP
This article delves into crucial web security policies—Same-Origin Policy (SOP), Cross-Origin Resource Sharing (CORS), and Content Security Policy (CSP)—explaining their functions, importance, and common misconfigurations that can lead to vulnerabilities.
1. Same-Origin Policy (SOP)
The Same-Origin Policy (SOP) restricts web pages from making requests to a different origin than the one they came from.
Example: Facebook Scenario
- Malicious page URL: http://evil.com/login
- Legitimate Facebook URL: https://www.facebook.com/loginIf a user is logged in to Facebook, the malicious page cannot access Facebook cookies or user data due to SOP.
Conditions for "Same-Origin"
For two pages to be "same-origin":
Same Protocol
http://example.com ≠ https://example.comSame Domain
example.com ≠ sub.example.comSame Port
example.com:80 ≠ example.com:8080
Example: IE SOP Flaw
Two sites share IP
192.168.0.1:In old IE versions,
site1.comcould accesssite2.comdata, bypassing SOP.
2. Cross-Origin Resource Sharing (CORS)
CORS allows servers to specify which origins can access their resources.
Example: Restaurant API
Server Response:
Only fooddelivery.com can access the API.
CORS Misconfigurations
1. Wildcard with Sensitive Data
Risky if the endpoint serves private user data.
Example: Bank API returning account balance.
2. Credentials with Wildcard
Scenario:
Logged-in user visits
http://evil.comAttacker can make authenticated requests to a vulnerable site and steal data.
3. Reflecting the Origin Header
Attack Example:
Attacker controls
evil.comSends request with Origin header set to
http://evil.comServer responds allowing full access.
3. Content Security Policy (CSP)
CSP prevents malicious content from executing.
Example: Facebook CSP
Only scripts/images from
selfandfbcdn.netare allowed.Any external script is blocked by the browser.
Example: XSS Mitigation
Even if the user visits a page containing this script, it won't execute due to CSP.
Conclusion
SOP
Restrict cross-origin access
Malicious page cannot read Facebook cookies
Older IE SOP flaw bypass
CORS
Allow controlled cross-origin access
Restaurant API sharing menu
Wildcard with credentials
CSP
Prevent code injection/XSS
Blocking external scripts
Misconfigured directives allowing malicious scripts
Key Takeaways:
SOP provides a default safety net.
CORS enables flexible cross-origin access.
CSP protects against content injection.
Understanding and properly configuring these policies is essential for building secure web applications.
Last updated