From 5dc8550962c8b8f61479678d73e8582b6f0ee2d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Almeida?= Date: Sun, 17 May 2026 21:31:34 +0100 Subject: [PATCH] fix(ssh): drop stale polls + preserve port inheritance + sshd extra-keys MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ssh-sessions-card: race-protect polls. Polling setInterval + manual refresh shared one state setter, so a slow request that resolved after a newer call (or after a parent switched executorId) overwrote fresh data with stale rows. Bump a monotonic sequence on each refresh and drop the response when the sequence has moved on, plus reset on mount and invalidate on unmount so an in-flight reply from a teardown can't land. Cubic P2. ssh-config parseSSHExecutorConfig: don't default ssh_port to 22. Reading an executor row that had no ssh_port set rewrote port=22 on the form, which then round-tripped back to the backend. That silently broke ~/.ssh/config Port inheritance for aliases — a save could redirect connections to 22 even though the underlying alias was supposed to resolve to another port. Leave the field undefined so the backend resolver can apply the alias' value. Cubic P2. ssh-image entrypoint: guard authorized_keys.extra mount. When only /authorized_keys.extra (and not /authorized_keys) was bind-mounted, the >> redirect ran under set -e against a missing destination and aborted container startup. mkdir + touch the destination before the append and reapply the ownership/perms. CodeRabbit P2. --- .../executors/new/[type]/ssh-config.ts | 6 +++++- .../components/settings/ssh-sessions-card.tsx | 21 ++++++++++++++++--- apps/web/e2e/fixtures/ssh-image.ts | 7 +++++++ 3 files changed, 30 insertions(+), 4 deletions(-) diff --git a/apps/web/app/settings/executors/new/[type]/ssh-config.ts b/apps/web/app/settings/executors/new/[type]/ssh-config.ts index 91fdb6a5cf..1b456694cc 100644 --- a/apps/web/app/settings/executors/new/[type]/ssh-config.ts +++ b/apps/web/app/settings/executors/new/[type]/ssh-config.ts @@ -29,12 +29,16 @@ export function parseSSHExecutorConfig( ): Partial { const c = config ?? {}; const portRaw = c.ssh_port; + // A missing ssh_port intentionally stays `undefined` so the backend's + // resolver can apply ~/.ssh/config Port inheritance for the configured + // alias. Substituting 22 here would silently redirect connections that + // relied on the alias' Port. const port = portRaw ? Number.parseInt(portRaw, 10) : undefined; return { name, host_alias: c.ssh_host_alias ?? "", host: c.ssh_host ?? "", - port: Number.isFinite(port) ? (port as number) : 22, + port: port != null && Number.isFinite(port) ? port : undefined, user: c.ssh_user ?? "", identity_source: (c.ssh_identity_source as "agent" | "file") || "agent", identity_file: c.ssh_identity_file ?? "", diff --git a/apps/web/components/settings/ssh-sessions-card.tsx b/apps/web/components/settings/ssh-sessions-card.tsx index b6b736e464..aaf06bb684 100644 --- a/apps/web/components/settings/ssh-sessions-card.tsx +++ b/apps/web/components/settings/ssh-sessions-card.tsx @@ -1,6 +1,6 @@ "use client"; -import { useCallback, useEffect, useState } from "react"; +import { useCallback, useEffect, useRef, useState } from "react"; import { Badge } from "@kandev/ui/badge"; import { Button } from "@kandev/ui/button"; import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@kandev/ui/card"; @@ -19,24 +19,39 @@ export function SSHSessionsCard({ executorId }: SSHSessionsCardProps) { const [sessions, setSessions] = useState([]); const [loading, setLoading] = useState(false); const [error, setError] = useState(null); + // Monotonic sequence used to ignore stale poll responses: a slow request + // that resolves after a newer one (or after executorId changes) would + // otherwise overwrite the fresh row set. + const seqRef = useRef(0); const refresh = useCallback(async () => { + const seq = ++seqRef.current; setLoading(true); setError(null); try { const rows = await listSSHSessions(executorId); + if (seq !== seqRef.current) return; // a newer call (or executor switch) won the race setSessions(rows); } catch (e) { + if (seq !== seqRef.current) return; setError(e instanceof Error ? e.message : "Failed to load sessions"); } finally { - setLoading(false); + if (seq === seqRef.current) setLoading(false); } }, [executorId]); useEffect(() => { + // Reset sequence so a previous executor's pending response can't land + // here and pollute the new executor's data. + seqRef.current = 0; refresh(); const id = setInterval(refresh, REFRESH_INTERVAL_MS); - return () => clearInterval(id); + return () => { + clearInterval(id); + // Bump the sequence to invalidate any in-flight response from the + // unmounted instance. + seqRef.current = -1; + }; }, [refresh]); return ( diff --git a/apps/web/e2e/fixtures/ssh-image.ts b/apps/web/e2e/fixtures/ssh-image.ts index d561c9f2b0..4b5caa2142 100644 --- a/apps/web/e2e/fixtures/ssh-image.ts +++ b/apps/web/e2e/fixtures/ssh-image.ts @@ -50,8 +50,15 @@ fi # Optional second authorized_keys for a different user (passphrase-protected # key tests load a second key into ssh-agent and want it accepted too). +# Make sure the destination exists when only /authorized_keys.extra is mounted +# (otherwise "set -e" aborts the entrypoint on the >> redirect). if [ -f /authorized_keys.extra ]; then + mkdir -p /home/kandev/.ssh + touch /home/kandev/.ssh/authorized_keys cat /authorized_keys.extra >> /home/kandev/.ssh/authorized_keys + chown -R kandev:kandev /home/kandev/.ssh + chmod 700 /home/kandev/.ssh + chmod 600 /home/kandev/.ssh/authorized_keys fi # Run sshd in the foreground so docker logs and exit-codes propagate.