[Security 3/9] Production CORS allowlist with fail-safe denial - #245
Merged
Conversation
Ported from the security pack's index.ts change to the post-refactor app.ts (the Express app moved in the integration-test extraction). Adapted-from: Open-Legal-Products#227 (4c44c15, CORS half)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
[Security 3/9] Production CORS allowlist with fail-safe denial
TL;DR
Replace the single-origin CORS reflection with an explicit allowlist and fail-safe defaults: unknown origins get no
Access-Control-Allow-Originheader (the browser blocks the response) instead of an error, preflight stays a clean 2xx/4xx, and approved request headers/methods are enumerated.Risk to user data
Severity: medium–high. The browser's same-origin policy is what stops
evil.examplefrom reading our API's responses using a logged-in visitor's cookies/credentials. CORS is a controlled relaxation of that policy. A server that reflects anyOriginback — a common dev shortcut — has relaxed it for every site on the internet, so a malicious page a user visits could read their documents, chats, and account data out of our API.Flows affected
app.tsCORS middleware).OPTIONS) handling for all routes.Attack precedent
Reflected-origin and overly-permissive CORS (
Access-Control-Allow-Origin: *with credentials, or naive origin echoing) is a staple finding. See PortSwigger's CORS labs for the credential-theft chains this enables.Possible fixes, and what we chose
Originbackorigin: FRONTEND_URL(single string)Errorfor disallowed originscorspackage propagates the throw to Express's default handler, turning every disallowed cross-origin request (including preflight) into an HTTP 500. Noisy, and a 500 on preflight is a worse signal than a clean deny.Set, callback resolvesfalsefor unknown originsOrigin) still allowed.sequenceDiagram participant B as Browser on evil.example participant S as Mike backend B->>S: request with Origin: https://evil.example S->>S: allowedOrigins.has(origin)? → false S-->>B: response WITHOUT Access-Control-Allow-Origin Note over B: Same-origin policy kicks in →<br/>browser refuses to expose the body to JSFail-safe detail (in-code comment): the callback returns
callback(null, false)rather thancallback(new Error()), precisely so a disallowed origin is a silent deny, not a 500. Approved headers are limited toAuthorization/Content-Type, and methods are enumerated.What's in this PR
backend/src/app.ts— allowlistSet+ fail-safe origin callback + explicit headers/methods.cors.test.ts(4 integration tests: allowed origin reflected with credentials, disallowed origin omitted, disallowed origin is not a 5xx, header allowlist enforced).Reading
MDN: CORS · PortSwigger: CORS misconfigurations