diff --git a/application/state/useSessionState.reopenClosedTab.test.ts b/application/state/useSessionState.reopenClosedTab.test.ts index 71cf50d8d..5062bc030 100644 --- a/application/state/useSessionState.reopenClosedTab.test.ts +++ b/application/state/useSessionState.reopenClosedTab.test.ts @@ -5,6 +5,7 @@ import { createRoot } from "react-dom/client"; import { JSDOM } from "jsdom"; import type { Host } from "../../domain/models"; +import { buildSshDeepLinkEphemeralHostFromSaved } from "../../domain/sshDeepLink"; import { collectSessionIds } from "../../domain/workspace"; import { activeTabStore } from "./activeTabStore"; import { useSessionState } from "./useSessionState"; @@ -275,3 +276,59 @@ test("useSessionState prunes deleted hosts from a closed split workspace on reop assert.ok(Math.abs((restored.root.sizes?.[0] ?? 0) - 2 / 7) < 1e-9); assert.ok(Math.abs((restored.root.sizes?.[1] ?? 0) - 5 / 7) < 1e-9); }); + +test("useSessionState excludes ephemeral deep-link hosts with bastion/proxy/legacy settings from reopen history", async (t) => { + const { root } = setupDom(t); + let state!: ReturnType; + function Probe() { + state = useSessionState({ persistSessionRestore: false }); + return null; + } + await act(async () => { + root.render(React.createElement(React.StrictMode, null, React.createElement(Probe))); + }); + + const savedHost: Host = { + ...makeHost("h-bastion-target", "Bastion Target"), + hostChain: { hostIds: ["h-jump"] }, + proxyConfig: { type: "socks5", host: "127.0.0.1", port: 1080 }, + legacyAlgorithms: true, + }; + const ephemeralHost = buildSshDeepLinkEphemeralHostFromSaved( + savedHost, + { rawUrl: `ssh://${savedHost.username}:otp@${savedHost.hostname}`, hostname: savedHost.hostname, username: savedHost.username, password: "otp" }, + { id: "ephemeral-1", now: 1000 }, + ); + + let savedSessionId = ""; + let ephSessionId = ""; + await act(async () => { + savedSessionId = state.connectToHost(savedHost); + ephSessionId = state.connectToHost(ephemeralHost); + }); + + await act(async () => { + state.closeSession(savedSessionId); + }); + await act(async () => { + state.closeSession(ephSessionId); + }); + + // Only the saved vault host was recorded; the ephemeral host was skipped. + assert.equal(state.getClosedTabHistoryCount(), 1); + + const hostsById = new Map([[savedHost.id, savedHost]]); + await act(async () => { + const reopened = state.reopenLastClosedTab({ + availableHostIds: new Set(hostsById.keys()), + hostsById, + }); + assert.ok(reopened && reopened.kind === "session"); + assert.equal(reopened.session.hostId, savedHost.id); + assert.equal(reopened.session.ephemeralHost, undefined); + }); + + assert.equal(state.sessions.length, 1); + assert.equal(state.sessions[0].hostId, savedHost.id); + assert.equal(state.getClosedTabHistoryCount(), 0); +}); diff --git a/domain/closedTabHistory.test.ts b/domain/closedTabHistory.test.ts index e06c0055e..07d10ab1d 100644 --- a/domain/closedTabHistory.test.ts +++ b/domain/closedTabHistory.test.ts @@ -2,7 +2,9 @@ import assert from "node:assert/strict"; import test from "node:test"; import type { Host, TerminalSession, Workspace } from "./models"; +import { buildSshDeepLinkEphemeralHostFromSaved } from "./sshDeepLink"; import { collectSessionIds } from "./workspace"; +import { createHostTerminalSession } from "../application/state/sessionFactories"; import { DEFAULT_CLOSED_TAB_HISTORY_LIMIT, createClosedSessionEntry, @@ -113,7 +115,7 @@ test("pushClosedTabEntry enforces bounded LIFO limit and deduplicates by sourceI assert.equal(secondPop.entry?.sourceId, "s-24"); }); -test("isClosedSessionSnapshotRestorable handles vault hosts, local terminals, quick serial, and ephemeral hosts", () => { +test("isClosedSessionSnapshotRestorable handles vault hosts, local terminals, quick serial, and rejects ephemeral hosts", () => { const availableHostIds = new Set(["host-live"]); assert.equal( @@ -157,15 +159,120 @@ test("isClosedSessionSnapshotRestorable handles vault hosts, local terminals, qu ); assert.equal( isClosedSessionSnapshotRestorable( - createClosedSessionSnapshot(makeSession("eph-1", { + { + sourceSessionId: "eph-1", hostId: "ephemeral-1", - ephemeralHost: true, + hostLabel: "Deep Link", hostname: "jump.example.test", - }))!, + username: "ops", + protocol: "ssh", + ephemeralHost: true, + }, availableHostIds, ), - true, + false, ); + assert.equal( + isClosedSessionSnapshotRestorable({ + sourceSessionId: "eph-1", + hostId: "ephemeral-1", + hostLabel: "Deep Link", + hostname: "jump.example.test", + username: "ops", + protocol: "ssh", + ephemeralHost: true, + }), + false, + ); +}); + +test("ephemeral hosts with bastion hostChain, proxyConfig, and legacyAlgorithms are excluded from closed tab history", () => { + const savedBastionTarget = makeHost("h-prod-internal", { + label: "Internal Prod", + hostname: "10.0.0.42", + username: "ops", + hostChain: { hostIds: ["h-bastion-1"] }, + proxyConfig: { type: "socks5", host: "127.0.0.1", port: 1080 }, + legacyAlgorithms: true, + }); + + const ephemeralHost = buildSshDeepLinkEphemeralHostFromSaved( + savedBastionTarget, + { rawUrl: "ssh://ops:one-time-password@10.0.0.42", hostname: "10.0.0.42", username: "ops", password: "one-time-password" }, + { id: "ephemeral-deeplink-1", now: 1234 }, + ); + assert.equal(ephemeralHost.ephemeral, true); + assert.deepEqual(ephemeralHost.hostChain, { hostIds: ["h-bastion-1"] }); + assert.deepEqual(ephemeralHost.proxyConfig, { type: "socks5", host: "127.0.0.1", port: 1080 }); + assert.equal(ephemeralHost.legacyAlgorithms, true); + + const ephemeralSession = createHostTerminalSession("sess-eph-1", ephemeralHost); + assert.equal(ephemeralSession.ephemeralHost, true); + + // Ephemeral standalone tabs are never captured because their Host record + // (including hostChain, proxyConfig, legacyAlgorithms, and one-time auth) is + // purged on close and cannot be faithfully reconstructed via fallback host. + assert.equal(createClosedSessionSnapshot(ephemeralSession), null); + assert.equal(createClosedSessionEntry(ephemeralSession), null); + + // An all-ephemeral workspace is excluded. + const ephOnlyWs: Workspace = { + id: "ws-eph-only", + title: "Ephemeral Only", + root: { id: "p-eph", type: "pane", sessionId: ephemeralSession.id }, + }; + assert.equal( + createClosedWorkspaceEntry(ephOnlyWs, [{ ...ephemeralSession, workspaceId: ephOnlyWs.id }]), + null, + ); + + // A mixed workspace drops the ephemeral pane and preserves the restorable saved host pane. + const savedSession = makeSession("sess-saved-1", { + workspaceId: "ws-mixed", + hostId: savedBastionTarget.id, + }); + const mixedWs: Workspace = { + id: "ws-mixed", + title: "Mixed", + focusedSessionId: ephemeralSession.id, + root: { + id: "split-mixed", + type: "split", + direction: "vertical", + sizes: [0.6, 0.4], + children: [ + { id: "p-saved", type: "pane", sessionId: savedSession.id }, + { id: "p-eph", type: "pane", sessionId: ephemeralSession.id }, + ], + }, + }; + const mixedEntry = createClosedWorkspaceEntry(mixedWs, [ + savedSession, + { ...ephemeralSession, workspaceId: mixedWs.id }, + ]); + assert.ok(mixedEntry); + assert.deepEqual(collectSessionIds(mixedEntry.root), [savedSession.id]); + assert.equal(mixedEntry.focusedSessionId, savedSession.id); + + // Even a pre-built ephemeral entry is rejected by pruneClosedTabEntries, + // popClosedTabEntry, and materializeClosedTabEntry. + const craftedEphemeralEntry: ClosedTabEntry = { + kind: "session", + sourceId: "sess-eph-crafted", + closedAt: 1, + session: { + sourceSessionId: "sess-eph-crafted", + hostId: ephemeralHost.id, + hostLabel: ephemeralHost.label, + hostname: ephemeralHost.hostname, + username: ephemeralHost.username, + protocol: "ssh", + ephemeralHost: true, + }, + }; + assert.deepEqual(pruneClosedTabEntries([craftedEphemeralEntry]), []); + assert.equal(popClosedTabEntry([craftedEphemeralEntry]).entry, null); + assert.equal(materializeClosedTabEntry(craftedEphemeralEntry), null); }); test("createClosedWorkspaceEntry captures nested layout ratios, focus, and view mode, and skips empty workspaces", () => { diff --git a/domain/closedTabHistory.ts b/domain/closedTabHistory.ts index dc65ba2a5..640c1c708 100644 --- a/domain/closedTabHistory.ts +++ b/domain/closedTabHistory.ts @@ -160,7 +160,14 @@ export function createClosedSessionSnapshot( session: TerminalSession, options?: { lastCwd?: string }, ): ClosedTerminalSessionSnapshot | null { - if (session.hiddenFromTabs) return null; + // Hidden MCP sessions were never tabs. Ephemeral hosts (password deep links, + // unsaved quick-connects, and saved-host overrides built by + // buildSshDeepLinkEphemeralHostFromSaved) have their in-memory Host record + // purged on close, so host-level routing/settings (hostChain, proxyConfig, + // legacyAlgorithms, deviceType, one-time auth) cannot be faithfully + // reconstructed from a TerminalSession snapshot alone. Mirror + // domain/sessionRestore.ts and exclude them. + if (session.hiddenFromTabs || session.ephemeralHost) return null; const protocol = sanitizeProtocol(session.protocol); const pluginConnection = sanitizePluginConnection(session.pluginConnection, protocol); @@ -272,14 +279,19 @@ export function createClosedWorkspaceEntry( * Determine whether a closed session snapshot can still be reopened given the * current set of vault host ids. * - * Self-contained sessions (local terminals, quick-connect serial ports, and - * ephemeral connections with non-secret host metadata) do not depend on a vault - * host record and remain restorable even when `availableHostIds` is empty. + * Self-contained sessions (local terminals and quick-connect serial ports) do + * not depend on a vault host record and remain restorable even when + * `availableHostIds` is empty. Ephemeral host sessions are rejected because + * their in-memory Host record (routing, proxy/jump chain, legacy algorithms, + * and one-time credentials) is purged when the tab closes. */ export function isClosedSessionSnapshotRestorable( snapshot: ClosedTerminalSessionSnapshot, availableHostIds?: ReadonlySet, ): boolean { + if (snapshot.ephemeralHost === true) { + return false; + } if (!availableHostIds) return true; if (snapshot.protocol === "local" && snapshot.hostId === LOCAL_TERMINAL_HOST_ID) { return true; @@ -291,9 +303,6 @@ export function isClosedSessionSnapshotRestorable( ) { return true; } - if (snapshot.ephemeralHost === true) { - return Boolean(snapshot.hostname); - } return Boolean(snapshot.hostId) && availableHostIds.has(snapshot.hostId); } @@ -450,8 +459,9 @@ export function materializeClosedTabEntry( const createId = options?.createId ?? (() => crypto.randomUUID()); if (entry.kind === "session") { + if (entry.session.ephemeralHost === true) return null; const newSessionId = createId(); - const liveHost = entry.session.ephemeralHost ? undefined : options?.hostsById?.get(entry.session.hostId); + const liveHost = options?.hostsById?.get(entry.session.hostId); const session = materializeSessionSnapshot(entry.session, { id: newSessionId, liveHost, @@ -465,7 +475,9 @@ export function materializeClosedTabEntry( const rootSessionIds = collectSessionIds(entry.root); const snapshotBySourceId = new Map( - entry.sessions.map((snapshot) => [snapshot.sourceSessionId, snapshot] as const), + entry.sessions + .filter((snapshot) => snapshot.ephemeralHost !== true) + .map((snapshot) => [snapshot.sourceSessionId, snapshot] as const), ); const validSourceIds = rootSessionIds.filter((id) => snapshotBySourceId.has(id)); if (validSourceIds.length === 0) return null;