Skip to content

Commit 368b4fe

Browse files
fix(server): IPv4-mapped loopback SSH device targets count as this machine (#35774)
1 parent 25feac2 commit 368b4fe

2 files changed

Lines changed: 22 additions & 4 deletions

File tree

‎apps/server/src/device/localSshDeviceHost.test.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ const spawner = ChildProcessSpawner.make((command) =>
2727
remote: "hostname 192.0.2.1\nport 22\n",
2828
loopback: "hostname 127.0.1.1\nport 22\n",
2929
ipv6: "hostname ::1\nport 22\n",
30+
mapped: "hostname ::ffff:127.0.0.1\nport 22\n",
3031
forwarded: "hostname 127.0.0.1\nport 2222\n",
3132
proxy: "hostname 127.0.0.1\nport 22\nproxyjump bastion\n",
3233
command: "hostname 127.0.0.1\nport 22\nproxycommand nc remote 22\n",
@@ -59,7 +60,7 @@ const provide = <A, E>(
5960
it.effect("skips SSH aliases resolving to this machine, including loopback", () =>
6061
provide(
6162
Effect.gen(function* () {
62-
for (const target of ["mac-mini", "loopback", "ipv6"]) {
63+
for (const target of ["mac-mini", "loopback", "ipv6", "mapped"]) {
6364
expect(yield* isLocalSshDeviceHost(host(target))).toBe(true);
6465
}
6566
}),

‎apps/server/src/device/localSshDeviceHost.ts‎

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,21 @@ export const LocalDeviceHostAddresses = Context.Reference<ReadonlySet<string>>(
1919
},
2020
);
2121

22+
/** Unwraps IPv4-mapped IPv6 (`::ffff:127.0.0.1`, `::ffff:7f00:1`) to plain IPv4. */
23+
const unmapIPv4 = (address: string) => {
24+
if (NodeNet.isIP(address) !== 6) return address;
25+
let canonical: string;
26+
try {
27+
canonical = new URL(`http://[${address}]`).hostname;
28+
} catch {
29+
return address;
30+
}
31+
const match = /^\[::ffff:([0-9a-f]{1,4}):([0-9a-f]{1,4})\]$/.exec(canonical);
32+
if (!match) return address;
33+
const [high, low] = [parseInt(match[1]!, 16), parseInt(match[2]!, 16)];
34+
return [high >> 8, high & 0xff, low >> 8, low & 0xff].join(".");
35+
};
36+
2237
export const isLocalSshDeviceHost = Effect.fn("isLocalSshDeviceHost")(function* (
2338
host: SshDeviceHostConfig,
2439
) {
@@ -57,9 +72,11 @@ export const isLocalSshDeviceHost = Effect.fn("isLocalSshDeviceHost")(function*
5772
const localAddresses = yield* LocalDeviceHostAddresses;
5873
return (
5974
addresses.length > 0 &&
60-
addresses.every(
61-
(address) => localAddresses.has(address) || address === "::1" || address.startsWith("127."),
62-
)
75+
addresses
76+
.map(unmapIPv4)
77+
.every(
78+
(address) => localAddresses.has(address) || address === "::1" || address.startsWith("127."),
79+
)
6380
);
6481
});
6582

0 commit comments

Comments
 (0)