-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA256 NEFARIOUSPLAN-CANONICAL-V1 {"body_md":"## The key was a string. The string was what the proxy wrote down.\n\n`packages/better-auth/src/utils/get-request-ip.ts`, at the parent commit of the CVE-2026-45364 fix:\n\n```ts\nexport function getIp(\n req: Request | Headers,\n options: BetterAuthOptions,\n): string | null {\n if (options.advanced?.ipAddress?.disableIpTracking) {\n return null;\n }\n const headers = \"headers\" in req ? req.headers : req;\n const defaultHeaders = [\"x-forwarded-for\"];\n const ipHeaders =\n options.advanced?.ipAddress?.ipAddressHeaders || defaultHeaders;\n\n for (const key of ipHeaders) {\n const value = \"get\" in headers ? headers.get(key) : headers[key];\n if (typeof value === \"string\") {\n const ip = value.split(\",\")[0]!.trim();\n if (isValidIP(ip)) {\n return ip;\n }\n }\n }\n // ...\n return null;\n}\n\nfunction isValidIP(ip: string): boolean {\n const ipv4 = z.ipv4().safeParse(ip);\n if (ipv4.success) return true;\n const ipv6 = z.ipv6().safeParse(ip);\n if (ipv6.success) return true;\n return false;\n}\n```\n\nTake the first comma-separated entry of the configured IP header. Trim. Validate as either IPv4 or IPv6 via zod. Return the string. That is the function's entire contract: a string the zod parser admitted as a valid IP. It is also the string that becomes, verbatim, half of the rate-limit key.\n\n`packages/better-auth/src/api/rate-limiter/index.ts`, same parent commit:\n\n```ts\nconst ip = getIp(req, ctx.options);\nif (!ip) {\n return;\n}\nconst key = ip + path;\n```\n\nThe other half is the request path. There is no separator between them. The concatenation is the lookup key into the `rateLimit` table, or the secondary-storage key-value store, or the in-memory `Map`. The store holds one row per key. Each row carries a count and a timestamp.\n\nThe data structure does what it is told. It does not know that `2001:db8::1` and `2001:DB8::1` are the same address. It does not know that a residential IPv6 customer is allocated a /64. It has a key, the key is a string, and the string is what arrived.\n\n## The counter was per /128. The caller is a /64.\n\nRFC 6177, \"IPv6 Address Assignment to End Sites,\" published March 2011, names the unit of assignment:\n\n> The minimum allocation by an RIR should be a /48 to end sites, but smaller assignments to specific subscriber types might be appropriate, e.g., a single /64 when only one subnet is needed.\n\nIn practice every major residential ISP, every commercial fiber provider, every consumer VPN, every cloud provider issuing IPv6, and every mobile carrier assigns end sites a /64 at minimum. A /64 contains 2^64 addresses. The end site, the one apartment, the one rented VM, the one phone, picks any of those addresses as the source for an outbound connection. RFC 4941 privacy extensions actively rotate. RFC 8981 temporary addresses rotate by default on every major operating system. The /128 the proxy writes into `x-forwarded-for` is one of trillions the caller can pick from before the kernel asks for a new one.\n\nThe fix's own test for the bypass:\n\n```ts\nit(\"should support IPv6 subnet rate limiting\", () => {\n // Simulate attacker rotating through IPv6 addresses in their /64 allocation\n const attackIPs = [\n \"2001:db8:abcd:1234:0000:0000:0000:0001\",\n \"2001:db8:abcd:1234:1111:2222:3333:4444\",\n \"2001:db8:abcd:1234:ffff:ffff:ffff:ffff\",\n ];\n\n const normalized = attackIPs.map((ip) =>\n normalizeIP(ip, { ipv6Subnet: 64 }),\n );\n\n const uniqueValues = new Set(normalized);\n expect(uniqueValues.size).toBe(1);\n expect(normalized[0]).toBe(\"2001:0db8:abcd:1234:0000:0000:0000:0000\");\n});\n```\n\nThree of `attackIPs`'s 2^64 possible siblings. Post-fix, with `ipv6Subnet: 64`, they collapse to one key. Pre-fix, they were three keys with three counters. Each /128 from the same /64 was a separate caller. The pre-fix rate limiter on `/sign-in/email` allowed three attempts per address per ten seconds, and the attacker rotating through their /64 had three attempts times 18 quintillion.\n\nThe paths the limiter protects, from `getDefaultSpecialRules` in the same file:\n\n```ts\nfunction getDefaultSpecialRules() {\n return [\n {\n pathMatcher(path: string) {\n return (\n path.startsWith(\"/sign-in\") ||\n path.startsWith(\"/sign-up\") ||\n path.startsWith(\"/change-password\") ||\n path.startsWith(\"/change-email\")\n );\n },\n window: 10,\n max: 3,\n },\n {\n pathMatcher(path: string) {\n return (\n path === \"/request-password-reset\" ||\n path === \"/send-verification-email\" ||\n path.startsWith(\"/forget-password\") ||\n path === \"/email-otp/send-verification-otp\" ||\n path === \"/email-otp/request-password-reset\"\n );\n },\n window: 60,\n max: 3,\n },\n ];\n}\n```\n\nThese are the credential-stuffing surfaces. Three sign-in attempts per ten seconds. Three password-reset triggers per minute. Three change-password requests per ten seconds. Each is enforced per /128. For an IPv6 caller, \"per /128\" is unenforced.\n\n## The same /128 has at least six textual spellings.\n\nSix representations of one address, copied verbatim from the fix's bypass-prevention test:\n\n```ts\nconst representations = [\n \"2001:db8::1\",\n \"2001:DB8::1\",\n \"2001:0db8::1\",\n \"2001:db8:0::1\",\n \"2001:0db8:0:0:0:0:0:1\",\n \"2001:db8::0:1\",\n];\n// post-fix, all six normalizeIP() to:\n// \"2001:0db8:0000:0000:0000:0000:0000:0001\"\n```\n\nDifferences: letter case in the hex groups, zero-padding inside a group, choice of where to place the `::` zero-compression, and short versus full form. Every representation is a valid IPv6 string per RFC 4291. `z.ipv6()` accepts all six and returns the input unchanged. Pre-fix, the same /128 was six distinct rate-limit keys.\n\nA more pointed trio, also from the test suite:\n\n```ts\n\"::ffff:c000:0201\" // hex-encoded IPv4 inside IPv6 mapped form\n\"::ffff:192.0.2.1\" // dotted-quad IPv4 inside IPv6 mapped form\n\"192.0.2.1\" // plain IPv4\n```\n\nAll three are `192.0.2.1`. The CVE-2026-45364 description names them all by name: \"uppercase, compression, IPv4-mapped, hex-encoded IPv4-in-IPv6.\"\n\nThe textual-variation bypass requires the upstream proxy to pass through the client's chosen form. The common case is a Better Auth deployment behind nginx with `X-Forwarded-For` appended rather than overwritten, which is nginx's default. The client sends an `X-Forwarded-For: ` header; nginx appends its TCP source after it; Better Auth's `getIp` reads the first entry, which is the client's. The same caller varies the spelling, the limiter opens a new bucket. This stacks on top of the /64 rotation, which works regardless of the proxy's XFF policy because the source address itself is what changes.\n\n## The fix is two patches. The first release named the wrong one.\n\nPR #7470, opened January 19, 2026 by Alex Yang (himself65), titled `fix(rate-limit): support IPv6 address normalization and subnet`. The diff adds `normalizeIP()` and `createRateLimitKey()` to `@better-auth/core/utils/ip`, threads them through `getIp` and the rate-limit key construction. The new option type:\n\n```ts\n/**\n * IPv6 subnet prefix length for rate limiting.\n *\n * IPv6 addresses can be grouped by subnet to prevent attackers from\n * bypassing rate limits by rotating through multiple addresses in\n * their allocation.\n *\n * @default 128 (individual address)\n */\nipv6Subnet?: 128 | 64 | 48 | 32 | undefined;\n```\n\nThe default is `128`. PR #7470 merged and shipped as v1.4.16 on January 20. The v1.4.16 release notes list one rate-limit item: \"Support IPv6 address normalization and subnet.\"\n\nWith `ipv6Subnet` defaulting to `128`, what v1.4.16 closed: the textual-variation bypass. `2001:db8::1` and `2001:DB8::1` now produce the same canonical key. The IPv4-mapped variations now collapse to the IPv4 form.\n\nWhat v1.4.16 did not close: the /64 rotation. With the default at /128, three sign-in attempts per address per ten seconds still ran per /128, and the attacker still held 2^64 addresses per /64. The doc comment on the new option named what `ipv6Subnet: 64` would do; the default did not do it.\n\nPR #7509, opened January 21 by the same author, titled `fix: set default ipv6 subnet to 64`. The functional diff is one constant in one type declaration:\n\n```diff\n- * @default 128 (individual address)\n+ * @default 64 (/64 subnet)\n```\n\nThe runtime change is `?? 64` instead of `?? 128` in `normalizeIP`. The rest of the patch is updating tests that had asserted the old default. Merged and shipped as v1.4.17 the same day. The v1.4.17 release notes list one rate-limit item: \"Set default ipv6 subnet to 64.\"\n\nThe GHSA, published May 11, 2026, names 1.4.17 as the fix for CVE-2026-45364. It does not name 1.4.16. The CVE description leads with the /64 bypass: \"IPv6 clients controlling a typical /64 allocation could rotate through 2^64 distinct source addresses without exhausting the per-address counter, defeating rate limiting on /sign-in/email, /sign-up/email, /forget-password, and every other path the limiter protects.\" That bypass survives v1.4.16. v1.4.17 is one line, one day later, and the patch only closes the CVE when both releases are taken together.\n\nThe first release named \"fix\" for an IPv6 rate-limit bypass and the release that actually fixed the IPv6 rate-limit bypass shipped twenty-one hours apart and were written by the same person. The release notes describe them as two separate items.\n\n## Convention Is The Allowlist applied to a rate-limit key.\n\nCVE-2026-45364 is the [Convention Is The Allowlist](/patterns/convention-is-the-allowlist) shape applied to a rate-limit identity scheme. The catalog's prior exhibits live in HTTP client parsers (Streamlink's [M3U8 file-scheme acceptance](/posts/streamlink-cve-2026-44353-fileadapter-mounted-parser-not-told)), TypeScript-typed query DSLs (Kysely's [`K extends string`](/posts/kysely-cve-2026-44635-k-extends-string-was-the-allowlist)), and SSH protocol-state dispatch (Erlang/OTP's [authenticated-but-never-asked dispatcher](/posts/erlang-otp-cve-2025-32433-authenticated-was-tracked-never-gated)).\n\nThe convention here is the implicit one that \"one IP equals one caller.\" For IPv4, where end sites are assigned individual addresses or small CIDR blocks billed per address, the convention has historically held well enough to be load-bearing in every per-IP rate limiter ever written. For IPv6, where end sites are assigned /64 or /48 blocks and a single device legitimately uses dozens of source addresses (RFC 4941 privacy extensions, RFC 8981 temporary addresses, server-side outbound rotation), the convention does not hold. The \"one IP equals one caller\" identity grain is documentation, in the heads of authors who learned rate limiting in the IPv4 era.\n\nThe implementation parsed inbound IPv6 at the broad syntactic type, \"any string `z.ipv6()` admits,\" and used the return value as the caller identity. The pattern's \"set of accepted values remains whatever happens to be supported by the surrounding subsystems today\" rendered, for this rate limiter, as \"every textual form zod accepts, one address per /128.\" The patch closes the demonstrated chain by narrowing twice: `normalizeIP` collapses textual forms to a canonical lowercase fully-expanded representation, and the `ipv6Subnet: 64` default collapses /128 cardinality to /64.\n\nThe narrowing is applied at the rate-limit call site. The same `getIp` function continues to feed telemetry, audit logging, session-attribution events, and any downstream consumer that imports `@better-auth/core/utils`. Those consumers now also see the `/64`-collapsed canonical form, regardless of whether they wanted caller-identity grain or source-address grain. The rate limiter's identity contract is now the contract every consumer of `getIp` runs with, configured by whatever `ipv6Subnet` the integrator passes.\n\nPoC: [github.com/advisories/GHSA-p6v2-xcpg-h6xw](https://github.com/advisories/GHSA-p6v2-xcpg-h6xw).","closing_line":"The counter ran. The data structure did exactly what its API said it would do. The string was a string.","hook_md":"The Better Auth rate limiter exists to count one thing. How many times a caller has hit `/sign-in/email` in the last ten seconds. The default policy is three. The fourth attempt returns 429. The data structure backing the counter is a key-value store. Pre-1.4.17, the key was a string. The string was the textual IPv6 address the upstream proxy wrote into the first comma-separated entry of `x-forwarded-for`, validated by `z.ipv6()`, returned unchanged.\n\nA typical IPv6 client holds a /64 prefix. That is 18,446,744,073,709,551,616 distinct source addresses. The counter saw each one as a new caller. CVE-2026-45364 is the gap between \"what `z.ipv6()` admits\" and \"who is one caller.\"","post_id":554,"slug":"better-auth-cve-2026-45364-rate-limiter-counted-strings-not-callers","title":"CVE-2026-45364: Better Auth's Rate Limiter Counted IPv6 Strings. The Caller Owned 2^64 of Them.","type":"initial","unreadable_sentence":"The release that named the fix shipped on January 20. The release that closed the bypass the CVE leads with shipped on January 21. The GHSA's fix line names 1.4.17 because 1.4.16 was not the fix."} -----BEGIN PGP SIGNATURE----- iHUEARYIAB0WIQRf0htP5+SjynlxywneZjl4jgkQJgUCaqcCggAKCRDeZjl4jgkQ Jul8APsG/uAYBwi9YoQ/4NYZCVMRAU7O0yPfcsnZ1sqcMulvywD/eUBsDlgoWLWg 7lS4cK3tNAlzNufFHUBuOjjNCbtYXQQ= =bTX5 -----END PGP SIGNATURE-----