refactor(rate-limit): parse client IPs with node:net #341

Sammanfogat
supernaut sammanfogade 2 incheckningar från refactor/ip-parse-node-net in i main 2026-10-10 17:10:03 +00:00
Ägare

What

Replace the hand-rolled IPv4/IPv6 parser in src/lib/rate-limit.ts with node:net isIPv4/isIPv6 and URL-based normalisation. About 110 lines of parsing go. The file's comment essays shrink to one-line whys. Patch bump to 1.29.2.

ipBucket keeps its contract: IPv6 keys as its /56, IPv4 exactly, ::ffff:a.b.c.d as the IPv4 address, unparseable input as-is.

Behaviour

Compared old and new ipBucket on 200,000 random strings built from address fragments, plus a fixed list of edge cases. Output is identical for every input Node accepts as an IP (brackets, zone ids, compressed forms, mapped forms, dotted tails).

The two parsers differ only on malformed input the old code accepted by mistake. Those now pass through unchanged, like any other unparseable value:

  • IPv4 octets with leading zeros (010.0.0.1, which is ambiguous octal).
  • A dotted quad outside the last position of an IPv6 address (1.2.3.4::).

Tests

Existing ipBucket tests are unchanged. Added cases:

  • every spelling of a mapped address (::ffff:cb00:7109, uppercase long form, bracketed, with zone id, ::ffff:0:0);
  • addresses that only look mapped;
  • compressed forms at the start, middle and end, and the full eight-group form;
  • bracket and zone combinations;
  • more unparseable pass-through values.

pnpm check, pnpm lint, pnpm test and pnpm build pass.

Comments

The long header and budget comments (sizing rationale for the sign-up and resend budgets, the shared-egress decision) are cut to one-line whys, per the comment rule. The full text stays in git history of this file and issue #142.

## What Replace the hand-rolled IPv4/IPv6 parser in `src/lib/rate-limit.ts` with `node:net` `isIPv4`/`isIPv6` and URL-based normalisation. About 110 lines of parsing go. The file's comment essays shrink to one-line whys. Patch bump to 1.29.2. `ipBucket` keeps its contract: IPv6 keys as its /56, IPv4 exactly, `::ffff:a.b.c.d` as the IPv4 address, unparseable input as-is. ## Behaviour Compared old and new `ipBucket` on 200,000 random strings built from address fragments, plus a fixed list of edge cases. Output is identical for every input Node accepts as an IP (brackets, zone ids, compressed forms, mapped forms, dotted tails). The two parsers differ only on malformed input the old code accepted by mistake. Those now pass through unchanged, like any other unparseable value: - IPv4 octets with leading zeros (`010.0.0.1`, which is ambiguous octal). - A dotted quad outside the last position of an IPv6 address (`1.2.3.4::`). ## Tests Existing `ipBucket` tests are unchanged. Added cases: - every spelling of a mapped address (`::ffff:cb00:7109`, uppercase long form, bracketed, with zone id, `::ffff:0:0`); - addresses that only look mapped; - compressed forms at the start, middle and end, and the full eight-group form; - bracket and zone combinations; - more unparseable pass-through values. `pnpm check`, `pnpm lint`, `pnpm test` and `pnpm build` pass. ## Comments The long header and budget comments (sizing rationale for the sign-up and resend budgets, the shared-egress decision) are cut to one-line whys, per the comment rule. The full text stays in git history of this file and issue #142.
supernaut lade till 2 incheckningar 2026-10-10 16:33:04 +00:00
supernaut tvångsskickade refactor/ip-parse-node-net från db5af7d23a
Alla kontroller lyckades
ci / ci (pull_request) Successful in 2m17s
till 67d3410f93
Alla kontroller lyckades
ci / ci (pull_request) Successful in 2m1s
2026-10-10 16:50:05 +00:00
Jämför
supernaut tvångsskickade refactor/ip-parse-node-net från 67d3410f93
Alla kontroller lyckades
ci / ci (pull_request) Successful in 2m1s
till 6740c5c039
Alla kontroller lyckades
ci / ci (pull_request) Successful in 1m57s
2026-10-10 17:07:34 +00:00
Jämför
supernaut schemalade den här ändringsförfrågan för automatisk sammanfogning när alla kontroller lyckas 2026-10-10 17:08:18 +00:00
supernaut sammanfogade incheckning d1ddebb73f till main 2026-10-10 17:10:03 +00:00
supernaut tog bort grenen refactor/ip-parse-node-net 2026-10-10 17:10:03 +00:00
supernaut refererade denna ändringsförfrågan från en incheckning 2026-10-10 17:10:04 +00:00
Logga in för att delta i denna konversation.
Inga granskare
Ingen milstolpe
Inget projekt
Inga tilldelade
1 deltagare
Notiser
Förfallodatum
Förfallodatumet är ogiltigt eller utanför gränserna. Använd formatet "åååå-mm-dd".

Inget förfallodatum satt.

Beroenden

Inga beroenden satta

Referens
bitborg/bitborg-web!341
Ingen beskrivning angiven.