Repository navigation
fix(node): omit the Content-Length of a redirect with a Transfer-Encoding - #18417
Conversation
micalevisk
left a comment
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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?
| // A "Transfer-Encoding" forbids a "Content-Length", as Express knows | |
| // A "Transfer-Encoding" forbids a "Content-Length" (RFC 9112) |
There was a problem hiding this comment.
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() |
There was a problem hiding this comment.
Nit: the test title already says this, so the comment can go.
| // 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).
There was a problem hiding this comment.
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
|
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 thanks again, say the word if you would rather land it without the extra spec and i will drop that one commit |
|
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
b99ba5c to
22bf757
Compare
|
@micalevisk makes total sense, dropped the extra integration spec and kept just the two unit nits so it stays minimal |
|
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! |
PR Checklist
Please check if your PR fulfills the following requirements:
PR Type
What kind of change does this PR introduce?
What is the current behavior?
NodeResponse#redirect()always writes aContent-Length, even when the response already carries aTransfer-EncodingRFC 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
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()andNodeResponse#endWithBody().redirect()was the one left behind, in the same file, on the same header pairIssue Number: N/A
What is the new behavior?
the redirect leaves the
Content-Lengthout when aTransfer-Encodingis set, the waysend()already does since #18403Transfer-Encodingnow answers chunked and stays parseable:302,transfer-encoding: chunked, nocontent-length, body intactTransfer-Encodingis untouched:302,content-length: 24, same body as beforeSteps to reproduce
Content-Length can't be present with Transfer-Encoding, so the redirect never arrivesthe 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?
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 withParse 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 underpackages/platform-nodeis a body-parser charset test that also fails on pristine master, so it is not from this change.tsc -b packagespasses, and oxlint plus prettier are cleanthe 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