A cors.origins predicate that throws takes the process down — one cross-origin request is enough #72

Closed
opened 2026-08-30 17:45:39 +00:00 by chrnx · 0 comments
Owner

CorsOptions.origins accepts a predicate, and the predicate runs on the request path outside any try. One that throws does not produce a 500 — it takes the process with it.

What happens

src/http/server.ts:88, above the region handle() runs in:

if (opts?.cors) Object.assign(injected, resolveCors(opts.cors, req));

resolveCors (src/security.ts:86) calls originAllowed (:78), which calls the predicate:

if (typeof spec === 'function') return spec(origin);

The request listener is async, so the throw becomes a rejected promise nobody awaits. Node's default is to exit:

Error: lookup failed
    at origins (crash.mjs:2:69)
    at originAllowed (dist/index.mjs:850:42)
    at resolveCors (dist/index.mjs:858:45)
    at Server.<anonymous> (dist/index.mjs:1480:45)
    at Server.emit (node:events:519:28)
    at parserOnIncoming (node:_http_server:1186:12)
PROCESS EXIT code= 1

That is createApp({ modules: [], cors: { origins: () => { throw new Error('lookup failed'); } } }), one GET carrying an Origin header, against the built dist/. Node 22.22.2.

Across the paths:

Path Result
Node, GET with Origin no response; process exits 1
Node, OPTIONS preflight same
Node, onError configured same — onError never runs
Node, no Origin header 200; the predicate is not reached
buildFetch, GET with Origin promise rejects out of app.fetch

Why it matters

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 to send one — which is how it survives a green suite.

A predicate is the whole reason the option is a predicate: an allowlist in Redis, a tenant lookup, a regex over a parsed URL. Every one of those has a failure mode. The framework hands the user a callback on the hot path and gives it no boundary, so the first transient failure in that lookup is not a 503; it is the deploy going down.

The README says errors convert centrally "with a single createApp({ onError }) hook that covers every error response." That hook does not reach here. The predicate runs before handle(), so nothing in the error pipeline ever sees the throw.

Scope

  • Wrap the resolveCors call on both adapters — src/http/server.ts:88 and computeInjected at src/http/core.ts:135 — so a throwing predicate produces a response instead of an unhandled rejection.
  • Decide which response. Failing closed (no CORS headers, request proceeds) leaks less than failing open and matches resolveCors's own contract for a rejected origin: "Returns {} when the origin is not allowed." A 500 through renderError is the other defensible answer. The current behaviour is neither.
  • Either way, log it. A predicate that starts throwing silently changes the CORS policy, and silence is how that becomes a support ticket instead of an alert.
  • The fetch path rejects rather than crashing, but it should not diverge from Node either.

Related

Found while reviewing Expressive-Tea/green-tea#23 on GitHub. That PR's request gate originally acquired a slot above this line, so a throwing predicate also burned the slot permanently; the PR moved the acquire below it, which fixes the leak and leaves the crash exactly where it was. Independent of the PR — reproduced on contrib at 4b04421 with nothing applied.

`CorsOptions.origins` accepts a predicate, and the predicate runs on the request path outside any `try`. One that throws does not produce a `500` — it takes the process with it. ## What happens `src/http/server.ts:88`, above the region `handle()` runs in: ```ts if (opts?.cors) Object.assign(injected, resolveCors(opts.cors, req)); ``` `resolveCors` (`src/security.ts:86`) calls `originAllowed` (`:78`), which calls the predicate: ```ts if (typeof spec === 'function') return spec(origin); ``` The request listener is `async`, so the throw becomes a rejected promise nobody awaits. Node's default is to exit: ``` Error: lookup failed at origins (crash.mjs:2:69) at originAllowed (dist/index.mjs:850:42) at resolveCors (dist/index.mjs:858:45) at Server.<anonymous> (dist/index.mjs:1480:45) at Server.emit (node:events:519:28) at parserOnIncoming (node:_http_server:1186:12) PROCESS EXIT code= 1 ``` That is `createApp({ modules: [], cors: { origins: () => { throw new Error('lookup failed'); } } })`, one `GET` carrying an `Origin` header, against the built `dist/`. Node 22.22.2. Across the paths: | Path | Result | |---|---| | Node, `GET` with `Origin` | no response; process exits 1 | | Node, `OPTIONS` preflight | same | | Node, `onError` configured | same — `onError` never runs | | Node, no `Origin` header | `200`; the predicate is not reached | | `buildFetch`, `GET` with `Origin` | promise rejects out of `app.fetch` | ## Why it matters 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 to send one — which is how it survives a green suite. A predicate is the whole reason the option is a predicate: an allowlist in Redis, a tenant lookup, a regex over a parsed URL. Every one of those has a failure mode. The framework hands the user a callback on the hot path and gives it no boundary, so the first transient failure in that lookup is not a `503`; it is the deploy going down. The README says errors convert centrally "with a single `createApp({ onError })` hook that covers every error response." That hook does not reach here. The predicate runs before `handle()`, so nothing in the error pipeline ever sees the throw. ## Scope - Wrap the `resolveCors` call on both adapters — `src/http/server.ts:88` and `computeInjected` at `src/http/core.ts:135` — so a throwing predicate produces a response instead of an unhandled rejection. - Decide which response. Failing closed (no CORS headers, request proceeds) leaks less than failing open and matches `resolveCors`'s own contract for a rejected origin: *"Returns `{}` when the origin is not allowed."* A `500` through `renderError` is the other defensible answer. The current behaviour is neither. - Either way, log it. A predicate that starts throwing silently changes the CORS policy, and silence is how that becomes a support ticket instead of an alert. - The fetch path rejects rather than crashing, but it should not diverge from Node either. ## Related Found while reviewing Expressive-Tea/green-tea#23 on GitHub. That PR's request gate originally acquired a slot above this line, so a throwing predicate also burned the slot permanently; the PR moved the acquire below it, which fixes the leak and leaves the crash exactly where it was. Independent of the PR — reproduced on `contrib` at 4b04421 with nothing applied.
chrnx closed this issue 2026-09-04 13:32:27 +00:00
Sign in to join this conversation.