Bound and time-limit JSON-RPC response reads - #256
Merged
agoodminute merged 1 commit intoSep 8, 2026
Conversation
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.
Summary
Connection::recvhad no real bounds on reading the daemon's HTTP-framed response: the header loop had no count or byte cap, the body was read as a single unbounded line, andContent-Lengthwas only checked after the read completed, so it didn't actually bound anything. A zeroContent-Lengthalso caused an integer underflow. Separately, the read timeout applied per syscall rather than per request, so a slow or partial response could keep resetting it indefinitely.This PR replaces the
Linesreader with aBufReaderplus two bounded readers that check their budget before growing the buffer, read the body by an already-validatedContent-Length, reject a zero length outright, and give eachrecvcall a whole-request deadline derived from the socket timeout.Why
These paths had no upper bound on memory growth or wait time for an unusual or malformed response, which could show up as excessive memory use or a stuck worker thread in edge cases — for example a very large reply, a response delivered in very small increments, or an empty body.
Changes
Connection::recvnow enforces a whole-request deadline (recv_within), re-armed before every blocking read rather than relying on the socket's per-syscall timeout alone.New bounded readers:
read_line_bounded— caps a single line's length before it's buffered, used for both the status line and headers.read_body_bounded— reads exactlyContent-Lengthbytes, growing incrementally rather than allocating up front.New caps, environment-tunable like the existing daemon timeouts:
DAEMON_MAX_HEADER_LINE_BYTESDAEMON_MAX_HEADER_COUNTDAEMON_MAX_HEADER_TOTAL_BYTESDAEMON_MAX_BODY_BYTESA zero
Content-Lengthis now rejected outright instead of underflowing.