Bounty
XE-16726 · Bounty

[Medium] GET /statechain/kv full-load + full-sort per request, no per-client limit

Closed · duplicate —claimed Medium xe#653 filed 2026-11-17 05:32 UTC by kecedanzz77-bit

Report

Summary

handleStateKVAll (api/handler.go:976-986) loads the entire statechain KV store and sorts all keys on every request, even for ?limit=1. Same bug class as #648, but a different endpoint not covered by #648. The KV is unbounded (epoch keys are write-once and never deleted), so per-request cost grows monotonically with chain age.

Root Cause

  • handleStateKVAll calls GetAllStateKV() -> KVStore.GetAll() (statechain/kvstore.go:521-537), which copies the entire KV map.
  • paginateMap (api/handler.go:624-637) then collects and sort.Strings ALL keys (O(N log N)) before slicing one page.
  • The ?prefix= variant is no better: GetByPrefix scans the whole map with strings.HasPrefix.
  • No perClientLimit wrapper (only /accounts and /pending have it); the only throttle is the global read rate limit (200 req/s/IP, api/handler.go:301).

Impact

CPU DoS on the node. With 50,000 KV entries, a limit=1 request copies 50,000 entries and sorts 50,000 keys in ~93 ms. At 200 req/s/IP, one client can drive ~19 CPU-seconds per wall-clock second -- enough to saturate the node's cores and starve consensus validation (same process). GetByPrefix("epoch.4999") scanned all 50,000 entries to match 11.

Verification (executed PoC)

Harness with 50,000 KV entries: limit=1 request took 93 ms (full copy + full sort). Audited at HEAD 8cc209d (testnet-0006).

Note on related reports

Same class as #648 (/leases, /conflicts, /reputation, /directory full-table scans), but this endpoint is not covered by #648. #86 (OpenAPI schema), #284 (lock contention on /statechain/blocks), #390 (/blocks/recent offset) are different mechanisms/endpoints.

Suggested fix

Apply the perClientLimit wrapper (as on /accounts, /pending) and/or serve from a sorted index / store-level prefix scan instead of copy-then-sort.

Thread

Joseph Denneteam2026-11-22 23:26 UTCdesk

Thanks. This is the same finding as https://bounty.xe.network/XE-0861, which was filed first; the programme pays the first verifiable submission, so this one closes as a duplicate. If you believe it is a different bug, file a new report that links https://bounty.xe.network/XE-16726 and says what differs; it is read like any other.