From 2eafc6fe69c64bdad1f33f48470488616252068c Mon Sep 17 00:00:00 2001 From: Tim Dorr Date: Wed, 22 Jan 2025 17:47:18 -0500 Subject: [PATCH 1/2] Strip HTTP/2 pseudo headers from dev server requests When HTTPS is enabled, the dev server switches to HTTP/2. This creates H2 psuedo-headers in the request that are currently incompatible with unidici's parsing. This strips out those headers, with special handling for :authority (see RFC 9113: https://www.rfc-editor.org/rfc/rfc9113.html#section-8.3.1-2.3.5). Fixes #12558 --- .../react-router-dev/vite/node-adapter.ts | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/packages/react-router-dev/vite/node-adapter.ts b/packages/react-router-dev/vite/node-adapter.ts index 45fbcae0ce..8803e34ce1 100644 --- a/packages/react-router-dev/vite/node-adapter.ts +++ b/packages/react-router-dev/vite/node-adapter.ts @@ -1,4 +1,4 @@ -import type { IncomingHttpHeaders, ServerResponse } from "node:http"; +import type { IncomingMessage, ServerResponse } from "node:http"; import { once } from "node:events"; import { Readable } from "node:stream"; import { splitCookiesString } from "set-cookie-parser"; @@ -12,7 +12,20 @@ export type NodeRequestHandler = ( res: ServerResponse ) => Promise; -function fromNodeHeaders(nodeHeaders: IncomingHttpHeaders): Headers { +function fromNodeHeaders(nodeReq: IncomingMessage): Headers { + let nodeHeaders = nodeReq.headers; + + if (nodeReq.httpVersionMajor >= 2) { + nodeHeaders = { ...nodeHeaders }; + if (nodeHeaders[":authority"]) { + nodeHeaders.host = nodeHeaders[":authority"] as string; + } + delete nodeHeaders[":authority"]; + delete nodeHeaders[":method"]; + delete nodeHeaders[":path"]; + delete nodeHeaders[":scheme"]; + } + let headers = new Headers(); for (let [key, values] of Object.entries(nodeHeaders)) { @@ -50,7 +63,7 @@ export function fromNodeRequest( let controller: AbortController | null = new AbortController(); let init: RequestInit = { method: nodeReq.method, - headers: fromNodeHeaders(nodeReq.headers), + headers: fromNodeHeaders(nodeReq), signal: controller.signal, }; From d8078a0d689b16e5bff9cf30fdf5f110f400ffb1 Mon Sep 17 00:00:00 2001 From: Tim Dorr Date: Wed, 22 Jan 2025 17:50:44 -0500 Subject: [PATCH 2/2] Add a changeset --- .changeset/sharp-sloths-swim.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/sharp-sloths-swim.md diff --git a/.changeset/sharp-sloths-swim.md b/.changeset/sharp-sloths-swim.md new file mode 100644 index 0000000000..5e1ae016a9 --- /dev/null +++ b/.changeset/sharp-sloths-swim.md @@ -0,0 +1,5 @@ +--- +"@react-router/dev": patch +--- + +Strip HTTP/2 pseudo headers from dev server requests - Fixes using HTTPS with the dev server