http/ip_canonical.ts

IP address canonicalization — collapse equivalent string forms into a single key per RFC 5952 (IPv6) plus the dotted form for IPv4-mapped IPv6 addresses.

Why this exists. Without canonicalization, the four representations ::1, ::01, ::0001, and 0:0:0:0:0:0:0:1 are the same IPv6 address but produce four distinct strings — so an attacker rotating equivalent forms behind a trusted-passthrough proxy could defeat per-IP rate limiting (each form gets a fresh bucket) and pollute audit_log.ip forensics. The collision can extend to IPv4-mapped IPv6 forms (::ffff:127.0.0.1 vs 0:0:0:0:0:ffff:7f00:1 vs the bare 127.0.0.1) — three keys for one address.

Canonicalization runs through canonicalize_ip which:

  1. Lowercases and char-set filters (IP_LITERAL_CHARS) — non-IP strings ('unknown', 'attacker:controlled', '::1\n') pass through unchanged so downstream strict validators can still reject them.
  2. Parses with parse_ipv4_bits / parse_ipv6_bits — the grammar of Rust's IpAddr::from_str, which the spine parses with.
  3. Re-emits the canonical RFC 5952 string (lowercase hex, longest-zero-run compressed, IPv4-mapped emitted in the dotted form mandated by RFC 5952 §5).
  4. Strips the ::ffff: prefix from dotted IPv4-mapped forms so the bucket collapses to plain IPv4 — the strip moves AFTER canonicalization because the dotted form is the only form the strip can recognize symmetrically.

Mirrors the Rust spine's fuz_http::normalize_ip, which uses the same parse-then-canonicalize-then-strip ordering for the same rate-limit-key-poisoning surface.

rate_limit_ip_key is the bucket key an IP-keyed rate limiter uses: an IPv6 address collapses to its /64, the smallest block a host is commonly assigned, so rotating through one's own subnet buys no fresh budget. Only the limiter call sites apply it — the client IP stays the full address everywhere else (audit rows, token last_used_ip, REMOTE_ADDR).

The module imports nothing: the action dispatcher keys its IP limiter through it, so it must not pull the optional hono peer.

view source

Declarations
#

6 declarations

canonicalize_ip
#

http/ip_canonical.ts view source

(ip: string): string import {canonicalize_ip} from '@fuzdev/fuz_app/http/ip_canonical.js';

Canonicalize an IP address string.

Returns the RFC 5952 canonical form for parseable IPv4 or IPv6 input. Returns the input unchanged (only lowercased) when the input is non-IP ('unknown'), malformed ('attacker:controlled', '::1\n'), or any string the strict char-set filter rejects.

Idempotent. canonicalize_ip(canonicalize_ip(x)) === canonicalize_ip(x) for every input.

Order-safe for IPv4-mapped IPv6. The ::ffff: prefix strip runs AFTER the canonical emit because the canonical form of an IPv4-mapped IPv6 address is the dotted form (::ffff:127.0.0.1, not ::ffff:7f00:1). Stripping before canonicalize would miss the full-hex form. Closes the normalize_ipv4_mapped_collapse_is_order_safe test from the Rust port.

ip

type string

returns

string

examples

canonicalize_ip('::0001') // → '::1' canonicalize_ip('0:0:0:0:0:0:0:1') // → '::1' canonicalize_ip('2001:0DB8::0001') // → '2001:db8::1' canonicalize_ip('::ffff:127.0.0.1') // → '127.0.0.1' canonicalize_ip('0:0:0:0:0:ffff:7f00:1') // → '127.0.0.1' canonicalize_ip('::ffff:1') // → '::ffff:1' (NOT IPv4-mapped — group[5] is 0, not ffff) canonicalize_ip('127.0.0.1') // → '127.0.0.1' canonicalize_ip('not-an-ip') // → 'not-an-ip' (passes through) canonicalize_ip('::1\n') // → '::1\n' (fails char-set; passes through) canonicalize_ip('203.0.113.1:8080') // → '203.0.113.1:8080' (passes through; validate_ip_strict rejects)

IP_LITERAL_CHARS
#

