fix(transport): bound the incoming line buffer in AsyncRwTransport#1049
Open
onatozmenn wants to merge 1 commit into
Open
fix(transport): bound the incoming line buffer in AsyncRwTransport#1049onatozmenn wants to merge 1 commit into
onatozmenn wants to merge 1 commit into
Conversation
`AsyncRwTransport::receive` read a line with `read_until`, which has no ceiling, so a peer that never sends a newline makes `line_buf` grow until the process runs out of memory. This affects every stdio server built on `rmcp::transport::io::stdio()` and every client using `TokioChildProcess`. `JsonRpcMessageCodec` already implements this bound correctly, but that logic lives in `Decoder::decode`, which this transport never calls. Reading through `fill_buf`/`consume` lets the limit be checked before each append, which `read_until` cannot do: it does not return until it reaches a delimiter or EOF, so by the time its result could be inspected the memory is already committed. An oversized line is dropped and logged and the connection stays open, matching the discard behaviour the codec's decoder already uses. The default is 16 MiB, the same bound the streamable HTTP client applies to a single SSE event, so a payload that is acceptable over HTTP is also acceptable over stdio. `with_max_line_length` overrides it and `usize::MAX` restores the previous unbounded behaviour. Cancellation safety is preserved. `fill_buf` consumes nothing if the future is dropped, and the copy into `line_buf` and the matching `consume` have no await between them, so a partially read line still survives to the next call, which is what modelcontextprotocol#947 relies on. Fixes modelcontextprotocol#1030
onatozmenn
marked this pull request as ready for review
July 24, 2026 19:57
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 #1030
Motivation and Context
AsyncRwTransport::receivereads a line withread_until, which has no ceiling. A peer that never sends a newline makesline_bufgrow until the process runs out of memory. That read path backs every stdio server built onrmcp::transport::io::stdio()and every client usingTokioChildProcess, so one unterminated line from an untrusted peer is enough to take the process down.JsonRpcMessageCodecalready hasmax_lengthand enforces it correctly, but that code lives inDecoder::decodeand this transport never calls the decoder, so the check was unreachable from here.How
The limit can't be bolted onto
read_until. It doesn't return until it hits a delimiter or EOF, so by the time you could look at the length the memory is already committed.receivenow reads throughfill_buf/consume, which lets the limit be checked before each append.An oversized line is dropped and logged and the connection stays open, which is what the codec's decoder already does with
is_discardingrather than tearing the session down.The default is 16 MiB. I picked that to match
DEFAULT_MAX_SSE_EVENT_SIZE, the bound the streamable HTTP client already applies to a single SSE event, so a payload that's fine over HTTP is also fine over stdio and callers sending large embedded resources don't get broken by the bound appearing.with_max_line_lengthoverrides it, andusize::MAXgives back the old unbounded behaviour.Cancellation safety is preserved, which matters because #947 depends on it.
fill_bufconsumes nothing if the future is dropped, and the copy intoline_bufand the matchingconsumehave no await between them, so a partially read line still survives to the nextreceivecall.How Has This Been Tested?
New
crates/rmcp/tests/test_async_rw_max_line_length.rs, 7 tests:unbounded_limit_still_delivers_an_oversized_messageis the control for that. Withusize::MAXthe exact same message comes through, so the assertion above is really testing the bound and not something incidentalread_untiltofill_bufswapAlso run on Rust 1.96, the pinned toolchain:
test_stdio_response_concurrency, 2 passed. That's the fix(transport): make AsyncRwTransport::receive cancel-safe (#941) #947 cancellation regression, and it's the one I most wanted to see stay green after touching this looptest_with_js, which fails in my container only because there's no Node in itcargo clippy --all-targets --all-features -- -D warnings, cleancargo fmtclean on both changed filesBreaking Changes
No API break. It is a behaviour change though: a single message over 16 MiB used to be delivered and is now dropped. Anyone relying on that can call
.with_max_line_length(usize::MAX).If you'd rather have a different default, or would rather the bound be opt-in for now, that's an easy change. 16 MiB was the most defensible number I could find inside the repo rather than one I made up.
Types of changes
Checklist
Investigated and prepared with AI assistance.