perf(cors): resolve the request's headers once, not two or three times
`security` and `cors` were computed twice for every request and three times for a
preflight: once in the adapter, to seed the headers a response written *above*
`handle()` still has to carry — a shed 503, a 413 from the body reader — and again in
`dispatch`, plus a third time inside `corsPreflightHeaders`.
Nothing was incorrect, because the predicate is expected to be pure with respect to one
request. The cost is that `cors.origins` is a predicate precisely so it can be a lookup:
an allowlist in Redis, a tenant query. The framework charged the caller's latency budget
and their backend two to three times for an answer whose inputs did not change in
between, and made a predicate with a counter in it count double.
`handle()` now takes the headers the adapter already computed and hands them down, with
`computeInjected` kept as the fallback for a caller that has none.
`corsPreflightHeaders` extends that result instead of re-deriving it. The Node adapter
was carrying its own copy of `computeInjected`, inlined; it uses the real one now, which
is what removes the duplicate rather than papering over it.
Measured against the built `dist/`, same script that produced the numbers in the issue:
GET with Origin 2x -> 1x
OPTIONS preflight 3x -> 1x
buildFetch, Origin 2x -> 1x
GET without Origin 0x -> 0x (never reached the predicate; unchanged)
The risk here is not the call count, it is that `injected` carries the security headers
onto every response shape — so the tests that matter are the existing ones across
`transport-security`, `http-headers`, `fetch`, `parity` and `web-stream`, all green.
The two new tests assert the count directly, on both adapters, which is the assertion
that keeps it from drifting back.
Closes #77.
Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
6 files changed
+120
-41
No files matched your search
@@ -384,4 +384,39 @@ describe('request hardening', () => {
|
||||
server.close();
|
||||
});
|
||||
|
||||
|
||||
// `origins` is a predicate because the shapes it exists for are lookups — an allowlist in Redis, a
|
||||
// tenant query. Running one twice for a GET and three times for a preflight charges the caller's
|
||||
// latency budget and their backend for an answer whose inputs did not change in between.
|
||||
it('consults a cors.origins predicate once per request, not once per header computation', async () => {
|
||||
let calls = 0;
|
||||
const server = createHttpServer(
|
||||
[{ method: 'GET', pattern: '/ping', transport: 'buffer', handler }],
|
||||
[], undefined, undefined,
|
||||
{ cors: { origins: () => { calls++; return true; } } },
|
||||
);
|
||||
await new Promise<void>((r) => server.listen(0, r));
|
||||
const origin = { origin: 'https://a.com' };
|
||||
|
||||
const plain = await fetch(`${getUrl(server)}/ping`, { headers: origin });
|
||||
expect(plain.headers.get('access-control-allow-origin')).toBe('https://a.com');
|
||||
expect(calls).toBe(1);
|
||||
|
||||
calls = 0;
|
||||
const preflight = await fetch(`${getUrl(server)}/ping`, {
|
||||
method: 'OPTIONS', headers: { ...origin, 'access-control-request-method': 'GET' },
|
||||
});
|
||||
expect(preflight.status).toBe(204);
|
||||
expect(preflight.headers.get('access-control-allow-methods')).toContain('GET');
|
||||
expect(preflight.headers.get('access-control-allow-origin')).toBe('https://a.com');
|
||||
expect(calls).toBe(1);
|
||||
|
||||
// No Origin header never reaches the predicate at all — that part was already right.
|
||||
calls = 0;
|
||||
await fetch(`${getUrl(server)}/ping`);
|
||||
expect(calls).toBe(0);
|
||||
|
||||
server.close();
|
||||
});
|
||||
|
||||
});
|
||||
Reference in new issue
Block a user