Skip to content
Myr

fix(utils/ipaddr): render the unspecified address binary as "::" (#4998)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

`convertIPv6BinaryToString(0n)` returned a single ":" instead of the canonical "::" for the IPv6 unspecified address. The all-zero address compresses every group, so the generic zero-run logic collapsed the sections array down to a lone ":" placeholder, which the trailing `/:{2,}/` replacement could not fix because it only collapses runs of two or more colons. This produced an invalid IPv6 string (`net.isIP(':')` is `0`) and broke the round-trip with `convertIPv6ToBinary`, which throws on ":". It also meant the unspecified address was canonicalized to a bogus key in the ip-restriction middleware's string rule map, so a rule such as `denyList: ['::']` could be registered under ":" rather than "::". Handle the all-zero value explicitly, mirroring the existing IPv4-mapped early return.

authored by Sarath Francis · committed by GitHub · parent 5ef800e · Browse files · 2 files+13 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+13 added−0 removed

  • tests:+7 added−0 removed
  • other:+6 added−0 removed

2 files+13 added−0 removed

modified:src/utils/ipaddr.test.ts+7 added
modified:src/utils/ipaddr.ts+6 added
323 unmodified lines
324 * @param ipV6 binary IPv6 Address325 * @return normalized IPv6 Address in string326 */327export const convertIPv6BinaryToString = (ipV6: bigint): string => {328 if (ipV6 === 0n) {329 // The unspecified address compresses every group, which the generic330 // logic below would render as a single ":". Handle it explicitly.331 return '::'332 }333334 if (isIPv4MappedIPv6(ipV6)) {335 return `::ffff:${convertIPv4BinaryToString(convertIPv4MappedIPv6ToIPv4(ipV6))}`336 }337
36 unmodified lines