Skip to content

fix(node): omit the Content-Length of a redirect with a Transfer-Encoding - #18417

Merged
kamilmysliwiec merged 2 commits into
nestjs:masterfrom
hktitof:fix/node-omit-the-content-length-of-a-redirect-with-a
Nov 6, 2026
Merged

kamilmysliwiec merged 2 commits into
nestjs:masterfrom
hktitof:fix/node-omit-the-content-length-of-a-redirect-with-a

Conversation

@hktitof

@hktitof hktitof commented Nov 5, 2026

Copy link
Copy Markdown
Contributor

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Other... Please describe:

What is the current behavior?

NodeResponse#redirect() always writes a Content-Length, even when the response already carries a Transfer-Encoding

this.statusCode = statusCode;
this.setHeader('Location', location);
appendVary(this, 'Accept');
this.setHeader('Content-Length', Buffer.byteLength(body));
this.end(this.req.method === 'HEAD' ? undefined : body);

RFC 9112 forbids a message that carries both, and Node's own client parser enforces it, so the redirect answer is not a degraded response, it is an unparseable one

PROBE redirect+TE+text  -> {"error":"Error: Parse Error: Content-Length can't be present with Transfer-Encoding"}
PROBE redirect+TE+empty -> {"error":"Error: Parse Error: Content-Length can't be present with Transfer-Encoding"}

the empty-body case fails too, so this is not about the body length: the two headers alone are enough for the client to drop the response before it reads the status line

#18403 fixed this same invariant in the two other writers of the file, NodeAdapter#reply() and NodeResponse#endWithBody(). redirect() was the one left behind, in the same file, on the same header pair

Issue Number: N/A

What is the new behavior?

the redirect leaves the Content-Length out when a Transfer-Encoding is set, the way send() already does since #18403

// A "Transfer-Encoding" forbids a "Content-Length", as Express knows
if (!this.hasHeader('Transfer-Encoding')) {
  this.setHeader('Content-Length', Buffer.byteLength(body));
}
this.end(this.req.method === 'HEAD' ? undefined : body);
  • a redirect that carries a Transfer-Encoding now answers chunked and stays parseable: 302, transfer-encoding: chunked, no content-length, body intact
  • a redirect without a Transfer-Encoding is untouched: 302, content-length: 24, same body as before
  • the same check the two sibling writers use, so the three agree

Steps to reproduce

  1. set the header on the response and redirect
app.use((req, res) => {
  res.setHeader('Transfer-Encoding', 'chunked');
  res.redirect('/somewhere');
});
  1. read the answer with any HTTP/1.1 client
  2. expected: the redirect arrives
  3. actual on master: the client fails the parse with Content-Length can't be present with Transfer-Encoding, so the redirect never arrives

the spec in this PR is the reverse of that: copied onto master it fails with that parse error and passes 163 with the fix

Does this PR introduce a breaking change?

  • Yes
  • No

a response that carried both headers was unusable, so nothing could have depended on it, and a response that carries only one is unchanged

Other information

one test covers the redirect with a Transfer-Encoding, both with a text body and through the negotiation path. with the check reverted the new test fails with Parse Error: Content-Length can't be present with Transfer-Encoding, with it applied the file passes 163 tests and the rest of the package still passes. the only failure under packages/platform-node is a body-parser charset test that also fails on pristine master, so it is not from this change. tsc -b packages passes, and oxlint plus prettier are clean

the only overlap with an open PR is file scope: #18380 (serve http2 with the node adapter) touches the same two files but adds an http2 server, it does not touch this header pair

verified on a cpu box, no gpu is involved anywhere in this lane

@micalevisk
micalevisk self-requested a review November 5, 2026 17:36

@micalevisk micalevisk left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks, this is the piece #18403 missed.

I checked it out at 2bafd37: the new spec fails on master with Parse Error: Content-Length can't be present with Transfer-Encoding and passes with the fix. The platform-node suite, tsc -b packages, oxlint and prettier are all clean here. I also probed HEAD, an empty body (Accept: application/json) and the HTML body with a Transfer-Encoding set. All three now come out without a Content-Length, and a redirect without one still sends it.

Two small nits inline, neither blocking.

Optional: #18403 also pinned this end to end in integration/http-adapters (the transfer-encoding module). A @Header('Transfer-Encoding', 'chunked') + @Redirect() route there would cover the decorator path. It has to run for node only, though, since Express 5.3.0 still fails it (expressjs/express#7506).

this.setHeader('Location', location);
appendVary(this, 'Accept');
this.setHeader('Content-Length', Buffer.byteLength(body));
// A "Transfer-Encoding" forbids a "Content-Length", as Express knows

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nit: Express doesn't know this one yet. In Express 5.3.0, res.redirect() still sets the Content-Length unconditionally; only res.send() got the guard. The same bug is open upstream as expressjs/express#7506, with the fix pending in expressjs/express#7508. Could we drop the parity claim so the next reader doesn't go looking for it?

Suggested change
// A "Transfer-Encoding" forbids a "Content-Length", as Express knows
// A "Transfer-Encoding" forbids a "Content-Length" (RFC 9112)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

fair point @micalevisk, dropped the parity claim so the comment now just says a Transfer-Encoding forbids a Content-Length per RFC 9112, pushed as a separate commit b99ba5c on top so the approved head 2bafd37 stays exactly where you tested it


it('should leave out the Content-Length when the Transfer-Encoding is set', async () => {
const response = await respond((req, res) => {
// A "Transfer-Encoding" forbids a "Content-Length", as with send()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nit: the test title already says this, so the comment can go.

Suggested change
// A "Transfer-Encoding" forbids a "Content-Length", as with send()

Since the description notes that the empty-body case breaks too, it would be nice to pin it as well: the same assertions with headers: { accept: 'application/json' } and expect(response.body).toHaveLength(0).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

done @micalevisk, removed that comment since the title already says it and pinned the empty body case too, headers: { accept: 'application/json' } with a chunked body now asserts no Content-Length and an empty body, both of them throw the same Parse Error on the old code and pass now

@hktitof

hktitof commented Nov 5, 2026

Copy link
Copy Markdown
Contributor Author

thanks for the careful check @micalevisk, both nits are in as one follow up commit b99ba5c and i left the commit you tested untouched

on the integration idea i went ahead and added it, a @Header('Transfer-Encoding', 'chunked') plus @Redirect() route on the transfer encoding module of integration/http-adapters, and that describe runs on the node adapter only like you said bcs express still writes the Content-Length there

thanks again, say the word if you would rather land it without the extra spec and i will drop that one commit

@micalevisk

Copy link
Copy Markdown
Member

I think that it would be better to introduce lesser changes as possible for a fix

address the review nits: drop the express parity claim per RFC 9112 and pin the empty body redirect case
@hktitof
hktitof force-pushed the fix/node-omit-the-content-length-of-a-redirect-with-a branch from b99ba5c to 22bf757 Compare November 5, 2026 19:12
@hktitof

hktitof commented Nov 5, 2026

Copy link
Copy Markdown
Contributor Author

@micalevisk makes total sense, dropped the extra integration spec and kept just the two unit nits so it stays minimal

@kamilmysliwiec
kamilmysliwiec merged commit 6cfe956 into nestjs:master Nov 6, 2026
4 checks passed
@hktitof

hktitof commented Nov 6, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the review and merge @micalevisk @kamilmysliwiec, glad this fix is in now, keeping it to the two files was the right call, cheers!

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants