Skip to content

client-pull: express UpstreamRefused's evidence invariants in the type #1377

Closed

Description

@yigitdot

Follow-up from review of #1367 (G-GOV-03). No defect today — this is about making a security-relevant invariant unrepresentable rather than documented.

Current shape

pub struct UpstreamRefused {
    pub error: StreamError,
    pub response: Option<StreamResponse>,  // added in #1367
}

The field docs assert four invariants, all enforced only by convention:

  1. response == Some(r) iff the refusal arrived at the open stage; None iff a mid-stream ClientMessage::StreamError frame (which carries no signature).
  2. When Some(r): self.error == r.error.unwrap() — the wire code is duplicated in two places that nothing keeps in sync.
  3. When Some(r): r.body.ok == false.
  4. When Some(r): r.slash_sig recovers to expected_signer.

Both fields are pub, there is no constructor, and the struct is exported from the crate root with 8+ construction sites outside client-pull. The only thing keeping 2–4 true is that the single production path goes through the private refusal(), which derives error from response.

Why it is worth doing

This is not diagnostic state. response is the payload a challenger replays to SlashJudge, which burns a challengeBond and slashes an operator's stake. crates/e2e/src/client.rs already consumes refused.response and forwards it to an on-chain challenge with no re-verification, trusting invariant 4 entirely on the strength of a doc comment. The failure mode this admits is a silently mis-attributed accusation.

Suggested shape

pub enum UpstreamRefused {
    /// Open-stage: the peer signed `ok == false`, verified against `expected_signer`.
    Open { error: StreamError, response: StreamResponse },
    /// Mid-stream `ClientMessage::StreamError` — unsigned, never evidence.
    MidStream { error: StreamError },
}

with private fields, a fallible open(response) that derives error from the response, mid_stream(error), a total error() accessor, and evidence() -> Option<&StreamResponse>.

Invariant 1 becomes the variant tag; 2 becomes unviolatable; 3 follows from open() requiring response.error.is_some() (which validate() already ties to ok == false). Invariant 4 cannot be enforced here (the type does not know the expected signer) — leave that check where it is and document it.

Cost

Lower than it looks: 5 downcast consumers change .error to .error() and still match fine; 8 test construction sites become UpstreamRefused::mid_stream(err), which is shorter than the current literal. No public-API snapshot churn — decdn-client-pull is not covered by that job, which runs -p decdn-protocol only.

Related

  • The type is also still used as an anyhow context marker (crates/node/src/node_origin.rs, inside mod tests). Since downcast_ref returns the outermost match, a .context(UpstreamRefused { response: None }) layered over an evidence-carrying error would silently drop the signed attestation. Constructors would make that misuse obviously wrong.
  • Derived Debug now renders the full signed body plus the 65-byte slash_sig; a manual Debug eliding the signature to a length would keep tracing::error!(?err) readable.

Metadata

Metadata

Assignees

Labels

kind:securityprio:mediumrust

Type

No type

Fields

Priority

None yet

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions