Skip to content

Fix res.send binary bodies, res.redirect, res.set, res.cookie and res.jsonp - #64

Merged
abedmurrar merged 4 commits into
masterfrom
fix/response-bugs
Nov 22, 2026
Merged

abedmurrar merged 4 commits into
masterfrom
fix/response-bugs

Conversation

@abedmurrar

@abedmurrar abedmurrar commented Nov 22, 2026 •

Copy link
Copy Markdown
Owner

Summary

Five res.* bug fixes from the "Bug fixes on the compatible line" roadmap rows:

Upstream fixes are ported with credit; two places go further than upstream (detached buffers, and res.send after an unknown res.set type), explained below.

Changes

  • src/response.ts
  • test/regressions/response.js: the 7 matching bug() tests are normal it() tests.
  • test/fork-response-bodies.js (new):
    • every typed-array kind (Int8 to BigUint64, and Float16Array where Node has it), whole and as a byteOffset/byteLength window, with ETags on and off;
    • DataView windows, empty views, views of detached buffers, cross-realm views;
    • ArrayBuffer, SharedArrayBuffer and a cross-realm ArrayBuffer, a detached ArrayBuffer, and a plain object with byteLength (still JSON);
    • HEAD, 304 on the bytes' ETag, the 1,000-element path, a preset Content-Type;
    • res.set with an unknown type through end, the object form, an empty value, send and json, a known shorthand, and an invalid type with / (still a 500);
    • res.cookie with maxAge 0, the options object unchanged, and expires undefined, null or a Date (unchanged);
    • res.redirect on GET and HEAD, and without Transfer-Encoding;
    • res.jsonp with several callbacks and a partly kept callback.
  • test/differential/:
    • scenarios.js: 6 new scenarios, and 12 hand-written scenarios in allowed, each with its reason.
    • generated.js and generated.spec.js: the generated scenarios get an allowed list too, keyed by call (res.send Float64Array, res.send ArrayBuffer, res.send DataView). Some of their scenarios give both sides the same 304 or 412, so each allowed call must differ in at least one scenario rather than in all of them; a test after the scenarios checks that.
  • Docs: ROADMAP rows [x], History.md "Bug fixes" bullets with credit, docs/fork/differences.md runtime entries.

Behaviour

Call Express 5.3.0 This PR
res.send(new Uint8Array([1,2,3]).buffer) {} as application/json bytes 01 02 03, application/octet-stream
res.send(new SharedArrayBuffer(n)) {} as JSON its bytes
res.send(new DataView(buf, 1, 3)) 200 with an empty body the 3 bytes
res.send(new Uint16Array([0x0102])) with ETag 1 byte, 02 2 bytes, 02 01 on little-endian
same, etag off, under 1,000 elements 500 (res.end rejects it) 2 bytes
res.send(new BigInt64Array(...)) 500 its bytes
res.send(Buffer), res.send(Uint8Array), a detached view unchanged unchanged
res.redirect(...) after Transfer-Encoding: chunked Content-Length too; Node's client rejects it no Content-Length
res.set('Content-Type', 'custom').end() Content-Type: false Content-Type: custom
res.set('Content-Type', 'custom').send('x') text/html; charset=utf-8 custom
res.set('Content-Type', 'custom').json(...), .send(Buffer), .sendFile(...) the method's default type custom
res.cookie('a', 'b', { maxAge: 60000, expires: 0 }) Max-Age=60; Path=/; Expires=... Max-Age=60; Path=/
res.jsonp(...) with ?callback=!!! text/javascript: /**/ typeof === 'function' && ({...}); application/json: {...}

Not changed: res.set('Content-Type', 'foo/bar baz').send('x') still answers 500 ("Invalid type"), as in 5.3.0. expires values other than 0 (undefined, null, a Date) still get the maxAge date.

Proof

Benchmark

n/a: bug fix; benchmark and instruction-count workflows are paused.

Risks / follow-ups

  • Unknown Content-Type now sticks. After res.set('Content-Type', 'custom'), res.send, res.json, res.jsonp and res.sendFile keep custom where 5.3.0 silently used their default type (it had stored false). That is what "keep the value as given" means, and nothing throws, but an app that relied on the default type would see a different header. The upstream PR alone would have made those calls answer 500.
  • A Content-Type with no / set by hand (res.setHeader('Content-Type', 'html')) followed by res.send('x') used to answer 500 and now sends it as given. An invalid type with a / still answers 500; I left that alone because no roadmap row covers it.
  • Shared memory: a non-Buffer view is sent through a Buffer over the same memory instead of a copy, as Buffer bodies always were. Changing the array right after res.send, before the socket writes it, can change what goes out; 5.3.0 already did this for small Uint8Array bodies without ETags.
  • Typings: typings/ still types CookieOptions.expires as a Date, as @types/express does, so TypeScript users need a cast to pass 0. Left unchanged because no signature changed.
  • Middleware suites: they run in CI on this PR.

