Skip to content

rate-limit: the two From<&Resolved*> for RateLimitConfig impls are untested and transposition-blind #1457

Closed

Description

@thiras

Split out of the review on #1456 (rather than widening a deletion PR).

What

crates/node/src/rate_limit.rs has two From impls mapping ResolvedDht / ResolvedProbe (in decdn-common) onto the engine-local RateLimitConfig. They are the single mapping point the runtime uses (crates/node/src/runtime/mod.rs:857 and :902), and the comment above them used to claim centralizing there "lets the compiler catch a field transposition between the two parallel representations."

That claim is false. After #1440 removed trusted_ips: HashSet<IpAddr>, all eight remaining fields are f64 / u32 / usize, so every one can be swapped with a same-typed sibling and still compile:

Swap Compiles?
per_peer_rate_per_sec ↔ per_ip_rate_per_sec ↔ global_rate_per_sec yes
per_peer_burst ↔ per_ip_burst ↔ global_burst yes
max_tracked_per_ip ↔ max_tracked_per_peer yes

The exhaustive struct literals do catch an added or dropped field, which is worth having — but that is arity, not transposition, and any struct literal without ..Default::default() gets it for free. The comment has been corrected in #1456 to say what it actually buys; this issue is about closing the gap.

Why it matters

grep finds no test covering either impl — every rate-limiter test builds RateLimitConfig by hand rather than going through the From. Two aggravating factors:

  • The two impl bodies are now byte-identical, so a transposition introduced in one and not the other is invisible to review-by-diff.
  • The defaults differ meaningfully. DHT is per_peer 20/40, per_ip 100/200; probe is per_peer 5/5, per_ip 50/200 (ADR 005 deliberately tighter, since probes are unauthenticated and cheap to flood). A per_peer ↔ per_ip transposition in the probe impl would loosen the per-peer probe cap from 5 req/s to 50 req/s — a security-relevant misconfiguration on an unauthenticated path, silently.

Proposed fix

One test per impl, ~20 lines total: build a ResolvedDht / ResolvedProbe with eight pairwise-distinct sentinel values and assert each lands in its matching RateLimitConfig slot. That makes the centralization argument load-bearing and costs less than the comment defending it did.

Then restore the stronger wording in the comment at crates/node/src/rate_limit.rs:101-108.

Related

  • Remove the trusted-IP rate-limit exemption (ADR 005/022) #1440 / refactor: drop two dead knobs — min-reputation floor and trusted-IP rate-limit exemption #1456 — removed the one type-distinguished field, which is what took this from ~90% false to 100% false.
  • A larger follow-up worth considering separately: there are now five structurally identical eight-field rate-limit structs across two crates (Dht/ProbeRateLimitConfig, ResolvedDht/ResolvedProbe, RateLimitConfig), differing only in Default values and doc prose. A shared RateLimitTuning in the config-types leaf crate is newly unblocked now that trusted_ips (the field that would have dragged std::net in) is gone. The cost is losing the per-ADR default doc prose ("Absent => 20 req/s" vs "Absent => 5 req/s"), which is a genuine feature — judgement call, not a must.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Fields

Priority

None yet

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions