fix(cors): deny the origin when the origins predicate throws #76

Closed
chrnx wants to merge 1 commits from fix/cors-predicate-boundary into develop
pull from: fix/cors-predicate-boundary
Owner

Closes #72.

CorsOptions.origins accepts a predicate, it runs on the request path, and it runs above the region where errors convert to a response. A predicate that threw became a rejected promise nobody awaited — and Node's default for that is to exit. One cross-origin GET took the deploy down, with onError never reaching it.

The trigger is a browser. originAllowed only runs when an Origin header is present, so the crash is reachable by any cross-origin client and by no test that forgets the header — which is how it survived a green suite.

The fix

All four call sites funnel through originAllowed, so the guard goes there once: the Node request handler, computeInjected on both adapters, and the preflight path.

A predicate that throws denies the origin. That is the honest reading — a lookup that failed has not said yes — and it is the only direction that is safe by default: a broken allowlist must never widen into an open one. The failure is reported through the app logger rather than swallowed; HttpOptions already carries one since the maxConcurrentRequests merge, so nothing new had to be threaded through to reach it.

Converting the throw into a 500 was the alternative. It was rejected because two of the four call sites run before any error boundary exists, so it would have meant restructuring three of them to gain a worse default — a transient Redis blip answering 500 instead of "this origin cannot read the response".

Only a throwing predicate changes behaviour. Every other origins shape is untouched, and the security headers still land on the response either way.

Verified against the built dist/, the same way the issue reproduced it

Before, every one of these exited the process with code 1:

GET with Origin      -> 404  acao=null
OPTIONS preflight    -> 204  acao=null
GET without Origin   -> 404  acao=null
buildFetch path      -> 404  acao=null
PROCESS STILL ALIVE — exiting cleanly

(404 because the repro app declares no modules; the point is that a response arrives at all.)

Tests

  • test/security.test.ts — the predicate throws: the origin is denied, it fails closed with credentials: true too, and the failure reaches the logger.
  • test/http.test.ts — end to end on the Node server: 200 with no access-control-allow-origin, security headers intact, process alive.
  • test/web-stream.test.ts — amends a test from the maxConcurrentRequests contribution. It asserted the old crash as its baseline while proving the gate does not leak a slot on the CORS path. It still proves exactly that, now against a transient failure that is answered rather than fatal.

Gates

lint               0 errors (48 pre-existing warnings)
complexity:check   every function <= 25
typecheck          clean
format:check       clean
build              clean
npm test           457 passed / 67 files
test:deno          12 passed
test:bun           7 passed
test:edge          PASS

Noticed while fixing, not fixed here

The predicate is consulted more than once per request — computeInjected runs in the adapter and again inside handle(), so a GET calls it twice and a preflight three times. Visible in the repro as two and three warning lines for one request. It is user code with a network call in it being run two to three times per request; worth its own issue rather than a drive-by in a crash fix.

Closes #72. `CorsOptions.origins` accepts a predicate, it runs on the request path, and it runs above the region where errors convert to a response. A predicate that threw became a rejected promise nobody awaited — and Node's default for that is to exit. One cross-origin `GET` took the deploy down, with `onError` never reaching it. The trigger is a browser. `originAllowed` only runs when an `Origin` header is present, so the crash is reachable by any cross-origin client and by no test that forgets the header — which is how it survived a green suite. ## The fix All four call sites funnel through `originAllowed`, so the guard goes there once: the Node request handler, `computeInjected` on both adapters, and the preflight path. A predicate that throws **denies the origin**. That is the honest reading — a lookup that failed has not said yes — and it is the only direction that is safe by default: a broken allowlist must never widen into an open one. The failure is reported through the app logger rather than swallowed; `HttpOptions` already carries one since the `maxConcurrentRequests` merge, so nothing new had to be threaded through to reach it. Converting the throw into a 500 was the alternative. It was rejected because two of the four call sites run before any error boundary exists, so it would have meant restructuring three of them to gain a worse default — a transient Redis blip answering 500 instead of "this origin cannot read the response". Only a throwing predicate changes behaviour. Every other `origins` shape is untouched, and the security headers still land on the response either way. ## Verified against the built `dist/`, the same way the issue reproduced it Before, every one of these exited the process with code 1: GET with Origin -> 404 acao=null OPTIONS preflight -> 204 acao=null GET without Origin -> 404 acao=null buildFetch path -> 404 acao=null PROCESS STILL ALIVE — exiting cleanly (404 because the repro app declares no modules; the point is that a response arrives at all.) ## Tests - `test/security.test.ts` — the predicate throws: the origin is denied, it fails closed with `credentials: true` too, and the failure reaches the logger. - `test/http.test.ts` — end to end on the Node server: `200` with no `access-control-allow-origin`, security headers intact, process alive. - `test/web-stream.test.ts` — **amends a test from the `maxConcurrentRequests` contribution.** It asserted the old crash as its baseline while proving the gate does not leak a slot on the CORS path. It still proves exactly that, now against a transient failure that is answered rather than fatal. ## Gates lint 0 errors (48 pre-existing warnings) complexity:check every function <= 25 typecheck clean format:check clean build clean npm test 457 passed / 67 files test:deno 12 passed test:bun 7 passed test:edge PASS ## Noticed while fixing, not fixed here The predicate is consulted **more than once per request** — `computeInjected` runs in the adapter and again inside `handle()`, so a `GET` calls it twice and a preflight three times. Visible in the repro as two and three warning lines for one request. It is user code with a network call in it being run two to three times per request; worth its own issue rather than a drive-by in a crash fix.
chrnx added 1 commit 2026-09-01 13:39:38 +00:00
fix(cors): deny the origin when the origins predicate throws
CI / dco (pull_request) Successful in 4s
CI / runtimes (bun) (pull_request) Successful in 13s
CI / runtimes (deno) (pull_request) Successful in 18s
CI / runtimes (edge) (pull_request) Successful in 14s
CI / build (pull_request) Successful in 29s
bb0b01cf26
`CorsOptions.origins` accepts a predicate, and it runs on the request path above the
region where errors convert to a response. A predicate that threw became a rejected
promise nobody awaited, and Node's default for that is to exit — so one cross-origin
GET took the process down, with `onError` never reaching it.

The trigger is a browser: `originAllowed` only runs when an `Origin` header is present,
so the crash is reachable by any cross-origin client and by no test that forgets the
header, which is how it survived a green suite.

Guard the call at `originAllowed`, the single point all four call sites funnel through
(the Node handler, `computeInjected` on both adapters, and the preflight path). A
predicate that throws denies the origin: a lookup that failed has not said yes, and a
broken allowlist must never widen into an open one. The failure is reported through the
app logger rather than swallowed — `HttpOptions` already carries one.

The published behaviour changes for a throwing predicate only. Every other origin shape
is untouched, and the security headers still land on the response.

Amends one test from the `maxConcurrentRequests` contribution, which asserted the old
crash as its baseline while proving the gate does not leak a slot. It still proves that,
against a transient failure that is now answered rather than fatal.

Closes #72.

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
chrnx closed this pull request 2026-09-01 13:43:34 +00:00

Pull request closed

This pull request cannot be reopened because the branch was deleted.
Sign in to join this conversation.