-
Notifications
You must be signed in to change notification settings - Fork 4.4k
node:http2.createServer — fix h2c compatibility with strict peers #29075
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+450
−7
Merged
Changes from 2 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
dd5ca0d
Fix node:http2.createServer h2c compatibility with strict peers
robobun f4cb494
[autofix.ci] apply automated fixes
autofix-ci[bot] 41eae87
http2: address review feedback on h2c fix
robobun 40d8888
http2: fix sentTrailers state on _final noTrailers shortcut
robobun 9d6522d
http2: clamp enablePush=false in ServerHttp2Session.settings too
robobun 9b14d91
http2: fix StreamResponded read-modify-write race on synchronous handler
robobun 36043de
Merge branch 'main' into farm/879ce9fc/http2-h2c-server-fix
Jarred-Sumner c2e0d45
http2: null-guard respond() options, validate settings() input before…
robobun 3934885
http2: strip waitForTrailers when respond() forces endStream
robobun cbd75aa
fix: cite RFC 9113 §6.5.2 (not §7.2.2) in ENABLE_PUSH comments
cirospaciari bfdb9b8
Merge branch 'main' into farm/879ce9fc/http2-h2c-server-fix
cirospaciari 263870e
docs: cite RFC 9113 §8.1 and clarify empty-trailer rejection is a pee…
cirospaciari File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,173 @@ | ||
| // https://github.com/oven-sh/bun/issues/29073 | ||
| // | ||
| // `node:http2.createServer` fails for h2c (cleartext HTTP/2). | ||
| // | ||
| // Two separate bugs caused strict HTTP/2 peers (curl's nghttp2, Node's | ||
| // http2 client) to reject Bun's server with "callback failure": | ||
| // | ||
| // 1. The server's initial SETTINGS frame advertised `ENABLE_PUSH=1`. | ||
| // RFC 9113 §7.2.2 says any value other than 0 for ENABLE_PUSH sent | ||
| // by a server MUST be treated by the client as a PROTOCOL_ERROR. | ||
| // | ||
| // 2. `res.end("ok")` wrote an extra empty DATA frame followed by an | ||
| // empty trailer HEADERS frame. The compat `Http2ServerResponse` | ||
| // layer sets `waitForTrailers: true` and then unconditionally calls | ||
| // `sendTrailers({})` — Bun was emitting a zero-length trailer block | ||
| // instead of the single empty DATA with END_STREAM that Node sends. | ||
| import { expect, test } from "bun:test"; | ||
| import { once } from "node:events"; | ||
| import http2 from "node:http2"; | ||
| import net from "node:net"; | ||
|
|
||
| function parseFrames(buf: Buffer) { | ||
| const frames: { length: number; type: number; flags: number; streamId: number; payload: Buffer }[] = []; | ||
| let offset = 0; | ||
| while (offset + 9 <= buf.length) { | ||
| const length = buf.readUIntBE(offset, 3); | ||
| const type = buf[offset + 3]; | ||
| const flags = buf[offset + 4]; | ||
| const streamId = buf.readUInt32BE(offset + 5) & 0x7fffffff; | ||
| if (offset + 9 + length > buf.length) break; | ||
| frames.push({ | ||
| length, | ||
| type, | ||
| flags, | ||
| streamId, | ||
| payload: buf.slice(offset + 9, offset + 9 + length), | ||
| }); | ||
| offset += 9 + length; | ||
| } | ||
| return frames; | ||
| } | ||
|
|
||
| // Send a minimal HTTP/2 request over a raw TCP socket (prior-knowledge h2c) | ||
| // so we can inspect the exact bytes the server writes. This is what curl | ||
| // does with --http2-prior-knowledge. | ||
| async function rawH2cRequest(port: number) { | ||
| const preface = Buffer.from("PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n"); | ||
| // SETTINGS frame (empty payload) | ||
| const settings = Buffer.from([0, 0, 0, 0x04, 0, 0, 0, 0, 0]); | ||
|
|
||
| // HEADERS frame: stream 1, END_HEADERS|END_STREAM. | ||
| // HPACK: :method GET (0x82), :scheme http (0x86), :path / (0x84), | ||
| // :authority localhost:PORT (literal name-indexed 0x41 + length + value). | ||
| const authority = `localhost:${port}`; | ||
| const authLiteral = Buffer.alloc(2 + authority.length); | ||
| authLiteral[0] = 0x41; | ||
| authLiteral[1] = authority.length; | ||
| authLiteral.write(authority, 2); | ||
| const hpack = Buffer.concat([Buffer.from([0x82, 0x86, 0x84]), authLiteral]); | ||
| const headersFrame = Buffer.alloc(9 + hpack.length); | ||
| headersFrame.writeUIntBE(hpack.length, 0, 3); | ||
| headersFrame[3] = 0x01; // HEADERS | ||
| headersFrame[4] = 0x04 | 0x01; // END_HEADERS | END_STREAM | ||
| headersFrame.writeUInt32BE(1, 5); // stream id 1 | ||
| hpack.copy(headersFrame, 9); | ||
|
|
||
| const sock = net.connect({ port, host: "127.0.0.1" }); | ||
| await once(sock, "connect"); | ||
| sock.write(preface); | ||
| sock.write(settings); | ||
| sock.write(headersFrame); | ||
|
|
||
| const chunks: Buffer[] = []; | ||
| sock.on("data", chunk => chunks.push(chunk)); | ||
| // Wait for the server to close the connection (after END_STREAM trailer / | ||
| // final DATA frame). | ||
| const timer = setTimeout(() => sock.destroy(), 3000); | ||
| await once(sock, "close"); | ||
|
claude[bot] marked this conversation as resolved.
Outdated
|
||
| clearTimeout(timer); | ||
| return parseFrames(Buffer.concat(chunks)); | ||
| } | ||
|
|
||
| test("http2.createServer serves h2c response with well-formed frames (#29073)", async () => { | ||
| const server = http2.createServer((req, res) => { | ||
| res.setHeader("X-Foo", "bar"); | ||
| res.writeHead(200, { "Content-Type": "text/plain; charset=utf-8" }); | ||
| res.end("ok"); | ||
| }); | ||
| await once(server.listen(0), "listening"); | ||
| try { | ||
| const port = (server.address() as net.AddressInfo).port; | ||
| const frames = await rawH2cRequest(port); | ||
|
|
||
| // A) The server's initial SETTINGS frame must not advertise | ||
| // ENABLE_PUSH != 0 — that's a connection error per RFC 9113 §7.2.2. | ||
| const serverSettings = frames.find(f => f.type === 4 && (f.flags & 0x1) === 0); | ||
| expect(serverSettings).toBeDefined(); | ||
| // Walk settings entries (6 bytes each: 2-byte id, 4-byte value) and | ||
| // assert there is no ENABLE_PUSH setting with a nonzero value. | ||
| const settingsPayload = serverSettings!.payload; | ||
| for (let i = 0; i + 6 <= settingsPayload.length; i += 6) { | ||
| const id = settingsPayload.readUInt16BE(i); | ||
| const value = settingsPayload.readUInt32BE(i + 2); | ||
| if (id === 0x02) { | ||
| // SETTINGS_ENABLE_PUSH | ||
| expect(value).toBe(0); | ||
| } | ||
| } | ||
|
|
||
| // B) The response HEADERS frame should be present on stream 1. | ||
| const responseHeaders = frames.find(f => f.type === 1 && f.streamId === 1); | ||
| expect(responseHeaders).toBeDefined(); | ||
|
|
||
| // C) Exactly one DATA frame should carry the body payload; the stream | ||
| // must be terminated by a DATA frame with END_STREAM, NOT by a | ||
| // trailer HEADERS frame with an empty header block. Empty trailer | ||
| // blocks are rejected by strict peers (nghttp2 callback failure). | ||
| const stream1Data = frames.filter(f => f.type === 0 && f.streamId === 1); | ||
| expect(stream1Data.length).toBeGreaterThanOrEqual(1); | ||
| const bodyBytes = Buffer.concat(stream1Data.map(f => f.payload)); | ||
| expect(bodyBytes.toString("utf8")).toBe("ok"); | ||
| // The last DATA frame on stream 1 must carry END_STREAM. | ||
| const lastData = stream1Data[stream1Data.length - 1]; | ||
| expect(lastData.flags & 0x1).toBe(0x1); | ||
|
|
||
| // There must be no trailer HEADERS frame emitted by the server on | ||
| // stream 1 after the response headers (would be a second HEADERS | ||
| // frame on the same stream). | ||
| const stream1Headers = frames.filter(f => f.type === 1 && f.streamId === 1); | ||
| expect(stream1Headers).toHaveLength(1); | ||
| } finally { | ||
|
robobun marked this conversation as resolved.
|
||
| server.close(); | ||
| } | ||
| }); | ||
|
|
||
| // Exercise the exact client→server path from the issue (curl prior | ||
| // knowledge succeeds, so Node's http2 client should too against Bun). | ||
| test("http2.connect client can read h2c response from http2.createServer (#29073)", async () => { | ||
| const server = http2.createServer((req, res) => { | ||
| res.setHeader("X-Foo", "bar"); | ||
| res.writeHead(200, { "Content-Type": "text/plain; charset=utf-8" }); | ||
| res.end("ok"); | ||
| }); | ||
| await once(server.listen(0), "listening"); | ||
| try { | ||
| const port = (server.address() as net.AddressInfo).port; | ||
| const client = http2.connect(`http://127.0.0.1:${port}`); | ||
| try { | ||
| const req = client.request({ ":path": "/" }); | ||
| req.setEncoding("utf8"); | ||
|
|
||
| const { promise, resolve, reject } = Promise.withResolvers<{ status: number; body: string }>(); | ||
| let body = ""; | ||
| let status = 0; | ||
| req.on("response", headers => { | ||
| status = headers[":status"] as number; | ||
| }); | ||
| req.on("data", chunk => { | ||
| body += chunk; | ||
| }); | ||
| req.on("end", () => resolve({ status, body })); | ||
| req.on("error", reject); | ||
| req.end(); | ||
|
|
||
| const result = await promise; | ||
| expect(result).toEqual({ status: 200, body: "ok" }); | ||
| } finally { | ||
| client.close(); | ||
| } | ||
| } finally { | ||
| server.close(); | ||
| } | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.