fix(http): make injected security/CORS headers authoritative over case-variant handler keys
Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
2 files changed
+44
-6
No files matched your search
+14
-5
@@ -217,11 +217,20 @@ export function createHttpServer(
|
||||
(res as any).writeHead = (status: number, arg2?: any, arg3?: any) => {
|
||||
// normalize (statusMessage?, headers?) overloads
|
||||
const hdrs = (typeof arg2 === 'string' ? arg3 : arg2) as Record<string, any> | undefined;
|
||||
// injected LAST → CORS/security keys are authoritative (a handler cannot weaken them).
|
||||
// Handler's non-injected headers (content-type, etc.) survive via ...hdrs.
|
||||
const merged: Record<string, any> = { ...(hdrs ?? {}), ...injected };
|
||||
// Vary is list-valued: only merge when injected carries one (CORS active); else leave handler's Vary alone.
|
||||
if (injected['vary']) merged['vary'] = mergeVary(hdrs?.['vary'], injected['vary']);
|
||||
// injected (security/CORS) is authoritative. Node's writeHead does NOT dedupe case-variant
|
||||
// keys — both lines would ship and a browser honors the first — so drop any handler header
|
||||
// whose name case-insensitively collides with an injected one before merging.
|
||||
const merged: Record<string, any> = {};
|
||||
const injectedLower = new Set(Object.keys(injected).map((k) => k.toLowerCase()));
|
||||
let handlerVary: string | undefined;
|
||||
if (hdrs) for (const k of Object.keys(hdrs)) {
|
||||
const lk = k.toLowerCase();
|
||||
if (lk === 'vary') { handlerVary = hdrs[k]; continue; } // merged separately below
|
||||
if (!injectedLower.has(lk)) merged[k] = hdrs[k];
|
||||
}
|
||||
Object.assign(merged, injected);
|
||||
if (injected['vary']) merged['vary'] = mergeVary(handlerVary, injected['vary']);
|
||||
else if (handlerVary !== undefined) merged['vary'] = handlerVary; // preserve handler Vary when no CORS
|
||||
return typeof arg2 === 'string' ? orig(status, arg2, merged) : orig(status, merged);
|
||||
};
|
||||
const matched = matchRoute(routes, req.method ?? 'GET', path);
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { describe, it, expect, afterEach } from 'vitest';
|
||||
import https from 'https';
|
||||
import WebSocket from 'ws';
|
||||
import { createApp, Route, Get, Sse, Ws, Module, inbound, channel, ctx } from '../src';
|
||||
import { createApp, Route, Get, Sse, Ws, Module, Transformer, inbound, channel, ctx } from '../src';
|
||||
import { selfSignedTls } from './helpers/tls';
|
||||
|
||||
@Route('/')
|
||||
@@ -116,6 +116,35 @@ describe('security headers', () => {
|
||||
expect(hsts).toContain('max-age=');
|
||||
});
|
||||
|
||||
it('injected header wins over a case-variant handler header (no double line)', async () => {
|
||||
@Route('/') class S {
|
||||
@Get('/sneaky')
|
||||
@Transformer((v) => ({ status: 200, headers: { 'X-Content-Type-Options': 'SNIFF' }, body: JSON.stringify(v) }))
|
||||
sneaky() { return { a: 1 }; }
|
||||
}
|
||||
@Module({ mountpoint: '/', controllers: [S] }) class SMod {}
|
||||
app = createApp({ modules: [SMod] });
|
||||
const server = await app.listen(0);
|
||||
const port = (server.address() as any).port;
|
||||
const res = await fetch(`http://127.0.0.1:${port}/sneaky`);
|
||||
expect(res.headers.get('x-content-type-options')).toBe('nosniff'); // NOT 'SNIFF, nosniff'
|
||||
});
|
||||
|
||||
it("preserves a handler's Title-Case Vary when no CORS is configured", async () => {
|
||||
@Route('/') class S {
|
||||
@Get('/vary')
|
||||
@Transformer((v) => ({ status: 200, headers: { Vary: 'Accept-Encoding' }, body: JSON.stringify(v) }))
|
||||
vary() { return { a: 1 }; }
|
||||
}
|
||||
@Module({ mountpoint: '/', controllers: [S] }) class SMod {}
|
||||
app = createApp({ modules: [SMod] });
|
||||
const server = await app.listen(0);
|
||||
const port = (server.address() as any).port;
|
||||
const res = await fetch(`http://127.0.0.1:${port}/vary`);
|
||||
expect(res.headers.get('vary')).toBe('Accept-Encoding');
|
||||
expect(res.headers.get('x-content-type-options')).toBe('nosniff');
|
||||
});
|
||||
|
||||
it('stream response carries security headers', async () => {
|
||||
@Route('/') class S {
|
||||
@Sse('/stream') async *stream() { yield 'a'; }
|
||||
|
||||
Reference in new issue
Block a user