Files
Netcatty/components/terminal/useTerminalAuthState.test.ts
T
陈大猫 6400ce1e75 fix(terminal): detach stale keychain identity when saving retry credentials (#1970)
* fix(terminal): detach stale keychain identity when saving retry credentials

When a host references a Keychain identity (host.identityId) with wrong
credentials, re-entering credentials via the auth-retry dialog's
"Continue & Save" saved them onto the host but kept the identity
reference. On reconnect, resolveHostAuth prefers identity credentials
over host fields, so the stale keychain credentials kept failing.

Clear identityId when the user explicitly saves new credentials so the
saved username/password (or key) actually takes effect.

Fixes #1956

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix(terminal): keep saved retry credentials over group-inherited identity

Writing identityId: undefined is treated as "unset" by applyGroupDefaults,
so a group-level identity would be re-inherited on reconnect and shadow the
credentials saved from the auth-retry dialog. Store an empty string instead
and register identityId in EMPTY_STRING_OVERRIDES_GROUP_DEFAULT so the
explicit "no identity" override sticks. All consumers check identityId
truthiness, so "" behaves as "no identity".

Addresses Codex review on #1970.

Co-authored-by: Cursor <cursoragent@cursor.com>

---------

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-07-06 19:50:33 +08:00

114 lines
2.8 KiB
TypeScript

import assert from "node:assert/strict";
import test from "node:test";
import { applyGroupDefaults } from "../../domain/groupConfig";
import { resolveHostAuth } from "../../domain/sshAuth";
import type { Host, Identity } from "../../types";
import { buildSavedAuthHostUpdate } from "./hooks/useTerminalAuthState";
const baseHost: Host = {
id: "host-1",
label: "Test Host",
hostname: "example.com",
port: 22,
username: "olduser",
authMethod: "key",
identityId: "identity-1",
identityFileId: "key-1",
tags: [],
os: "linux",
};
test("password save clears identityId and identityFileId", () => {
const updated = buildSavedAuthHostUpdate(baseHost, {
authMethod: "password",
username: "root",
password: "secret",
keyId: null,
});
assert.equal(updated.identityId, "");
assert.equal(updated.identityFileId, undefined);
assert.equal(updated.password, "secret");
assert.equal(updated.savePassword, true);
assert.equal(updated.authMethod, "password");
assert.equal(updated.username, "root");
});
test("key save clears identityId and sets identityFileId", () => {
const updated = buildSavedAuthHostUpdate(baseHost, {
authMethod: "key",
username: "deploy",
password: "",
keyId: "key-2",
});
assert.equal(updated.identityId, "");
assert.equal(updated.identityFileId, "key-2");
assert.equal(updated.password, undefined);
});
test("resolveHostAuth uses saved host credentials after identityId is cleared", () => {
const identity: Identity = {
id: "identity-1",
label: "old",
username: "olduser",
authMethod: "password",
password: "wrong",
created: 0,
};
const updated = buildSavedAuthHostUpdate(
{ ...baseHost, identityId: "identity-1" },
{
authMethod: "password",
username: "root",
password: "correct",
keyId: null,
},
);
const resolved = resolveHostAuth({
host: updated,
keys: [],
identities: [identity],
});
assert.equal(resolved.username, "root");
assert.equal(resolved.password, "correct");
});
test("saved credentials override a group-inherited identity", () => {
const identity: Identity = {
id: "identity-1",
label: "old",
username: "olduser",
authMethod: "password",
password: "wrong",
created: 0,
};
const updated = buildSavedAuthHostUpdate(
{ ...baseHost, identityId: undefined },
{
authMethod: "password",
username: "root",
password: "correct",
keyId: null,
},
);
const effective = applyGroupDefaults(updated, { identityId: "identity-1" });
assert.equal(effective.identityId, "");
const resolved = resolveHostAuth({
host: effective,
keys: [],
identities: [identity],
});
assert.equal(resolved.username, "root");
assert.equal(resolved.password, "correct");
});