Skip to content

Commit 8c649df

Browse files
fix(server): IPv4-mapped loopback SSH device targets count as this machine
isLocalSshDeviceHost only matched plain IPv4 loopback, ::1, and interface addresses, so ::ffff:127.0.0.1 was saved as a remote device host. Unwrap IPv4-mapped IPv6 addresses to IPv4 before the check. Fixes #35762 Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 37b9d95 commit 8c649df

1 file changed

Lines changed: 20 additions & 3 deletions

File tree

‎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)