A cors.origins predicate runs two or three times per request — it is user code with a network call in it #77

Closed
opened 2026-09-01 13:42:52 +00:00 by chrnx · 0 comments
Owner

CorsOptions.origins accepts a predicate, and the framework consults it more than once for a single request: twice for a normal request, three times for a preflight, on both the Node and the Fetch adapter.

What happens

CORS headers are computed at two independent points, and the preflight path computes them a third time.

Node, one GET carrying an Origin:

# Call site Path
1 src/http/server.ts:104 resolveCors directly, to seed injected before routing
2 src/http/core.ts:280 handle() → computeInjected → resolveCors

Node, one OPTIONS preflight — the same two, plus:

| 3 | src/http/core.ts:290 | corsPreflightHeaders → resolveCors |

The Fetch adapter has the same shape: src/http/web.ts:142 calls computeInjected before the gate, then handle() calls it again at core.ts:280.

Every one of those reaches originAllowed, and originAllowed calls the predicate.

Measured

Against the built dist/, with a predicate that logs each call, one request each:

GET with Origin      -> predicate called 2x
OPTIONS preflight    -> predicate called 3x
buildFetch, Origin   -> predicate called 2x
GET without Origin   -> predicate called 0x   (originAllowed is not reached)

Why it matters

A predicate is the whole reason the option takes a function. The shapes it exists for are an allowlist in Redis, a tenant lookup, a regex over a parsed URL — the first two are I/O. The framework runs that I/O two to three times to answer one question whose inputs did not change between the calls, and it charges the user's latency budget and their Redis for the duplicates.

It is also observable in ways a user will read as a bug in their own code: a predicate with a counter in it counts double, and one that logs prints twice. That surfaced immediately once #72 gave the failure path a log line — one broken request produced two identical warnings.

Nothing is incorrect today: the predicate is expected to be pure with respect to one request, so the second call returns what the first did. The cost is duplicated work and a surprising contract, not a wrong answer.

Scope

The two computations exist for a real reason — injected has to be seeded before routing so that every response, including ones written above handle(), carries the headers. The fix is to compute once per request and share the result, rather than to remove either call site:

  • resolve the CORS headers once at the top of the request and hand the result down, instead of recomputing inside handle();
  • corsPreflightHeaders then extends that result rather than re-deriving it.

Worth checking at the same time whether buildSecurityHeaders is duplicated the same way — it sits in the same computeInjected and is pure computation, so it is cheap, but it is the same double call.

If sharing turns out to be structurally awkward, the honest fallback is to document that the predicate may be consulted more than once per request and must be cheap and side-effect free — but that is the worse outcome, since the option exists precisely for lookups that are neither.

Related

Found while fixing #72, which put a log line on the predicate's failure path and made the duplication visible. Deliberately not fixed there: a crash fix is the wrong place to restructure how headers are computed.

`CorsOptions.origins` accepts a predicate, and the framework consults it more than once for a single request: twice for a normal request, three times for a preflight, on both the Node and the Fetch adapter. ## What happens CORS headers are computed at two independent points, and the preflight path computes them a third time. Node, one `GET` carrying an `Origin`: | # | Call site | Path | |---|---|---| | 1 | `src/http/server.ts:104` | `resolveCors` directly, to seed `injected` before routing | | 2 | `src/http/core.ts:280` | `handle()` → `computeInjected` → `resolveCors` | Node, one `OPTIONS` preflight — the same two, plus: | 3 | `src/http/core.ts:290` | `corsPreflightHeaders` → `resolveCors` | The Fetch adapter has the same shape: `src/http/web.ts:142` calls `computeInjected` before the gate, then `handle()` calls it again at `core.ts:280`. Every one of those reaches `originAllowed`, and `originAllowed` calls the predicate. ## Measured Against the built `dist/`, with a predicate that logs each call, one request each: GET with Origin -> predicate called 2x OPTIONS preflight -> predicate called 3x buildFetch, Origin -> predicate called 2x GET without Origin -> predicate called 0x (originAllowed is not reached) ## Why it matters A predicate is the whole reason the option takes a function. The shapes it exists for are an allowlist in Redis, a tenant lookup, a regex over a parsed URL — the first two are I/O. The framework runs that I/O two to three times to answer one question whose inputs did not change between the calls, and it charges the user's latency budget and their Redis for the duplicates. It is also observable in ways a user will read as a bug in their own code: a predicate with a counter in it counts double, and one that logs prints twice. That surfaced immediately once #72 gave the failure path a log line — one broken request produced two identical warnings. Nothing is *incorrect* today: the predicate is expected to be pure with respect to one request, so the second call returns what the first did. The cost is duplicated work and a surprising contract, not a wrong answer. ## Scope The two computations exist for a real reason — `injected` has to be seeded before routing so that every response, including ones written above `handle()`, carries the headers. The fix is to compute once per request and share the result, rather than to remove either call site: - resolve the CORS headers once at the top of the request and hand the result down, instead of recomputing inside `handle()`; - `corsPreflightHeaders` then extends that result rather than re-deriving it. Worth checking at the same time whether `buildSecurityHeaders` is duplicated the same way — it sits in the same `computeInjected` and is pure computation, so it is cheap, but it is the same double call. If sharing turns out to be structurally awkward, the honest fallback is to document that the predicate may be consulted more than once per request and must be cheap and side-effect free — but that is the worse outcome, since the option exists precisely for lookups that are neither. ## Related Found while fixing #72, which put a log line on the predicate's failure path and made the duplication visible. Deliberately not fixed there: a crash fix is the wrong place to restructure how headers are computed.
chrnx closed this issue 2026-09-04 13:32:28 +00:00
Sign in to join this conversation.