Skip to content

fix(res.send): preserve raw bytes of ArrayBuffer views - #7574

Open
jipeng6036-del wants to merge 3 commits into
expressjs:masterfrom
jipeng6036-del:fix/send-arraybuffer-view-bytes
Open

jipeng6036-del wants to merge 3 commits into
expressjs:masterfrom
jipeng6036-del:fix/send-arraybuffer-view-bytes

Conversation

@jipeng6036-del

@jipeng6036-del jipeng6036-del commented Oct 31, 2026 •

Copy link
Copy Markdown

res.send() already treats ArrayBuffer views as binary, but non-byte TypedArrays can be truncated to one byte per element or rejected by Node, depending on ETag generation and payload size. Wrap the view's existing byte range before calculating length, hashing or sending it, so the body, Content-Length and ETag agree.

Based on #7542 by Prince Jain (ca6d20298f21a751f55c596214a5feb3f8f4902f); his implementation commit, author and source reference are retained. The regression tests in test/res.send.js cover offset windows, DataView, wider typed arrays with ETag enabled and disabled, a large response, HEAD, and 304 revalidation. They compare raw bytes without assuming the host byte order.

Fixes #7538.

Validation:

  • Node 24.14.0/Windows at 51505499: npm run lint and npm run test-ci pass (1,296 tests). All six HTTP regressions fail on the base commit and pass with the fix.
  • At the previous test-only revision fca6bf6b, all 27 jobs in the unchanged fork workflows passed, including Node 16–26/Linux/Windows, lint, coverage and CodeQL: CodeQL, legacy, ci.
  • At fca6bf6b, three independently packed and installed npm artifacts were exercised with native http.request, without NODE_PATH overrides: baseline 15/40, original patch 40/40, this version 40/40. Every packed file matches its source Git blob.

Reproduction source and verification records are bound to commit fca6bf6b44be148981340301e6f5ec37c1ff849c.

Prepared and validated with Codex; no human review is claimed.

princejain756 and others added 2 commits October 31, 2026 08:18
…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

(cherry picked from commit ca6d202)
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 <princejain756@gmail.com>

@agammann agammann left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I traced the new Buffer conversion through res.send: it happens before Content-Length and ETag calculation, so the byte range selected by byteOffset/byteLength is also what reaches res.end. The HTTP tests exercise nonzero-offset DataView and wider typed arrays, plus HEAD and conditional 304 responses, which checks the header/body consistency this change is meant to restore.

@subhiandrews subhiandrews left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

code-check review

1 suggestion

Reviewed with code-check · Apache-2.0

Comment thread test/res.send.arraybuffer.js Outdated

describe('res.send ArrayBuffer views', function () {
views.forEach(function (view) {
[false, 'weak', 'strong'].forEach(function (etag) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion · tests

The two test sets for this fix overlap. The new file runs 13 view types x 6 HTTP cases (78 tests, each with its own app and request). The three tests added to test/res.send.js (lines 220-273: Uint16Array, Uint16Array with etag disabled, DataView) repeat cases the matrix already covers. Running the HEAD, 304 and chunked checks for every view type also tests unchanged code paths, because the fix only touches the length and Buffer wrapping in res.send. Keep the coverage in one place, and keep only the distinct behaviours: an offset window, a DataView, a wider type with ETag on and off, a large payload, and HEAD/304 once.

code-check · pragmatist, skeptic

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for pointing out the overlap. I moved the coverage into test/res.send.js and kept six HTTP regressions for offset windows, DataView, ETag on/off, a large payload, HEAD, and 304. All six fail on the original base and pass with the fix. The full suite passes (1,296 tests), and lint passes.

@subhiandrews subhiandrews left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

code-check review

No issues found.

Reviewed with code-check · Apache-2.0

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

4 participants