Skip to content

Commit f6433db

Browse files
Umais-Adeedclaudejuliusmarminge
authored
fix(desktop): an SSH password prompt that ended leaves the dialog instead of stacking (#34192)
Co-authored-by: Claude Opus 5.5 <[email protected]> Co-authored-by: Julius Marminge <[email protected]>
1 parent e93f6ea commit f6433db

7 files changed

Lines changed: 144 additions & 20 deletions

File tree

‎apps/desktop/src/ipc/channels.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ export const BOOTSTRAP_SSH_BEARER_SESSION_CHANNEL = "desktop:bootstrap-ssh-beare
7070
export const FETCH_SSH_SESSION_STATE_CHANNEL = "desktop:fetch-ssh-session-state";
7171
export const SSH_PASSWORD_PROMPT_CHANNEL = "desktop:ssh-password-prompt";
7272
export const RESOLVE_SSH_PASSWORD_PROMPT_CHANNEL = "desktop:resolve-ssh-password-prompt";
73+
export const SSH_PASSWORD_PROMPT_SETTLED_CHANNEL = "desktop:ssh-password-prompt-settled";
7374
export const GET_SERVER_EXPOSURE_STATE_CHANNEL = "desktop:get-server-exposure-state";
7475
export const SET_SERVER_EXPOSURE_MODE_CHANNEL = "desktop:set-server-exposure-mode";
7576
export const SET_TAILSCALE_SERVE_ENABLED_CHANNEL = "desktop:set-tailscale-serve-enabled";

‎apps/desktop/src/preload.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,17 @@ contextBridge.exposeInMainWorld("desktopBridge", {
195195
ipcRenderer.removeListener(IpcChannels.SSH_PASSWORD_PROMPT_CHANNEL, wrappedListener);
196196
};
197197
},
198+
onSshPasswordPromptSettled: (listener) => {
199+
const wrappedListener = (_event: Electron.IpcRendererEvent, requestId: unknown) => {
200+
if (typeof requestId !== "string") return;
201+
listener(requestId);
202+
};
203+
204+
ipcRenderer.on(IpcChannels.SSH_PASSWORD_PROMPT_SETTLED_CHANNEL, wrappedListener);
205+
return () => {
206+
ipcRenderer.removeListener(IpcChannels.SSH_PASSWORD_PROMPT_SETTLED_CHANNEL, wrappedListener);
207+
};
208+
},
198209
resolveSshPasswordPrompt: (requestId, password) =>
199210
ipcRenderer.invoke(IpcChannels.RESOLVE_SSH_PASSWORD_PROMPT_CHANNEL, { requestId, password }),
200211
getServerExposureState: () => ipcRenderer.invoke(IpcChannels.GET_SERVER_EXPOSURE_STATE_CHANNEL),

‎apps/desktop/src/ssh/DesktopSshPasswordPrompts.test.ts‎

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ import * as TestClock from "effect/testing/TestClock";
88
import type * as Electron from "electron";
99

1010
import * as ElectronWindow from "../electron/ElectronWindow.ts";
11-
import { SSH_PASSWORD_PROMPT_CHANNEL } from "../ipc/channels.ts";
11+
import {
12+
SSH_PASSWORD_PROMPT_CHANNEL,
13+
SSH_PASSWORD_PROMPT_SETTLED_CHANNEL,
14+
} from "../ipc/channels.ts";
1215
import * as DesktopSshPasswordPrompts from "./DesktopSshPasswordPrompts.ts";
1316

1417
interface SentMessage {
@@ -144,6 +147,52 @@ describe("DesktopSshPasswordPrompts", () => {
144147
}).pipe(Effect.provide(layer(testWindow.window)), Effect.scoped);
145148
});
146149

