Repository navigation
Conversation
Coverage Report for CI Build 42575855398Coverage increased (+0.05%) to 87.889%Details
Uncovered Changes
Coverage RegressionsNo coverage regressions found. Coverage Stats
💛 - Coveralls |
caarloshenriq
left a comment
There was a problem hiding this comment.
Concept ACK 83fc434
One gap: the relay still buffers unbounded. into_forward_req in ohttp_relay/mod.rs calls body.collect() with no limit, and it’s reachable through any gateway that opts in, in the same process as the directory. Since the commit message says every request handler, could this either cover the relay too, or narrow the message and leave the relay for a follow-up?
| /// The largest raw request body any route accepts, a conservative | ||
| /// in-memory ceiling so a body is never buffered unbounded. The V2 | ||
| /// mailbox payload has its own smaller cap in `MAX_MAILBOX_PAYLOAD_BYTES`. | ||
| pub(crate) const MAX_REQUEST_BODY_BYTES: usize = 65536; |
There was a problem hiding this comment.
Every OHTTP request payjoin sends is exactly ENCAPSULATED_MESSAGE_BYTES (8192), so the gateway could cap at that instead of 64 KiB. Is there a reason to allow 8x the largest valid request on that route? The V1 routes might still need the larger ceiling
| match http_body_util::Limited::new(body, MAX_REQUEST_BODY_BYTES).collect().await { | ||
| Ok(collected) => collected.to_bytes(), | ||
| Err(_) => return Ok(bad_request_body_res), | ||
| }; |
There was a problem hiding this comment.
This route answers an oversized body with the V1 reject JSON and a 400, not the 413 the description mentions. That seems right for BIP78 senders, which expect the JSON error, but a short comment here would make it clear it’s deliberate rather than a missed collect_within_limit
83fc434 to
53253be
Compare
|
Thanks for the careful review @caarloshenriq. Addressed in the latest push: Relay buffering good catch,
Capping the gateway at
So I kept a single conservative ceiling it bounds memory (the goal) without coupling this PR to the request/response size model. Happy to tighten the gateway to the exact request size as a follow-up that reconciles those budgets and fixes the test helper's padding I can open an issue for it if you think it's worth doing. |
|
Concept ACK, tACK 53253be with one test request below.
Tested: fmt, clippy ( AI-assisted review; I ran the tests and mutations myself. |
7577400 to
7345f57
Compare
Every inbound request read the whole body into memory with body.collect() before checking its size, so a client could force the server to buffer an arbitrarily large body on any route. The OHTTP gateway and the relay forward path had no size check at all. Cap each read with http_body_util::Limited at the largest size that path accepts: a conservative MAX_REQUEST_BODY_BYTES ceiling for the gateway, the relay forward, and the direct V1 routes, and the tighter MAX_MAILBOX_PAYLOAD_BYTES for the v2 mailbox payload. Oversized bodies are rejected before they are buffered, and the manual post-buffer length checks are removed. Signed-off-by: codaMW <[email protected]>
7345f57 to
44927a4
Compare
|
Thanks @Xoulomon, @caarloshenriq. Addressed:
AI-assisted, I ran the tests and mutation myself. |
CaptainLevi2k04
left a comment
There was a problem hiding this comment.
Looks good overall, just a question about the V1 limits!!
| let body_bytes = | ||
| match http_body_util::Limited::new(body, MAX_REQUEST_BODY_BYTES).collect().await { |
There was a problem hiding this comment.
Even if this fired, MAX_MAILBOX_PAYLOAD_BYTES below catches the same body with the same 400...
So this bound is also unreachable...
Same question as put_payjoin_v1
| if req.len() > V1_MAX_BUFFER_SIZE { | ||
| return Err(HandlerError::PayloadTooLarge); | ||
| } | ||
| let req = collect_within_limit(body, MAX_REQUEST_BODY_BYTES).await?; |
There was a problem hiding this comment.
This limit can't fire the put_payjoin_v1... this only sees bodies the gateway already capped at ~8 KiB.
It's dead defense-in-depth...
Could be Kept with a comment saying so, or drop it?
CaptainLevi2k04
left a comment
There was a problem hiding this comment.
Suggested replacement:
"Reject request bodies larger than a route accepts before buffering them, closing an unbounded-memory read on the OHTTP gateway and the relay (#2031)" (or mention the mailbox).
| ## Unreleased | ||
|
|
||
| - Let relay operators accept gateway purposes besides BIP77 with `[ohttp_relay] extra_allowed_purposes` (#2024) | ||
| - Reject request bodies larger than a route accepts before buffering them, closing an unbounded-memory read on the OHTTP gateway and the V1 routes (#2031) |
There was a problem hiding this comment.
Since the V1 limits are unreachable (as discussed), saying this fixes an unbounded-memory read on 'V1 routes' is misleading. The real fix is for the OHTTP gateway and the relay. Can we reword this?
Closes the request-body RAM gap in #941 ("tower_http::limit, remove our custom body size limiter").
Every handler in
payjoin-mailroomread the full request body into memory withbody.collect()and only then checked its length, so a client could make the server buffer an arbitrarily large body on any route;handle_ohttp_gatewayhad no size check at all, and the relay'sinto_forward_reqforwarded an unbounded body.Each read is now bounded with
http_body_util::Limitedat the largest size that path legitimately accepts, so an oversized body is rejected before it is buffered, and the manual post-buffer checks are removed:MAX_REQUEST_BODY_BYTES(64 KiB) ceiling. The encapsulated request is only ~8 KiB, so this bounds memory without rejecting valid traffic.post_mailbox(v2 payload): the tighterMAX_MAILBOX_PAYLOAD_BYTES, preserving the "too large to return through the gateway" rule from Reject mailbox payloads the gateway can't return #1983.Oversized bodies are rejected with 413 on the gateway, V1 PUT, and relay paths;
post_fallback_v1answers a BIP78 v1 sender with the JSON reject body and 400 instead, as those senders expect. The relay previously answered 400 with the raw error string; it now returns 413 via a newohttp_relay::Error::PayloadTooLarge, matching the directory.On approach:
axum::extract::DefaultBodyLimitonly governs theBytes/Jsonextractors, which these raw-body handlers don't use. A router-levelRequestBodyLimitLayerdoes build on axum 0.8 (thanks @Xoulomon for checking), but it only returns 413 up front whenContent-Lengthis set; without that header the overflow reaches the handler wrapped inaxum::Error, so adowncast_ref::<LengthLimitError>()misses it unless it walkssource(). The per-handlerLimitedcatches the overflow regardless ofContent-Length, which is why I kept it. I renamedV1_MAX_BUFFER_SIZEtoMAX_REQUEST_BODY_BYTESsince it now serves as the general raw-request ceiling.Tested (
payjoin-mailroom,--all-features):request_body_over_limit_is_rejected_at_ingress/request_body_at_limit_passes_ingress: a body one byte over the gateway ceiling returns 413 before buffering; a body at the ceiling passes.relay_rejects_body_over_limit_before_forwarding/relay_forwards_body_at_limit: the relay rejects an over-limit body with 413 and never forwards it; a body at the limit reaches the gateway. Mutating the relay bound tousize::MAXflips the over-limit test (the body reaches the gateway, 200 instead of 413).The V1
put_payjoin_v1andpost_fallback_v1bounds are defense-in-depth and not independently testable:put_payjoin_v1only runs on a decapsulated request whose body the gateway has already bounded, and an oversizedpost_fallback_v1body is caught by the downstreamMAX_MAILBOX_PAYLOAD_BYTEScheck with the identical 400 + reject JSON.Tightening the gateway to the exact request size (vs the conservative 64 KiB) needs a request/response size-budget reconciliation, tracked as a follow-up in #2041.
AI disclosure: I used an AI assistant for code navigation, for working through the approach, and to draft this description. I reviewed and understand the change, and ran all tests and the mutation myself.