Skip to content

Commit 04a15bf

Browse files
only21milclaude
andcommitted
fix(shared): expanded IPv4-mapped loopback spellings count as loopback
isLocalLoopbackHost only matched the compressed ::ffff: prefix, so equivalent spellings such as 0:0:0:0:0:ffff:7f00:1 returned false for callers that pass raw host strings. Parse the literal as a full IPv6 address (a canonical dotted tail becomes two hextets) and check the ::ffff:127.0.0.0/104 prefix. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent e5736f9 commit 04a15bf

2 files changed

Lines changed: 13 additions & 5 deletions

File tree

‎packages/shared/src/hostClassification.test.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,12 +117,15 @@ describe("isLocalLoopbackHost", () => {
117117
"::FFFF:127.1.2.3",
118118
new URL("http://[::ffff:127.0.0.1]:3773/").hostname,
119119
"::ffff:7fff:fffe",
120+
"0:0:0:0:0:ffff:7f00:1",
121+
"0:0:0:0:0:FFFF:127.0.0.1",
120122
]) {
121123
expect(isLocalLoopbackHost(host), host).toBe(true);
122124
}
123125
for (const host of [
124126
"::ffff:192.168.1.10",
125127
"::7f00:1",
128+
"::ffff:0:7f00:1",
126129
"::ffff:127..0.1",
127130
"::ffff:0x7f.0.0.1",
128131
"[::ffff:7f00:1",

‎packages/shared/src/hostClassification.ts‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,17 @@ export const isLocalLoopbackHost = (host: string): boolean => {
7878
if (normalized === "localhost" || normalized === "::1") return true;
7979
if (parseIpv4Address(normalized)?.[0] === 127) return true;
8080
// Mapped forms must be well formed. normalizeHostname forgives stray brackets and trailing dots,
81-
// and the shared dotted parser also reads `127..0.1` and `0x7f.0.0.1`.
81+
// and the shared dotted parser also reads `127..0.1` and `0x7f.0.0.1`. A canonical dotted tail
82+
// becomes two hextets so every spelling, `0:0:0:0:0:ffff:7f00:1` included, parses as IPv6.
8283
const literal = (/^\[.*\]$/u.test(host) ? host.slice(1, -1) : host).toLowerCase();
83-
const mapped = /^[\da-f:.]+$/u.test(literal) ? parseIpv4MappedIpv6Address(literal) : null;
84-
return (
85-
mapped?.[0] === 127 && (!literal.includes(".") || literal === `::ffff:${mapped.join(".")}`)
86-
);
84+
const hex = literal.replace(/:(\d+\.\d+\.\d+\.\d+)$/u, (match, dotted: string) => {
85+
const ipv4 = parseIpv4Address(dotted);
86+
return ipv4?.join(".") === dotted
87+
? `:${((ipv4[0]! << 8) | ipv4[1]!).toString(16)}:${((ipv4[2]! << 8) | ipv4[3]!).toString(16)}`
88+
: match;
89+
});
90+
const ipv6 = /^[\da-f:]+$/u.test(hex) ? parseIpv6Address(hex) : null;
91+
return ipv6 !== null && ipv6PrefixMatches(ipv6, [0, 0, 0, 0, 0, 0xffff, 0x7f00, 0], 104);
8792
};
8893

8994
/** MagicDNS or fd7a:115c:a1e0::/48 identifies Tailscale; shared 100.64.0.0/10 addresses alone do not. */

0 commit comments

Comments
 (0)