Review CORS policy
CORS is a browser-enforced relaxation of the same-origin policy; a wrong policy hands another site read access to your users' data. The dangerous patterns are reflecting any origin and combining a wildcard with credentials.
Procedure
Observe the headers for a benign request and for a foreign origin:
curl -sI -H 'Origin: https://evil.example' https://api.example.com/me | rg -i \ '^access-control-allow-(origin|credentials|methods|headers)|^vary'Check for origin reflection — the response echoing the request's
Origin:if the `Access-Control-Allow-Origin` equals `https://evil.example`, the policy reflects; with `Access-Control-Allow-Credentials: true` this is a full data-exfiltration bug.Confirm the wildcard trap:
Access-Control-Allow-Origin: *together withAccess-Control-Allow-Credentials: trueis rejected by browsers, but a reflected origin is not — reflection with credentials is the real risk.Inspect the origin-matching code for prefix/suffix mistakes:
rg -n "Access-Control-Allow-Origin|allow_origin|add_cors|cors\(" src/Bugs to look for:
origin.startsWith("https://app.example.com")(accepts...example.com.evil.example),origin.endsWith("example.com")(acceptsevil-example.com), or a regex without anchors / with.unescaped.Fix with a strict allowlist and echo only exact matches:
ALLOWED = {"https://app.example.com", "https://admin.example.com"} if origin in ALLOWED: resp.headers["Access-Control-Allow-Origin"] = origin resp.headers["Access-Control-Allow-Credentials"] = "true" resp.headers["Vary"] = "Origin" # cache correctnessDo not reflect for unauthenticated public APIs either; use
*without credentials when data is genuinely public.Keep the method/header allowlist tight (
GET,POSTnot*), and only enable credentials if the endpoint actually needs cookies.Test with a credentialed fetch from a foreign page and confirm the browser blocks the read.
Pitfalls
nullorigin (from sandboxed iframes andfile://) is sometimes on the allowlist; it is attacker-controllable.- Missing
Vary: Originlets a shared cache serve one origin's CORS response to another. - A permissive CORS on a JSON API with cookie auth is equivalent to no same-origin policy.
- Wildcard subdomain patterns (
*.example.com) include hosts you do not control if any subdomain is user-registered. - Preflight (
OPTIONS) handled by a different layer than the actual route can disagree on the policy. - Reflections of the
Originheader also break when it is absent; handle the null case explicitly.
Verification
for o in 'https://evil.example' 'null' 'https://app.example.com'; do \
acao=$(curl -sI -H "Origin: $o" https://api.example.com/me | rg -i '^access-control-allow-origin' | tr -d '\r'); \
echo "$o -> $acao"; \
done
Pass: only the known app origin is echoed; null and evil.example get no
Access-Control-Allow-Origin (or a non-credentialed *). Report the observed policy, the matching
rule, and the origins now allowed.