{"id":"XE-0112","kind":"bounty","visibility":"public","source":"github","source_repo":"xeprotocol/xe","source_ref":"172","source_url":"https://github.com/xeprotocol/xe/issues/172","reporter_id":37,"title":"api: rate-limiter map hits maxRateLimiterIPs, then every NEW client IP is denied by a permanent 0/0 limiter (Medium, measured)","area":"node-api","claimed_tier":null,"assessed_tier":null,"verdict":null,"duplicate_of":null,"tracked":false,"internal_ref":"","internal_url":null,"status":"triaging","closed_reason":null,"github_state":"open","disclosed_at":null,"award_xe":null,"award_approved":false,"paid_ref":"","created_at":"2026-10-29T13:57:28.882503+00:00","updated_at":"2026-11-10T00:11:49.311505+00:00","source_created_at":"2026-10-29T06:01:35+00:00","told_at":"2026-11-08T20:18:57+00:00","status_changed_at":"2026-11-08T20:17:47.926139+00:00","url":"https://bounty.xe.network/XE-0112","body":"## HTTP API rate-limiter map hits `maxRateLimiterIPs`, then every NEW client IP is denied by a permanent 0/0 limiter\n\n**Component:** `api/handler.go` — `ipRateLimiter.getLimiter`\n**Severity (my assessment):** Medium — availability only, no funds or consensus impact\n**Track:** Volume & resilience → *\"Starvation: so many connections to chat or the API that other users can't get through\"* and *\"Resource exhaustion: load that runs a node out of memory\"*\n\n### Summary\n\n`ipRateLimiter.getLimiter` keys buckets by the string returned from `clientIP(r)`. When\nthe map reaches `maxRateLimiterIPs` (100,000) it returns a **brand-new limiter with rate 0\nand burst 0** instead of evicting anything:\n\n```go\nfunc (rl *ipRateLimiter) getLimiter(ip string) *rate.Limiter {\n    rl.mu.Lock()\n    defer rl.mu.Unlock()\n    e, ok := rl.entries[ip]\n    if !ok {\n        if len(rl.entries) >= maxRateLimiterIPs {\n            return rate.NewLimiter(0, 0)      // permanent deny for this IP\n        }\n        e = &rateLimiterEntry{limiter: rate.NewLimiter(rl.rate, rl.burst)}\n        rl.entries[ip] = e\n    }\n    e.lastSeen = time.Now()\n    return e.limiter\n}\n```\n\nThe only eviction is `sweepLoop`, which drops entries idle for `rateLimiterTTL`\n(5 minutes). There is no LRU and no limit on how fast the map can be filled.\n\n### Why it is reachable\n\n`clientIP` trusts `X-Forwarded-For` whenever `RemoteAddr` is loopback\n(`api/handler.go:380-388`). That is the normal deployment shape: the embedded UI reverse\nproxy (`api/ui.go`, `httputil.NewSingleHostReverseProxy`) reaches the API over loopback,\nand an operator running the API with `-api-bind 0.0.0.0` exposes it directly. In either\ncase the bucket key is attacker-chosen.\n\n### Measured (local node built from this repo, `xe node -api-port 18080`)\n\n| Step | RSS | FD | Threads |\n|---|---|---|---|\n| baseline | 67,212 kB | 14 | 11 |\n| 300 heavy reads | 67,212 kB | 14 | 11 |\n| 500 raw TCP held open | 67,664 kB | 514 | 11 |\n| 40 SSE held (admin-gated → 403) | 67,928 kB | 54 | 11 |\n| 20,000 distinct XFF IPs | 150,152 kB | 14 | 11 |\n| +80,000 more distinct XFF IPs | 156,796 kB | 14 | 11 |\n\n≈ **4.2 kB RSS per spoofed IP**; reaching the 100,000-entry cliff costs roughly **420 MB**\nand takes about a minute at the measured ~1,900 req/s.\n\nOnce the cliff is reached (reproduced):\n\n- **brand-new IP → `429` on every request (5/5)**\n- **previously-seen IP → `200` on every request (5/5)**\n\nThe attacker, whose IPs are already in the map, keeps full access while every genuine new\nclient is denied — a selective denial of service, not merely a slowdown.\n\n### Honest limits on this report\n\n- **The public testnet API currently ignores XFF** (its `RemoteAddr` is non-loopback), so\n  the spoofing precondition does not hold for `ldn.core.test.network` as deployed today:\n  150 requests with one spoofed XFF all returned 200 because the real client IP was used.\n  This is a code defect whose live exploitability depends on the operator's bind/proxy\n  configuration, and I am not claiming otherwise.\n- No funds, consensus, finality or ledger state is affected.\n\n### Measured clean (no finding)\n\n- **Slowloris does not work.** 200 silent TCP connections were reaped exactly at the\n  10-second `ReadHeaderTimeout` (FD 214 → 14).\n- **Read endpoints do not grow memory.** 300 heavy reads left RSS unchanged.\n- **No cross-endpoint starvation.** 8 concurrent threads on `/blocks/recent` (p50 3.01s)\n  left `/health` at p50 0.57s.\n\n### Not a duplicate\n\n- **#81 / #117 / #168** are the opposite failure: `clientIP` collapses *all* clients into\n  **one shared bucket**. This is the map growing to a hard cap after which **new** IPs get\n  a **0/0** limiter. Different mechanism, different impact, different fix.\n- **#137** adds per-IP limiting + LRU eviction to `peerRateLimiter` in `net/ratelimit.go`\n  (libp2p path). This is `api/handler.go` (HTTP path), which still has FIFO-only sweeping\n  and the 0/0 cliff.\n- **#139** (open, unmerged) caps concurrent chat SSE per IP — unrelated to this map.\n- **#67** is SSH-gateway connection slots.\n\n### Suggested direction\n\nBound the map with an LRU (as #137 did for the P2P limiter) so filling it evicts the\nleast-recently-used bucket instead of denying every new IP; and/or only trust\n`X-Forwarded-For` when the immediate hop is a configured trusted proxy.\n\n### Reproduction\n\n```\nxe node -api-port 18080 -api-bind 127.0.0.1 -data ./data\n\n# fill the map (distinct XFF per request; trusted because RemoteAddr is loopback)\nfor n in $(seq 0 99999); do\n  curl -s -o /dev/null -H \"X-Forwarded-For: 10.$((n/65536)).$(((n/256)%256)).$((n%256))\" \\\n    http://127.0.0.1:18080/health\ndone\n\n# brand-new IP is denied; an IP already in the map still works\ncurl -s -o /dev/null -w '%{http_code}\\n' -H 'X-Forwarded-For: 198.51.100.7' http://127.0.0.1:18080/health   # 429\ncurl -s -o /dev/null -w '%{http_code}\\n' -H 'X-Forwarded-For: 10.0.0.0'        http://127.0.0.1:18080/health   # 200\n```\n\nMeasurement scripts and raw output: `~/bb-work/xe/nodetest/resource_measure.py`,\n`~/bb-work/xe/nodetest/fill_ratelimit.py`, `resource_results.json`, `fill_ratelimit.log`.\n\nCampaign pre-notice: #170. Results will also be posted there.\n\nXE address for reward: `425701cced5161806191f9421921766d9590523a2d595df32cb5367dc4477b41`\nDisclosure credit: Nexa Stark\n\n","patch_refs":[{"id":174,"repo":"xeprotocol/xe","kind":"pr","ref":"174","title":"fix(api): replace 0/0 hard cap with LRU eviction in ipRateLimiter (#172)","url":"https://github.com/xeprotocol/xe/pull/174","note":"","state":"open","author":"arturbarszcz78","added_by":"desk","created_at":"2026-10-29T13:57:30.091343+00:00"}],"thread":[{"id":1166,"source":"github","source_ref":"6726597412","author":"Joseph Denne","team":true,"body":"Thank you for the report and the measurements. We have confirmed the behaviour you describe and it is being handled; we will update this ticket as the fix progresses.","created_at":"2026-11-08T20:18:57+00:00"}]}