fix(cors): deny the origin when the origins predicate throws
`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>
This commit is contained in:
6 files changed
+101
-15
No files matched your search
+16
-6
@@ -52,8 +52,10 @@ describe('asReadableStream', () => {
|
||||
});
|
||||
|
||||
describe('buildFetch request gate', () => {
|
||||
it('does not permanently exhaust the gate when CORS setup throws', async () => {
|
||||
let attempts = 0;
|
||||
it('rides out a transient CORS predicate failure without leaking a gate slot', async () => {
|
||||
// A flag rather than an attempt counter: `computeInjected` runs both in this adapter and again
|
||||
// inside `handle()`, so the predicate is consulted more than once per request.
|
||||
let lookupDown = true;
|
||||
|
||||
const fetchHandler = buildFetch(
|
||||
[
|
||||
@@ -68,8 +70,7 @@ describe('buildFetch request gate', () => {
|
||||
limits: { maxConcurrentRequests: 2 },
|
||||
cors: {
|
||||
origins: () => {
|
||||
attempts++;
|
||||
if (attempts <= 2) throw new Error('transient CORS failure');
|
||||
if (lookupDown) throw new Error('transient CORS failure');
|
||||
return true;
|
||||
},
|
||||
},
|
||||
@@ -81,11 +82,20 @@ describe('buildFetch request gate', () => {
|
||||
headers: { origin: 'https://example.com' },
|
||||
});
|
||||
|
||||
await expect(fetchHandler(request())).rejects.toThrow('transient CORS failure');
|
||||
await expect(fetchHandler(request())).rejects.toThrow('transient CORS failure');
|
||||
// While the lookup is down the origin is denied — the request is answered, just without the
|
||||
// header that would let the browser read it. A failed lookup has not said yes.
|
||||
for (let i = 0; i < 2; i++) {
|
||||
const denied = await fetchHandler(request());
|
||||
expect(denied.status).toBe(200);
|
||||
expect(denied.headers.get('access-control-allow-origin')).toBeNull();
|
||||
}
|
||||
|
||||
// Once the lookup recovers the origin is allowed again, which also proves the two failures
|
||||
// returned their slots: a budget of 2 that leaked would have shed this one.
|
||||
lookupDown = false;
|
||||
const recovered = await fetchHandler(request());
|
||||
expect(recovered.status).toBe(200);
|
||||
expect(recovered.headers.get('access-control-allow-origin')).toBe('https://example.com');
|
||||
});
|
||||
|
||||
it('rejects requests above maxConcurrentRequests and releases the slot afterwards', async () => {
|
||||
|
||||
Reference in new issue
Block a user