Skip to content

Commit 46336b3

Browse files
committed
fix(node/web): honor writeHead headers when statusMessage is not a string
1 parent 6882f6d commit 46336b3

2 files changed

Lines changed: 67 additions & 1 deletion

File tree

‎src/adapters/_node/web/response.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,8 @@ export class WebServerResponse extends ServerResponse {
113113
const result =
114114
typeof statusMessage === "string"
115115
? super.writeHead(statusCode, statusMessage, stripTransferEncoding(headers))
116-
: super.writeHead(statusCode, stripTransferEncoding(statusMessage));
116+
:
117+
super.writeHead(statusCode, stripTransferEncoding(headers ?? statusMessage));
117118
this.#onHeadersSent?.();
118119
return result;
119120
}

‎test/node-adapters.test.ts‎

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,10 @@ describe("adapters", () => {
321321
name: "writeHead with statusMessage",
322322
setHeaders: (res) => res.writeHead(200, "OK", { "Transfer-Encoding": "chunked" }),
323323
},
324+
{
325+
name: "writeHead with a non-string statusMessage",
326+
setHeaders: (res) => res.writeHead(200, undefined, { "Transfer-Encoding": "chunked" }),
327+
},
324328
{
325329
name: "writeHead flat array form",
326330
setHeaders: (res) => res.writeHead(200, ["transfer-encoding", "chunked"] as any),
@@ -353,6 +357,67 @@ describe("adapters", () => {
353357
}
354358
});
355359

360+
// `writeHead()` has two overloads and Node picks between them with
361+
// `obj ??= reason`: the headers argument is whichever of the last two is
362+
// present, and the third wins when both are. A reason phrase that is not a
363+
// string (`undefined`/`null`) therefore still leaves real headers in the third
364+
// argument -- what proxying an HTTP/2 upstream, which carries no status
365+
// message, produces. Dropping them would silently strip response headers the
366+
// handler set, including security ones like CSP.
367+
describe("writeHead forwards headers for every argument form", () => {
368+
const cases: { name: string; setHeaders: (res: any) => void }[] = [
369+
{
370+
name: "2-arg object form",
371+
setHeaders: (res) => res.writeHead(200, { "x-frame-options": "DENY" }),
372+
},
373+
{
374+
name: "3-arg with a string statusMessage",
375+
setHeaders: (res) => res.writeHead(200, "OK", { "x-frame-options": "DENY" }),
376+
},
377+
{
378+
name: "3-arg with an undefined statusMessage",
379+
setHeaders: (res) => res.writeHead(200, undefined, { "x-frame-options": "DENY" }),
380+
},
381+
{
382+
name: "3-arg with a null statusMessage",
383+
setHeaders: (res) => res.writeHead(200, null, { "x-frame-options": "DENY" }),
384+
},
385+
{
386+
name: "3-arg flat array form",
387+
setHeaders: (res) => res.writeHead(200, undefined, ["x-frame-options", "DENY"]),
388+
},
389+
{
390+
name: "3-arg nested array form",
391+
setHeaders: (res) => res.writeHead(200, undefined, [["x-frame-options", "DENY"]]),
392+
},
393+
];
394+
395+
for (const { name, setHeaders } of cases) {
396+
test(name, async () => {
397+
const handler: NodeHttp1Handler = (_req, res) => {
398+
setHeaders(res as any);
399+
res.end("hello");
400+
};
401+
const res = await toFetchHandler(handler)(new Request("http://localhost/"));
402+
expect(res.status).toBe(200);
403+
expect(await res.text()).toBe("hello");
404+
expect(res.headers.get("x-frame-options")).toBe("DENY");
405+
});
406+
}
407+
408+
// Node prefers the third argument when both could be headers; mirror it so
409+
// the bridge and a plain `http.Server` cannot disagree on which one applies.
410+
test("the third argument wins over a non-string second one", async () => {
411+
const handler: NodeHttp1Handler = (_req, res) => {
412+
(res as any).writeHead(200, { "x-from": "reason" }, { "x-from": "obj" });
413+
res.end("hello");
414+
};
415+
const res = await toFetchHandler(handler)(new Request("http://localhost/"));
416+
expect(res.headers.get("x-from")).toBe("obj");
417+
expect(await res.text()).toBe("hello");
418+
});
419+
});
420+
356421
// https://github.com/h3js/srvx/issues/248
357422
// Hop-by-hop headers (RFC 9110 §7.6.1) describe a single transport hop, not
358423
// the message. Node auto-generates `Connection` on the synthetic wire and a

0 commit comments

Comments
 (0)