Skip to content

Commit e5736f9

Browse files
only21milclaude
andcommitted
fix(shared): reject nested brackets and trailing dots in mapped loopback hosts
The mapped parser normalizes again, so a host such as `[[::ffff:7f00:1]]` or `[::ffff:7f00:1.]` still reached it as a valid literal. Check the literal once, with at most one matching pair of brackets removed, before parsing. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent f608e0a commit e5736f9

2 files changed

Lines changed: 7 additions & 6 deletions

File tree

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,8 @@ describe("isLocalLoopbackHost", () => {
126126
"::ffff:127..0.1",
127127
"::ffff:0x7f.0.0.1",
128128
"[::ffff:7f00:1",
129+
"[[::ffff:7f00:1]]",
130+
"[::ffff:7f00:1.]",
129131
]) {
130132
expect(isLocalLoopbackHost(host), host).toBe(false);
131133
}

‎packages/shared/src/hostClassification.ts‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,12 @@ export const isLocalLoopbackHost = (host: string): boolean => {
7777
const normalized = normalizeHostname(host);
7878
if (normalized === "localhost" || normalized === "::1") return true;
7979
if (parseIpv4Address(normalized)?.[0] === 127) return true;
80-
// Mapped forms must be well formed: normalizeHostname forgives an unmatched bracket, and the
81-
// shared dotted parser also reads `127..0.1` and `0x7f.0.0.1`.
82-
const mapped =
83-
host.startsWith("[") === host.endsWith("]") ? parseIpv4MappedIpv6Address(normalized) : null;
80+
// 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`.
82+
const literal = (/^\[.*\]$/u.test(host) ? host.slice(1, -1) : host).toLowerCase();
83+
const mapped = /^[\da-f:.]+$/u.test(literal) ? parseIpv4MappedIpv6Address(literal) : null;
8484
return (
85-
mapped?.[0] === 127 &&
86-
(!normalized.includes(".") || normalized === `::ffff:${mapped.join(".")}`)
85+
mapped?.[0] === 127 && (!literal.includes(".") || literal === `::ffff:${mapped.join(".")}`)
8786
);
8887
};
8988

0 commit comments

Comments
 (0)