Every plugin limiter counts IPv6 per /64 (setIpv6Grouping)

clientIp() returns the whole IPv6 address and every limiter keyed on it, so
one host with a /64 had 2^64 budgets: 50 addresses of one /64 against a
limit of 5 all passed (IAWL idea 9, owner's Q4). consumeRateLimit and
checkRateLimit now count an IPv6 key by its /64 (an IPv4-mapped address by
its IPv4); the mail service's per-visitor account-mail key too. A second
counter per /48 at 10x the limit is available and off by default
(setIpv6Grouping({ wide: 48 })); setIpv6Grouping({ prefix: 128 }) counts
per address as before. ipRateKey(ip) is exported for own limiters.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
Radosław Smoliński
2026-12-01 10:42:02 +01:00
co-authored by Claude Opus 5.5
parent 15b5c6258d
commit 97c62f21f0
8 changed files with 238 additions and 17 deletions
+12
View File
@@ -436,6 +436,18 @@ A missing IP (the proxy strips the header, a wrong `IAPL_IP_HEADER`) does **not*
share one `unknown-ip` bucket with a ×20 limit (`UNKNOWN_IP_FACTOR`) — real
visitors get through, a flood without the header does not. `setUnknownIpFactor(n)` (root, once at startup)
changes the multiplier; `setUnknownIpFactor(0)` = the old behaviour (no limit), when a limiter sits in front.
**IPv6 is counted per network (1.17.2).** A host with IPv6 holds a whole /64 — 2^64 addresses, every home
connection and every server — so counting per address gave one visitor as many budgets as it had addresses. Every
limiter of the plugin (forms, the file upload endpoint, search, MCP, the consent log, stats, the doctor status, the
mail service's confirm route and account-mail limit) counts a **/64 as one client**; an IPv4-mapped address
(`::ffff:1.2.3.4`) counts as its IPv4. One home network shares the budget, as an IPv4 home behind one router always
did. Change it once at start (root export): `setIpv6Grouping({ prefix: 128 })` = per address (as before);
`setIpv6Grouping({ prefix: 56 })` another network size; `setIpv6Grouping({ wide: 48, wideFactor: 10 })` adds a
second counter per /48 at 10× the limit (a hosting customer often holds a /48 — off by default, because a mobile
carrier can put many phones in one /48). A value out of range throws. `ipRateKey(ip)` gives the key a limiter of
your own should count by (`consumeRateLimit` already applies it to a key that is an IP address).
**Check:** fifty requests to a limited route with `X-Real-IP: 2001:db8:1:1::1` … `::32` → `429` after the limit.
Without a pinned header the order
`cf-connecting-ip` → `x-real-ip` → XFF stays (switching to XFF-only, from the right, would put every visitor
behind Cloudflare into the bucket of the edge node) — that is why `IAPL_IP_HEADER` is mandatory in production