{"id":"XE-0099","kind":"bounty","visibility":"public","source":"github","source_repo":"xeprotocol/xe","source_ref":"168","source_url":"https://github.com/xeprotocol/xe/issues/168","reporter_id":32,"title":"Loopback XFF trust collapses all UI-proxy clients into one limiter bucket","area":null,"claimed_tier":"medium","assessed_tier":null,"verdict":"duplicate","duplicate_of":"XE-0082","tracked":false,"internal_ref":"","internal_url":"","status":"closed","closed_reason":"duplicate","github_state":"closed","disclosed_at":null,"award_xe":null,"award_approved":false,"paid_ref":"","created_at":"2026-10-29T13:57:26.719021+00:00","updated_at":"2026-11-10T00:11:30.078046+00:00","source_created_at":"2026-10-29T04:35:54+00:00","told_at":"2026-11-05T22:08:14+00:00","status_changed_at":"2026-11-05T22:08:11.176934+00:00","url":"https://bounty.xe.network/XE-0099","body":"# Loopback XFF trust collapses all UI-proxy clients into one limiter bucket\n\n## Summary\n\nThe UI proxies `/api/*` to the API on loopback, and the API's `clientIP`\ntrusts `X-Forwarded-For` on loopback connections, taking the last XFF\nelement. Go's reverse proxy appends its bare loopback IP to the header, so\nevery UI API request lands in the same rate-limiter bucket: one heavy UI\nuser exhausting the shared read burst (1000, then 200/s) or write burst\n(50, then 10/s) gets 429s for all UI users.\n\n## Impact\n\nAPI availability for UI users. One UI client can deny the API to every\nother UI client through the shared bucket; no funds are directly at\nstake, and direct non-loopback connections correctly ignore XFF, so this\nis scoped to UI-proxied traffic. The 429 consequence was proven at the\nlimiter level (see Proof of concept), not just inferred from bucket-key\nequality.\n\nSuggested program tier: **Medium**.\n\n## Root cause\n\n- `cmd/xe/main.go:1509-1514` + `api/ui.go:34-44` (Director plus the\n  `/api/` mount): the UI reverse-proxies API requests to\n  `http://127.0.0.1:<apiPort>`. The default `--api-bind` is already\n  `127.0.0.1`, so the loopback target holds by default, not only via the\n  wildcard normalization. The UI path requires `--ui` (default off).\n- `api/handler.go:375-393` `clientIP` (with `isLoopbackHost` at `:395-400`):\n  on a loopback `RemoteAddr` it returns the last comma-separated XFF\n  element verbatim — no port stripping, no normalization — falling back\n  to `X-Real-IP` and then the host. Direct non-loopback connections\n  return the host early and ignore XFF entirely.\n- `api/handler.go:364-372` `rateLimitMiddleware` keys the limiter by\n  `clientIP(r)` and returns 429 `rate limit exceeded` on exhaustion.\n  Constants (`handler.go:296-297`): read 200/s with burst 1000, write\n  10/s with burst 50.\n- Why the bucket is shared: Go's proxy appends its bare loopback IP, so\n  the backend sees `X-Forwarded-For: 9.9.9.9, 127.0.0.1` and the last\n  element — the key — is `127.0.0.1` for every UI client. An earlier\n  draft of this report claimed the proxy appends IP:port (a fresh bucket\n  per connection); the harness disproved that and proved the opposite,\n  and this report states the correction plainly.\n- Generalization, no scope change: the collapse holds for any fixed proxy\n  IP, not just loopback — with a non-loopback proxy address, clients\n  still collapse to one key (the proxy's IP), because non-loopback\n  `RemoteAddr` is returned verbatim.\n\n## Proof of concept\n\nTarget: `xeprotocol/xe` @ `978785929bc17a2f4ab30c383e5fc9cf4d634512`\n(2026-10-15). Two harnesses, both against the real `api` package; the\nrepository itself is not modified.\n\nAttached PoCs: `pocs/slice8-harness.t.txt` (original harness, 6 tests)\nand `pocs/reval-f06-test.go.txt` (re-validation harness, including the\nend-to-end 429 test), both byte-identical copies of the runnable\nsources.\n\nRun (Go 1.25.14):\n\n```\nexport PATH=\"$HOME/.local/go/bin:$PATH\"\nGOPROXY=off go test ./api/ -run 'TestSlice8' -count=5 -vet=off -v\n```\n\nObserved output (verbatim, re-validation run 2026-10-29): all 6 tests\nPASS on all 5 runs, zero failures. Every iteration of the proxy test\nlogged:\n\n```\nrequest 0: backend XFF=\"9.9.9.9, 127.0.0.1\" -> clientIP bucket key=\"127.0.0.1\"\nrequest 1: backend XFF=\"9.9.9.9, 127.0.0.1\" -> clientIP bucket key=\"127.0.0.1\"\n```\n\n### End-to-end 429 proof (re-validation 2026-10-29)\n\nA second test wired the real `NewUIHandler` through the real\n`rateLimitMiddleware` with the real limiter configuration (not a lowered\none) over real HTTP through two test servers. Client A (XFF `1.1.1.1`)\nthrough the UI proxy was allowed 1387 requests (burst 1000 plus 200/s\nrefill during the ~1.9 s drain) before the shared bucket 429'd; client B\n(XFF `2.2.2.2`) through the same UI proxy then got 429. A control on\nthe same middleware instance — a fresh non-loopback key — got 200,\nproving the 429 is bucket-keyed, not a global lockout.\n\nNo escape was found: `clientIP` has only two callers in the repository\n(`rateLimitMiddleware` and attestation); IPv6 loopback `[::1]` and the\n`localhost` hostname are both treated as loopback; a trailing-comma XFF\nwith an empty last element falls back to the host bucket `127.0.0.1` —\nstill the shared bucket; and X-Real-IP cannot override XFF when both are\npresent.\n\n## Reproduction steps\n\n1. Two fresh connections through the UI proxy to the API.\n2. Observe both bucketed under `127.0.0.1`.\n3. Exhaust the shared burst from one → all UI users get 429.\n\n## Recommended fix\n\nKey UI-proxied requests by an authenticated/session identifier, or stop\ntrusting XFF on loopback, or exempt the UI proxy path from the shared\nbucket.\n\n## Severity\n\n- CVSS 3.1: 5.3 — `CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:N/I:N/A:L`\n- CWE-400 (Uncontrolled Resource Consumption)\n\n## Caveats\n\n- The 429 proof is a loopback end-to-end run through the real middleware\n  stack, not a production deployment observation.\n- The conditional variant — fresh-bucket minting if a front proxy\n  forwards client headers without appending its own bare IP — is NOT part\n  of this claim: the front-proxy layout of the public deployment was not\n  tested and no deployment configuration for it exists in the repository.\n- No XE-specific prior report of the limiter/XFF mechanism was found\n  (dedup query Q6 recorded in the audit's DEDUP-BASELINE.md; only generic\n  XFF articles recurred).\n- This reproduction runs against the repository code at the stated HEAD. No\n  testnet transactions were sent as part of this report.\n\nPayout address (XE): eff2c51dd8457df740b919226cd9b37a2944e5abea7501094db80234f87f010e\n\nBest,\nChaplin\n\n[slice8-harness.t.txt](https://github.com/user-attachments/files/34112467/slice8-harness.t.txt)\n[reval-f06-test.go.txt](https://github.com/user-attachments/files/34112468/reval-f06-test.go.txt)","patch_refs":[{"id":182,"repo":"xeprotocol/xe","kind":"pr","ref":"187","title":"fix(api): walk XFF backwards skipping loopback to preserve client IP (fixes #168)","url":"https://github.com/xeprotocol/xe/pull/187","note":"","state":"open","author":"arturbarszcz78","added_by":"desk","created_at":"2026-10-29T13:57:30.091343+00:00"}],"thread":[{"id":401,"source":"github","source_ref":"6670810548","author":"Joseph Denne","team":true,"body":"Thanks. This is the same finding as https://bounty.xe.network/XE-0082, 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, reply with what differs and we will look again.","created_at":"2026-11-05T22:08:14+00:00"}]}