Repository navigation
Conversation
…truncated body If the request body exceeded maxBodySize, the buffered copy passed to beforeRequest is empty, but the original body is still streamed upstream. Recalculating the content-length from that empty buffer sent 'content-length: 0' followed by the full body. Now the original framing is kept in that case. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Member
|
This is a good find! Thanks for the PR. That said, I'm going to leave this open unmerged for now. I have some buffering changes coming imminently that will change truncation & streaming behaviour significantly, which I think will either fix this bug or change the fix completely. Either way I'll pull in your test here to make sure that gets properly covered. |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When a
beforeRequestcallback returns newheadersbut no new body, Mockttp recalculatesContent-LengthfromcompletedRequest.body.buffer. If the request body was larger thanmaxBodySize, that buffer is empty, because buffering gave up. The recalculated length is therefore0, while the original body is still streamed upstream in full. The upstream server receivesContent-Length: 0followed by the whole body, a malformed request. Node's HTTP server, for example, answers400 Bad Request.This hits any passthrough rule that only tweaks headers, such as adding an auth header or removing a cookie, on large uploads.
Repro
Fix
When there's no body override and the buffered body's size doesn't match the original
Content-Length, the body was truncated and can't be measured. In that case the original framing is kept. If the new headers left outcontent-length(and aren't chunked), the original value is restored on HTTP/1. If the new headers set their owncontent-length, it's left as they set it.Requests whose body was fully buffered behave exactly as before. So do requests with a body override, which are still measured from the new body.
Test
The new test sits next to "should still proxy larger request bodies even if beforeRequest is used", in the "when only tiny bodies are allowed" block of
http-proxying.spec.ts. It fails before the fix (expected '0' to equal '20') and passes after.Found while building breakpoints (pause, edit and resume) on top of Mockttp's
beforeRequest.All Node tests pass locally (
mocha test/**/*.spec.ts: 846 passing, 2 pending).