Skip to content

Payjoin-mailroom: cap the OHTTP gateway at the exact request size and reconcile request/response size budgets #2041

Open

Description

@codaMW

Follow-up from #2031 (review thread with @caarloshenriq).

#2031 bounds every mailroom body read with a single conservative ceiling, MAX_REQUEST_BODY_BYTES (64 KiB), to stop unbounded buffering. For the OHTTP gateway route specifically, the true maximum valid request is far smaller, so the route could be capped tighter. Doing that cleanly is blocked on a request-vs-response size-budget mismatch that shouldn't be entangled with the memory-bounding fix:

  • ENCAPSULATED_MESSAGE_BYTES (8192) is the response/message size encapsulate() returns [u8; 8192].
  • The encapsulated request that actually transits the gateway is larger (~8232 bytes in largest_mailbox_payload_round_trips_through_gateway), because of request-side framing (N_ENC encapsulated key + tag + bhttp header).
  • MAX_MAILBOX_PAYLOAD_BYTES (8137) already exceeds the request plaintext budget PADDED_BHTTP_REQ_BYTES (8104) by 33 bytes the mailbox constants are response-sized.
  • The through_gateway test helper pads the bhttp to BHTTP_REQ_BYTES (8144 = ENCAPSULATED_MESSAGE_BYTES − nonce − tag, the response-framing budget) rather than the request budget, so it builds an ~8232-byte encapsulated request that only passed because the gateway was previously unbounded.

Proposed work

  • Introduce an explicit max encapsulated request constant derived from the request framing, distinct from the response-sized constants.
  • Cap the gateway route at that constant (replacing the 64 KiB ceiling on that route only).
  • Fix through_gateway to pad to the request budget.
  • Keep MAX_REQUEST_BODY_BYTES for the V1 routes, which carry variable-size BIP78/PSBT bodies.

Acceptance

  • Gateway rejects a request above the true max encapsulated request size with 413.
  • largest_mailbox_payload_round_trips_through_gateway passes with the corrected helper padding.
  • V1 routes unchanged.

Activity

  1. caarloshenriq commented on Nov 10, 2026

    @caarloshenriq
    Contributor

    Agree with the diagnosis, but I think the framing of "request is larger than response" is off, and that changes the fix.

    The max encapsulated request is already ENCAPSULATED_MESSAGE_BYTES. The client pads bhttp to PADDED_BHTTP_REQ_BYTES = ENCAPSULATED_MESSAGE_BYTES - (N_ENC + N_T + OHTTP_REQ_HEADER_BYTES) = 8192 - (65 + 16 + 7) = 8104 (payjoin/src/core/ohttp.rs) precisely so that the encapsulated request is exactly 8192 bytes, which is why ohttp_encapsulate returns [u8; ENCAPSULATED_MESSAGE_BYTES]. Requests and responses are the same size on the wire; only the overhead differs (88 vs 48 bytes), hence the different plaintext budgets (8104 vs 8144). The ~8232-byte request only exists because through_gateway pads to the response budget. So I'd cap the gateway route at ENCAPSULATED_MESSAGE_BYTES directly rather than introduce a new constant with the same value, and have the test helper import PADDED_BHTTP_REQ_BYTES instead of re-deriving it.

    (Whether the gateway should require == 8192 rather than <= 8192, since BIP77 fixes the size, is a separate question that affects third-party clients. I'd keep it out of scope here.)

    The acceptance criterion contradicts itself. With the helper padding to 8104, largest_mailbox_payload_round_trips_through_gateway cannot pass as written: a POST carrying MAX_MAILBOX_PAYLOAD_BYTES (8137) plus bhttp request framing exceeds 8104, so the gateway returns 413. MAX_MAILBOX_PAYLOAD_BYTES is the largest payload the gateway can return; the largest a conforming client can post is smaller and depends on the request path length. Similarly, mailbox_payload_too_large_to_return_is_rejected would still pass, but because the gateway rejects it before the mailbox check runs.

    The mailbox limit itself is still right, since V1 bodies reach the mailbox unwrapped and only have to fit the GET response. It's worth a comment noting that it's response-side and that the gateway is tighter in practice.

    Suggested scope:

    • Cap the gateway route at ENCAPSULATED_MESSAGE_BYTES, 413 above it.
    • Pad through_gateway to PADDED_BHTTP_REQ_BYTES.
    • Split the tests:
      • round-trip with the largest postable payload (computed from the request bhttp framing, not hardcoded)
      • MAX_MAILBOX_PAYLOAD_BYTES / +1 exercised directly (handler or V1 route)
      • a dedicated gateway 413 test at ENCAPSULATED_MESSAGE_BYTES + 1
    • Keep MAX_REQUEST_BODY_BYTES on the V1 routes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions