<!-- XE-13271 · [Medium] API rate-limiter IP map cap fails closed to deny-all: filling 100k entries blocks every new client · testnet -->
*Markdown view of <https://bounty.xe.network/XE-13271>. The page is the record; this is the same page for reading.*

XE-13271 · Bounty

# \[Medium\] API rate-limiter IP map cap fails closed to deny-all: filling 100k entries blocks every new client

Closed · duplicate
—claimed Medium
[xe#396](https://github.com/xeprotocol/xe/issues/396)
filed 2026-11-11 15:41 UTC
by DICKY KUSUMA WARDHANI

Verdict
:   Duplicate

Tier
:   —

Claimed
:   Medium

Area
:   Node / API

Duplicate of
:   [XE-0112](https://bounty.xe.network/XE-0112) triaging

Tracked
:   internally

Updated
:   2026-11-20 00:07 UTC

Told
:   2026-11-20 00:05 UTC

### Patches

- [xe#410](https://github.com/xeprotocol/xe/pull/410) open by arturbarszcz78
  fix(api): evict LRU entries on rate limiter capacity and group IPv6 prefixes (#396)
- [xe#413](https://github.com/xeprotocol/xe/pull/413) closed by kleitonbernardino-lab
  fix(api): implement LRU eviction for rate limiter IP map (Fixes #396)

### History

- [543828](https://bounty.xe.network/log/543828) 2026-11-20 00:06 UTC github.state\_pushed
- [543827](https://bounty.xe.network/log/543827) 2026-11-20 00:06 UTC github.labels
- [543773](https://bounty.xe.network/log/543773) 2026-11-20 00:05 UTC reply.mirrored
- [543648](https://bounty.xe.network/log/543648) 2026-11-20 00:04 UTC proposal.adopted
- [543647](https://bounty.xe.network/log/543647) 2026-11-20 00:04 UTC reply.sent
- [543646](https://bounty.xe.network/log/543646) 2026-11-20 00:04 UTC ticket.status new → closed
- [542335](https://bounty.xe.network/log/542335) 2026-11-20 00:04 UTC proposal.adopted
- [542334](https://bounty.xe.network/log/542334) 2026-11-20 00:04 UTC ticket.facts area
- [542333](https://bounty.xe.network/log/542333) 2026-11-20 00:04 UTC proposal.adopted
- [542332](https://bounty.xe.network/log/542332) 2026-11-20 00:04 UTC ticket.facts
- [542331](https://bounty.xe.network/log/542331) 2026-11-20 00:04 UTC proposal.adopted
- [542330](https://bounty.xe.network/log/542330) 2026-11-20 00:04 UTC ticket.facts

[Full history](https://bounty.xe.network/log?ref=XE-13271) · [JSON](https://bounty.xe.network/v1/tickets/XE-13271) · [markdown](https://bounty.xe.network/XE-13271.md)

### Report

Repo: https://github.com/xeprotocol/xe @ 89a542e
Suggested severity: Medium
Payout XE address: da2ad2483b592cb5a35224cec5529bf0c48f4770ed6fe40a455c312b5c1dcbcf

# API rate-limiter IP map cap fails closed to deny-all: filling 100k entries blocks every new client (DoS)

- **Lokasi:** `api/handler.go`
  - `maxRateLimiterIPs = 100000` (baris 292), `rateLimiterTTL = 5 * time.Minute` (baris 291\)
  - `(*ipRateLimiter).getLimiter` (baris 334–349)
  - `rateLimitMiddleware` (baris 365–377) — dipakai `Mux()` untuk semua rute API
- **HEAD:** `89a542e`
- **Tier yang disarankan:** Medium (availability — DoS jarak jauh terhadap API publik, tanpa autentikasi)
- **Cek duplikat:** `grep -i "rateLimiter|maxRateLimiterIPs|rate limit" tickets.md` — tidak ada tiket tentang HTTP API rate limiter ini.
  - XE-0025 \= rate limit *P2P message* per libp2p peer ID (net/, bukan HTTP API).
  - XE-4329 \= starvation *global messenger* rate limiter P2P.
  - XE-0030 \= race check-then-act pada rate limit *attestation*.
  - Ketiganya mekanisme dan komponen berbeda; tidak ada yang membahas cap 100k \+ zero-rate limiter pada `api/handler.go`.

## Root cause

```
// api/handler.go:334-349
func (rl *ipRateLimiter) getLimiter(ip string) *rate.Limiter {
	rl.mu.Lock()
	defer rl.mu.Unlock()
	e, ok := rl.entries[ip]
	if !ok {
		if len(rl.entries) >= maxRateLimiterIPs {
			return rate.NewLimiter(0, 0) // <-- gagal tertutup: tidak pernah Allow()
		}
		e = &rateLimiterEntry{limiter: rate.NewLimiter(rl.rate, rl.burst)}
		rl.entries[ip] = e
	}
	e.lastSeen = time.Now()
	return e.limiter
}
```

Ketika map penuh (100.000 IP), setiap IP baru mendapat limiter `rate.NewLimiter(0, 0)` — rate 0, burst 0 — yang `Allow()`-nya selalu `false`. Entri itu **tidak disimpan** di map, jadi IP tersebut tidak pernah pulih selagi map penuh. `sweepLoop` hanya menghapus entri yang idle \> 5 menit, sehingga penyerang cukup mempertahankan \~100.000/300 detik ≈ **334 req/detik** dari alamat-alamat baru untuk membuat map tetap penuh tanpa batas waktu. Klien yang sudah punya entri juga ikut terblokir begitu entrinya kedaluwarsa setelah 5 menit idle.

## Dampak

- Siapa pun tanpa autentikasi dapat membuat API node tidak bisa dipakai oleh **semua klien baru** (dan akhirnya semua klien) — `429 rate limit exceeded` untuk setiap request, termasuk `GET /health`, submit blok, dan tunnel.
- 100.000 alamat IP sumber adalah angka kecil bagi penyerang dengan IPv6 (satu /64 menyediakan alamat praktis tak terbatas; tiap request cukup 1x `GET /health` untuk membuat entri). Node API publik (mis. core node testnet yang di-bind `0.0.0.0`) dapat diserang langsung dari internet; tiap limiter (read/write/probe) punya map sendiri dengan cap yang sama.
- Ini bukan sekadar "map tak terbatas" (XE-3024, XE-3519 membahas map lain): di sini **cap-nya sendiri yang menjadi senjata** — fail-closed ke deny-all, bukan fail-open.

## PoC (terverifikasi, `go test`, lalu file test dihapus)

Test sementara di `api/ratelimit_poc_test.go` (package `api`, akses internal):

```
func TestPoCRateLimiterMapExhaustion(t *testing.T) {
	rl := newIPRateLimiter(readRate, readBurst)
	for i := 0; i < maxRateLimiterIPs; i++ {
		rl.getLimiter(fmt.Sprintf("2001:db8::%d", i))
	}
	victim := rl.getLimiter("203.0.113.99")
	if victim.Allow() {
		t.Fatalf("expected fresh IP to be rate-limited after map exhaustion")
	}
	established := rl.getLimiter("2001:db8::0")
	if !established.Allow() {
		t.Fatalf("expected established IP to still be allowed")
	}
}
```

Hasil (`PATH=/tmp/go/bin:$PATH go test ./api/ -run TestPoCRateLimiterMapExhaustion -v -count=1`):

```
=== RUN   TestPoCRateLimiterMapExhaustion
    ratelimit_poc_test.go:24: map full at 100000 entries: new IP Allow()==false, established IP Allow()==true
--- PASS: TestPoCRateLimiterMapExhaustion (0.53s)
```

Reproduksi manual (tanpa kode): dari host IPv6, kirim 100.001 request ke `GET /health` dengan alamat sumber berbeda (atau `X-Forwarded-For` berbeda bila API diakses via loopback/UI proxy, karena `clientIP` memakai entri terakhir XFF saat remote loopback), lalu request dari IP baru yang sah akan selalu dijawab `429 rate limit exceeded`.

## Perbaikan yang disarankan

- Saat map penuh, **evict entri terlama/idle** (LRU) atau kembalikan limiter bersama (shared limiter) alih-alih zero-rate limiter; jangan pernah mengembalikan limiter yang tidak akan pernah mengizinkan request.
- Atau kunci limiter berdasarkan /64 (IPv6) / /24 (IPv4) agar satu penyerang tidak dapat mengisi map dengan alamat-alamat dalam prefix yang sama.

### Thread

**kleitonbernardino-lab**2026-11-11 21:39 UTCgithub

Opened pull request #413 resolving this vulnerability via $O(1)$ LRU eviction:

1. **LRU Doubly Linked List Integration (`api/handler.go`)**:

   - Added `container/list` tracking (`rl.lru`) to `ipRateLimiter`.
   - Embedded list element reference in `rateLimiterEntry`.
2. **$O(1)$ Eviction Under Map Capacity**:

   - When `len(rl.entries) >= maxRateLimiterIPs`, evicts the least-recently-used entry from `rl.lru.Back()` and deletes it from `rl.entries`.
   - Admits new incoming client IPs at nominal rate limits rather than returning a zero-rate limiter.
   - Promotes active client IPs to the head of the LRU queue on every access via `rl.lru.MoveToFront`.
3. **Optimized Early-Terminating Sweeper**:

   - `sweepLoop` iterates from the LRU tail backwards and terminates early as soon as the first unexpired entry is encountered, reducing sweep overhead from $O(N)$ to $O(K)$.
4. **Unit Tests (`api/ratelimit_test.go`)**:

   - Added `TestRateLimiterLRUEviction` and `TestRateLimiterSweep` proving bounded map capacity, LRU promotion, and admission of fresh IPs under capacity exhaustion.

**Joseph Denne**team2026-11-20 00:05 UTCgithub

Thanks. This is the same finding as https://bounty.xe.network/XE-0112, 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-13271 and says what differs; it is read like any other.
