harden(sip-transport): cap stream body at Content-Length before buffering (#115) - #119
Merged
Merged
Conversation
…ring `TcpTransport::read_from_stream` read `Content-Length` bytes into its buffer with no upper bound. A stream peer could declare an arbitrarily large body and drive unbounded buffer growth (and `body_start + content_length` could overflow `usize`) — a memory-exhaustion DoS. The UDP path already bounds this via the parser's MAX_CONTENT_LENGTH; the stream path did not. Reject a declared Content-Length above MAX_STREAM_BODY_LENGTH (65536, matching the parser) as soon as the headers are in, before waiting for or allocating the body. Regression test: a peer declares an over-cap body, sends a few bytes, and holds the connection open. With the cap, accept() returns Err promptly; without it, the body-read loop blocks forever, so the test times out (verified RED). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XH6jhrG4ZCL8xfCktK4Zms
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.
Fixes #115 —
TcpTransport::read_from_streamread a peer-declaredContent-Lengthnumber of bytes into its buffer with no upper bound, allowing unbounded buffer growth (and a latentusizeoverflow inbody_start + content_length). The UDP path is already bounded by the parser MAX_CONTENT_LENGTH; the stream path was not.Fix
Reject a declared Content-Length above
MAX_STREAM_BODY_LENGTH(65536, matching the parser) as soon as the headers are in — before allocating for, or awaiting, the body.Test + RED control
tcp_rejects_oversized_content_length_without_buffering_body: a peer declares an over-cap body, sends a few bytes, and holds the connection open. With the cap,accept()returnsErr(Parse)promptly; without it, the body-read loop blocks forever waiting for the declared length that never arrives. RED run with the cap reverted:With the fix applied: transport tests 35 passed; full
cargo test --workspace --exclude pjsip-shimgreen;cargo clippy --workspace --exclude pjsip-shim -- -D warningsclean. Toolchain 1.97.0.🤖 Generated with Claude Code