From 6040a5a6b46c657a7172657eb062ff0e908e14e7 Mon Sep 17 00:00:00 2001 From: Prince Jain Date: Thu, 22 Oct 2026 21:20:40 +0530 Subject: [PATCH 1/3] fix(response): send raw bytes of non-Uint8Array typed arrays in res.send() A non-Buffer ArrayBuffer view reached res.end() unconverted, which 500s on small payloads, or passed through Buffer.from(view), which writes one byte per element instead of the view's bytes, silently truncating values like 0x0102 to 0x02. Wrap any non-Buffer view in a Buffer sharing its memory, so Content-Length, ETag and the body all agree on the raw bytes. Fixes #7538 (cherry picked from commit ca6d20298f21a751f55c596214a5feb3f8f4902f) --- lib/response.js | 6 ++++++ test/res.send.js | 51 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) diff --git a/lib/response.js b/lib/response.js index 0d5a4ae4d4d..bd293717bb2 100644 --- a/lib/response.js +++ b/lib/response.js @@ -167,6 +167,12 @@ res.send = function send(body) { if (Buffer.isBuffer(chunk)) { // get length of Buffer len = chunk.length + } else if (ArrayBuffer.isView(chunk)) { + // wrap non-Buffer views (TypedArrays, DataView) in a Buffer + // sharing their memory so the raw bytes are sent as-is + chunk = Buffer.from(chunk.buffer, chunk.byteOffset, chunk.byteLength) + encoding = undefined; + len = chunk.length } else if (!generateETag && chunk.length < 1000) { // just calculate length when no ETag + small chunk len = Buffer.byteLength(chunk, encoding) diff --git a/test/res.send.js b/test/res.send.js index e765c8c60c1..1ce858859ac 100644 --- a/test/res.send.js +++ b/test/res.send.js @@ -217,6 +217,57 @@ describe('res', function(){ .expect(200, "hey", done); }) + it('should send the bytes of other typed arrays', function(done){ + var app = express(); + + app.use(function(req, res){ + res.send(new Uint16Array([0x0102, 0x0304])); + }) + + request(app) + .get("/") + .expect("Content-Type", "application/octet-stream") + .expect(200) + .expect(function(res){ + assert.deepStrictEqual(res.body, Buffer.from([0x02, 0x01, 0x04, 0x03])); + }) + .end(done); + }) + + it('should send the bytes of other typed arrays when etag is disabled', function(done){ + var app = express(); + app.set('etag', false); + + app.use(function(req, res){ + res.send(new Uint16Array([0x0102, 0x0304])); + }) + + request(app) + .get("/") + .expect("Content-Type", "application/octet-stream") + .expect(200) + .expect(function(res){ + assert.deepStrictEqual(res.body, Buffer.from([0x02, 0x01, 0x04, 0x03])); + }) + .end(done); + }) + + it('should send the bytes of a DataView', function(done){ + var app = express(); + + app.use(function(req, res){ + res.send(new DataView(new Uint8Array([1, 2, 3]).buffer)); + }) + + request(app) + .get("/") + .expect(200) + .expect(function(res){ + assert.deepStrictEqual(res.body, Buffer.from([1, 2, 3])); + }) + .end(done); + }) + it('should not override ETag', function (done) { var app = express() From fca6bf6b44be148981340301e6f5ec37c1ff849c Mon Sep 17 00:00:00 2001 From: jipeng6036-del Date: Sat, 31 Oct 2026 08:21:15 -0700 Subject: [PATCH 2/3] test(response): cover raw view bytes across HTTP response paths Exercise byte-offset windows, cross-realm/shared views, ETags, HEAD, chunked responses and cache revalidation. Derive the original typed-array expectations from its raw storage instead of assuming little-endian order. Co-authored-by: Prince Jain --- test/res.send.arraybuffer.js | 128 +++++++++++++++++++++++++++++++++++ test/res.send.js | 10 +-- 2 files changed, 134 insertions(+), 4 deletions(-) create mode 100644 test/res.send.arraybuffer.js diff --git a/test/res.send.arraybuffer.js b/test/res.send.arraybuffer.js new file mode 100644 index 00000000000..e79b2285b49 --- /dev/null +++ b/test/res.send.arraybuffer.js @@ -0,0 +1,128 @@ +'use strict'; + +var assert = require('node:assert'); +var Buffer = require('node:buffer').Buffer; +var vm = require('node:vm'); +var express = require('..'); +var request = require('supertest'); + +function typedWindow (Type, values) { + var padded = new Type(values.length + 2); + padded.set(values, 1); + return padded.subarray(1, -1); +} + +function rawBytes (view) { + return Buffer.from(Buffer.from(view.buffer, view.byteOffset, view.byteLength)); +} + +function binaryParser (res, callback) { + var chunks = []; + res.on('data', function (chunk) { chunks.push(chunk); }); + res.on('end', function () { callback(null, Buffer.concat(chunks)); }); + res.on('error', callback); +} + +var views = [ + { name: 'Uint16Array window', create: function () { return typedWindow(Uint16Array, [0x0102, 0x0304]); } }, + { name: 'Int16Array window', create: function () { return typedWindow(Int16Array, [-1, -32768]); } }, + { name: 'Uint32Array window', create: function () { return typedWindow(Uint32Array, [0x01020304]); } }, + { name: 'Float64Array window', create: function () { return typedWindow(Float64Array, [-0, Infinity]); } }, + { name: 'BigInt64Array window', create: function () { return typedWindow(BigInt64Array, [-1n, 0x0102030405060708n]); } }, + { name: 'DataView window', create: function () { + return new DataView(new Uint8Array([0xaa, 0, 0xff, 0x80, 0xbb]).buffer, 1, 3); + } }, + { name: 'empty DataView window', create: function () { return new DataView(new ArrayBuffer(4), 2, 0); } }, + { name: 'large Uint16Array', create: function () { return new Uint16Array(1001).fill(0x0102); } }, + { name: 'cross-realm Uint16Array', create: function () { return vm.runInNewContext('new Uint16Array([0x0102, 0x0304])'); } }, + { name: 'cross-realm DataView', create: function () { + return vm.runInNewContext('new DataView(new Uint8Array([170, 0, 255, 187]).buffer, 1, 2)'); + } }, + { name: 'shared-buffer Uint16Array', create: function () { + var view = new Uint16Array(new SharedArrayBuffer(8), 2, 2); + view.set([0x0102, 0x0304]); + return view; + } }, + { name: 'Uint8Array control', create: function () { return typedWindow(Uint8Array, [0, 0xff, 0x80]); } }, + { name: 'Buffer window control', create: function () { return Buffer.from([0xaa, 0, 0xff, 0xbb]).subarray(1, 3); } } +]; + +describe('res.send ArrayBuffer views', function () { + views.forEach(function (view) { + [false, 'weak', 'strong'].forEach(function (etag) { + it('sends ' + view.name + ' with etag=' + etag, function (done) { + var app = express(); + var body = view.create(); + var expected = rawBytes(body); + app.set('etag', etag); + app.use(function (req, res) { res.send(body); }); + + request(app).get('/') + .buffer(true).parse(binaryParser) + .expect('Content-Type', 'application/octet-stream') + .expect('Content-Length', String(expected.length)) + .expect(200) + .expect(function (res) { + assert.deepStrictEqual(res.body, expected); + assert.strictEqual(res.headers.etag, etag ? app.get('etag fn')(expected) : undefined); + }) + .end(done); + }); + }); + + it('sends ' + view.name + ' with chunked encoding and a custom type', function (done) { + var app = express(); + var body = view.create(); + var expected = rawBytes(body); + app.use(function (req, res) { + res.set('Content-Type', 'application/custom'); + res.set('Transfer-Encoding', 'chunked'); + res.set('ETag', '"caller-tag"'); + res.send(body); + }); + request(app).get('/') + .buffer(true).parse(binaryParser) + .expect('Content-Type', 'application/custom') + .expect('Transfer-Encoding', 'chunked') + .expect('ETag', '"caller-tag"') + .expect(200) + .expect(function (res) { + assert.strictEqual(res.headers['content-length'], undefined); + assert.deepStrictEqual(res.body, expected); + }) + .end(done); + }); + + it('reports byte length without sending ' + view.name + ' for HEAD', function (done) { + var app = express(); + var body = view.create(); + var expected = rawBytes(body); + app.use(function (req, res) { res.send(body); }); + request(app).head('/') + .buffer(true).parse(binaryParser) + .expect('Content-Length', String(expected.length)) + .expect('ETag', app.get('etag fn')(expected)) + .expect(200) + .expect(function (res) { + assert.strictEqual(res.text, undefined); + assert.deepStrictEqual(res.body, {}); + }) + .end(done); + }); + + it('revalidates ' + view.name + ' using the raw-byte ETag', function (done) { + var app = express(); + var body = view.create(); + var expected = rawBytes(body); + app.use(function (req, res) { res.send(body); }); + request(app).get('/') + .set('If-None-Match', app.get('etag fn')(expected)) + .expect(304) + .expect(function (res) { + assert.strictEqual(res.headers['content-length'], undefined); + assert.strictEqual(res.text, ''); + }) + .end(done); + }); + }); +}); diff --git a/test/res.send.js b/test/res.send.js index 1ce858859ac..1ef1282473b 100644 --- a/test/res.send.js +++ b/test/res.send.js @@ -219,9 +219,10 @@ describe('res', function(){ it('should send the bytes of other typed arrays', function(done){ var app = express(); + var body = new Uint16Array([0x0102, 0x0304]); app.use(function(req, res){ - res.send(new Uint16Array([0x0102, 0x0304])); + res.send(body); }) request(app) @@ -229,17 +230,18 @@ describe('res', function(){ .expect("Content-Type", "application/octet-stream") .expect(200) .expect(function(res){ - assert.deepStrictEqual(res.body, Buffer.from([0x02, 0x01, 0x04, 0x03])); + assert.deepStrictEqual(res.body, Buffer.from(body.buffer, body.byteOffset, body.byteLength)); }) .end(done); }) it('should send the bytes of other typed arrays when etag is disabled', function(done){ var app = express(); + var body = new Uint16Array([0x0102, 0x0304]); app.set('etag', false); app.use(function(req, res){ - res.send(new Uint16Array([0x0102, 0x0304])); + res.send(body); }) request(app) @@ -247,7 +249,7 @@ describe('res', function(){ .expect("Content-Type", "application/octet-stream") .expect(200) .expect(function(res){ - assert.deepStrictEqual(res.body, Buffer.from([0x02, 0x01, 0x04, 0x03])); + assert.deepStrictEqual(res.body, Buffer.from(body.buffer, body.byteOffset, body.byteLength)); }) .end(done); }) From 515054998a4626f2d057b8b2938c81977276ef17 Mon Sep 17 00:00:00 2001 From: jipeng6036-del Date: Fri, 20 Nov 2026 17:40:14 -0800 Subject: [PATCH 3/3] test(response): consolidate ArrayBuffer view regressions --- test/res.send.arraybuffer.js | 128 ----------------------------------- test/res.send.js | 100 +++++++++++++++++++++++---- 2 files changed, 85 insertions(+), 143 deletions(-) delete mode 100644 test/res.send.arraybuffer.js diff --git a/test/res.send.arraybuffer.js b/test/res.send.arraybuffer.js deleted file mode 100644 index e79b2285b49..00000000000 --- a/test/res.send.arraybuffer.js +++ /dev/null @@ -1,128 +0,0 @@ -'use strict'; - -var assert = require('node:assert'); -var Buffer = require('node:buffer').Buffer; -var vm = require('node:vm'); -var express = require('..'); -var request = require('supertest'); - -function typedWindow (Type, values) { - var padded = new Type(values.length + 2); - padded.set(values, 1); - return padded.subarray(1, -1); -} - -function rawBytes (view) { - return Buffer.from(Buffer.from(view.buffer, view.byteOffset, view.byteLength)); -} - -function binaryParser (res, callback) { - var chunks = []; - res.on('data', function (chunk) { chunks.push(chunk); }); - res.on('end', function () { callback(null, Buffer.concat(chunks)); }); - res.on('error', callback); -} - -var views = [ - { name: 'Uint16Array window', create: function () { return typedWindow(Uint16Array, [0x0102, 0x0304]); } }, - { name: 'Int16Array window', create: function () { return typedWindow(Int16Array, [-1, -32768]); } }, - { name: 'Uint32Array window', create: function () { return typedWindow(Uint32Array, [0x01020304]); } }, - { name: 'Float64Array window', create: function () { return typedWindow(Float64Array, [-0, Infinity]); } }, - { name: 'BigInt64Array window', create: function () { return typedWindow(BigInt64Array, [-1n, 0x0102030405060708n]); } }, - { name: 'DataView window', create: function () { - return new DataView(new Uint8Array([0xaa, 0, 0xff, 0x80, 0xbb]).buffer, 1, 3); - } }, - { name: 'empty DataView window', create: function () { return new DataView(new ArrayBuffer(4), 2, 0); } }, - { name: 'large Uint16Array', create: function () { return new Uint16Array(1001).fill(0x0102); } }, - { name: 'cross-realm Uint16Array', create: function () { return vm.runInNewContext('new Uint16Array([0x0102, 0x0304])'); } }, - { name: 'cross-realm DataView', create: function () { - return vm.runInNewContext('new DataView(new Uint8Array([170, 0, 255, 187]).buffer, 1, 2)'); - } }, - { name: 'shared-buffer Uint16Array', create: function () { - var view = new Uint16Array(new SharedArrayBuffer(8), 2, 2); - view.set([0x0102, 0x0304]); - return view; - } }, - { name: 'Uint8Array control', create: function () { return typedWindow(Uint8Array, [0, 0xff, 0x80]); } }, - { name: 'Buffer window control', create: function () { return Buffer.from([0xaa, 0, 0xff, 0xbb]).subarray(1, 3); } } -]; - -describe('res.send ArrayBuffer views', function () { - views.forEach(function (view) { - [false, 'weak', 'strong'].forEach(function (etag) { - it('sends ' + view.name + ' with etag=' + etag, function (done) { - var app = express(); - var body = view.create(); - var expected = rawBytes(body); - app.set('etag', etag); - app.use(function (req, res) { res.send(body); }); - - request(app).get('/') - .buffer(true).parse(binaryParser) - .expect('Content-Type', 'application/octet-stream') - .expect('Content-Length', String(expected.length)) - .expect(200) - .expect(function (res) { - assert.deepStrictEqual(res.body, expected); - assert.strictEqual(res.headers.etag, etag ? app.get('etag fn')(expected) : undefined); - }) - .end(done); - }); - }); - - it('sends ' + view.name + ' with chunked encoding and a custom type', function (done) { - var app = express(); - var body = view.create(); - var expected = rawBytes(body); - app.use(function (req, res) { - res.set('Content-Type', 'application/custom'); - res.set('Transfer-Encoding', 'chunked'); - res.set('ETag', '"caller-tag"'); - res.send(body); - }); - request(app).get('/') - .buffer(true).parse(binaryParser) - .expect('Content-Type', 'application/custom') - .expect('Transfer-Encoding', 'chunked') - .expect('ETag', '"caller-tag"') - .expect(200) - .expect(function (res) { - assert.strictEqual(res.headers['content-length'], undefined); - assert.deepStrictEqual(res.body, expected); - }) - .end(done); - }); - - it('reports byte length without sending ' + view.name + ' for HEAD', function (done) { - var app = express(); - var body = view.create(); - var expected = rawBytes(body); - app.use(function (req, res) { res.send(body); }); - request(app).head('/') - .buffer(true).parse(binaryParser) - .expect('Content-Length', String(expected.length)) - .expect('ETag', app.get('etag fn')(expected)) - .expect(200) - .expect(function (res) { - assert.strictEqual(res.text, undefined); - assert.deepStrictEqual(res.body, {}); - }) - .end(done); - }); - - it('revalidates ' + view.name + ' using the raw-byte ETag', function (done) { - var app = express(); - var body = view.create(); - var expected = rawBytes(body); - app.use(function (req, res) { res.send(body); }); - request(app).get('/') - .set('If-None-Match', app.get('etag fn')(expected)) - .expect(304) - .expect(function (res) { - assert.strictEqual(res.headers['content-length'], undefined); - assert.strictEqual(res.text, ''); - }) - .end(done); - }); - }); -}); diff --git a/test/res.send.js b/test/res.send.js index 1ef1282473b..d328aac46b9 100644 --- a/test/res.send.js +++ b/test/res.send.js @@ -217,20 +217,23 @@ describe('res', function(){ .expect(200, "hey", done); }) - it('should send the bytes of other typed arrays', function(done){ + it('should send only the bytes of a typed array window', function(done){ var app = express(); - var body = new Uint16Array([0x0102, 0x0304]); + var body = new Uint16Array([0xffff, 0x0102, 0x0304, 0xeeee]).subarray(1, 3); + var bytes = Buffer.from(body.buffer, body.byteOffset, body.byteLength); app.use(function(req, res){ res.send(body); - }) + }); request(app) - .get("/") - .expect("Content-Type", "application/octet-stream") + .get('/') + .expect('Content-Type', 'application/octet-stream') + .expect('Content-Length', String(bytes.length)) + .expect('ETag', app.get('etag fn')(bytes)) .expect(200) .expect(function(res){ - assert.deepStrictEqual(res.body, Buffer.from(body.buffer, body.byteOffset, body.byteLength)); + assert.deepStrictEqual(res.body, bytes); }) .end(done); }) @@ -238,34 +241,101 @@ describe('res', function(){ it('should send the bytes of other typed arrays when etag is disabled', function(done){ var app = express(); var body = new Uint16Array([0x0102, 0x0304]); + var bytes = Buffer.from(body.buffer); app.set('etag', false); app.use(function(req, res){ res.send(body); - }) + }); request(app) - .get("/") - .expect("Content-Type", "application/octet-stream") + .get('/') + .expect('Content-Type', 'application/octet-stream') + .expect('Content-Length', String(bytes.length)) .expect(200) .expect(function(res){ - assert.deepStrictEqual(res.body, Buffer.from(body.buffer, body.byteOffset, body.byteLength)); + assert.deepStrictEqual(res.body, bytes); + assert.strictEqual(res.headers.etag, undefined); }) .end(done); }) - it('should send the bytes of a DataView', function(done){ + it('should send only the bytes of a DataView window', function(done){ var app = express(); + var body = new DataView(new Uint8Array([0xaa, 0, 0xff, 0x80, 0xbb]).buffer, 1, 3); app.use(function(req, res){ - res.send(new DataView(new Uint8Array([1, 2, 3]).buffer)); - }) + res.send(body); + }); request(app) - .get("/") + .get('/') + .expect('Content-Type', 'application/octet-stream') + .expect('Content-Length', '3') + .expect(200) + .expect(function(res){ + assert.deepStrictEqual(res.body, Buffer.from([0, 0xff, 0x80])); + }) + .end(done); + }) + + it('should send the raw bytes of a large typed array', function(done){ + var app = express(); + var body = new Uint16Array(1001).fill(0x0102); + var bytes = Buffer.from(body.buffer); + + app.use(function(req, res){ + res.send(body); + }); + + request(app) + .get('/') + .expect('Content-Length', String(bytes.length)) + .expect('ETag', app.get('etag fn')(bytes)) .expect(200) .expect(function(res){ - assert.deepStrictEqual(res.body, Buffer.from([1, 2, 3])); + assert.deepStrictEqual(res.body, bytes); + }) + .end(done); + }) + + it('should report view length and etag without sending a HEAD body', function(done){ + var app = express(); + var body = new Uint16Array([0xffff, 0x0102, 0x0304, 0xeeee]).subarray(1, 3); + var bytes = Buffer.from(body.buffer, body.byteOffset, body.byteLength); + + app.use(function(req, res){ + res.send(body); + }); + + request(app) + .head('/') + .expect('Content-Length', String(bytes.length)) + .expect('ETag', app.get('etag fn')(bytes)) + .expect(200) + .expect(function(res){ + assert.strictEqual(res.text, undefined); + assert.deepStrictEqual(res.body, {}); + }) + .end(done); + }) + + it('should return 304 for a matching raw-byte view etag', function(done){ + var app = express(); + var body = new Uint16Array([0xffff, 0x0102, 0x0304, 0xeeee]).subarray(1, 3); + var bytes = Buffer.from(body.buffer, body.byteOffset, body.byteLength); + + app.use(function(req, res){ + res.send(body); + }); + + request(app) + .get('/') + .set('If-None-Match', app.get('etag fn')(bytes)) + .expect(304) + .expect(function(res){ + assert.strictEqual(res.headers['content-length'], undefined); + assert.strictEqual(res.text, ''); }) .end(done); })