Skip to content

fix(node): reject unknown payment channel before serving any bytes (#848) - #875

Merged
thiras merged 1 commit into
mainfrom
fix/848-reject-unknown-channel-pre-serve
Jun 10, 2026
Merged

thiras merged 1 commit into
mainfrom
fix/848-reject-unknown-channel-pre-serve

Conversation

@thiras

@thiras thiras commented Jun 10, 2026

Copy link
Copy Markdown
Contributor

What

Fixes #848 — a node served up to one voucher interval (default ~1 MB) or the whole blob for free to any peer requesting paid delivery on an unknown / never-opened payment channel.

Root cause

serve_stream looked up the channel as an Option, signed ok: true, and called deliver even when the lookup returned None. The unknown channel was only caught later in collect_voucher (mid-stream VoucherRejected { WrongChannel }) — after bytes had already shipped. For a sub-interval blob the entire blob shipped before the closing voucher was rejected; for a larger blob, the first full interval shipped. Repeatable per stream, bounded only by the dispatch limiter / stream cap. This contradicted the "channels must be pre-persisted" model (#327).

Fix

Add a pre-serve gate in serve_stream: when the channel lookup is None, refuse with the delivery-side StreamError::NotFound before signing the success response — no bytes flow.

  • NotFound mirrors the existing owner-mismatch gate and avoids leaking channel existence. The mid-stream WrongChannel reason cannot ride in the initial StreamResponse (it's is_mid_stream-only).
  • With channel now non-optional, the owner-binding check simplifies from (Some, Some) to if let Some(client) (behavior-preserving — the old tuple only fired when both were Some).
  • The collect_voucher None arm is now unreachable from its sole caller; kept as a defensive backstop with a comment noting so.

Test

Rewrote client_unknown_channel_is_rejected to:

  • assert the server's StreamResponse directly (!ok + NotFound) via the raw_request idiom — proves the success/serve path was never entered, rather than inferring from the buyer's error string;
  • use a 1.5 MiB blob (larger than the voucher interval) so the gate is proven to fire independent of blob size.

Verification

  • cargo nextest run -p decdn-node → 568/568 passed
  • cargo clippy -p decdn-node --all-targets → clean
  • cargo fmt -- --check → clean

Out of scope (follow-up)

Serve-path rejections increment no metric — a pre-existing observability gap across all reject branches in serve_stream, amplified by this issue. Tracked separately so an operator can alert on refused-stream rate.

🤖 Generated with Claude Code

)

`serve_stream` looked up the payment channel as an `Option`, signed
`ok: true`, and streamed bytes even when the channel was `None` (unknown
/ never opened). The unknown channel was only caught later in
`collect_voucher` (mid-stream `WrongChannel`), by which point up to one
voucher interval (the negotiated cadence, default 1 MB) — or the whole
blob, if smaller — had already shipped for free, repeatable per stream.

Add a pre-serve gate in `serve_stream`: when the channel lookup returns
`None`, refuse with the delivery-side `StreamError::NotFound` before
signing the success response. `NotFound` mirrors the existing
owner-mismatch gate and avoids leaking channel existence; the mid-stream
`WrongChannel` reason cannot ride in the initial `StreamResponse`. With
the channel now non-optional, the owner-binding check simplifies from
`(Some, Some)` to `if let Some(client)`.

Rewrite `client_unknown_channel_is_rejected` to assert the server's
`StreamResponse` directly (`!ok` + `NotFound`) via `raw_request`, and use
a 1.5 MiB blob so the gate is proven to fire independent of blob size.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>

@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 addresses issue #848 by refusing stream requests on unknown or never-opened channels pre-serve, returning a StreamError::NotFound error before any bytes are delivered. This prevents clients from downloading up to one voucher interval of data for free before a mid-stream rejection occurs. The verified client binding check has been simplified accordingly, and the loopback test client_unknown_channel_is_rejected has been updated to verify this pre-serve refusal behavior directly on the raw StreamResponse. There are no review comments, so I have no feedback to provide.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Copilot AI 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.

Pull request overview

This PR fixes a paid-delivery security gap in the node stream handler: requests that reference an unknown / never-opened payment channel are now rejected before a success response is signed and before any blob bytes can be served (closing the “free first voucher interval” egress described in #848).

Changes:

  • Add a pre-serve gate in serve_stream to refuse unknown channels with a signed StreamResponse { ok: false, error: NotFound }.
  • Simplify the verified-client owner binding check now that channel is guaranteed present on the serve path.
  • Rewrite the loopback test to assert directly on the server’s first StreamResponse using the raw request path, and use a 1.5 MiB blob to ensure the gate is independent of blob size.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
crates/node/src/handlers/client.rs Reject unknown channels pre-serve with NotFound, preventing any bytes from being delivered before channel validation.
crates/node/tests/client_loopback.rs Updates the regression test to assert the server-side refusal response directly (and with a payload large enough to cross an interval if served).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@github-actions

Copy link
Copy Markdown

Coverage report

Total: 85.93% (was 85.93%, ▼ 0.01%) — 34328/39951 lines

File Coverage Lines Δ
crates/node/src/handlers/client.rs 76.16% 492/646 ▼ 0.28%
History
Commit Total Δ vs base
ab4eda1 (HEAD) 85.93% ▼ 0.01%

@thiras
thiras merged commit ce4f4ec into main Jun 10, 2026
21 checks passed
@thiras
thiras deleted the fix/848-reject-unknown-channel-pre-serve branch June 10, 2026 19:54
thiras added a commit that referenced this pull request Jun 12, 2026
…884)

* feat(node): meter refused serve_stream paid-delivery requests (#876)

Every paid-delivery reject branch in `serve_stream` refused with a signed
`StreamResponse { ok: false }` and a `tracing::warn!` but incremented no
metric, so a refused stream was invisible to operator dashboards — there was
no way to alert on "rate of refused streams" or distinguish a misconfigured
client from an abuse campaign. This is the operator-side counterpart to the
#848/#875 silent-failure work.

Add six per-reason reject counters (the `iroh_metrics` backend has no runtime
labels, so one Counter per reason matches the existing `dispatch_rejected_*` /
`dht_rate_limit_rejected_*` convention):

  decdn_serve_stream_rejected_{evicted_since_probe,cache_miss,internal_error,
  blob_too_large,unknown_channel,owner_mismatch}_total

A private `ServeRejectReason` enum is threaded into the central `respond_error`
helper, which increments the matching counter at one site. The reason is the
single source of truth: `wire_error()` derives the signed `StreamError`, so the
three branches that deliberately share the wire `NotFound` (to avoid leaking
channel existence) stay wire-indistinguishable while remaining separately
countable server-side — and an inconsistent error/reason pairing is
unrepresentable at the call sites.

Tests: a metrics unit test asserts all six counters start at zero and each
method bumps exactly its own counter; five loopback tests assert the correct
counter advances on a real refusal (all reasons except InternalError, which
needs a faulting-cache double — deferred).

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>

* docs(node): sharpen serve_stream reject-counter comments (#876)

Address PR review nits: name the three wire-conflated NotFound reasons
explicitly instead of the ambiguous "reasons below", and scope the
"finer-grained than the wire" claim to the three NotFound cases (the other
three reasons map 1:1 to distinct wire errors). Comment-only; no behavior
change.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>

---------

Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

node: unknown/absent payment channel is not validated before bytes are served — free egress of the first voucher interval

2 participants