Skip to content

fix(response): send raw bytes of non-Uint8Array typed arrays in res.send() - #7542

Open
princejain756 wants to merge 1 commit into
expressjs:masterfrom
princejain756:fix-send-typed-array-bytes
Open

princejain756 wants to merge 1 commit into
expressjs:masterfrom
princejain756:fix-send-typed-array-bytes

Conversation

@princejain756

Copy link
Copy Markdown

res.send() mishandles ArrayBuffer views that are not Uint8Array:

res.send(new Uint16Array(2).fill(0x0102))
  • small payload without ETag: res.end() rejects the view and returns 500
  • with ETag, or above 1000 elements: Buffer.from(view) writes one byte per element, so 0x0102 reaches the wire as 0x02, and Content-Length/ETag disagree with the body

Any non-Buffer view is now wrapped in a Buffer sharing its memory before length, ETag and write, so the raw bytes are sent as-is. Uint8Array and DataView inputs take the same path and produce identical output.

Fixes #7538

@Gustavo-Luiz2304

Copy link
Copy Markdown

I reviewed this together with #7574 (which carries this same commit) and #7511.

The fix looks correct to me. Converting every non-Buffer ArrayBuffer view with Buffer.from(view.buffer, view.byteOffset, view.byteLength) before the length / ETag / res.end() steps means all three use the same bytes. Because the conversion happens in the length block, it covers both etag on/off and the < 1000 / >= 1000 paths in one place. I merged it onto current master locally: it applies cleanly, and npm test (1293 passing) and lint pass. Without the lib/ change, the new tests fail as expected.

A few notes:

  1. Endianness in the tests. The [0x02, 0x01, 0x04, 0x03] expectations assume a little-endian host. fix(res.send): preserve raw bytes of ArrayBuffer views #7574 changes them to Buffer.from(body.buffer, body.byteOffset, body.byteLength). That works on any host, but it's the same expression the implementation uses, so it no longer checks the bytes independently. Building the input from known bytes avoids both problems, for example:

    var body = new Uint16Array(new Uint8Array([0x02, 0x01, 0x04, 0x03]).buffer)
    // expected: Buffer.from([0x02, 0x01, 0x04, 0x03]) on any host
  2. No coverage for offset windows yet. The byteOffset / byteLength handling is the subtle part. A view over part of a larger buffer, like new Uint16Array(buf, 2, 2) or a DataView with an offset, would catch a regression to Buffer.from(view.buffer). fix(res.send): preserve raw bytes of ArrayBuffer views #7574 has cases like this.

  3. Overlap with fix(res.send): preserve DataView byte ranges #7511. fix(res.send): preserve DataView byte ranges #7511 fixes only the DataView case (res.send(DataView) silently drops the view’s bytes #7510). The generic ArrayBuffer.isView branch here already covers that. The two PRs conflict in test/res.send.js, so it's probably worth landing just one.

  4. Plain ArrayBuffer is still unhandled. res.send(new ArrayBuffer(n)) still goes to res.json() (res.send(ArrayBuffer) silently sends {} as JSON #7362). That's fine to keep separate, just noting it isn't covered here.

…end()

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 expressjs#7538
@princejain756
princejain756 force-pushed the fix-send-typed-array-bytes branch from ca6d202 to 9d49e4a Compare November 9, 2026 07:51
@princejain756

Copy link
Copy Markdown
Author

Good call on both points @Gustavo-Luiz2304. Building the Uint16Array over new Uint8Array([0x02, 0x01, 0x04, 0x03]).buffer keeps the byte assertion independent of host endianness without mirroring the Buffer.from(view.buffer, view.byteOffset, view.byteLength) call in the test. I updated those two cases and added a slice test covering new Uint16Array(raw, 2, 2) and new DataView(raw, 1, 5) so byteOffset and byteLength are checked directly.

@dvmhmdsd

Copy link
Copy Markdown

There are 5 open PRs for this now, so I ran the same bodies through all of them (etag on and off) to compare:

Body master #7542 / #7574 #7511 #7363 #7410
Uint16Array / Float32Array wrong bytes, or 500 with etag off ✅ ❌ ❌ ❌
DataView empty body ✅ ✅ ❌ ❌ (500)
ArrayBuffer {} ❌ ❌ ✅ ✅
SharedArrayBuffer {} ❌ ❌ ✅ ❌

I think this one (or #7574, since it's the same commit with more tests) is the right base. About the Uint16Array concern from #7433: I don't think anyone can depend on the current output, because with etag off it's already a 500. Sending the raw bytes is also what new Response(new Uint16Array(...)) does, so it feels like the expected behavior.

ArrayBuffer could be added here too. I tried this locally and everything passed (tests + lint):

} else if (ArrayBuffer.isView(chunk) || isAnyArrayBuffer(chunk)) {
  if (isAnyArrayBuffer(chunk)) chunk = Buffer.from(chunk)

isAnyArrayBuffer is from node:util types. It also covers SharedArrayBuffer, and buffers created in another realm (like a vm context).

@princejain756

Copy link
Copy Markdown
Author

Thanks for running that matrix across all 5 PRs @dvmhmdsd, that comparison is super helpful. Agreed on the Uint16Array concern too since etag: false already returns a 500 on master.

For raw ArrayBuffer and SharedArrayBuffer, isAnyArrayBuffer would also need to be checked in the switch (typeof chunk) branch around line 151 so it sets Content-Type to bin instead of falling through to this.json(chunk). I kept this PR scoped to ArrayBuffer.isView since #7362 tracks raw ArrayBuffer separately (and changing {} to binary is a behavior shift), though I am happy to add isAnyArrayBuffer here if a maintainer prefers landing both together.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

res.send() with a non-Uint8Array typed array loses data, or returns 500 without ETag

3 participants