@devries-cameron20 / t3code-1 / commits / 63edc304c0
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

fix(desktop): SSH suggestions from known_hosts keep their port (#35556)

Julius Marminge committed Nov 12, 2026 at 11:58 UTC 63edc304c035cbf429a07dba6ef99aa8d3b10b46
3 files changed +77 -29
apps/web/src/components/settings/AddEnvironmentDialog.tsx
+9 -2
@@ -66,6 +66,10 @@ import {
66 } from "./ConnectionsSettings.logic";
67
68 const EMPTY_DISCOVERED_SSH_HOSTS: ReadonlyArray<DesktopDiscoveredSshHost> = [];
69 +// known_hosts can list one host on several ports. Its own bracketed form keeps an IPv6
70 +// host's port separate from the address; no portless known_hosts entry has that form.
71 +const sshHostSuggestionValue = (target: DesktopDiscoveredSshHost) =>
72 + target.port === null ? target.alias : `[${target.alias}]:${target.port}`;
73
74 export function AddEnvironmentDialog({
75 open,
@@ -294,6 +298,7 @@ export function AddEnvironmentDialog({
298 setIsAddingSavedBackend(false);
299 return;
300 }
301 + if (target.port !== null) resolved = { ...resolved, port: target.port };
302 setSavedBackendSshUsername(resolved.username ?? "");
303 setSavedBackendSshPort(resolved.port === null ? "" : String(resolved.port));
304 await connectSavedBackendSshTarget(resolved);
@@ -494,7 +499,7 @@ export function AddEnvironmentDialog({
499 </label>
500 <Autocomplete
501 items={filteredDiscoveredSshHosts}
497 - itemToStringValue={(target) => target.alias}
502 + itemToStringValue={sshHostSuggestionValue}
503 mode="none"
504 openOnInputClick
505 open={sshHostSuggestionsOpen}
@@ -507,7 +512,9 @@ export function AddEnvironmentDialog({
512 setSavedBackendSshHost(value);
513 if (eventDetails.reason !== "item-press") return;
514
510 - const target = filteredDiscoveredSshHosts.find((host) => host.alias === value);
515 + const target = filteredDiscoveredSshHosts.find(
516 + (host) => sshHostSuggestionValue(host) === value,
517 + );
518 if (target) void handleSelectSshHostSuggestion(target);
519 }}
520 >
packages/ssh/src/config.test.ts
+37 -10
@@ -4,7 +4,7 @@ import * as Effect from "effect/Effect";
4 import * as FileSystem from "effect/FileSystem";
5 import * as Path from "effect/Path";
6
7 -import { discoverSshHosts, parseKnownHostsHostnames } from "./config.ts";
7 +import { discoverSshHosts, parseKnownHosts } from "./config.ts";
8
9 function makeTempHomeDir() {
10 return Effect.gen(function* () {
@@ -166,7 +166,7 @@ describe("ssh config", () => {
166 alias: "bastion.example.com",
167 hostname: "bastion.example.com",
168 username: null,
169 - port: null,
169 + port: 2222,
170 source: "known-hosts",
171 },
172 {
@@ -279,10 +279,37 @@ describe("ssh config", () => {
279 }).pipe(Effect.provide(NodeServices.layer), Effect.scoped),
280 );
281
282 + it.effect("keeps each explicit known_hosts port as its own destination", () =>
283 + Effect.gen(function* () {
284 + const fs = yield* FileSystem.FileSystem;
285 + const path = yield* Path.Path;
286 + const homeDir = yield* makeTempHomeDir();
287 + const sshDir = path.join(homeDir, ".ssh");
288 + yield* fs.makeDirectory(sshDir);
289 + yield* fs.writeFileString(
290 + path.join(sshDir, "known_hosts"),
291 + [
292 + "[100.64.0.1]:2222 ssh-ed25519 AAAA",
293 + "[100.64.0.1]:2223 ssh-ed25519 AAAA",
294 + "[fd7a:115c:a1e0::1]:2222 ssh-ed25519 AAAA",
295 + "",
296 + ].join("\n"),
297 + );
298 +
299 + const hosts = yield* discoverSshHosts({ homeDir });
300 + assert.deepEqual(
301 + hosts
302 + .map((host) => `${host.hostname} ${host.port}`)
303 + .toSorted((left, right) => left.localeCompare(right)),
304 + ["100.64.0.1 2222", "100.64.0.1 2223", "fd7a:115c:a1e0::1 2222"],
305 + );
306 + }).pipe(Effect.provide(NodeServices.layer), Effect.scoped),
307 + );
308 +
309 it.effect("parses known_hosts entries without returning hashed hosts", () =>
310 Effect.sync(() => {
311 assert.deepEqual(
285 - parseKnownHostsHostnames(
312 + parseKnownHosts(
313 [
314 "github.com ssh-ed25519 AAAA",
315 "gitlab.com,gitlab-alias ssh-ed25519 BBBB",
@@ -296,13 +323,13 @@ describe("ssh config", () => {
323 ].join("\n"),
324 ),
325 [
299 - "::1",
300 - "2001:db8::1",
301 - "github.com",
302 - "gitlab-alias",
303 - "gitlab.com",
304 - "port.example.com",
305 - "ssh.example.com",
326 + { hostname: "::1", port: null },
327 + { hostname: "2001:db8::1", port: null },
328 + { hostname: "github.com", port: null },
329 + { hostname: "gitlab-alias", port: null },
330 + { hostname: "gitlab.com", port: null },
331 + { hostname: "port.example.com", port: null },
332 + { hostname: "ssh.example.com", port: 2200 },
333 ],
334 );
335 }),
packages/ssh/src/config.ts
+31 -17
@@ -237,23 +237,32 @@ const collectSshConfigAliasesFromFile = Effect.fnUntraced(function* (
237 }
238 });
239
240 -function normalizeKnownHostsHostname(rawHost: string): string {
240 +interface KnownHost {
241 + readonly hostname: string;
242 + readonly port: number | null;
243 +}
244 +
245 +function parseKnownHostsHost(rawHost: string): KnownHost | null {
246 const bracketMatch = /^\[([^\]]+)\]:(\d+)$/u.exec(rawHost);
247 if (bracketMatch?.[1]) {
243 - return bracketMatch[1];
248 + const port = Number(bracketMatch[2]);
249 + return port >= 1 && port <= 65535 ? { hostname: bracketMatch[1], port } : null;
250 }
251
252 if (!rawHost.includes(":")) {
247 - return rawHost;
253 + return { hostname: rawHost, port: null };
254 }
255
256 const firstColonIndex = rawHost.indexOf(":");
257 const lastColonIndex = rawHost.lastIndexOf(":");
252 - return firstColonIndex === lastColonIndex ? rawHost.slice(0, lastColonIndex) : rawHost;
258 + return {
259 + hostname: firstColonIndex === lastColonIndex ? rawHost.slice(0, lastColonIndex) : rawHost,
260 + port: null,
261 + };
262 }
263
255 -export function parseKnownHostsHostnames(raw: string): ReadonlyArray<string> {
256 - const hostnames = new Set<string>();
264 +export function parseKnownHosts(raw: string): ReadonlyArray<KnownHost> {
265 + const hosts = new Map<string, KnownHost>();
266
267 for (const line of raw.split(/\r?\n/u)) {
268 const trimmed = line.trim();
@@ -270,23 +279,28 @@ export function parseKnownHostsHostnames(raw: string): ReadonlyArray<string> {
279 }
280
281 for (const rawHost of hostField.split(",")) {
273 - const host = normalizeKnownHostsHostname(rawHost).trim();
274 - if (host.length === 0 || hasSshPattern(host)) {
282 + const parsed = parseKnownHostsHost(rawHost.trim());
283 + if (parsed === null) continue;
284 + const hostname = parsed.hostname.trim();
285 + if (hostname.length === 0 || hasSshPattern(hostname)) {
286 continue;
287 }
277 - hostnames.add(host);
288 + hosts.set(`${hostname}\u0000${parsed.port ?? ""}`, { hostname, port: parsed.port });
289 }
290 }
291
281 - return [...hostnames].toSorted((left, right) => left.localeCompare(right));
292 + return [...hosts.values()].toSorted(
293 + (left, right) =>
294 + left.hostname.localeCompare(right.hostname) || (left.port ?? 0) - (right.port ?? 0),
295 + );
296 }
297
284 -const readKnownHostsHostnames = Effect.fnUntraced(function* (filePath: string) {
298 +const readKnownHosts = Effect.fnUntraced(function* (filePath: string) {
299 const fs = yield* FileSystem.FileSystem;
300 if (!(yield* fs.exists(filePath))) {
287 - return NO_HOSTS;
301 + return [];
302 }
289 - return parseKnownHostsHostnames(yield* fs.readFileString(filePath));
303 + return parseKnownHosts(yield* fs.readFileString(filePath));
304 });
305
306 export const discoverSshHosts = Effect.fnUntraced(
@@ -316,7 +330,7 @@ export const discoverSshHosts = Effect.fnUntraced(
330 hostNameRules,
331 [],
332 );
319 - const knownHosts = yield* readKnownHostsHostnames(path.join(sshDirectory, "known_hosts"));
333 + const knownHosts = yield* readKnownHosts(path.join(sshDirectory, "known_hosts"));
334 const discovered = new Map<string, DesktopDiscoveredSshHost>();
335
336 const configuredHostnames = new Set<string>();
@@ -332,15 +346,15 @@ export const discoverSshHosts = Effect.fnUntraced(
346 });
347 }
348
335 - for (const hostname of knownHosts) {
349 + for (const { hostname, port } of knownHosts) {
350 if (discovered.has(hostname) || configuredHostnames.has(hostname.toLowerCase())) {
351 continue;
352 }
339 - discovered.set(hostname, {
353 + discovered.set(`${hostname}\u0000${port ?? ""}`, {
354 alias: hostname,
355 hostname,
356 username: null,
343 - port: null,
357 + port,
358 source: "known-hosts",
359 });
360 }