From 09828734625116186fd9d66004209bec9e8d5208 Mon Sep 17 00:00:00 2001 From: Diego Resendez Date: Mon, 6 Jul 2026 19:14:17 -0500 Subject: [PATCH] fix(http): make injected security/CORS headers authoritative over case-variant handler keys Signed-off-by: Diego Resendez --- src/http.ts | 19 ++++++++++++++----- test/transport-security.test.ts | 31 ++++++++++++++++++++++++++++++- 2 files changed, 44 insertions(+), 6 deletions(-) diff --git a/src/http.ts b/src/http.ts index 9f67fc9..6abc63c 100644 --- a/src/http.ts +++ b/src/http.ts @@ -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 | 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 = { ...(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 = {}; + 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); diff --git a/test/transport-security.test.ts b/test/transport-security.test.ts index 69b6b31..043fc28 100644 --- a/test/transport-security.test.ts +++ b/test/transport-security.test.ts @@ -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'; }