{"id":"XE-16726","kind":"bounty","visibility":"public","source":"github","source_repo":"xeprotocol/xe","source_ref":"653","source_url":"https://github.com/xeprotocol/xe/issues/653","reporter_id":2920,"title":"[Medium] GET /statechain/kv full-load + full-sort per request, no per-client limit","area":"node-api","claimed_tier":"medium","assessed_tier":null,"verdict":"duplicate","duplicate_of":"XE-0861","tracked":false,"internal_ref":"","internal_url":"","status":"closed","closed_reason":"duplicate","github_state":"open","disclosed_at":null,"award_xe":null,"award_approved":false,"paid_ref":"","created_at":"2026-11-17T05:34:14.960675+00:00","updated_at":"2026-11-22T23:26:16.898615+00:00","source_created_at":"2026-11-17T05:32:15+00:00","told_at":"2026-11-22T23:26:16.914857+00:00","status_changed_at":"2026-11-22T23:26:16.904765+00:00","url":"https://bounty.xe.network/XE-16726","body":"## Summary\n`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.\n\n## Root Cause\n- `handleStateKVAll` calls `GetAllStateKV()` -> `KVStore.GetAll()` (`statechain/kvstore.go:521-537`), which copies the entire KV map.\n- `paginateMap` (`api/handler.go:624-637`) then collects and `sort.Strings` ALL keys (O(N log N)) before slicing one page.\n- The `?prefix=` variant is no better: `GetByPrefix` scans the whole map with `strings.HasPrefix`.\n- 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`).\n\n## Impact\nCPU 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.\n\n## Verification (executed PoC)\nHarness with 50,000 KV entries: `limit=1` request took 93 ms (full copy + full sort). Audited at HEAD `8cc209d` (testnet-0006).\n\n## Note on related reports\nSame 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.\n\n## Suggested fix\nApply the `perClientLimit` wrapper (as on `/accounts`, `/pending`) and/or serve from a sorted index / store-level prefix scan instead of copy-then-sort.","patch_refs":[],"thread":[{"id":18876,"source":"desk","source_ref":"reply:16553","author":"Joseph Denne","team":true,"body":"Thanks. This is the same finding as https://bounty.xe.network/XE-0861, which was filed first; the programme pays the\nfirst verifiable submission, so this one closes as a duplicate. If you believe it is a different\nbug, file a new report that links https://bounty.xe.network/XE-16726 and says what differs; it is read like any other.","created_at":"2026-11-22T23:26:16.898615+00:00"}]}