Commit 4da7a4baaff for nodejs
commit 4da7a4baaff89e3b8a95078694b62d1c537cd7da
Author: Paolo Insogna <paolo@cowtech.it>
Date: Mon Nov 23 18:15:43 2026 +0100
http: compute body chunk length from decoded bytes
Convert base64, base64url and hex strings to a Buffer before computing
the chunk length, so the announced length matches the bytes written
even when the input contains characters skipped while decoding.
Assisted-By: OpenAI GPT-5.6 Sol <openai/gpt-5.6-sol>
Signed-off-by: Paolo Insogna <paolo@cowtech.it>
PR-URL: https://github.com/nodejs/node/pull/67615
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Ethan Arrowood <ethan@arrowood.dev>
diff --git a/lib/internal/http/outgoing.js b/lib/internal/http/outgoing.js
index c328ed2b06c..6dfdbb24441 100644
--- a/lib/internal/http/outgoing.js
+++ b/lib/internal/http/outgoing.js
@@ -1081,6 +1081,23 @@ function strictContentLength(msg) {
);
}
+// Encodings for which `Buffer.byteLength()` is only an upper bound of the
+// number of bytes `Buffer.from()` actually produces (e.g. base64 skips
+// whitespace and stops at invalid characters, hex stops at the first
+// invalid pair).
+function decodeLossyEncodedChunk(chunk, encoding) {
+ if (typeof chunk === 'string' && typeof encoding === 'string') {
+ switch (encoding.toLowerCase()) {
+ case 'base64':
+ case 'base64url':
+ case 'hex':
+ return Buffer.from(chunk, encoding);
+ }
+ }
+
+ return chunk;
+}
+
function write_(msg, chunk, encoding, callback, fromEnd) {
if (typeof callback !== 'function')
callback = nop;
@@ -1092,6 +1109,9 @@ function write_(msg, chunk, encoding, callback, fromEnd) {
'chunk', ['string', 'Buffer', 'Uint8Array'], chunk);
}
+ // Compute lengths from the bytes that will actually be sent.
+ chunk = decodeLossyEncodedChunk(chunk, encoding);
+
let err;
if (msg.finished) {
err = new ERR_STREAM_WRITE_AFTER_END();
@@ -1323,6 +1343,8 @@ OutgoingMessage.prototype.end = function end(chunk, encoding, callback) {
this[kSocket].cork();
}
+ chunk = decodeLossyEncodedChunk(chunk, encoding);
+
if (maybePrepareFinalChunk(this, chunk, encoding)) {
// If just one final write is required, with nothing to follow, we
// attach finish to the write to avoid a separate send() & tick step
diff --git a/test/parallel/test-http-outgoing-encoded-chunk-length.js b/test/parallel/test-http-outgoing-encoded-chunk-length.js
new file mode 100644
index 00000000000..b28b0f54ddf
--- /dev/null
+++ b/test/parallel/test-http-outgoing-encoded-chunk-length.js
@@ -0,0 +1,96 @@
+'use strict';
+const common = require('../common');
+const assert = require('assert');
+const http = require('http');
+const net = require('net');
+
+// The length announced for a chunk (or in Content-Length) must match the
+// number of bytes actually written, also for encodings where the decoded size
+// is smaller than the one estimated by `Buffer.byteLength()`.
+
+const cases = [
+ // [string, encoding, expected decoded bytes]
+ ['QUJD REVG', 'base64', 'ABCDEF'],
+ ['QUJD\r\n\r\nREVG', 'BASE64', 'ABCDEF'],
+ ['QUJD REVG', 'base64url', 'ABCDEF'],
+ ['41424 3', 'hex', 'AB'],
+ ['4142zz4344', 'hex', 'AB'],
+ ['414', 'hex', 'A'],
+];
+
+// Reads the raw request, returning the body bytes de-framed according to
+// the framing used. Fails if the framing is not exactly consumed.
+function parseChunked(raw) {
+ let rest = raw;
+ let body = '';
+ for (;;) {
+ const eol = rest.indexOf('\r\n');
+ assert.notStrictEqual(eol, -1);
+ const size = parseInt(rest.slice(0, eol), 16);
+ rest = rest.slice(eol + 2);
+ if (size === 0) {
+ assert.strictEqual(rest, '\r\n');
+ return body;
+ }
+ body += rest.slice(0, size);
+ assert.strictEqual(rest.slice(size, size + 2), '\r\n');
+ rest = rest.slice(size + 2);
+ }
+}
+
+function run(send, isComplete, verify) {
+ const server = net.createServer(common.mustCall((socket) => {
+ let data = '';
+ socket.setEncoding('latin1');
+ socket.on('data', (chunk) => {
+ data += chunk;
+ const idx = data.indexOf('\r\n\r\n');
+ if (idx === -1) return;
+ const head = data.slice(0, idx);
+ const raw = data.slice(idx + 4);
+ if (isComplete(head, raw)) {
+ verify(head, raw);
+ socket.end('HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n');
+ }
+ });
+ }));
+
+ server.listen(0, common.mustCall(() => {
+ const req = http.request({ port: server.address().port, method: 'POST', agent: false },
+ common.mustCall((res) => {
+ res.resume();
+ res.on('end', common.mustCall(() => server.close()));
+ }));
+ send(req);
+ }));
+}
+
+for (const [str, encoding, expected] of cases) {
+ // Chunked, write() then end()
+ run((req) => {
+ req.write(str, encoding);
+ req.end();
+ }, (head, raw) => raw.endsWith('0\r\n\r\n'), common.mustCall((head, raw) => {
+ assert.match(head, /Transfer-Encoding: chunked/i);
+ assert.strictEqual(parseChunked(raw), expected);
+ }));
+
+ // Chunked, write() twice so a following chunk would be mis-framed
+ run((req) => {
+ req.write(str, encoding);
+ req.write('tail');
+ req.end();
+ }, (head, raw) => raw.endsWith('0\r\n\r\n'), common.mustCall((head, raw) => {
+ assert.strictEqual(parseChunked(raw), `${expected}tail`);
+ }));
+
+ // Single final chunk: Content-Length is computed
+ run((req) => {
+ req.end(str, encoding);
+ }, (head, raw) => raw.length >= expected.length, common.mustCall((head, raw) => {
+ const match = /Content-Length: (\d+)/i.exec(head);
+ assert.ok(match);
+ assert.strictEqual(Number(match[1]), expected.length);
+ assert.strictEqual(raw, expected);
+ }));
+}