fix(ssh): drop stale polls + preserve port inheritance + sshd extra-keys

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.
This commit is contained in:
José Almeida
2026-05-17 21:31:34 +01:00
parent 2a342927e7
commit 5dc8550962
3 changed files with 30 additions and 4 deletions
@@ -29,12 +29,16 @@ export function parseSSHExecutorConfig(
): Partial<SSHExecutorConfig> {
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 ?? "",
@@ -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<SSHSession[]>([]);
const [loading, setLoading] = useState(false);
const [error, setError] = useState<string | null>(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 (
+7
View File
@@ -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.