Browser Security: Same-Origin, CORS and CSRF

Security
44 / 47

Browsers isolate sites from each other (same-origin policy). CORS is the controlled way for a server to relax that for reading; CSRF is the trick of making the browser send a logged-in user's cookies to a site they did not intend to use.

The Analogy (Read This First)

The same-origin policy is a rule that one office cannot read another office's mail. CORS is a guest list at the receiving office. CSRF is someone forging a note that makes you, with your own badge, do something in your own office.

Deep Dive Analysis

Origin. An origin is scheme + host + port. https://app.example.com and https://api.example.com are different origins. The same-origin policy stops a page from one origin from *reading* responses from another. It does not stop the request from being sent.

CORS. For a cross-origin request, the browser sends an Origin header and only exposes the response to the page if the server replies with a matching Access-Control-Allow-Origin. Requests that could change data (non-simple methods, JSON content type, custom headers) are preceded by a preflight OPTIONS request asking which methods and headers are allowed.

A correct policy. Compare the request's Origin against an allow-list of exact origins and echo back only a match (adding Vary: Origin). Pitfalls: reflecting any origin; using * together with credentials (browsers refuse it, and a reflect-everything workaround is a real vulnerability); trusting the null origin; and substring matches such as endsWith('example.com'), which accept evilexample.com.

ALLOWED = {'https://app.example.com', 'https://admin.example.com'}

def cors_headers(origin):
    if origin in ALLOWED:
        return {'Access-Control-Allow-Origin': origin, 'Vary': 'Origin'}
    return {}

CORS is not authentication. It is enforced by browsers only, protects the user's reading of data, and does nothing against curl, scripts or servers. The API still needs real auth on every request.

CSRF. Browsers attach cookies automatically. If you are logged in to bank.com and visit a malicious page containing a form that posts to bank.com/transfer, the browser sends your cookie along, and the bank sees an authenticated request. The attacker cannot read the response, but the action happens.

Defences. (1) CSRF tokens: a random per-session value placed in forms or a header, which the attacker's page cannot read; the server rejects state-changing requests without a matching token, compared in constant time. (2) SameSite cookies (Lax or Strict) so the browser omits the cookie on cross-site requests. (3) Check Origin/Referer. (4) Never change state on GET.

APIs using bearer tokens in an Authorization header (not cookies) are not CSRF-prone, because the browser does not attach that header automatically, but they trade it for the need to store the token safely (XSS then becomes the risk).

Key Bullets
  • Same-origin policy blocks reading cross-origin responses, not sending requests.
  • CORS = server-controlled exceptions; match Origin against an exact allow-list.
  • Never combine a wildcard or reflected origin with credentials.
  • CORS is browser-only and is not a substitute for authentication.
  • CSRF is defeated by tokens, SameSite cookies and not changing state on GET.
Trade-offs
Cookie sessions: automatic, HttpOnly protects them from scripts | Need CSRF defencesBearer tokens in headers: no CSRF | Token storage is exposed to XSSSameSite=Strict: strongest CSRF protection | Breaks some cross-site navigation flows
Real-World Examples
A single-page app on one domain calling an API on anotherA hidden form that posts a money transfer using your sessionA preflight request before a JSON POST

Curated Curation & Deep Insights

ScaleDojo Certified
Video Tutorial Pending

Our system architects are vetting high-quality, authorized video guides for Browser Security: Same-Origin, CORS and CSRF with zero third-party platform links.

Verification in Progress
Reading Material Pending

We are preparing premium, zero-competitor deep-dives for Browser Security: Same-Origin, CORS and CSRF. Authorized reading references will appear automatically.

Verification in Progress