From c7e997621453878de1cc2336d0689a56e7f82672 Mon Sep 17 00:00:00 2001 From: Matteo Collina Date: Fri, 6 Nov 2026 10:59:06 +0100 Subject: [PATCH] fix(milo): apply idle socket validation and reject unsolicited responses The experimental Milo parser missed both parts of the GHSA-35p6-xmwp-9g52 fix that the llhttp parser has: - it never set kSocketUsed, which was private to client-h1.js, so the idle socket validation never ran before an idle socket was reused; - it did not reject a response that starts while no request is running, so buffered unsolicited bytes were attributed to the request queued next. Move kSocketUsed to core/symbols.js and set it when a Milo response completes, as the llhttp parser does. Reject a response that starts, or completes its headers, while no request is running, with the same 'bad response' SocketError. Refs: https://github.com/nodejs/undici/security/advisories/GHSA-35p6-xmwp-9g52 Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Sfj4nEWQ4ovycNDjbvXcRs Signed-off-by: Matteo Collina --- lib/core/symbols.js | 1 + lib/dispatcher/client-h1.js | 4 +-- lib/dispatcher/parser-h1.js | 20 +++++++++++++- test/response-queue-poisoning.js | 45 ++++++++++++++++++++++++++++++++ 4 files changed, 67 insertions(+), 3 deletions(-) diff --git a/lib/core/symbols.js b/lib/core/symbols.js index 62163ebe2d6..5858730143a 100644 --- a/lib/core/symbols.js +++ b/lib/core/symbols.js @@ -65,6 +65,7 @@ module.exports = { kConstruct: Symbol('constructable'), kListeners: Symbol('listeners'), kHTTPContext: Symbol('http context'), + kSocketUsed: Symbol('socket used'), kMaxConcurrentStreams: Symbol('max concurrent streams'), kHostAuthority: Symbol('host authority'), kHTTP2InitialWindowSize: Symbol('http2 initial window size'), diff --git a/lib/dispatcher/client-h1.js b/lib/dispatcher/client-h1.js index a26b9c014e8..3a04fa9d761 100644 --- a/lib/dispatcher/client-h1.js +++ b/lib/dispatcher/client-h1.js @@ -53,7 +53,8 @@ const { kResume, kHTTPContext, kClosed, - kUseMilo + kUseMilo, + kSocketUsed } = require('../core/symbols.js') const constants = require('../llhttp/constants.js') @@ -66,7 +67,6 @@ const FastBuffer = Buffer[Symbol.species] const removeAllListeners = util.removeAllListeners const kIdleSocketValidation = Symbol('kIdleSocketValidation') const kIdleSocketValidationTimeout = Symbol('kIdleSocketValidationTimeout') -const kSocketUsed = Symbol('kSocketUsed') const kTypeOfService = Symbol('kTypeOfService') let extractBody diff --git a/lib/dispatcher/parser-h1.js b/lib/dispatcher/parser-h1.js index c1440170fd2..b9f7c15f124 100644 --- a/lib/dispatcher/parser-h1.js +++ b/lib/dispatcher/parser-h1.js @@ -34,7 +34,9 @@ const { kMaxResponseSize, kResume, kHTTPContext, - kMaxHeadersSize + kMaxHeadersSize, + kPending, + kSocketUsed } = require('../core/symbols.js') const FastBuffer = Buffer[Symbol.species] @@ -598,6 +600,13 @@ class H1Parser { return this.fail(this.milo.ERROR_CALLBACK_ERROR, 'Other side closed') } + // A response while no request is running is unsolicited. Do not + // attribute it to a request that is queued but not yet written. + if (client[kRunning] === 0) { + util.destroy(socket, new SocketError('bad response', util.getSocketInfo(socket))) + return this.fail(this.milo.ERROR_CALLBACK_ERROR, 'Bad response') + } + const request = client[kQueue][client[kRunningIdx]] if (!request) { return this.fail(this.milo.ERROR_CALLBACK_ERROR, 'Missing request') @@ -673,6 +682,7 @@ class H1Parser { request.onResponseEnd(trailers) client[kQueue][client[kRunningIdx]++] = null + socket[kSocketUsed] = client[kPending] === 0 if (socket[kWriting]) { assert(client[kRunning] === 0) @@ -773,6 +783,14 @@ class H1Parser { return this.fail(milo.ERROR_CALLBACK_ERROR, 'Other side closed') } + if (client[kRunning] === 0) { + util.destroy( + socket, + new SocketError('bad response', util.getSocketInfo(socket)) + ) + return this.fail(milo.ERROR_CALLBACK_ERROR, 'Bad response') + } + const request = client[kQueue][client[kRunningIdx]] if (!request) { return this.fail(milo.ERROR_CALLBACK_ERROR, 'Missing request') diff --git a/test/response-queue-poisoning.js b/test/response-queue-poisoning.js index 9cb482791e3..bd22983a369 100644 --- a/test/response-queue-poisoning.js +++ b/test/response-queue-poisoning.js @@ -55,3 +55,48 @@ test('should not reuse an idle socket with buffered unsolicited response bytes', assert.strictEqual(response2.headers['poison-free-socket'], undefined) assert.strictEqual(await readBody(response2.body), '/request2') }) + +for (const useMilo of [false, true]) { + test(`should not attribute buffered unsolicited response bytes to a queued request (useMilo: ${useMilo})`, async () => { + let evilServerSocket + + const server = createServer((req, res) => { + if (!evilServerSocket) { + evilServerSocket = req.socket + } + + res.end(req.url) + }) + after(() => server.close()) + + await new Promise(resolve => server.listen(0, resolve)) + + const client = new Client(`http://localhost:${server.address().port}`, { + keepAliveTimeout: 300e3, + useMilo + }) + after(() => client.close()) + + const response1 = await client.request({ path: '/request1', method: 'GET' }) + assert.strictEqual(await readBody(response1.body), '/request1') + + // The unsolicited response reaches the client's receive buffer, but is + // not read yet when the next request is dispatched. + const response2 = await new Promise((resolve, reject) => { + setTimeout(() => { + evilServerSocket.write( + 'HTTP/1.1 200 OK\r\n' + + 'Poison-Free-Socket: true\r\n' + + 'Connection: keep-alive\r\n' + + 'Keep-Alive: timeout=300\r\n' + + 'Content-Length: 0\r\n' + + '\r\n' + ) + client.request({ path: '/request2', method: 'GET' }).then(resolve, reject) + }, 10) + }) + + assert.strictEqual(response2.headers['poison-free-socket'], undefined) + assert.strictEqual(await readBody(response2.body), '/request2') + }) +}