Same-Origin Policy, CORS, and CSP
Build an accurate mental model of what the browser does, from navigation and HTTP through parsing, rendering, storage, workers, security, and DevTools, so you can predict behavior and debug real pages.
What you will be able to do
#- Define an origin and apply the same-origin policy.
- Explain CORS preflight and credentialed requests.
- Use CSP to reduce cross-site scripting risk.
Overview
#The browser's security model assumes pages are hostile to each other.
Same-origin policy
An origin is scheme, host, and port. Scripts cannot read responses from another origin unless the server opts in. This stops a page from reading your bank's response using your cookies.
CORS
For cross-origin fetch, the browser sends an Origin header. For non-simple requests it sends a preflight OPTIONS request. The server must reply with Access-Control-Allow-Origin and, for credentials, Access-Control-Allow-Credentials plus a specific origin (not *).
CSP
Content Security Policy restricts where scripts, styles, and other resources may load from. script-src 'self' blocks inline and third-party scripts. Prefer nonces or hashes over unsafe-inline. CSP is a strong defense against cross-site scripting.
Other defenses
HttpOnlyandSameSitecookies limit theft and cross-site sending.- Subresource Integrity pins the exact hash of a script.
X-Content-Type-Options: nosniffstops MIME sniffing.
Questions to carry forward
- What makes a CORS request require a preflight?
- Why is unsafe-inline dangerous?
- How does SRI protect a CDN script?
Exercises
#Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.
Exercise 1Predict
A page at https://app.example.com fetches https://api.example.com/data with credentials. The server replies Access-Control-Allow-Origin: *. What happens?
Exercise 2Interview
A teammate says CORS is a server-side security feature. Correct them and explain what CORS actually protects.
Reveal the reference answer
reference.textCORS is enforced by the browser, not the server. It protects users by preventing a page from reading cross-origin responses unless the target origin explicitly allows it. The server only sends headers that grant access; the browser decides whether to expose the response to the page.Authored expected output: An answer stating the browser enforces CORS and it protects cross-origin reads.
Servers cannot enforce CORS because a non-browser client ignores it. The policy exists to protect the user's data in the browser.
Common misconceptions
#- CORS is enforced by the server.
- Access-Control-Allow-Origin: * works with credentials.
- The same-origin policy compares full URLs including paths.
Follow-up practice
#- browser-internals-debugging-capstone
Your progress
#Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.
Verification: authored. Answers and outputs on this page are authored reference material, not executed results.