Stack

  1. this PR, on master
  2. Honour a per-call etag option in res.sendFile #66 (sendFile etag option), on top of this one
  3. Fix res.render after a send, res.render options, and view names ending in "." #68 (rendering), on top of Honour a per-call etag option in res.sendFile #66

They share lines in ROADMAP.md, History.md, docs/fork/differences.md and the differential allowed lists, so they are stacked. Merge in that order.

….jsonp

src/response.ts, five fixes from the "Bug fixes on the compatible line"
roadmap rows:

- res.send sends the bytes of an ArrayBuffer, SharedArrayBuffer, DataView
  or any typed array (expressjs#7362, expressjs#7510, expressjs#7538). A view that isn't a Buffer is
  wrapped in a Buffer over its own byteOffset/byteLength window before the
  length, ETag and write, so the body, Content-Length and ETag agree with
  or without ETags. Ported from PR expressjs#7574 by Jasper (jipeng6036-del), based
  on PR expressjs#7542 by Prince Jain. ArrayBuffer and SharedArrayBuffer support
  from PR expressjs#7363 by Siddharth Mehta, detected with
  util.types.isAnyArrayBuffer instead of Symbol.toStringTag (which any
  object can fake). Not in upstream: a detached buffer sends an empty body,
  as 5.3.0 did for views, where Buffer.from would throw.
- res.redirect leaves out Content-Length when Transfer-Encoding is set,
  as res.send does (expressjs#7506). Ported from PR expressjs#7508 by 0d00ciallo0721.
- res.set('Content-Type', unknown) keeps the value as given instead of
  writing "false" (expressjs#7034). Ported from PR expressjs#7035 by Varun Chawla. Not in
  upstream: res.send with a string body keeps a Content-Type with no "/"
  as it is, since it has no charset to add. With the upstream change alone,
  res.set('Content-Type', 'custom').send('x') (and .json()) would throw
  "Invalid type" and answer 500, where 5.3.0 answered 200.
- res.cookie with expires: 0 and a maxAge sends Max-Age without Expires
  (expressjs#5150). Ported from PR expressjs#5151 by Mart (nbkhope).
- res.jsonp sanitises the callback before deciding on JSONP, so a callback
  with no allowed characters gets plain JSON instead of broken JavaScript.
  Ported from PR expressjs#7161 by Andrew Stellman.

Tests:
- The 7 matching bug() tests in test/regressions/response.js are normal
  tests now.
- test/fork-response-bodies.js: every typed-array kind, whole and as a
  window, with ETags on and off; DataView windows; ArrayBuffer,
  SharedArrayBuffer and a cross-realm ArrayBuffer; cross-realm views;
  detached buffers; empty views; HEAD; 304; the 1,000-element path; a
  preset Content-Type; res.set's unknown type through end, the object
  form, an empty value, send and json, plus a known shorthand and an
  invalid type with "/" (still a 500); res.cookie with maxAge 0, the
  options object, and expires undefined, null or a Date; res.redirect on
  GET and HEAD; res.jsonp with several callbacks.
- Differential: 6 new scenarios, and 12 hand-written ones in `allowed`.
  The generated scenarios get an `allowed` list too, by call (res.send
  Float64Array, ArrayBuffer and DataView), where at least one scenario of
  each call must differ.

Planted reverts: the view fix fails 59 tests, the ArrayBuffer branch 14,
the detached guard 2, redirect 7, res.set 10 (its res.send part 2),
res.cookie 5, res.jsonp 7. REGRESSIONS_ALL=1: 22 failing bug tests
before, 15 after (exactly these 7 fixed, none new).
@coveralls

coveralls commented Nov 22, 2026 •

Copy link
Copy Markdown

Coverage Report for CI Build 44981645728

Coverage remained the same at 100.0%

Details

  • Coverage remained the same as the base build.
  • Patch coverage: 46 of 46 lines across 1 file are fully covered (100%).
  • No coverage regressions found.

Uncovered Changes

No uncovered changes found.

Coverage Regressions

No coverage regressions found.


Coverage Stats

Coverage Status
Relevant Lines: 9381
Covered Lines: 9381
Line Coverage: 100.0%
Coverage Strength: 32195.12 hits per line

💛 - Coveralls

# Conflicts:
#	ROADMAP.md
#	test/differential/scenarios.js
@abedmurrar
abedmurrar merged commit fd3d75b into master Nov 22, 2026
12 checks passed
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.

2 participants