From f51662418c29c776ae0edc406364293f5b0fd728 Mon Sep 17 00:00:00 2001 From: Ayla Croft Date: Sun, 29 Nov 2026 14:57:08 -0500 Subject: [PATCH] Bandit 1.15 as the security floor, and the lock off every advised version The lock pinned bandit 1.13.1, plug 1.20.3 and plug_crypto 2.2.0, which carry fourteen published advisories between them, so the gate's audit failed every pull request. The lock moves to bandit 1.15.0, plug 1.20.4, plug_crypto 2.2.1 and telemetry 1.4.3 (ex_doc 0.40.5 for the pages). The optional requirement moves to {:bandit, "~> 1.15"}: the HTTP transport serves HTTP/2 by default, so each Bandit advisory reaches a host that serves it. 1.15 rather than 1.14.1 for GHSA-j9wq-h5xx-5qx5, which Bandit's repository published and OSV has not imported. Plug's requirement is unchanged: this package calls neither Plug.Parsers' multipart parser nor Plug.Crypto. On the new floor no control frame holds an HTTP/2 body read: a stream fed WINDOW_UPDATE, PING, SETTINGS or PRIORITY frames is answered 408 at the deadline (measured, 301 to 329 ms for 300), so the residue the threat model stated is closed by the adapter. The tripwire that pinned the hold becomes one test per frame kind pinning the bound. connection_timeout stays as the backstop and as a bound a host can set below read_timeout; its tests drive it that way. The cap test's listener advertises a stream window the size of the body, since Bandit now enforces the window it advertises. The README, the threat model's row, the transport's documentation and the CHANGELOG say so. Signed-off-by: Ayla Croft --- CHANGELOG.md | 34 ++++++ README.md | 26 ++--- docs/threat-model.md | 2 +- lib/beam_mcp/transport/http.ex | 94 ++++++++-------- mix.exs | 16 ++- mix.lock | 12 +- test/beam_mcp/transport/http_bandit_test.exs | 112 ++++++++++++------- test/support/beam_mcp/h2c.ex | 13 ++- 8 files changed, 189 insertions(+), 120 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0c7df3fb..134b3822 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -99,6 +99,40 @@ All notable changes to this project are documented here. The format follows - The adapter's read timeout is still answered by this package as its `408` JSON-RPC refusal: the whole-body deadline is this package's. +### Security: Bandit 1.15 or later + +- The optional requirement moves from `{:bandit, "~> 1.13"}` to `{:bandit, "~> 1.15"}`. `Bandit` + published fourteen advisories against 1.13 after it shipped. Twelve, fixed in 1.14.0, cover + HTTP/2 flow control and the stream count, HPACK and SETTINGS work, 1-byte chunked and + WebSocket frames, chunk extensions that smuggle a request, `Content-Length` parsing, and the + body read timeout reset by control frames. GHSA-pxjf-g536-7r93 (1-byte HTTP/2 DATA frames) + is fixed in 1.14.1, and GHSA-j9wq-h5xx-5qx5 (logging HTTP/2 trailers) in 1.15.0. This + package's HTTP transport serves HTTP/2 by default, so a host that serves it on an older + `Bandit` is exposed. A host that has `Bandit` beside this package now resolves a fixed one; + a host on the stdio transport alone, or on another adapter, is unaffected (the dependency + stays optional). **If `mix deps.get` reports a conflict,** your own requirement holds + `Bandit` below 1.15: raise it to `"~> 1.15"`. `Bandit` 1.15 needs the same `plug`, + `thousand_island`, `hpax` and Elixir floors as 1.13. Its changes a host may see: + HTTP/2 now allows 500 concurrent streams per connection by default (set + `max_concurrent_streams` to change it), an HTTP/2 request whose `host` disagrees with + `:authority` is reset, responses under 300 bytes are not compressed, and an idle connection + process hibernates after 15 s (its CHANGELOG). +- On the new floor, the HTTP/2 residue the threat model stated is closed by the adapter. Before + `Bandit` 1.14 a stream kept open by WINDOW_UPDATE frames alone was held past `read_timeout`; + on 1.15 a stream fed WINDOW_UPDATE, PING, SETTINGS or PRIORITY frames while its body is owed + is answered `408` at the deadline, and a test per frame kind pins it. `connection_timeout:` + is unchanged and stays as the backstop: at its default the read now ends first, and a host + that sets it below `read_timeout` has a connection whose body is still owed at that deadline + closed with a `GOAWAY`, before the stream's own `408`. The README, the threat model's row and + the transport's documentation say so. +- `plug` 1.20.3 carries EEF-CVE-2026-120287, in `Plug.Parsers.MULTIPART`, and `plug_crypto` + 2.2.0 carries EEF-CVE-2026-105963, in `Plug.Crypto`. This package calls neither, so its + `plug` requirement is unchanged. A host that uses them should take `plug` 1.20.4 and + `plug_crypto` 2.2.1. +- This repository's own lock moves `bandit` 1.13.1 to 1.15.0, `plug` 1.20.3 to 1.20.4, + `plug_crypto` 2.2.0 to 2.2.1 and `telemetry` 1.4.2 to 1.4.3 (and `ex_doc` to 0.40.5 for the + pages). + ### Security: Bandit 1.13 or later - The optional requirement moves from `{:bandit, "~> 1.5"}` to `{:bandit, "~> 1.13"}`, for diff --git a/README.md b/README.md index 36941dcb..f1fe7afd 100644 --- a/README.md +++ b/README.md @@ -364,20 +364,18 @@ the connection is clean, and an ordinary response is possible. less than one frame, so every DATA frame, an empty one included, returns to this clock). Measured through a real `Bandit` listener: `408` at 300, 301, 327 ms for a 300 ms deadline and 1,500, 1,500, 1,501 ms for 1,500 ms; over HTTP/2 a twenty-frame drip answered at the - deadline. One residue is the adapter's, stated on the threat model's row with its cost: over - HTTP/2 a stream kept open by WINDOW_UPDATE frames alone is held past the deadline by the - adapter's own wait, which nothing outside it can end through an interface the adapter - offers: one frame per deadline holds a stream process indefinitely, whatever body then - comes is refused, and each frame costs thirteen bytes with nothing accumulated. (A HEADERS - without END_STREAM held a stream the same way until `Bandit` 1.13, which resets the stream - as a malformed request; this transport hands that error back to the adapter rather than - answering it.) The stream is the adapter's to - end, but the **connection** is this package's: `connection_timeout:` (default twice - `read_timeout`) closes a connection whose body read has been held that long, with nothing - else on it still within its own deadline, using a `GOAWAY` the client can read, so the - residue is bounded in duration by this package and in count by `max_concurrent_streams`, - and its cost is per connection (the client's other streams still open on it end with the - `GOAWAY`; a host multiplexing long streams raises `connection_timeout`). + deadline. A control frame does not hold the read: on `Bandit` 1.15, this package's floor, a + stream fed WINDOW_UPDATE, PING, SETTINGS or PRIORITY frames while its body is owed is answered + `408` at the deadline (measured 2026-11-29, 301 to 329 ms for 300), and a HEADERS without + END_STREAM is reset as a malformed request, an error this transport hands back to the adapter + rather than answering it. Before `Bandit` 1.14 a WINDOW_UPDATE re-armed the adapter's own wait + and held a stream past the deadline, the residue the threat model's row recorded; a test per + frame kind now pins the bound. `connection_timeout:` (default twice `read_timeout`) closes a + connection whose body read is still waiting at that deadline, with nothing else on it still + within its own, using a `GOAWAY` the client can read: the backstop against an adapter that + holds a read again, and a bound you can set below `read_timeout`. Its cost is per connection + (the client's other streams still open on it end with the `GOAWAY`; a host multiplexing long + streams raises `connection_timeout`). The `408` is this package's refusal: the JSON-RPC error object every refusal carries, with `connection: close` over HTTP/1.1 as for every refusal issued before the body is read (over HTTP/2 the stream ends with the response; the header would be a diff --git a/docs/threat-model.md b/docs/threat-model.md index 2e28e777..aafc941b 100644 --- a/docs/threat-model.md +++ b/docs/threat-model.md @@ -55,7 +55,7 @@ reading of each entry's title, not a claim of coverage. | **Header injection; header–body disagreement** (the MCP-layer smuggling class) | REFUSED | Over HTTP every request carries `MCP-Protocol-Version`, `Mcp-Method`, and for the three methods that name a target `Mcp-Name`, plus any `Mcp-Param-{Name}` a tool's schema declares through `x-mcp-header`; each is held to the body and a disagreement, a missing header, or a second disagreeing value is `-32020` / `400`. A schema may annotate only primitive-typed properties; a tool whose schema annotates an object or a non-primitive is a host fault (every call on it is `500` / `-32603` until the schema is corrected, and the host's log names the header), while a caller's header that disagrees with the body is `-32020` naming the schema's header, never the caller's string. A request's `_meta` is read at `params._meta` only; the top level is refused with `-32602` naming the place, not accepted as a fallback. | `test/beam_mcp/transport/http_test.exs` "a header disagreeing with the body's _meta is a HeaderMismatch" "the header is matched to params._meta: a disagreement is -32020 with 400" "Mcp-Param-{Name}: a second, disagreeing value is not ignored" "a header the schema requires and the client omits is refused" "a refusal names the schema's header, never the caller's string" "an annotated `object` property is the same fault" "the refusal covers every header the transport reads, not just the version header" | ASI02 Tool Misuse (the wire half) | | **Request smuggling at the HTTP layer** (`Content-Length` / `Transfer-Encoding` disagreement, pipelining) | DELEGATED | The HTTP server frames requests; this Plug reads one framed body through `Plug.Conn.read_body/2` and nothing else. What it adds: a refusal issued before the body is read closes the connection, so a refused caller's body is never read as the next request. The framing itself is `Bandit`'s (or the host's server's). | `test/beam_mcp/transport/http_test.exs` "a body over the cap is refused and the connection closed" | none | | **DNS rebinding through `Origin`** | REFUSED | `allowed_origins:` is required at `init/1` (there is no default), and a disallowed or second disagreeing `Origin` is `403`, issued before the body is read. A request with no `Origin` is admitted: browsers send one on the cross-origin requests rebinding relies on, and a non-browser client (the usual MCP client) sends none; the host's `authorize/1` still runs. Binding the listener to localhost, which the specification says a local server SHOULD, is the HTTP server's option and the host's. | `test/beam_mcp/transport/http_test.exs` "init/1 raises without :allowed_origins" "a disallowed Origin gets 403" "Origin: a second, disallowed Origin is not ignored" "an absent Origin is not an invalid one" | ASI03 Identity & Privilege Abuse | -| **Slow clients; drip bodies** | BOUNDED | `read_timeout:` on `BeamMCP.Transport.HTTP` is one whole-body deadline, this package's own (the body is read in pieces against one monotonic clock, each read given what remains), so a client that has sent its headers and then drips the body is answered `408` when it lapses, however many bytes arrived and however the adapter splits the reads. The adapter's own `:read_timeout` is not that: a per-read clock, so a cap-sized body (two adapter reads) got two deadlines, 1,909 ms for 1,000; and over HTTP/2, which `Bandit` serves on the same plaintext listener by prior knowledge, its reader gathers DATA frames inside one read on a per-frame clock, and a one-byte-per-frame drip of a valid call was served after 20 s under a 300 ms deadline (both measured by review lanes, 2026-09-16; both closed: over HTTP/2 the reader is asked for less than one frame, so every DATA frame returns to this clock, an empty one included: under a length of zero a drip of empty frames had held the read, 2,321 ms for 300). Measured through a real `Bandit` listener: `408` at 300, 301, 327 ms for a 300 ms deadline and 1,500, 1,500, 1,501 ms for 1,500 ms; over HTTP/2 a twenty-frame drip answered at the deadline. **One residue is the adapter's:** over HTTP/2 a stream kept open by WINDOW_UPDATE frames alone re-arms the adapter's own wait for the whole of what the read gave it, so one such frame per deadline holds the stream indefinitely (under the default, one frame per fifteen seconds), and that wait is a receive on the adapter's messages that nothing outside it can end through an interface the adapter offers. Two levers exist and both are declined: forging the adapter's own message into the stream's mailbox would end it and couples this package to a private protocol a release of the adapter can change silently; ending the stream process gives the client no answer at all: the adapter forgets the stream and drops its frames. **The connection, though, is this package's to end.** `connection_timeout:` (a positive integer of milliseconds, default twice `read_timeout`) closes the whole connection with a `GOAWAY` the client can read when a body read has been held that long and nothing else on the connection is still within its own body deadline: an OTP `GenServer.stop` on the socket handler (found by the `handle_shutdown/2` callback it exports, not by an adapter name), which runs the handler's own orderly termination: `GOAWAY(NO_ERROR)`, then the socket closed, not a reset. So the residue the adapter owns per stream is bounded in DURATION here: measured (2026-09-16), a stream pinned by WINDOW_UPDATE and one by HEADERS-without-END_STREAM, each fed a frame every 100 ms for three seconds under `connection_timeout: 600`, are closed with `GOAWAY(NO_ERROR)` at 607 and 603 ms, where without the bound (a high `connection_timeout`) each lived as long as the frames came, 3,341 and 3,333 ms (the HEADERS figures are `Bandit` before 1.13; on 1.13 that stream is reset at its first such frame, so the bound now applies to the WINDOW_UPDATE hold alone). The cost is per connection and stated: the client's other streams still open on that connection end with the `GOAWAY`; a legitimate stream that already answered is unharmed (measured: on a shared connection the legitimate stream is answered `200`, then the held stream's `GOAWAY(NO_ERROR, last_stream_id 3)` at 602 ms), but a stream still in flight when the close fires dies with it, so a host multiplexing streams that outlive one body read raises `connection_timeout`. The deadline is twice the read deadline by default: one `read_timeout` for the body to arrive, a second before a still-blocked read is taken for a hold rather than a slow arrival (the loop answers slow arrivals per DATA frame within `read_timeout`, so a read still blocked at twice it received only control frames). What such a stream costs the client and the host, by frame: a WINDOW_UPDATE is thirteen bytes, nothing accumulates, and nothing is logged (measured, 2026-09-16: twenty windows at 100 ms under a 300 ms deadline, the body at 2,114 ms, `408` at 2,124 ms); a HEADERS without END_STREAM (a malformed request under RFC 9113 §8.1) held the stream the same way on `Bandit` before 1.13 and wrote a warning line per frame to the host's log carrying the client's header bytes (twenty frames, twenty lines, `408` at 2,118 ms; a review lane, 2026-09-16). `Bandit` 1.13 resets that stream with `PROTOCOL_ERROR` at the first such frame, and this package hands the adapter's error back to the adapter instead of answering it `500` / `-32603`, which it did on 1.13 until it was measured (2026-10-19, `Bandit` 1.13.1: the stream reset in under 300 ms, a second request on the same connection served `200`). Every exception raised inside a call into the adapter is the adapter's to answer, a census holding every such call to one boundary function. What it costs the host: one stream process held for as long as the frames keep coming, up to the adapter's streams-per-connection setting (`http_2_options: [default_local_settings: [max_concurrent_streams: n]]` on the `Bandit` listener, unlimited under its defaults), the connection-flood row's delegation, reached by a client that never sends a body byte. `http_2_options`'s `max_concurrent_streams` caps how many held streams one connection can pin (count), `connection_timeout` bounds how long each is held (duration), and `http_2_options: [enabled: false]` removes HTTP/2 from the listener and with it this residue whole; no MCP client this package has been run against speaks HTTP/2 by prior knowledge. Whatever body then comes is refused, since the clock is read on return. A change in the adapter (a deadline for its read rather than a per-frame wait) lifts the per-stream residue the package now bounds at the connection; the per-stream WINDOW_UPDATE test named below fails the day the adapter honours the deadline it is given, and carries a high `connection_timeout` so the package's connection bound does not close it first. The `408` is this package's JSON-RPC refusal, with `connection: close` over HTTP/1.1 (over HTTP/2 the stream ends with the response; the header would be a malformed one there, and every pre-body refusal that carried it was a stream reset to an HTTP/2 client until this page); nothing is written to the host's log for it: the adapter's own error-level line at its read timeout no longer fires, since the deadline is this package's. The default is 15,000 ms, a chosen number with its reasoning beside the constant: a cap-sized body needs 68.3 KiB/s (~559 kbit/s) sustained to arrive under it (arithmetic, since every measurement this package has is on loopback), and a drip attack costs the host at most the body cap per connection for fifteen seconds (a legitimate request served in under 0.01 s with 12,000 drip connections opened against it, 2026-09-07; cumulative opens, not a steady state). It is a DoS control and the host's: longer behind a slow link, shorter facing the open internet. For two releases this package passed none and the value in force was `Bandit`'s default for such a call, not a server option, and not a choice. | `test/beam_mcp/transport/http_bandit_test.exs` "a drip client is answered 408 at the deadline set, at two values" "a body over the adapter's read length and under the cap is one deadline, not two" "over HTTP/2 a drip of one byte per frame is still answered at the deadline, and the refusal is readable" "over HTTP/2 an empty DATA frame returns to the deadline's clock like any other" "over HTTP/2 a stream kept open by WINDOW_UPDATE frames alone is held past the deadline by the adapter, and nothing late is served" "over HTTP/2 a HEADERS frame without END_STREAM in the middle of a body is reset by the adapter as malformed, not answered as our fault" "over HTTP/2 a connection pinned by WINDOW_UPDATE frames alone is closed with a readable GOAWAY at the connection deadline" "over HTTP/2 HEADERS frames without END_STREAM no longer pin a connection: the stream is reset and the connection still serves" "over HTTP/2 a legitimate stream on the pinned connection is served before the close, and the close is a GOAWAY not a reset" "init/1 takes connection_timeout: and refuses a value that is not a positive integer" "over HTTP/2 a whole body under the deadline is served, and a refusal before the body is a response, not a stream reset" "init/1 takes read_timeout: and refuses a value that is not a positive integer" "the default is the stated one, and a drip client under it is not answered inside a second" `test/beam_mcp/transport/http_test.exs` "a read that raises with no status of its own is handed back unchanged, not answered as -32603" `test/beam_mcp/transport/adapter_boundary_test.exs` "every call into the adapter under lib/ sits inside adapter/1, and adapter/1 is the boundary" "the census sees every shape of a call into the adapter that review lanes planted" | LLM10:2025 | +| **Slow clients; drip bodies** | BOUNDED | `read_timeout:` on `BeamMCP.Transport.HTTP` is one whole-body deadline, this package's own (the body is read in pieces against one monotonic clock, each read given what remains), so a client that has sent its headers and then drips the body is answered `408` when it lapses, however many bytes arrived and however the adapter splits the reads. The adapter's own `:read_timeout` is not that: a per-read clock, so a cap-sized body (two adapter reads) got two deadlines, 1,909 ms for 1,000; and over HTTP/2, which `Bandit` serves on the same plaintext listener by prior knowledge, its reader gathers DATA frames inside one read on a per-frame clock, and a one-byte-per-frame drip of a valid call was served after 20 s under a 300 ms deadline (both measured by review lanes, 2026-09-16; both closed: over HTTP/2 the reader is asked for less than one frame, so every DATA frame returns to this clock, an empty one included: under a length of zero a drip of empty frames had held the read, 2,321 ms for 300). Measured through a real `Bandit` listener: `408` at 300, 301, 327 ms for a 300 ms deadline and 1,500, 1,500, 1,501 ms for 1,500 ms; over HTTP/2 a twenty-frame drip answered at the deadline. **The adapter's residue, closed upstream.** Before `Bandit` 1.14, over HTTP/2 a stream kept open by WINDOW_UPDATE frames alone re-armed the adapter's own wait for the whole of what the read gave it, so one such frame per deadline held the stream indefinitely, inside a receive on the adapter's messages that nothing outside it can end through an interface the adapter offers (measured, 2026-09-16: twenty windows at 100 ms under a 300 ms deadline, `408` at 2,124 ms; a HEADERS without END_STREAM did the same on `Bandit` before 1.13 and wrote a warning line per frame carrying the client's header bytes). Two levers exist and both stay declined: forging the adapter's own message into the stream's mailbox couples this package to a private protocol a release of the adapter can change silently; ending the stream process gives the client no answer at all. `Bandit` 1.14.0 bounds the read by the timeout it is given (GHSA-p2qv-vx88-385x) and caps HTTP/2 streams at 500 per connection by default (GHSA-fmr8-qrvr-4ww5), and this package's floor is `Bandit` 1.15. Measured on 1.15.0 (2026-11-29): a stream fed a stream WINDOW_UPDATE, a connection WINDOW_UPDATE, a PING, an empty SETTINGS or a PRIORITY every 100 ms while its body is owed is answered `408` at 329, 302, 302, 302 and 301 ms for a 300 ms deadline, and a HEADERS without END_STREAM is reset with `PROTOCOL_ERROR` at the first such frame, which this package hands back to the adapter instead of answering it `500` / `-32603` (2026-10-19, `Bandit` 1.13.1: reset in under 300 ms, a second request on the same connection served `200`). Every exception raised inside a call into the adapter is the adapter's to answer, a census holding every such call to one boundary function. A test per frame kind pins the bound, so a release of the adapter that holds the read again fails the gate. **The connection stays this package's to end.** `connection_timeout:` (a positive integer of milliseconds, default twice `read_timeout`) closes the whole connection with a `GOAWAY` the client can read when a body read has been waiting that long and nothing else on the connection is still within its own body deadline: an OTP `GenServer.stop` on the socket handler (found by the `handle_shutdown/2` callback it exports, not by an adapter name), which runs the handler's own orderly termination: `GOAWAY(NO_ERROR)`, then the socket closed, not a reset. It was built to bound the hold above in duration (measured 2026-09-16 under `connection_timeout: 600`: held streams closed with `GOAWAY(NO_ERROR)` at 607 and 603 ms, where they had lived 3,341 and 3,333 ms). On the floor no client behaviour measured reaches it at the default, since the read ends at `read_timeout` first; it stays as the backstop against an adapter that holds a read again, and as a bound a host can set below `read_timeout`, where a connection whose body is still owed at the connection deadline is closed before the stream's own `408` would come (tested that way). The cost is per connection and stated: the client's other streams still open on that connection end with the `GOAWAY`; a legitimate stream that already answered is unharmed (measured: on a shared connection the legitimate stream is answered `200`, then the waiting stream's `GOAWAY(NO_ERROR, last_stream_id 3)` at 602 ms), but a stream still in flight when the close fires dies with it, so a host multiplexing streams that outlive one body read raises `connection_timeout`. `http_2_options: [default_local_settings: [max_concurrent_streams: n]]` on the `Bandit` listener sets the streams per connection, and `http_2_options: [enabled: false]` removes HTTP/2 from the listener; no MCP client this package has been run against speaks HTTP/2 by prior knowledge. Whatever body comes after the deadline is refused, since the clock is read on return. The `408` is this package's JSON-RPC refusal, with `connection: close` over HTTP/1.1 (over HTTP/2 the stream ends with the response; the header would be a malformed one there, and every pre-body refusal that carried it was a stream reset to an HTTP/2 client until this page); nothing is written to the host's log for it: the adapter's own error-level line at its read timeout no longer fires, since the deadline is this package's. The default is 15,000 ms, a chosen number with its reasoning beside the constant: a cap-sized body needs 68.3 KiB/s (~559 kbit/s) sustained to arrive under it (arithmetic, since every measurement this package has is on loopback), and a drip attack costs the host at most the body cap per connection for fifteen seconds (a legitimate request served in under 0.01 s with 12,000 drip connections opened against it, 2026-09-07; cumulative opens, not a steady state). It is a DoS control and the host's: longer behind a slow link, shorter facing the open internet. For two releases this package passed none and the value in force was `Bandit`'s default for such a call, not a server option, and not a choice. | `test/beam_mcp/transport/http_bandit_test.exs` "a drip client is answered 408 at the deadline set, at two values" "a body over the adapter's read length and under the cap is one deadline, not two" "over HTTP/2 a drip of one byte per frame is still answered at the deadline, and the refusal is readable" "over HTTP/2 an empty DATA frame returns to the deadline's clock like any other" "over HTTP/2 a stream WINDOW_UPDATE every 100 ms does not hold a body read: the 408 comes at the deadline" "over HTTP/2 a connection WINDOW_UPDATE every 100 ms does not hold a body read: the 408 comes at the deadline" "over HTTP/2 a PING every 100 ms does not hold a body read: the 408 comes at the deadline" "over HTTP/2 an empty SETTINGS every 100 ms does not hold a body read: the 408 comes at the deadline" "over HTTP/2 a PRIORITY every 100 ms does not hold a body read: the 408 comes at the deadline" "over HTTP/2 a HEADERS frame without END_STREAM in the middle of a body is reset by the adapter as malformed, not answered as our fault" "over HTTP/2 a body read still waiting at the connection deadline closes its connection with a readable GOAWAY" "over HTTP/2 the connection is not closed while another stream on it is still within its own deadline" "over HTTP/2 HEADERS frames without END_STREAM no longer pin a connection: the stream is reset and the connection still serves" "over HTTP/2 a legitimate stream on the pinned connection is served before the close, and the close is a GOAWAY not a reset" "init/1 takes connection_timeout: and refuses a value that is not a positive integer" "over HTTP/2 a whole body under the deadline is served, and a refusal before the body is a response, not a stream reset" "init/1 takes read_timeout: and refuses a value that is not a positive integer" "the default is the stated one, and a drip client under it is not answered inside a second" `test/beam_mcp/transport/http_test.exs` "a read that raises with no status of its own is handed back unchanged, not answered as -32603" `test/beam_mcp/transport/adapter_boundary_test.exs` "every call into the adapter under lib/ sits inside adapter/1, and adapter/1 is the boundary" "the census sees every shape of a call into the adapter that review lanes planted" | LLM10:2025 | | **A body that does not declare its length** (`transfer-encoding: chunked`) | REFUSED | `411` before the body is read, with `connection: close`. An MCP request is one complete JSON message under a 1 MiB cap and a chunked coding buys it nothing, while it defeats every whole-body bound: the adapter reads a chunked body chunk by chunk, each chunk on its own clock, gathering chunks until the length asked for is filled; a client sending one byte per chunk was served after 43 s under a 15 s deadline (a review lane, 2026-09-16). | `test/beam_mcp/transport/http_bandit_test.exs` "a chunked body is refused with 411 before it is read, so no drip can reset the deadline chunk by chunk" | LLM10:2025 | | **Connection floods; too many in flight** | DELEGATED | Each in-flight request at the body cap costs about 1.05 MiB, linear to 8,000 concurrent with no plateau (measured 2026-09-07); the ceiling on how many is `num_acceptors × num_connections` of the server (100 × 16,384 under `Bandit`'s defaults), the host's capacity decision. The nesting bound above is what keeps that per-request figure at the body's size rather than thirty-six times it. | none (a server setting) | LLM10:2025 | | **Header count and size; TLS** | DELEGATED | Both the HTTP server's. This Plug speaks plaintext to the server that terminates TLS for it; a deployment that needs TLS configures it there. | none | none | diff --git a/lib/beam_mcp/transport/http.ex b/lib/beam_mcp/transport/http.ex index 579ff78b..d07071db 100644 --- a/lib/beam_mcp/transport/http.ex +++ b/lib/beam_mcp/transport/http.ex @@ -75,19 +75,15 @@ if Code.ensure_loaded?(Plug) do given what remains, so a client that has sent its headers and then drips the body is answered `408` when it lapses, however many bytes arrived and however the adapter splits the reads (over HTTP/2 the adapter's reader is asked for less than one frame, so every - DATA frame, an empty one included, returns to this clock; a stream kept open by - WINDOW_UPDATE frames alone is held past the deadline by the adapter's own wait, which - nothing outside it can end through an interface the adapter offers: one frame per - deadline holds a stream process indefinitely, and whatever body then comes is refused; - each frame costs the client thirteen bytes and the host nothing accumulated. An - exception the adapter raises (on `Bandit` 1.13, a HEADERS without END_STREAM in the - middle of a body, a malformed request) is handed back to the adapter, which resets the - stream; it is not answered as `-32603`. The threat model states the residue, and the - two `Bandit` listener options that bound the - exposure: `http_2_options: [default_local_settings: [max_concurrent_streams: n]]` - caps the held streams per connection, `http_2_options: [enabled: false]` removes - HTTP/2 from the listener). That per-stream hold the adapter owns is bounded in - DURATION here by `:connection_timeout` below. A body must declare its + DATA frame, an empty one included, returns to this clock; a control frame does not hold + the read: on `Bandit` 1.15, this package's floor, a stream fed WINDOW_UPDATE, PING, + SETTINGS or PRIORITY frames while its body is owed is answered `408` at the deadline, + where before 1.14 a WINDOW_UPDATE re-armed the adapter's own wait and held the stream + past it. An exception the adapter raises (a HEADERS without END_STREAM in the middle of + a body, a malformed request) is handed back to the adapter, which resets the stream; it + is not answered as `-32603`. The adapter caps the streams per connection itself, 500 by + default, set with `http_2_options: [default_local_settings: [max_concurrent_streams: n]]`; + `http_2_options: [enabled: false]` removes HTTP/2 from the listener). A body must declare its length; `transfer-encoding: chunked` is refused with `411` before the read, since a chunked body is read chunk by chunk on a per-chunk clock that no deadline above it can bound. The `408` is this Plug's @@ -95,20 +91,22 @@ if Code.ensure_loaded?(Plug) do with the response); nothing is written to the host's log for it: the adapter's own error-level line at its read timeout no longer fires, since the deadline is this Plug's. * `:connection_timeout`: positive integer, milliseconds, default twice `:read_timeout`. - The whole-body deadline above is a stream's; over HTTP/2 a stream held open by control - frames alone cannot be ended from outside the adapter, but its **connection** can. When - a body read has been blocked this long and nothing else on the connection is still - within its own body deadline, the connection is closed with a `GOAWAY` the client can - read (an OTP `GenServer.stop` on the socket handler, not a forged adapter message or a - reset). So the residue the adapter owns is bounded in duration by this option and in - count by `http_2_options`'s `max_concurrent_streams`. The cost is per connection: the - client's other legitimate streams still open on that connection end with it, so a host - multiplexing streams that outlive one body read raises this. Twice the read deadline by - default: one for the body to arrive, a second before a still-blocked read is taken for a - hold. A DoS control, and the host's to set: longer behind - a slow link, shorter facing the open internet. The default is a chosen number with its - reasoning beside the constant, not the adapter's default (which it was, unstated, for - two releases). + The whole-body deadline above is a stream's; this one is the stream's **connection**'s, + over HTTP/2. When a body read has been waiting this long and nothing else on the + connection is still within its own body deadline, the connection is closed with a + `GOAWAY` the client can read (an OTP `GenServer.stop` on the socket handler, not a + forged adapter message or a reset). It was built for an adapter that held a stream's + read past the read deadline, which no interface the adapter offers could end; on + `Bandit` 1.15 no client behaviour measured does, so at the default the read ends first + and this stands down. It stays as a backstop against an adapter that holds a read again, + and as a bound a host can set below `:read_timeout` to close a connection whose body is + still owed sooner than the stream's own `408` would come. The cost is per connection: + the client's other legitimate streams still open on that connection end with it, so a + host multiplexing streams that outlive one body read raises this. Twice the read + deadline by default: one for the body to arrive, a second before a still-waiting read is + taken for a hold. A DoS control, and the host's to set. The default is a chosen number + with its reasoning beside the constant, not the adapter's default (which it was, + unstated, for two releases). The bytes are the ones the client sent, not a re-encoding of them. A signature covers bytes, so handing a hook `Jason.encode!(Jason.decode!(body))` would break every correct @@ -744,11 +742,13 @@ if Code.ensure_loaded?(Plug) do defp read_body_bounded(conn, read_timeout, connection_timeout) do deadline = System.monotonic_time(:millisecond) + read_timeout - # Over HTTP/2 the read can be held past its deadline by control frames alone, which no - # clock in this loop can end (029a's residue: the adapter's receive is not ours to - # interrupt). The connection is, though: a watchdog closes it at the connection deadline - # if the read is still blocked and nothing else on the connection is still within its - # own deadline. Armed only over HTTP/2; over HTTP/1 the loop below bounds every read. + # Over HTTP/2 the adapter's receive is not ours to interrupt (029a). Before Bandit 1.14 + # control frames alone held it past its deadline; on 1.15, the floor, none measured does. + # The connection can be ended, though: a watchdog closes it at the connection deadline if + # the read is still waiting and nothing else on the connection is still within its own + # deadline -- a backstop against an adapter that holds the read again, and a bound a host + # can set below the read deadline. Armed only over HTTP/2; over HTTP/1 the loop below + # bounds every read. watchdog = arm_connection_watchdog(conn, connection_timeout) try do @@ -813,7 +813,7 @@ if Code.ensure_loaded?(Plug) do # stream records its own connection deadline in its process dictionary and spawns a # watchdog that closes the whole connection at the connection deadline IF the read is # still blocked and no other stream on the connection is still within its own deadline. - # Over HTTP/1 there is no residue -- the loop bounds every read -- so nothing is armed. + # Over HTTP/1 the loop bounds every read, so nothing is armed. defp arm_connection_watchdog(conn, connection_timeout) do if http_protocol(conn) == :"HTTP/2" and is_integer(connection_timeout) do arm_watchdog_on(connection_pid(), connection_timeout) @@ -939,25 +939,19 @@ if Code.ensure_loaded?(Plug) do # remains". A body at the cap is then refused by the size rule on the next frame, or # accepted when the stream ends. # - # What this cannot reach: a frame that carries no DATA. A WINDOW_UPDATE re-arms the - # adapter's own wait for the whole of what this call gave it -- one such frame per - # deadline holds the stream indefinitely -- and that wait is a - # receive on the adapter's messages that nothing outside it can end through an interface - # the adapter offers. Two levers exist and both are declined: forging the adapter's own + # What this cannot reach: a frame that carries no DATA. Its effect on the read is the + # adapter's, inside a receive that nothing outside it can end through an interface the + # adapter offers. Two levers exist and both are declined: forging the adapter's own # message into the stream process's mailbox couples this package to a private protocol # that a release of the adapter can change silently; ending the stream process gives the - # client no answer at all (the adapter forgets the stream and drops its frames). So a - # stream kept open by control frames alone is held by the adapter past the deadline, and - # whatever body comes is refused here, since the clock is read on return. A WINDOW_UPDATE - # is thirteen bytes and nothing accumulates. (A HEADERS without END_STREAM -- a malformed - # request under RFC 9113, 8.1 -- was the second such frame until `Bandit` 1.13, which now - # raises a stream error for it; `adapter/1` hands that back to the adapter, which resets - # the stream.) The host's cost is one stream process held for as long as the frames - # keep coming, up to the adapter's streams-per-connection setting -- but bounded in - # DURATION by the connection watchdog above: the stream itself cannot be ended from - # outside the adapter, so at the connection deadline the whole connection is closed with - # a GOAWAY. The threat model states the residue as the adapter's, with tests that fail - # the day the adapter honours the deadline it is given. + # client no answer at all (the adapter forgets the stream and drops its frames). Before + # `Bandit` 1.14 a WINDOW_UPDATE re-armed the adapter's wait for the whole of what this call + # gave it, so one such frame per deadline held the stream indefinitely (and a HEADERS + # without END_STREAM did the same until 1.13, which now raises a stream error for it that + # `adapter/1` hands back). 1.14 bounds the read by the timeout given (GHSA-p2qv-vx88-385x), + # and on 1.15, this package's floor, WINDOW_UPDATE, PING, SETTINGS and PRIORITY frames were + # each measured answered at the deadline; tests pin each, so an adapter release that holds + # the read again fails the gate. The connection watchdog above stays as the backstop. defp piece_length(true, size), do: min(@read_piece, @max_body_bytes - size) defp piece_length(false, _size), do: -1 diff --git a/mix.exs b/mix.exs index 30be7578..d7c866ee 100644 --- a/mix.exs +++ b/mix.exs @@ -138,11 +138,17 @@ defmodule BeamMCP.MixProject do # optional: a host using only the stdio transport must not be made to pull an HTTP # server in, and `optional: true` keeps them out of that host's dependency tree. {:plug, "~> 1.16", optional: true}, - # 1.13 is a security floor, not a feature one: below it Bandit carries EEF-CVE-2026-105659 - # (HTTP/2 send deadlines reset by every WINDOW_UPDATE) and EEF-CVE-2026-105660 (an HTTP/1 - # request target choosing conn.host and conn.port). A host that pulls Bandit in beside this - # transport resolves to a fixed one; a host that does not is unaffected (optional). - {:bandit, "~> 1.13", optional: true}, + # 1.15 is a security floor, not a feature one. Below 1.13 Bandit carries + # EEF-CVE-2026-105659 and -105660; below 1.14.0, twelve more advisories published + # 2026-10-26 (HTTP/2 flow control, stream count, HPACK and SETTINGS work, 1-byte chunked + # and WebSocket frames, chunk extensions that smuggle, bignum Content-Length, the body read + # timeout reset by control frames); below 1.14.1, GHSA-pxjf-g536-7r93 (1-byte HTTP/2 DATA + # frames); below 1.15.0, GHSA-j9wq-h5xx-5qx5 (logging HTTP/2 trailers). This transport + # serves HTTP/2 by default, so each reaches a host that serves it. A host that pulls Bandit + # in beside this transport resolves to a fixed one; a host that does not is unaffected + # (optional). Plug's EEF-CVE-2026-120287 is in Plug.Parsers.MULTIPART, which this package + # never calls, so Plug's floor stays and only the lock moves. + {:bandit, "~> 1.15", optional: true}, {:credo, "~> 1.7", only: [:dev, :test], runtime: false}, {:ex_doc, "~> 0.31", only: :dev, runtime: false}, # Property tests. Test-time only; it does not enter the published package. diff --git a/mix.lock b/mix.lock index 4e8905b6..8a73d00d 100644 --- a/mix.lock +++ b/mix.lock @@ -1,21 +1,21 @@ %{ - "bandit": {:hex, :bandit, "1.13.1", "1ce25a7beb750ed3fa2e905962f1f9a679cb585f1da0ae45637d06e385ce2eae", [:mix], [{:hpax, "~> 1.1", [hex: :hpax, repo: "hexpm", optional: false]}, {:plug, "~> 1.18", [hex: :plug, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}, {:thousand_island, "~> 1.6", [hex: :thousand_island, repo: "hexpm", optional: false]}, {:websock, "~> 0.5", [hex: :websock, repo: "hexpm", optional: false]}], "hexpm", "39028f0b5a21404661410a6c19275060a9822c9dcca70b7c3e4e4fa50b11da58"}, + "bandit": {:hex, :bandit, "1.15.0", "df5991a93535e83f9e939bee4af539e0ce17269a0d42fc63819daae60c7d23df", [:mix], [{:hpax, "~> 1.1", [hex: :hpax, repo: "hexpm", optional: false]}, {:plug, "~> 1.18", [hex: :plug, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}, {:thousand_island, "~> 1.6", [hex: :thousand_island, repo: "hexpm", optional: false]}, {:websock, "~> 0.5", [hex: :websock, repo: "hexpm", optional: false]}], "hexpm", "e3b29c4a5107f4bf7dfaa263c50b9b97ddcc132622197c110e2e10a921641c44"}, "bunt": {:hex, :bunt, "1.0.0", "081c2c665f086849e6d57900292b3a161727ab40431219529f13c4ddcf3e7a44", [:mix], [], "hexpm", "dc5f86aa08a5f6fa6b8096f0735c4e76d54ae5c9fa2c143e5a1fc7c1cd9bb6b5"}, "credo": {:hex, :credo, "1.7.19", "cc52129665fc7c15143d47838fda0f9cd6dac9ceced7bf4da6f85fcbfe64b12a", [:mix], [{:bunt, "~> 0.2.1 or ~> 1.0", [hex: :bunt, repo: "hexpm", optional: false]}, {:file_system, "~> 0.2 or ~> 1.0", [hex: :file_system, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "2d8bc95d5a7bb99dd2613621d4f08c6a3575c3fd4b62e6a2b48a100352a557b8"}, "earmark_parser": {:hex, :earmark_parser, "1.4.46", "67607a0532e810c6f630a515c548d0b24949643f168cc556303bee4cf96105c7", [:mix], [], "hexpm", "9c44636e8a1c68c62f526b2dcd85d941dbbcee7ab82cf64ba06ce28bef8e89f5"}, - "ex_doc": {:hex, :ex_doc, "0.40.4", "66f2e42bf588594d5a8aab31cad87f2ddad09d0da1b1a2f379340ec2c2e497cb", [:mix], [{:earmark_parser, "~> 1.4.46", [hex: :earmark_parser, repo: "hexpm", optional: false]}, {:makeup_c, ">= 0.1.0", [hex: :makeup_c, repo: "hexpm", optional: true]}, {:makeup_elixir, "~> 0.14 or ~> 1.0", [hex: :makeup_elixir, repo: "hexpm", optional: false]}, {:makeup_erlang, "~> 0.1 or ~> 1.0", [hex: :makeup_erlang, repo: "hexpm", optional: false]}, {:makeup_html, ">= 0.1.0", [hex: :makeup_html, repo: "hexpm", optional: true]}], "hexpm", "6222b9e423d76584ee34df2c82a5ed72c2d53dc153f7f483ad28b378694186cc"}, + "ex_doc": {:hex, :ex_doc, "0.40.5", "d4f8d44bb54927ee388e6fb42bba4cca45b53d1cd612c290584ca898e2a160ee", [:mix], [{:earmark_parser, "~> 1.4.46", [hex: :earmark_parser, repo: "hexpm", optional: false]}, {:makeup_c, ">= 0.1.0", [hex: :makeup_c, repo: "hexpm", optional: true]}, {:makeup_elixir, "~> 0.14 or ~> 1.0", [hex: :makeup_elixir, repo: "hexpm", optional: false]}, {:makeup_erlang, "~> 0.1 or ~> 1.0", [hex: :makeup_erlang, repo: "hexpm", optional: false]}, {:makeup_html, ">= 0.1.0", [hex: :makeup_html, repo: "hexpm", optional: true]}], "hexpm", "38c7a1a951583ffd710ae34be654aceeff32c164666de53498f89d781bdc2689"}, "file_system": {:hex, :file_system, "1.1.1", "31864f4685b0148f25bd3fbef2b1228457c0c89024ad67f7a81a3ffbc0bbad3a", [:mix], [], "hexpm", "7a15ff97dfe526aeefb090a7a9d3d03aa907e100e262a0f8f7746b78f8f87a5d"}, "hpax": {:hex, :hpax, "1.1.0", "782931867cc23217c68fb5f68fe1a11f5e7544c7fda82c8a7019a5df5a4a1cdf", [:mix], [], "hexpm", "0b8d0f05832f55571d65ac720f79bf8994138ffbb133209dc4685eae0ad456a8"}, "jason": {:hex, :jason, "1.4.5", "2e3a008590b0b8d7388c20293e9dcc9cf3e5d642fd2a114e4cbbb52e595d940a", [:mix], [{:decimal, "~> 1.0 or ~> 2.0 or ~> 3.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "b0c823996102bcd0239b3c2444eb00409b72f6a140c1950bc8b457d836b30684"}, - "makeup": {:hex, :makeup, "1.2.2", "882d46dc0905e9ff7abf2aab61a7e6b3dcc555533977d8a23b06019e6c89ac94", [:mix], [{:nimble_parsec, "~> 1.4", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "9a1a24e5b343b8ae16abea0822c10a6f75da27af7fa802ada5251f7579bfccfa"}, + "makeup": {:hex, :makeup, "1.2.3", "c3ef0ff83acf305384acba0f70bff87d8732502baa1eb3eb081d8c154fbe18f5", [:mix], [{:nimble_parsec, "~> 1.4", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "15970751b7021c2bcd7ed56a54b3def23affd3969fb7c214dffdf28e97ce6f76"}, "makeup_elixir": {:hex, :makeup_elixir, "1.0.1", "e928a4f984e795e41e3abd27bfc09f51db16ab8ba1aebdba2b3a575437efafc2", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}, {:nimble_parsec, "~> 1.2.3 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "7284900d412a3e5cfd97fdaed4f5ed389b8f2b4cb49efc0eb3bd10e2febf9507"}, "makeup_erlang": {:hex, :makeup_erlang, "1.1.0", "835f7e60792e08824cda445639555d7bf1bbbddb1b60b306e33cb6f6db24dc74", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "1cd6780fb1dd1a03979abaed0fe82712b0625118fd5257d3ebbf73f960c73c3c"}, "mime": {:hex, :mime, "2.0.7", "b8d739037be7cd402aee1ba0306edfdef982687ee7e9859bee6198c1e7e2f128", [:mix], [], "hexpm", "6171188e399ee16023ffc5b76ce445eb6d9672e2e241d2df6050f3c771e80ccd"}, "nimble_parsec": {:hex, :nimble_parsec, "1.4.2", "8efba0122db06df95bfaa78f791344a89352ba04baedd3849593bfce4d0dc1c6", [:mix], [], "hexpm", "4b21398942dda052b403bbe1da991ccd03a053668d147d53fb8c4e0efe09c973"}, - "plug": {:hex, :plug, "1.20.3", "56c480c633ec2ce10140e236e15233bf576e1d323887d7c96711bd02ab5160db", [:mix], [{:mime, "~> 1.0 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:plug_crypto, "~> 1.1.1 or ~> 1.2 or ~> 2.0", [hex: :plug_crypto, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4.3 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "be266aee1b8536ef6409d58cf39a3121319f0ec47cfa1b24024485aa0e76ad76"}, - "plug_crypto": {:hex, :plug_crypto, "2.2.0", "144014737daaf485407f5ed77daeaad74d651b216a28c87543f8cc7043f8efc8", [:mix], [], "hexpm", "83a95744ab1c75876542b6fab135fcc176280e0f301a111c1f757fddcec95d2c"}, + "plug": {:hex, :plug, "1.20.4", "8fca521b31eb586dbb91968dd9fa95be6b60fb40f9ca77b005361e06836aed75", [:mix], [{:mime, "~> 1.0 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:plug_crypto, "~> 1.1.1 or ~> 1.2 or ~> 2.0", [hex: :plug_crypto, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4.3 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "96ee42c0b8b35bcb14e3e65afb85abe9bb7745a8576e8c602802bbb01e5acf72"}, + "plug_crypto": {:hex, :plug_crypto, "2.2.1", "bd1d0358a2e2f5ffb818217e64c394f9d24ed191d862b7fa1bcce264ace2b8ea", [:mix], [], "hexpm", "4417ccbd8f85e627321cd07d2ab9c4b621f1fd883b9905af241f9b4b780b94b6"}, "stream_data": {:hex, :stream_data, "1.4.0", "026f929db613aabea6208012ae9b8970d3fd5f88b3bdf26831bc536f98c42036", [:mix], [], "hexpm", "2b0ee3a340dcce1c8cf6302a763ee757d1e01c54d6e16d9069062509d68b1dc9"}, - "telemetry": {:hex, :telemetry, "1.4.2", "a0cb522801dffb1c49fe6e30561badffc7b6d0e180db1300df759faa22062855", [:rebar3], [], "hexpm", "928f6495066506077862c0d1646609eed891a4326bee3126ba54b60af61febb1"}, + "telemetry": {:hex, :telemetry, "1.4.3", "d0333fa869361e6833d6ff8e806ff4d8553fe1332fcce0af86c7381951a1dc2f", [:rebar3], [], "hexpm", "17a63581cf2e832a238287f5fe6811e8ca1f333249f1376ad653164a3c4770d4"}, "thousand_island": {:hex, :thousand_island, "1.6.0", "d6ad7c2220c96a270cc8ca1586ec4d8bde648a4a3a53f003ce093f41beec0288", [:mix], [{:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "4d4720470f091fc21a1311777c2ac66916c43621bffc068590c9f9bb3cd057a3"}, "websock": {:hex, :websock, "0.5.3", "2f69a6ebe810328555b6fe5c831a851f485e303a7c8ce6c5f675abeb20ebdadc", [:mix], [], "hexpm", "6105453d7fac22c712ad66fab1d45abdf049868f253cf719b625151460b8b453"}, } diff --git a/test/beam_mcp/transport/http_bandit_test.exs b/test/beam_mcp/transport/http_bandit_test.exs index d859184a..4ad73954 100644 --- a/test/beam_mcp/transport/http_bandit_test.exs +++ b/test/beam_mcp/transport/http_bandit_test.exs @@ -78,7 +78,8 @@ defmodule BeamMCP.Transport.HTTPBanditTest do # -- the listener ------------------------------------------------------------------------- - defp listen(extra) do + # `extra` is the Plug's options; `bandit` is the listener's own (e.g. `http_2_options:`). + defp listen(extra, bandit \\ []) do plug_opts = Keyword.merge( [ @@ -92,7 +93,7 @@ defmodule BeamMCP.Transport.HTTPBanditTest do spec = Supervisor.child_spec( - {Bandit, plug: {HTTP, plug_opts}, port: 0, ip: :loopback, startup_log: false}, + {Bandit, [plug: {HTTP, plug_opts}, port: 0, ip: :loopback, startup_log: false] ++ bandit}, id: {Bandit, make_ref()} ) @@ -994,9 +995,21 @@ defmodule BeamMCP.Transport.HTTPBanditTest do # past the cap -- and it decides WHEN the 413 comes: at the crossing frame, not after # the stream ends or the deadline lapses. A mutant that gave it a piece of slack held a # 64 KiB overrun in memory and answered a stream still open with 408 at the deadline. - port = listen(read_timeout: 300) + # + # This client writes every frame without reading the server's WINDOW_UPDATEs, so the + # listener advertises a stream window that covers the whole body. Bandit 1.14 enforces + # the stream window it advertises (GHSA-c3g7-2pg5-qvwh); at its default of 65,535 bytes + # this burst is a flow-control violation, reset with FLOW_CONTROL_ERROR before the cap is + # reached (measured on 1.15.0). A window the size of the body is what a server that + # expects bodies near the cap would advertise. cap = 1_048_576 frame = :binary.copy(<<"x">>, 16_384) + + port = + listen([read_timeout: 300], + http_2_options: [default_local_settings: [initial_window_size: 2 * cap]] + ) + sock = BeamMCP.H2C.open(port, h2_headers(cap + 3 * byte_size(frame))) # Sixty-four frames reach the cap exactly; the sixty-fifth crosses it. Two more are @@ -1010,28 +1023,39 @@ defmodule BeamMCP.Transport.HTTPBanditTest do assert body =~ "Request body exceeds #{cap} bytes" end - test "over HTTP/2 a stream kept open by WINDOW_UPDATE frames alone is held past the deadline by the adapter, and nothing late is served" do - # The residue the threat model states: a control frame re-arms the adapter's own wait, - # which nothing outside it can end, so the hold is the adapter's -- thirteen bytes per - # window, nothing accumulated, the body refused whenever it comes. This test records - # that; the day the adapter bounds it, this fails and the page's row is rewritten. - body = call_json(1, 200) - # High connection deadline: this records the adapter's per-stream hold, not 029b's - # connection bound. - port = listen(read_timeout: 300, connection_timeout: 30_000) - started = System.monotonic_time(:millisecond) - sock = BeamMCP.H2C.open(port, h2_headers(byte_size(body))) - - frames = - List.duplicate(&BeamMCP.H2C.window_update(&1, 1), 10) ++ - [&BeamMCP.H2C.data(&1, body, true)] - - drip_h2(sock, frames, 100) - {result, elapsed} = h2_answered_at(sock, started) - - assert {:ok, headers, _body} = result - assert {":status", "408"} in headers - assert elapsed >= 1_000, "the hold ended at #{elapsed} ms: the adapter bounds it now" + # Until Bandit 1.14 a WINDOW_UPDATE re-armed the adapter's own per-read wait, which nothing + # outside it could end: a stream fed one every 100 ms was held past a 300 ms deadline until + # its body came (at least 1,000 ms for ten frames), the residue the threat model stated, and + # a test pinned it so it + # would fail the day the adapter bounded it. That day came: 1.14.0 fixed control frames + # resetting the body read timeout (GHSA-p2qv-vx88-385x), and on 1.15.0 every control frame + # a client can send while a body is owed was measured answered at the deadline (slice 060's + # probe: 301-329 ms). These now pin the bound, each frame kind on its own, so a release of + # the adapter that re-arms its wait on any of them fails here. + for {kind, filler} <- [ + {"a stream WINDOW_UPDATE", quote(do: &BeamMCP.H2C.window_update(&1, 1))}, + {"a connection WINDOW_UPDATE", quote(do: &BeamMCP.H2C.window_update(&1, 1, 0))}, + {"a PING", quote(do: &BeamMCP.H2C.ping/1)}, + {"an empty SETTINGS", quote(do: &BeamMCP.H2C.settings/1)}, + {"a PRIORITY", quote(do: &BeamMCP.H2C.priority/1)} + ] do + test "over HTTP/2 #{kind} every 100 ms does not hold a body read: the 408 comes at the deadline" do + body = call_json(1, 200) + # High connection deadline: this is the adapter's per-stream read, not the watchdog. + port = listen(read_timeout: 300, connection_timeout: 30_000) + started = System.monotonic_time(:millisecond) + sock = BeamMCP.H2C.open(port, h2_headers(byte_size(body))) + + frames = List.duplicate(unquote(filler), 10) ++ [&BeamMCP.H2C.data(&1, body, true)] + drip_h2(sock, frames, 100) + {result, elapsed} = h2_answered_at(sock, started) + + assert {:ok, headers, _body} = result + assert {":status", "408"} in headers + + assert elapsed >= 300 and elapsed < 1_000, + "the answer came at #{elapsed} ms for a 300 ms deadline: the adapter holds the read again" + end end # Sends `filler` on `stream` every 100 ms for `ms` from another process; the connection is @@ -1045,17 +1069,23 @@ defmodule BeamMCP.Transport.HTTPBanditTest do end) end - test "over HTTP/2 a connection pinned by WINDOW_UPDATE frames alone is closed with a readable GOAWAY at the connection deadline" do - # The stream's read cannot be ended from outside the adapter (029a's residue); the - # connection can. A client that holds a body-read stream open with control frames past - # the connection deadline, with nothing else in flight, has its whole connection closed - # -- a GOAWAY it can read, not a reset. read_timeout 300, connection_timeout 600; the - # client keeps sending for three seconds, and is answered in well under one. + # THE WATCHDOG, ON THE PATH STILL REACHABLE. It was built for the hold above (029b): a + # stream's read could not be ended from outside the adapter, but its connection could. On + # Bandit 1.14 and later no client behaviour measured holds a read past `read_timeout`, so + # at the default (`connection_timeout` twice `read_timeout`) the read ends first and the + # watchdog stands down. It stays as a backstop against an adapter that holds a read again, + # and it is reached whenever a host sets `connection_timeout` below `read_timeout`: a body + # read still waiting at the connection deadline closes its connection. These tests drive it + # that way, with a client that owes its body and sends nothing (WINDOW_UPDATE frames do not + # change what is measured; they no longer hold anything). + test "over HTTP/2 a body read still waiting at the connection deadline closes its connection with a readable GOAWAY" do + # read_timeout 1000, connection_timeout 600, nothing else in flight: the whole connection + # is closed at 600 -- a GOAWAY the client can read, not a reset -- before the stream's + # own 408 would have come at 1000. body = call_json(1, 200) - port = listen(read_timeout: 300, connection_timeout: 600) + port = listen(read_timeout: 1_000, connection_timeout: 600) started = System.monotonic_time(:millisecond) sock = BeamMCP.H2C.open(port, h2_headers(byte_size(body))) - pin_stream(sock, 1, fn s, st -> BeamMCP.H2C.window_update(s, 1, st) end, 3_000) result = BeamMCP.H2C.response(sock, 5_000) elapsed = System.monotonic_time(:millisecond) - started @@ -1063,7 +1093,7 @@ defmodule BeamMCP.Transport.HTTPBanditTest do assert {:goaway, 0, _last} = result - assert elapsed >= 600 and elapsed < 1_500, + assert elapsed >= 600 and elapsed < 1_000, "the connection closed at #{elapsed} ms for a 600 ms connection deadline" end @@ -1092,20 +1122,19 @@ defmodule BeamMCP.Transport.HTTPBanditTest do end test "over HTTP/2 the connection is not closed while another stream on it is still within its own deadline" do - # A stream held past its connection deadline does not close the connection out from + # A read waiting past its connection deadline does not close the connection out from # under a sibling still within its deadline: the watchdog waits for the latest live # deadline, not the first. Stream 1 is armed at t~0 (deadline 600); stream 3 opens ~400 # ms later (deadline ~1000). The connection is closed at ~1000, not 600 -- without the - # wait it would close at 600 and cut stream 3 off. + # wait it would close at 600 and cut stream 3 off. read_timeout 3000, so neither stream's + # own 408 comes first. body = call_json(1, 200) - port = listen(read_timeout: 300, connection_timeout: 600) + port = listen(read_timeout: 3_000, connection_timeout: 600) started = System.monotonic_time(:millisecond) sock = BeamMCP.H2C.open(port, h2_headers(byte_size(body))) - pin_stream(sock, 1, fn s, st -> BeamMCP.H2C.window_update(s, 1, st) end, 3_000) Process.sleep(400) BeamMCP.H2C.request(sock, 3, h2_headers(byte_size(body))) - pin_stream(sock, 3, fn s, st -> BeamMCP.H2C.window_update(s, 1, st) end, 3_000) assert {:goaway, 0, _last} = BeamMCP.H2C.response(sock, 5_000, 1) elapsed = System.monotonic_time(:millisecond) - started @@ -1119,7 +1148,7 @@ defmodule BeamMCP.Transport.HTTPBanditTest do # The cost, stated on the row and measured here: the legitimate stream that already # answered is fine; a stream still open when the close fires dies with the connection. body = call_json(1, 0) - port = listen(read_timeout: 300, connection_timeout: 600) + port = listen(read_timeout: 3_000, connection_timeout: 600) sock = BeamMCP.H2C.open(port, h2_headers(byte_size(body))) # Stream 3: a complete body at once. Answered 200 before the close. @@ -1128,8 +1157,7 @@ defmodule BeamMCP.Transport.HTTPBanditTest do assert {:ok, legit_headers, _} = BeamMCP.H2C.response(sock, 5_000, 3) assert {":status", "200"} in legit_headers - # Stream 1: held open by control frames past the connection deadline. - pin_stream(sock, 1, fn s, st -> BeamMCP.H2C.window_update(s, 1, st) end, 3_000) + # Stream 1: its body still owed at the connection deadline (600, under the 3000 read). assert {:goaway, 0, _last} = BeamMCP.H2C.response(sock, 5_000, 1) BeamMCP.H2C.close(sock) end diff --git a/test/support/beam_mcp/h2c.ex b/test/support/beam_mcp/h2c.ex index f0f5e305..19c66611 100644 --- a/test/support/beam_mcp/h2c.ex +++ b/test/support/beam_mcp/h2c.ex @@ -57,11 +57,20 @@ defmodule BeamMCP.H2C do def request(sock, stream, headers) when rem(stream, 2) == 1, do: :gen_tcp.send(sock, frame(0x1, 0x4, stream, hpack(headers))) - # A stream-level WINDOW_UPDATE: thirteen bytes on the wire that carry no body. The adapter - # re-arms its per-read wait on one, which is how a stream is held past the deadline. + # A WINDOW_UPDATE: thirteen bytes on the wire that carry no body; stream 0 is the + # connection's. Before Bandit 1.14 the adapter re-armed its per-read wait on one, which is how + # a stream was held past the deadline (GHSA-p2qv-vx88-385x); 1.14 bounds the read. def window_update(sock, increment, stream \\ @stream), do: :gen_tcp.send(sock, frame(0x8, 0x0, stream, <<0::1, increment::31>>)) + # The other control frames a client can send while a body is owed, none of which carries a + # body byte: PING and an empty SETTINGS on the connection, PRIORITY on the stream. + def ping(sock), do: :gen_tcp.send(sock, frame(0x6, 0x0, 0, <<0::64>>)) + def settings(sock), do: :gen_tcp.send(sock, frame(0x4, 0x0, 0, <<>>)) + + def priority(sock, stream \\ @stream), + do: :gen_tcp.send(sock, frame(0x2, 0x0, stream, <<0::1, 0::31, 15::8>>)) + # A second HEADERS on the stream with END_HEADERS and without END_STREAM: malformed under # RFC 9113, 8.1 (trailers end the stream). Bandit before 1.13 read it as trailers, ignored it # with a warning line and re-armed its wait (the other way a stream was held); 1.13 resets the