http/ip_canonical.ts view source

RegExp import {IP_LITERAL_CHARS} from '@fuzdev/fuz_app/http/ip_canonical.js';

Allowed character set for a bare IP literal.

Covers the union of IPv4 (digits + .), IPv6 (hex digits + :), and IPv4-mapped IPv6 forms (::ffff:127.0.0.1). Anything outside this set — brackets, whitespace, control bytes, letters g–z — disqualifies the input from parsing.

Same regex http/proxy.ts's validate_ip_strict uses; exported here so both modules can share one source of truth.

ipv6_bigint_to_canonical
#

http/ip_canonical.ts view source

(bits: bigint): string import {ipv6_bigint_to_canonical} from '@fuzdev/fuz_app/http/ip_canonical.js';

Convert a 128-bit IPv6 binary value into its RFC 5952 canonical string form.

  • IPv4-mapped (groups[0..5] = 0, groups[5] = 0xffff) emits the ::ffff:a.b.c.d dotted form per RFC 5952 §5.
  • Otherwise: lowercase hex with no leading zeros per group (§4.1), the longest run of consecutive zero groups (≥ 2 groups) is replaced with :: (§4.2.1, §4.2.3), and on equal-length runs the first one wins (§4.2.3). Single-zero groups stay as 0 (§4.2.2).

Pure helper exported for the test suite to exercise the canonicalization invariants directly without a full parse_ipv6_bits round-trip.

bits

the 128-bit IPv6 value as bigint. Must satisfy 0n <= bits < 2n ** 128n; throws RangeError otherwise. Silent truncation would mask caller bugs since the bit-extraction loop only consumes the low 128 bits.

type bigint

returns

string

throws

  • RangeError - when `bits` is negative or exceeds 128 bits

parse_ipv4_bits
#

http/ip_canonical.ts view source

(ip: string): number | null import {parse_ipv4_bits} from '@fuzdev/fuz_app/http/ip_canonical.js';

Parse a dotted-quad IPv4 address to its 32-bit value.

Four decimal octets, each 0–255 with no leading zeros (01.2.3.4 is refused, as Rust's Ipv4Addr::from_str refuses it).

ip

type string

returns

number | null

the address as an unsigned 32-bit number, or null when ip isn't one

parse_ipv6_bits
#

http/ip_canonical.ts view source

(ip: string): bigint | null import {parse_ipv6_bits} from '@fuzdev/fuz_app/http/ip_canonical.js';

Parse an IPv6 address to its 128-bit value.

The grammar of Rust's Ipv6Addr::from_str: eight groups of one to four hex digits (either case), or fewer around a single :: standing for one or more zero groups, the last two groups optionally written as a dotted-quad IPv4 address. No zone (%eth0) — callers strip it first.

ip

type string

returns

bigint | null

the address as a bigint in [0, 2^128), or null when ip isn't one

rate_limit_ip_key
#

http/ip_canonical.ts view source

(ip: string): string import {rate_limit_ip_key} from '@fuzdev/fuz_app/http/ip_canonical.js';

The bucket key an IP-keyed rate limiter uses for client IP ip.

An IPv6 address keys its /64: the low 64 bits masked off, rendered in RFC 5952 canonical form with a /64 suffix (2001:db8:1:2::/64; the all-zero prefix is ::/64). A zone suffix (%eth0) is dropped first. An IPv4 address — an IPv4-mapped IPv6 one included — keys its canonical form, with no suffix. Anything that doesn't parse ('unknown' among them) is returned unchanged. Twin of the Rust spine's fuz_http::rate_limit_ip_key.

For limiter keys only: audit rows and every other record of the client keep the full address.

ip

type string

returns

string

examples

rate_limit_ip_key('2001:db8:1:2::1') // → '2001:db8:1:2::/64' rate_limit_ip_key('2001:db8:1:2:8000::') // → '2001:db8:1:2::/64' rate_limit_ip_key('fe80::1%eth0') // → 'fe80::/64' rate_limit_ip_key('203.0.113.7') // → '203.0.113.7' rate_limit_ip_key('unknown') // → 'unknown'

Imported by
#