Skip to content

ClientReputationLedger: bound HashMap with LRU eviction by last_seen_us #664

Closed

Description

@thiras

Context

Raised on PR #663 review feedback (gemini-code-assist, marked high-priority security): the in-memory `HashMap<Address, ClientReputation>` in `crates/incentive/src/client_reputation.rs` is unbounded.

Threat model framing

The keyspace is structurally bounded today: entries enter only via `record_voucher_received` / `record_voucher_withheld`, which the future `cdn/client/v1` handler (#317, currently `blocked`) drives only on stream completion. A stream requires an open payment channel — itself an L2 transaction with USDC deposit and gas cost (ADR 003 §Payment Channel Open / Close). "Sybil resistance is structural" via channel-open cost is exactly the framing in #482; each new ledger key costs the attacker an on-chain channel open.

So the unbounded `HashMap` is not exploitable as a network-input DoS until the wiring lands — and even then, growth is rate-limited by the per-encounter channel-open cost.

Why this still belongs as a follow-up

Defense-in-depth on the map size is still right for the runtime-wiring PR (the one that adds the redb-backed `DiskClientReputationStore` + runtime instantiation alongside #317). The eviction policy benefits from real signals — `last_seen_us`, `completion_ratio`, `total_bytes_delivered` — that the leaf-primitive PR has no live consumer for. Shipping the cap with the wiring also lets us size `max_tracked_clients` against the actual on-chain channel-open rate observed on the testnet.

Scope

  1. Cap knob. Add `max_tracked_clients: usize` to `ClientReputationConfig` (default ~100_000; tunable per operator).
  2. Eviction. On insertion at capacity, evict the entry with the smallest `last_seen_us` (oldest observation). Linear-scan eviction is acceptable at this N; matches the pattern from `crates/node/src/dispatch.rs` `ConnectionLimiter::gc_per_source`.
  3. Throttle/lock-hygiene. Per the review note, do the eviction-candidate selection while holding the write lock briefly; bulk shrinking (if ever needed) uses `select_nth_unstable_by_key`.
  4. Metrics. `decdn_client_reputation_tracked_clients` gauge; `decdn_client_reputation_evictions_total` counter.
  5. Tests.
    • Bounded growth under flood: insert > cap distinct addresses, assert map.len() == cap.
    • LRU correctness: oldest entry is the one evicted.
    • Cap=0 / cap=1 edge cases.

Dependencies / ordering

Should ship alongside or immediately before the `cdn/client/v1` admission wiring (so the cap is in place the first time the ledger sees live network input). Not blocking on the wiring PR — can land independently.

References

  • ADR 003 §Corrupted delivery — "Sybil resistance is structural" framing.
  • #482 — parent issue; this is the deferred hardening piece.
  • #317 — `cdn/client/v1` handler (will be the live consumer).
  • #645 — related: same keyspace-bound work for the DHT rate limiter.

🤖 Filed in response to PR #663 review feedback.

Activity

  1. added
    kind:security
    rust
    on May 22, 2026
  2. github-actions commented on May 22, 2026

    @github-actions

    🎯 Triage report

    Defense-in-depth hardening for ClientReputationLedger: bound the in-memory HashMap<Address, ClientReputation> with a configurable cap and LRU eviction by last_seen_us. Filed from PR #663 review; the threat is not immediately exploitable (growth requires per-address on-chain channel-opens), but the cap should land before the cdn/client/v1 wiring (#317) makes the ledger reachable from live network input. Scope is fully specified: config knob, eviction algorithm, lock-hygiene, metrics, and test cases.

    📊 Assessment

    Dimension Value Reasoning
    Type Task Well-scoped implementation work with explicit acceptance criteria; no ambiguity or design exploration required
    Labels security, technical, rust, testing (added) Existing labels accurate; testing added because tests are explicitly in scope
    Coding agent Suitable Clear requirements, defined eviction algorithm (linear-scan by last_seen_us), explicit metrics names, concrete test cases, and a prior pattern to follow (ConnectionLimiter::gc_per_source in dispatch.rs)

    🔗 Similar issues

    💡 Notes and suggestions

    Implementation notes:

    • The eviction algorithm is specified as linear-scan (O(N)) on last_seen_us; use HashMap::iter().min_by_key(|(_, v)| v.last_seen_us) to find the candidate, then HashMap::remove. This is acceptable at the stated default of 100k entries but worth a comment explaining the deliberate O(N) choice.
    • For bulk shrink (if ever added), select_nth_unstable_by_key on a Vec of keys avoids repeated min scans.
    • Lock hygiene: the eviction candidate selection must be done under the write lock in a single critical section to prevent TOCTOU on the size check. Pattern is already established in ConnectionLimiter::gc_per_source.
    • max_tracked_clients: 0 is an edge case — decide whether 0 means "unlimited" (disable cap) or "reject all insertions". The issue implies 0 should be a valid test edge case, so define the semantic explicitly in ClientReputationConfig docs.
    • Metrics shape: align label names with existing decdn_probe_rate_limit_rejections_total for consistency — include a reason or layer label dimension so dashboards stay uniform.

    Suggested sub-tasks:

    • Add max_tracked_clients: usize to ClientReputationConfig with default 100_000
    • Implement eviction path in record_voucher_received / record_voucher_withheld (whichever does the insert)
    • Register decdn_client_reputation_tracked_clients gauge and decdn_client_reputation_evictions_total counter
    • Tests: flood (> cap distinct addresses → map.len() == cap), LRU correctness, cap=0, cap=1
    • Update DiskClientReputationStore path to honour the same cap when rehydrating from redb

    Ordering: Can land independently of #317, but should be merged before the cdn/client/v1 handler PR that wires the ledger to live streams.

    Generated by Agentic Triage for issue #664 · ● 1.9M · ◷

    Add this agentic workflows to your repo

    To install this agentic workflow, run

    gh aw add githubnext/agentics/workflows/issue-triage.md@94ec5db57374c5b04a4b8eef8b4413f9af44d63f
    
  3. added
    kind:test
    on May 22, 2026
  4. self-assigned this
    on May 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

kind:securitykind:testrust

Type

Fields

Priority

None yet

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions