Compare commits

...
Author SHA1 Message Date
Codex 20b0ad946b fix: prefill saved password on SSH auth retry after MFA failure
When target SSH authentication fails after a manual OTP attempt, the full
credential dialog clears the host's saved password and requires retyping it.
Prefill that dialog from the saved direct host password while keeping the
field editable and requiring explicit submission. Respect disabled password
saving and exclude unreadable encrypted values.

Leave keyboard-interactive retry latches and jump-host failure handling
unchanged. Cover prefill, manual correction/clearing, no automatic submit,
saved-password variants, and jump-host failures.

Validation: 287 focused tests passed; ESLint on all three changed files and
git diff --check passed. No Electron smoke or live JMS environment used.

Fixes #4346
2026-11-25 09:11:07 +08:00
3 changed files with 159 additions and 1 deletions

No files matched your search

@@ -1832,11 +1832,91 @@ test("startSSH resolves jump host proxy credentials from an identity", async ()
assert.equal(jumpHosts[0]?.sshAuthReadyTimeoutMs, 360_000);
});
test("startSSH prefills the saved host password on target auth failure without retrying automatically", async () => {
let password = "";
let needsAuth = false;
let retryMessage: string | null = null;
let error: string | null = "previous error";
let status = "";
const attempts: Record<string, unknown>[] = [];
const ctx = createStarterContext({
host: {
id: "host-1",
label: "JMS",
hostname: "jms.example.test",
port: 2222,
username: "alice",
authMethod: "password",
requiresMfa: false,
savePassword: true,
password: "saved-secret",
},
terminalBackend: createMockTerminalBackend({
startSSHSession: async (options: Record<string, unknown>) => {
attempts.push(options);
throw new Error("All configured authentication methods failed");
},
}),
setNeedsAuth: (value: boolean) => { needsAuth = value; },
setAuthPassword: (value: string) => { password = value; },
setAuthRetryMessage: (value: string | null) => { retryMessage = value; },
setError: (value: string | null) => { error = value; },
setStatus: (value: string) => { status = value; },
});
await createTerminalSessionStarters(ctx as never).startSSH(createTermStub() as never);
assert.equal(attempts.length, 1);
assert.equal(attempts[0].username, "alice");
assert.equal(attempts[0].password, "saved-secret");
assert.equal(attempts[0].requiresMfa, false);
assert.equal(needsAuth, true);
assert.equal(password, "saved-secret");
assert.equal(retryMessage, "Authentication failed. Please check your credentials and try again.");
assert.equal(error, null);
assert.equal(status, "connecting");
});
test("startSSH auth retry only prefills a usable saved direct host password", async (t) => {
const cases = [
{ name: "no saved password", host: {}, expected: "" },
{ name: "saving disabled", host: { password: "unsaved-secret", savePassword: false }, expected: "" },
{ name: "unreadable password", host: { password: ENCRYPTED_CREDENTIAL_PLACEHOLDER, savePassword: true }, expected: "" },
{ name: "legacy default save setting", host: { password: "legacy-secret" }, expected: "legacy-secret" },
{ name: "whitespace password", host: { password: " ", savePassword: true }, expected: " " },
{ name: "identity password only", host: { identityId: "identity-1", savePassword: true }, expected: "" },
];
for (const item of cases) {
await t.test(item.name, async () => {
let password = "previous-value";
const ctx = createStarterContext({
host: {
id: "host-1", label: "Target", hostname: "target.example.test", username: "alice",
...item.host,
},
identities: [{
id: "identity-1", label: "Saved identity", username: "alice",
authMethod: "password", password: "identity-secret", created: 0,
}],
terminalBackend: createMockTerminalBackend({
startSSHSession: async () => { throw new Error("Authentication failed"); },
}),
setAuthPassword: (value: string) => { password = value; },
});
await createTerminalSessionStarters(ctx as never).startSSH(createTermStub() as never);
assert.equal(password, item.expected);
});
}
});
test("startSSH shows jump-host auth failures without opening target auth retry", async () => {
let error = "";
let needsAuth = false;
let retryMessage: string | null = "previous retry";
let status = "";
let password = "previous-value";
const progressLogs: string[] = [];
const terminalBackend = createMockTerminalBackend({
startSSHSession: async () => {
@@ -1853,6 +1933,8 @@ test("startSSH shows jump-host auth failures without opening target auth retry",
hostname: "target.example.test",
username: "alice",
hostChain: { hostIds: ["jump-1"] },
password: "target-secret",
savePassword: true,
},
resolvedChainHosts: [{
id: "jump-1",
@@ -1863,6 +1945,7 @@ test("startSSH shows jump-host auth failures without opening target auth retry",
}],
setError: (message: string) => { error = message; },
setNeedsAuth: (value: boolean) => { needsAuth = value; },
setAuthPassword: (value: string) => { password = value; },
setAuthRetryMessage: (message: string | null) => { retryMessage = message; },
setStatus: (next: string) => { status = next; },
updateStatus: (next: string) => { status = next; },
@@ -1879,6 +1962,7 @@ test("startSSH shows jump-host auth failures without opening target auth retry",
assert.equal(needsAuth, false);
assert.equal(retryMessage, null);
assert.equal(password, "");
assert.equal(status, "disconnected");
assert.match(error, /Jump host authentication failed for "Bastion"/);
assert.equal(progressLogs.some((line) => /Authentication failed\. Please try again/.test(line)), false);
@@ -911,7 +911,11 @@ export const createTerminalSessionStarters = (ctx: TerminalSessionStartersContex
"Authentication failed. Please check your credentials and try again.",
),
);
ctx.setAuthPassword("");
ctx.setAuthPassword(
ctx.host.savePassword !== false
? (sanitizeCredentialValue(ctx.host.password) ?? "")
: "",
);
ctx.setProgressLogs((prev) => [
...prev,
tr("terminal.auth.retryLog", "Authentication failed. Please try again."),
@@ -1,5 +1,7 @@
import assert from "node:assert/strict";
import test from "node:test";
import { createElement } from "react";
import { act, create, type ReactTestRenderer } from "react-test-renderer";
import { applyGroupDefaults } from "../../domain/groupConfig";
import { resolveHostAuth } from "../../domain/sshAuth";
@@ -7,7 +9,10 @@ import type { Host, Identity } from "../../types";
import {
buildSavedAuthHostUpdate,
isAuthPasswordProvided,
useTerminalAuthState,
} from "./hooks/useTerminalAuthState";
import { createTerminalSessionStarters, type PendingAuth } from "./runtime/createTerminalSessionStarters";
import { createMockTerminalBackend, createStarterContext, createTermStub } from "./runtime/testSupport/starterHarness";
const baseHost: Host = {
id: "host-1",
@@ -29,6 +34,71 @@ test("isAuthPasswordProvided accepts whitespace-only passwords (#2036)", () => {
assert.equal(isAuthPasswordProvided("secret"), true);
});
test("auth failure prefill reaches the auth hook and remains editable until explicit submit", async () => {
const environment = globalThis as typeof globalThis & { IS_REACT_ACT_ENVIRONMENT?: boolean };
const previous = environment.IS_REACT_ACT_ENVIRONMENT;
environment.IS_REACT_ACT_ENVIRONMENT = true;
const host: Host = {
...baseHost, authMethod: "password", identityId: "", identityFileId: undefined,
password: "saved-secret", savePassword: true,
};
const pendingAuthRef = { current: null as PendingAuth };
const term = createTermStub();
let auth: ReturnType<typeof useTerminalAuthState>;
let submissions = 0;
let renderer: ReactTestRenderer | undefined;
const Form = () => {
auth = useTerminalAuthState({
host, identities: [], pendingAuthRef, termRef: { current: term as never },
onStartSession: () => { submissions += 1; },
setStatus: () => {}, setProgressLogs: () => {},
});
return null;
};
try {
await act(async () => { renderer = create(createElement(Form)); });
await act(async () => {
await createTerminalSessionStarters(createStarterContext({
host, pendingAuthRef,
terminalBackend: createMockTerminalBackend({
startSSHSession: async () => { throw new Error("Authentication failed"); },
}),
setNeedsAuth: auth!.setNeedsAuth,
setAuthRetryMessage: auth!.setAuthRetryMessage,
setAuthPassword: auth!.setAuthPassword,
}) as never).startSSH(term as never);
});
assert.equal(auth!.needsAuth, true);
assert.equal(auth!.authUsername, host.username);
assert.equal(auth!.authMethod, "password");
assert.equal(auth!.authPassword, "saved-secret");
assert.equal(auth!.selectedIdentityId, null);
assert.equal(auth!.showAuthPassword, false);
assert.equal(auth!.isValid, true);
assert.equal(submissions, 0);
assert.equal(pendingAuthRef.current, null);
await act(async () => { auth!.setAuthPassword(""); });
assert.equal(auth!.authPassword, "");
assert.equal(auth!.isValid, false);
await act(async () => { auth!.submit({ saveToHost: false }); });
assert.equal(submissions, 0);
await act(async () => { auth!.setAuthPassword("corrected-secret"); });
assert.equal(auth!.isValid, true);
assert.equal(submissions, 0);
await act(async () => { auth!.submit({ saveToHost: false }); });
assert.equal(submissions, 1);
assert.equal(pendingAuthRef.current?.password, "corrected-secret");
assert.equal(host.password, "saved-secret");
} finally {
if (renderer) await act(async () => { renderer?.unmount(); });
if (previous === undefined) delete environment.IS_REACT_ACT_ENVIRONMENT;
else environment.IS_REACT_ACT_ENVIRONMENT = previous;
}
});
test("password save clears identityId and identityFileId", () => {
const updated = buildSavedAuthHostUpdate(baseHost, {
authMethod: "password",