Skip to content

Lesson 12 of 19 · 18m

CORS, Redirects, and Other Origin Confusion

Reflected Access-Control-Allow-Origin with credentials, prefix-checked redirect_uris, '//evil.com' open redirects, and postMessage('*').

Your API reads the incoming Origin header and writes it back out as Access-Control-Allow-Origin, plus Access-Control-Allow-Credentials: true. It works in dev, it works in staging, every origin "just works" — and you have now told every website on the internet that they may issue authenticated cross-origin requests through your users' browsers and read the responses. The CORS spec forbids Access-Control-Allow-Origin: * combined with credentials for exactly this reason, so developers "fix" the preflight failure by reflecting the origin instead — which is the same bug wearing a workaround.

The wrong mental model: CORS is a security boundary. It is not — it is a read permission the browser enforces on JavaScript. Simple requests (form posts, plain GETs, requests without custom headers) are sent cross-origin whether the server grants permission or not; the response just can't be read by the page. The actual security model is a two-part question: can the request be sent (CSRF, SameSite, token checks — the previous lesson) and can the response be read (CORS). Misconfigured CORS answers the second part wrongly.

The misconfigurations that matter

# Request:  Origin: https://evil.com
# Response you shipped:
Access-Control-Allow-Origin: https://evil.com
Access-Control-Allow-Credentials: true

Any script running anywhere now reads your authenticated API through the victim's session. The rule is boring: compare Origin against an explicit allowlist of exact origins — https://app.example.com, https://staging.example.com — and set Vary: Origin so caches don't serve one origin's CORS grant to another. Two traps inside that rule:

  • The null origin. Sandboxed iframes, file:// pages, and redirects can legitimately send Origin: null. Apps add "null" to the allowlist to make local tooling work, and then any attacker who wraps their page in a sandboxed iframe inherits it. If a null origin appears in production, something is misconfigured; never allowlist it.
  • Regex on strings instead of host comparison. origin.endsWith("example.com") accepts notexample.com. Parse the URL, compare scheme + host + port.

Redirect validation is the same bug one layer up

An open redirect — GET /login?next=//evil.com followed by a naive Location: header — is not only a phishing vector. It becomes lethal when it sits inside an auth flow, because OAuth callbacks carry codes and tokens in URLs. The redirect_uri parameter in OAuth exists to contain this, and RFC 6749 §3.1.2 is blunt about how: the authorization server must compare the client's redirect_uri against registered values — exact string match, not prefix, not suffix, not substring. redirect_uri.startsWith("https://app.example.com") accepts https://app.example.com.evil.com. Parse and compare exactly, or maintain an exact-match allowlist table.

Then the paired trap: next = "/dashboard"-style parameters inside your own app. The junior fix is next.startswith("/") — and //evil.com sails through, because it is scheme-relative. Browsers resolve //evil.com/path to https://evil.com/path. The correct check is next.startswith("/") and not next.startswith("//") — or better, parse the URL and require an empty host, or map the destination through an internal route table.

postMessage is the third door

iframe.contentWindow.postMessage(token, "*") sends that token to whatever origin is in the frame — including the attacker's page after a redirect, a hijacked ad frame, or a swapped URL. The second argument is the target origin and it exists for a reason: spell it — "https://embed.example.com" — and on the receiving side always check event.origin against your own allowlist before reading event.data.

Try this

curl -i -H 'Origin: https://evil.com' https://your-api/endpoint and look at what comes back in Access-Control-Allow-Origin. If it echoes the request's origin with Allow-Credentials: true, that is the finding, not the setup step. Then try https://your-app/login?next=//evil.com and ?next=https://app.example.com.evil.com — does the redirect fire?

Pitfalls

  • "Preflight passed" is not a defense. Simple requests never preflight; they just can't be read.
  • Vary: Origin missing lets a shared cache hand one origin's permissive CORS headers to every other origin.
  • Redirect_uri "validated by contains" — in/contains/startsWith all fold under attacker-registered subdomains or path tricks.
  • Reflecting Origin to fix a dev-time preflight error — the fix that ships forever.
  • postMessage with "*" on both ends — any frame in the window can claim the message.

Was this lesson helpful?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.