From ba34b01a65a9daa9ed88b6714687f1690c8a90b0 Mon Sep 17 00:00:00 2001 From: Roberto Bianchi Date: Mon, 30 Nov 2026 15:25:18 +0100 Subject: [PATCH] inspector: avoid mutable push in http2 writev When a client HTTP/2 request body was sent with writev and mixed strings and Buffers, the network inspection collected the chunks with .push() on a private array, which looks up Array.prototype.push at call time. Replacing that method from user code changed the request body reported to the inspector. Use indexed assignments instead. No existing test reached this branch, so add a test case that sends a corked request body made of a string and a Buffer. Signed-off-by: Roberto Bianchi --- lib/internal/inspector/network_http2.js | 4 +- test/parallel/test-inspector-network-http2.js | 47 +++++++++++++++++++ 2 files changed, 49 insertions(+), 2 deletions(-) diff --git a/lib/internal/inspector/network_http2.js b/lib/internal/inspector/network_http2.js index 0f4aabf59cfd..f090e385b76d 100644 --- a/lib/internal/inspector/network_http2.js +++ b/lib/internal/inspector/network_http2.js @@ -149,9 +149,9 @@ function onClientStreamBodyChunkSent({ stream, writev, data, encoding }) { const buffers = []; for (let i = 0; i < data.length; ++i) { if (typeof data[i].chunk === 'string') { - buffers.push(Buffer.from(data[i].chunk, data[i].encoding)); + buffers[buffers.length] = Buffer.from(data[i].chunk, data[i].encoding); } else { - buffers.push(data[i].chunk); + buffers[buffers.length] = data[i].chunk; } } chunk = Buffer.concat(buffers); diff --git a/test/parallel/test-inspector-network-http2.js b/test/parallel/test-inspector-network-http2.js index 3e17c048124f..c68d0e5a790e 100644 --- a/test/parallel/test-inspector-network-http2.js +++ b/test/parallel/test-inspector-network-http2.js @@ -87,6 +87,17 @@ const handleStream = common.mustCallAtLeast((stream, headers) => { }, kTimeout); })); break; + case '/writev': + stream.on('data', (chunk) => { + body += chunk; + }); + + stream.on('end', common.mustCall(() => { + assert.strictEqual(body, JSON.stringify(requestBody)); + stream.respond(responseHeaders); + stream.end(serverResponse); + })); + break; case '/trigger-error': stream.close(http2.constants.NGHTTP2_STREAM_CLOSED); stream.on('error', common.expectsError({ @@ -283,6 +294,40 @@ async function testHttp2(secure = false) { assert.ok(delta > kDelta); } +async function testHttp2Writev() { + const origin = `http://localhost:${http2Server.address().port}`; + const body = JSON.stringify(requestBody); + + const requestWillBeSent = once(session, 'Network.requestWillBeSent'); + const loadingFinished = once(session, 'Network.loadingFinished'); + session.on('Network.loadingFailed', common.mustNotCall()); + + const client = http2.connect(origin); + const request = client.request({ + ...requestHeaders, + [http2.constants.HTTP2_HEADER_PATH]: '/writev', + }); + // Corked writes that mix a string and a Buffer are sent as a single writev. + request.cork(); + request.write(body.slice(0, 5)); + request.write(Buffer.from(body.slice(5))); + request.end(); + + request.resume(); + request.on('end', () => { + client.close(); + }); + + const [ requestEvent ] = await requestWillBeSent; + // The request body can only be read once it has been sent completely. + await loadingFinished; + + const { postData } = await session.post('Network.getRequestPostData', { + requestId: requestEvent.params.requestId + }); + assert.strictEqual(postData, body); +} + async function testHttp2Error(secure = false) { const port = (secure ? http2SecureServer : http2Server).address().port; const origin = (secure ? 'https' : 'http') + `://localhost:${port}`; @@ -321,6 +366,8 @@ const testNetworkInspection = async () => { session.removeAllListeners(); await testHttp2(true); session.removeAllListeners(); + await testHttp2Writev(); + session.removeAllListeners(); await testHttp2Error(); session.removeAllListeners(); await testHttp2Error(true);