Skip to content

fix(protocol): bound ProbeResponse.rate_per_mb at the wire boundary (#378) - #396

Merged
alpergundogdu merged 3 commits into
mainfrom
claude/dreamy-margulis-391a14
May 5, 2026
Merged

alpergundogdu merged 3 commits into
mainfrom
claude/dreamy-margulis-391a14

Conversation

@alpergundogdu

Copy link
Copy Markdown
Contributor

Summary

Fixes #378 — ProbeResponse.rate_per_mb was a peer-controlled u64 with no upper bound at the wire boundary. A malicious node could advertise u64::MAX, overflowing the client selection score rate × rtt × scale / reputation² (#322) and silently winning selection — the inverse of what an honestly-priced rate would do.

Changes

  • Protocol (crates/protocol/src/message.rs): Add MAX_RATE_PER_MB = 10^12 (1 trillion base units, ~7 orders of magnitude of headroom from u64::MAX). Implement Deserialize manually for ProbeResponse so the wire boundary rejects oversize rates as MessageValidationError. Postcard wraps this as SerdeDeCustom, which the framing layer maps to FrameError::Decode → APP_ERR_MALFORMED_MESSAGE (ADR 013) on the server side and a decode error on the client side.
  • Wire stability: Add a fixed-byte test asserting the postcard layout for ProbeResponse is unchanged — manual Deserialize cannot silently diverge from the derived Serialize.
  • Config (crates/node/src/config/mod.rs): Tighten resolve_payment to enforce the same ceiling at startup and on hot reload (via PaymentSection::resolve), so an operator misconfiguring the rate fails fast rather than publishing ProbeResponses that every honest client decoder rejects.

Bound rationale

MAX_RATE_PER_MB = 1_000_000_000_000 (1 trillion base units):

  • USDC at 6 decimals: $1M per MB — comfortably above any realistic CDN rate.
  • Leaves ~10^7 headroom from u64::MAX for rate_per_mb × rtt_ms math.
  • Selection-formula callers (Implement node selection algorithm (rate × rtt × 1/reputation²) #322) should still use saturating arithmetic as defense-in-depth — the protocol bound is the boundary check, not a substitute for safe math.

ADR alignment

  • ADR 013 §Signed Field Freezing lists rate_per_mb as a planned signed field of ProbeResponse. The validating Deserialize impl is documented to move with the future ProbeResponseBody so the bound continues to apply after the signed-body refactor.
  • No wire format change — the on-wire bytes are byte-identical to what the derived Deserialize produced. No ALPN bump required.

Test plan

  • cargo build — passes
  • cargo clippy --all-targets --workspace -- -D warnings — passes
  • cargo test -p decdn-protocol — 33/33 pass (8 new: roundtrip-at-max, decode-rejects-above-max, decode-rejects-u64-max, message-decode-propagates, validate-consistency, wire-format-stability)
  • cargo test -p decdn-node — 215+ pass (2 new in config::tests: rejects-rate-above-protocol-max, accepts-rate-at-protocol-max)
  • cargo fmt -- --check — clean

Note: probe_rate_limit_returns_rate_limited_close_code exhibits pre-existing flakiness under workspace-wide concurrent test pressure (passes 3/3 in isolation, both with and without these changes).

…378)

ProbeResponse.rate_per_mb is peer-controlled and feeds the client
selection score `rate × rtt × scale / reputation²` (#322). With no
upper bound, a malicious node could advertise u64::MAX and overflow
the multiplication, silently winning the selection — the inverse of
what an honestly-priced rate would do.

- Add MAX_RATE_PER_MB = 10^12 (1 trillion base units) in the protocol
  crate. Comfortably above any realistic CDN rate; ~7 orders of
  magnitude of headroom from u64::MAX for downstream arithmetic.
- Implement Deserialize manually for ProbeResponse so the wire
  boundary rejects oversize rates as MessageValidationError, mapped
  by the framing layer to FrameError::Decode → APP_ERR_MALFORMED_MESSAGE
  on the server side and a decode error on the client side.
- Pin the wire layout with a fixed-byte assertion so the manual impl
  cannot silently diverge from the derived Serialize.
- Tighten resolve_payment to enforce the same ceiling at startup and
  on hot reload, so an operator misconfiguring the rate fails fast
  instead of publishing ProbeResponses every honest client rejects.

Selection-formula callers should still use saturating arithmetic as
defense-in-depth — the bound is the protocol-boundary check, not a
substitute for safe math.
@alpergundogdu

Copy link
Copy Markdown
Contributor Author

/gemini review

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request implements a protocol-level maximum limit for the rate_per_mb field in ProbeResponse to prevent potential overflows in client selection calculations. Validation is enforced both at the protocol boundary through a manual Deserialize implementation and within the node's configuration layer. New unit tests verify these boundary constraints and ensure the stability of the wire format. I have no feedback to provide.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a protocol-level ceiling on rate_per_mb to prevent potential overflows in client selection scores. The changes include adding a MAX_RATE_PER_MB constant, implementing validation logic in the node's configuration layer, and adding a manual Deserialize implementation for ProbeResponse to enforce the limit at the wire boundary. A review comment suggests using Serde's deserialize_with attribute instead of a manual Deserialize implementation with a mirror struct to reduce maintenance risk and ensure field synchronization.

Comment thread crates/protocol/src/message.rs Resolved
…_with

Address Gemini review feedback on #396: replace the hand-written
`impl Deserialize for ProbeResponse` + mirror struct with
`#[serde(deserialize_with = "deserialize_rate_per_mb")]` on the field.

The field-level hook keeps the struct's `Deserialize` derived, so
field order and codec stay in lockstep with the public type — there
is no mirror struct that can drift out of sync with the wire format.
Validation logic is unchanged; the wire bytes are byte-identical
(the existing `probe_response_wire_format_is_stable` test still
passes), and all 33 protocol tests remain green.
@alpergundogdu

Copy link
Copy Markdown
Contributor Author

Addressed in 5d12c35: replaced the manual Deserialize impl + mirror struct with #[serde(deserialize_with = "deserialize_rate_per_mb")] on the field. The struct's Deserialize is now back to a derive, so field order and codec stay in lockstep with the public type — no mirror struct to drift out of sync. The wire bytes are byte-identical (the probe_response_wire_format_is_stable test still passes).

/gemini review

The cargo doc job rejects intra-doc links from public items to
private ones (`-D rustdoc::private-intra-doc-links`). Reference the
hook by name in backticks rather than as a clickable link.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a protocol-level limit on the rate_per_mb field within ProbeResponse to mitigate potential overflow issues in client selection algorithms, addressing issue #378. The implementation includes a new MAX_RATE_PER_MB constant, a custom Serde deserialization hook for wire-boundary validation, and corresponding checks in the node's payment resolution logic to ensure configuration consistency. Additionally, the PR includes unit tests for validation logic and wire-format stability. I have no feedback to provide.

@alpergundogdu
alpergundogdu merged commit eb1536e into main May 5, 2026
9 checks passed
@alpergundogdu
alpergundogdu deleted the claude/dreamy-margulis-391a14 branch May 5, 2026 10:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant