Repository navigation
Honor Content-Length when leftover bytes follow HTTP headers - #339
Merged
Merged
Conversation
parse_http_request assigned the entire header-read leftover as the body and only recv'd more when leftover was shorter than Content-Length. Extra bytes from the same TCP read (CL:0 with a payload, or bytes past CL) were adopted as this request's body, so json_parse could fail on trailing junk or a smuggled POST body could run. Cap leftover to want, matching outbound read_response_body framing. Co-authored-by: Tyler Reckart <tylerreckart@users.noreply.github.com>
Keep both Unreleased changelog entries. Co-authored-by: Cursor <cursoragent@cursor.com>
Contributor
Author
Bugbot couldn't run - usage limit reachedBugbot is counted against Cursor usage for this user or team, and this run hit a usage or spend limit. A user or team admin can review and increase usage limits in the Cursor dashboard. (requestId: serverGenReqId_dc26fd79-f08d-46c3-85f7-9622322eb1f6) |
Keep both Unreleased changelog entries. Co-authored-by: Cursor <cursoragent@cursor.com>
Keep both Unreleased changelog entries. Co-authored-by: Cursor <cursoragent@cursor.com>
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.
Problem
parse_http_requestcopies the entire header-read leftover intoreq.bodyand onlyrecvs more when leftover is shorter thanContent-Length. TCP commonly delivers the header sentinel plus extra bytes in one read.When leftover is longer than CL, those extra bytes became this request's body:
Content-Length: 0with a following payload ran as a real POST body (smuggling / desync).json_parsefail on trailing characters, or polluted raw bodies.Outbound
ApiClient::read_response_bodyalready takes at most CL bytes from leftover. Inbound did not.Fix
Cap leftover to
wantbefore using it as the body. Short leftover still reads until CL. Connection: close drops the rest (pipelining remains out of scope).Tests
unit_http_request— exact CL, leftover overshoot, CL:0 does not adopt leftover, GET without CL drops leftover, short body rejected, duplicate CL rejected, Transfer-Encoding rejected. 7/7 locally (+ ASan/UBSan).Independently mergeable against
main(0456350). Code hunks (src/api/http_request.cpp,tests/test_http_request.cpp,CMakeLists.txtunit target) do not overlap #321–#338.git merge-tree --write-treeauto-merges those siblings; CHANGELOG[Unreleased]is the usual sibling note.Note
Medium Risk
Changes request-body boundaries on the API ingress path (
api_serverusesparse_http_request); incorrect framing could reject valid clients or still mis-handle edge cases, but the change tightens CL semantics rather than loosening them.Overview
Fixes inbound HTTP/1.1 body framing in
parse_http_request: when the first read after headers includes more bytes thanContent-Length, the parser now truncates the header leftover to at mostwantbytes before treating it as the body (then stillrecvs until the declared length). Previously the entire leftover became the body, soContent-Length: 0could still pick up a following payload, and overshoot bytes could break JSON parsing or widen smuggling/desync risk. Behavior is aligned with outboundApiClient::read_response_body.Adds
unit_http_request(tests/test_http_request.cpp) covering exact CL, leftover overshoot, CL:0, GET without CL ignoring pipelined tail, short body failure, duplicateContent-Length, and rejectedTransfer-Encoding. Documents the fix under[Unreleased]inCHANGELOG.md.Reviewed by Cursor Bugbot for commit 8b2639d. Bugbot is set up for automated code reviews on this repo. Configure here.