150+
it.effect.each(["answered", "cancelled", "timed out", "interrupted"] as const)(
151+
"tells the window a prompt ended when it is %s",
152+
(outcome) => {
153+
const testWindow = makeTestWindow();
154+
155+
return Effect.gen(function* () {
156+
const prompts = yield* DesktopSshPasswordPrompts.DesktopSshPasswordPrompts;
157+
const fiber = yield* prompts
158+
.request({
159+
destination: "devbox",
160+
username: "julius",
161+
prompt: "Enter the SSH password.",
162+
attempt: 1,
163+
})
164+
.pipe(Effect.exit, Effect.forkScoped);
165+
166+
yield* Effect.yieldNow;
167+
yield* Effect.yieldNow;
168+
const request = testWindow.sentMessages[0]?.args[0] as { readonly requestId: string };
169+
assert.ok(request);
170+
switch (outcome) {
171+
case "answered":
172+
yield* prompts.resolve({ requestId: request.requestId, password: "secret" });
173+
break;
174+
case "cancelled":
175+
yield* prompts.resolve({ requestId: request.requestId, password: null });
176+
break;
177+
case "timed out":
178+
yield* TestClock.adjust("1 second");
179+
break;
180+
case "interrupted":
181+
yield* Fiber.interrupt(fiber);
182+
break;
183+
}
184+
yield* Fiber.await(fiber);
185+
186+
assert.deepEqual(
187+
testWindow.sentMessages
188+
.filter((message) => message.channel === SSH_PASSWORD_PROMPT_SETTLED_CHANNEL)
189+
.map((message) => message.args),
190+
[[request.requestId]],
191+
);
192+
}).pipe(Effect.provide(layer(testWindow.window)), Effect.scoped);
193+
},
194+
);
195+
147196
it.effect("keeps a submitted password when a later presentation step fails", () => {
148197
const testWindow = makeTestWindow({
149198
isMinimizedError: new Error("failed to read minimized state"),

‎apps/desktop/src/ssh/DesktopSshPasswordPrompts.ts‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ import * as Ref from "effect/Ref";
1313
import * as Schema from "effect/Schema";
1414

1515
import * as ElectronWindow from "../electron/ElectronWindow.ts";
16-
import { SSH_PASSWORD_PROMPT_CHANNEL } from "../ipc/channels.ts";
16+
import {
17+
SSH_PASSWORD_PROMPT_CHANNEL,
18+
SSH_PASSWORD_PROMPT_SETTLED_CHANNEL,
19+
} from "../ipc/channels.ts";
1720

1821
const DEFAULT_SSH_PASSWORD_PROMPT_TIMEOUT_MS = 3 * 60 * 1000;
1922

@@ -38,6 +41,7 @@ const DesktopSshPromptPresentationOperation = Schema.Literals([
3841
"check-window-after-restore",
3942
"focus-window",
4043
"remove-window-close-listener",
44+
"send-prompt-settled",
4145
]);
4246
type DesktopSshPromptPresentationOperation = typeof DesktopSshPromptPresentationOperation.Type;
4347

@@ -371,11 +375,22 @@ export const make = Effect.fn("desktop.sshPasswordPrompts.make")(function* (
371375
cause,
372376
}),
373377
});
378+
// However the request ends, the window drops its prompt, so no ended prompt stays on screen.
379+
const notifySettled = runPresentationOperation("send-prompt-settled", () => {
380+
if (!window.value.isDestroyed()) {
381+
window.value.webContents.send(SSH_PASSWORD_PROMPT_SETTLED_CHANNEL, requestId);
382+
}
383+
}).pipe(Effect.ignore);
374384
const cleanup = runPresentationOperation("remove-window-close-listener", () => {
375385
if (!window.value.isDestroyed()) {
376386
window.value.removeListener("closed", cancelOnWindowClosed);
377387
}
378-
}).pipe(Effect.orDie, Effect.ensuring(removePending(pendingRef, requestId)), Effect.asVoid);
388+
}).pipe(
389+
Effect.orDie,
390+
Effect.ensuring(removePending(pendingRef, requestId)),
391+
Effect.ensuring(notifySettled),
392+
Effect.asVoid,
393+
);
379394
const waitForPassword = Deferred.await(deferred).pipe(
380395
Effect.timeoutOption(Duration.millis(passwordPromptTimeoutMs)),
381396
Effect.flatMap(

‎apps/web/src/components/desktop/SshPasswordPromptDialog.browser.test.tsx‎

Lines changed: 50 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,67 @@ import { expect, it } from "vite-plus/test";
44
import { render, stubGlobal } from "../../../test/render";
55
import { SshPasswordPromptDialog, startSshPasswordPromptQueue } from "./SshPasswordPromptDialog";
66

7-
it("shows a prompt that arrived before the dialog mounted", async () => {
7+
function stubPromptBridge() {
88
const listeners: Array<(request: DesktopSshPasswordPromptRequest) => void> = [];
9-
using _bridge = stubGlobal("desktopBridge", {
9+
const settledListeners: Array<(requestId: string) => void> = [];
10+
const bridge = stubGlobal("desktopBridge", {
1011
onSshPasswordPrompt: (listener: (request: DesktopSshPasswordPromptRequest) => void) => {
1112
listeners.push(listener);
1213
return () => listeners.splice(listeners.indexOf(listener), 1);
1314
},
15+
onSshPasswordPromptSettled: (listener: (requestId: string) => void) => {
16+
settledListeners.push(listener);
17+
return () => settledListeners.splice(settledListeners.indexOf(listener), 1);
18+
},
1419
resolveSshPasswordPrompt: async () => undefined,
1520
});
21+
return {
22+
prompt: (requestId: string, destination: string) => {
23+
for (const listener of listeners) {
24+
listener({
25+
requestId,
26+
destination,
27+
username: "julius",
28+
prompt: `julius@${destination}'s password:`,
29+
expiresAt: "2999-01-01T00:00:00.000Z",
30+
});
31+
}
32+
},
33+
settle: (requestId: string) => {
34+
for (const listener of settledListeners) listener(requestId);
35+
},
36+
[Symbol.dispose]: () => bridge[Symbol.dispose](),
37+
};
38+
}
39+
40+
it("drops a prompt the desktop ended, whether it is on screen or still waiting", async () => {
41+
using bridge = stubPromptBridge();
42+
const stopQueue = startSshPasswordPromptQueue();
43+
using _queue = { [Symbol.dispose]: stopQueue };
44+
await using screen = await render(<SshPasswordPromptDialog />);
45+
46+
bridge.prompt("ssh-1", "first-host");
47+
bridge.prompt("ssh-1", "first-host");
48+
bridge.prompt("ssh-2", "second-host");
49+
bridge.prompt("ssh-3", "third-host");
50+
await expect.element(screen.getByText("julius@first-host")).toBeVisible();
51+
52+
// ssh-2 ended while it waited behind ssh-1, and ssh-1 ended on screen.
53+
bridge.settle("ssh-2");
54+
bridge.settle("ssh-1");
55+
await expect.element(screen.getByText("julius@third-host")).toBeVisible();
56+
57+
bridge.settle("ssh-3");
58+
await expect.element(screen.getByText("SSH verification required")).not.toBeInTheDocument();
59+
});
60+
61+
it("shows a prompt that arrived before the dialog mounted", async () => {
62+
using bridge = stubPromptBridge();
1663
const stopQueue = startSshPasswordPromptQueue();
1764
using _queue = { [Symbol.dispose]: stopQueue };
1865

1966
// The desktop asks while the app is still on a page without the dialog (pairing, sign-in).
20-
for (const listener of listeners) {
21-
listener({
22-
requestId: "ssh-1",
23-
destination: "devbox",
24-
username: "julius",
25-
prompt: "julius@devbox's password:",
26-
expiresAt: "2999-01-01T00:00:00.000Z",
27-
});
28-
}
67+
bridge.prompt("ssh-1", "devbox");
2968

3069
await using screen = await render(<SshPasswordPromptDialog />);
3170
await expect.element(screen.getByText("SSH verification required")).toBeVisible();

‎apps/web/src/components/desktop/SshPasswordPromptDialog.tsx‎

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,17 @@ function setPromptQueue(next: readonly DesktopSshPasswordPromptRequest[]) {
4747
* that arrives while no dialog is mounted still waits for one.
4848
*/
4949
export function startSshPasswordPromptQueue(): () => void {
50-
return (
51-
window.desktopBridge?.onSshPasswordPrompt((request) => {
52-
setPromptQueue([...promptQueue, request]);
53-
}) ?? (() => undefined)
54-
);
50+
const bridge = window.desktopBridge;
51+
if (!bridge) return () => undefined;
52+
const stopPrompts = bridge.onSshPasswordPrompt((request) => {
53+
if (promptQueue.some((queued) => queued.requestId === request.requestId)) return;
54+
setPromptQueue([...promptQueue, request]);
55+
});
56+
const stopSettled = bridge.onSshPasswordPromptSettled(removeSshPasswordPrompt);
57+
return () => {
58+
stopPrompts();
59+
stopSettled();
60+
};
5561
}
5662

5763
function subscribePromptQueue(listener: () => void): () => void {
@@ -64,7 +70,8 @@ function subscribePromptQueue(listener: () => void): () => void {
6470
const readPromptQueue = () => promptQueue;
6571

6672
function removeSshPasswordPrompt(requestId: string) {
67-
if (promptQueue[0]?.requestId === requestId) setPromptQueue(promptQueue.slice(1));
73+
if (!promptQueue.some((request) => request.requestId === requestId)) return;
74+
setPromptQueue(promptQueue.filter((request) => request.requestId !== requestId));
6875
}
6976

7077
export function SshPasswordPromptDialog() {

‎packages/contracts/src/ipc.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1110,6 +1110,8 @@ export interface DesktopBridge {
11101110
) => Promise<AuthAccessTokenResult>;
11111111
fetchSshSessionState: (httpBaseUrl: string, bearerToken: string) => Promise<AuthSessionState>;
11121112
onSshPasswordPrompt: (listener: (request: DesktopSshPasswordPromptRequest) => void) => () => void;
1113+
/** Fires with a prompt's request id once it is answered, cancelled, expired or abandoned. */
1114+
onSshPasswordPromptSettled: (listener: (requestId: string) => void) => () => void;
11131115
resolveSshPasswordPrompt: (requestId: string, password: string | null) => Promise<void>;
11141116
getServerExposureState: () => Promise<DesktopServerExposureState>;
11151117
setServerExposureMode: (mode: DesktopServerExposureMode) => Promise<DesktopServerExposureState>;

0 commit comments

Comments
 (0)