Lesson 6 of 14 · 18m
CORS Is Not a Security Feature
Preflights, reflected origins, and credentialed requests — what CORS actually does, and what it was never meant to stop.
The ticket reads: "Security fix — restricted CORS to our domain, API is now protected." It is not. CORS was never a gate around your API; it's a set of headers that tell browsers when they're allowed to let one site's JavaScript read another site's responses. The enforcement lives entirely inside the browser. curl, requests, a botnet's HTTP client — none of them send an Origin header they didn't choose to send, and none of them read Access-Control-Allow-Origin at all. An API "protected by CORS" is protected the way a "no trespassing" sign protects an empty field.
What CORS actually does
The same-origin policy is the real security boundary: a page on evil.com cannot read responses from api.example.com. CORS is a relaxation of that boundary — your API opts in to letting listed origins read it. For a cross-origin request that isn't "simple" — anything with Content-Type: application/json, an Authorization header, or a method other than GET/HEAD/POST — the browser first sends a preflight:
OPTIONS /v1/invoices HTTP/1.1
Origin: https://app.example.com
Access-Control-Request-Method: PATCHYour server answers Access-Control-Allow-Origin: https://app.example.com and Access-Control-Allow-Methods: PATCH, or the browser never sends the real request. Notice the direction of trust: the browser asks, the browser enforces. Your server is answering a question, not blocking an attack.
The actual footguns
Reflecting origins with credentials. The spec forbids Access-Control-Allow-Origin: * alongside Access-Control-Allow-Credentials: true. So servers "fix" it by echoing whatever Origin arrived — and now every website on the internet can make authenticated requests through a victim's browser and read the responses. This is the classic CORS own-goal; it sits inside OWASP API8 (security misconfiguration) and it has produced real bounty findings for years. The rule: allowlist origins exactly — string compare against a configured list — or emit nothing.
The null origin. Sandboxed iframes, file:// pages, redirects, and data: URLs produce Origin: null. Reflecting null "for local development" ships the same hole with extra steps.
Believing CORS blocks CSRF. "Simple" requests — a plain HTML form POST, Content-Type: application/x-www-form-urlencoded — don't preflight at all. The browser sends them and just withholds the response. For a state-changing endpoint, the request already landed. This is why a required custom header (your own X-Requested-With-style flag, or a Content-Type: application/json requirement) is still the cheap CSRF gate — a form cannot produce either without triggering a preflight. For cookies, SameSite=Lax or Strict does the real work; CORS is decoration.
Where CORS belongs in the design
Use it for its actual purpose: letting your own front ends call your API, and publishing explicitly public, unauthenticated endpoints (Access-Control-Allow-Origin: * on a public status endpoint is fine — there's nothing to withhold). For everything else, the sequence is: authenticate, authorize, validate — and then, if a browser is involved, emit precise CORS headers so your own app works. Vary: Origin on every response that computes the header dynamically, or a shared cache will happily serve one origin's allowed headers to another.
And the ops-side consequence of the mental model: because CORS isn't enforcement, a curl reproduction of a "CORS-blocked" request is expected behavior, not a bypass. If someone reports "I bypassed your CORS," nothing was bypassed.
Try this
Write the CORS policy for an API consumed by app.example.com (browser, cookies) and by partner servers (Bearer tokens, no browser). List every response header on a PATCH preflight. Then check the curl path: the same request with no Origin header — what protects it? (Your auth middleware. It was always your auth middleware.)
Pitfalls
- Reflect-echo of
Origin+ credentials — the findable-by-a-shodan-query own-goal. Origin: nullallowlisted — sandboxed iframes are attacker-controllable.- CORS as CSRF defense — simple requests don't preflight; use a custom header or
SameSitecookies. - Wildcard
*with credentials — browsers reject it; the fix is not echoing the origin. - Missing
Vary: Origin— a shared cache poisons the allowlist.
Was this lesson helpful?
Keep reading
Related articles from the SudoSecurity blog.
Building a Production REST API With Node.js and Express
Build a REST API in Node.js and Express the way production systems need: input validation, JWT auth, refresh token rotation, status codes, rate limiting, idempotency keys, and centralized error handling.
GraphQL Over HTTP Is Finally a Spec. Your Server Probably Still Says 200.
The GraphQL over HTTP specification shipped its first edition on November 12, ending the decade of '200 Not OK.' Validation errors get 422, partial results get a new 294 status, and a dedicated media type tells clients the body is safe to parse.
Strings Are Lying to You: Unicode Length, Case and Normalization in PHP, Python and JavaScript
One family emoji is 18 bytes, 8 JavaScript code units, 5 Python characters and 1 thing a human sees. I ran the same Unicode tests through PHP 8.6, Python 3.15 and Node 26: each gave a different answer for length, case and truncation, a KELVIN SIGN lowercases into a real username, and PHP 8.6's...
PHP and WordPress Patched a Day Apart. Your Front End Decides Which PHP Bugs Reach You.
PHP 8.5.13, 8.4.28, 8.3.37 and 8.2.36 fix 18 CVEs, seven rated 9.2, and WordPress 7.1.5 fixes 20 more issues. Most of the PHP bugs only fire under specific settings, so I checked the official Docker images for each condition. The images were patched the same day, and the patch quietly turns off...