Skip to content

Commit 35fa1d7

Browse files
salman1993claude
andcommitted
fix(agents): reset API key reveal when the typed key goes away
Reveal state remembered the provider's env key name, so switching away and back, or clearing and retyping, showed the replacement key in plain text. Consent now covers one typed key and clears during render once that key is removed or the provider changes. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> Signed-off-by: Salman Mohammed <[email protected]>
1 parent bc1245e commit 35fa1d7

2 files changed

Lines changed: 23 additions & 5 deletions

File tree

‎src/bundled/agents/AgentSettingsFields.test.tsx‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,23 @@ it("reveals only a key typed for the current provider", async () => {
255255
const next = screen.getByLabelText("Anthropic API key");
256256
await user.type(next, "other-key");
257257
expect(next).toHaveAttribute("type", "password");
258+
259+
// Round trip: returning to the revealed provider must not re-expose a replacement.
260+
await user.click(screen.getByRole("button", { name: "Show API key" }));
261+
await user.click(screen.getByRole("combobox", { name: "LLM Provider" }));
262+
await user.click(await screen.findByRole("option", { name: "OpenAI" }));
263+
await user.click(screen.getByRole("combobox", { name: "LLM Provider" }));
264+
await user.click(await screen.findByRole("option", { name: "Anthropic" }));
265+
const returned = screen.getByLabelText("Anthropic API key");
266+
await user.type(returned, "replacement-key");
267+
expect(returned).toHaveAttribute("type", "password");
268+
269+
// Clearing the revealed key withdraws consent for whatever is typed next.
270+
await user.click(screen.getByRole("button", { name: "Show API key" }));
271+
expect(returned).toHaveAttribute("type", "text");
272+
await user.clear(returned);
273+
await user.type(returned, "retyped-key");
274+
expect(returned).toHaveAttribute("type", "password");
258275
} finally {
259276
view.unmount();
260277
control.dispose();

‎src/bundled/agents/AgentSettingsFields.tsx‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,11 @@ export function AgentSettingsFields({
7474
const apiKey = gooseProvider ? gooseApiKey(gooseProvider) : undefined;
7575
const savedKey = !!apiKey && environmentKeys.includes(apiKey.env);
7676
// Saved keys are write-only; only a key typed for this provider can be shown.
77-
const typedKey = !!apiKey && !!draft.environment[apiKey.env];
78-
const revealKey = typedKey && revealed === apiKey?.env;
77+
const typedKey = apiKey && draft.environment[apiKey.env] ? apiKey.env : null;
78+
// Reveal consent covers one typed key. Clear it during render once that key
79+
// is removed or the provider changes, so a replacement starts masked.
80+
if (revealed !== null && revealed !== typedKey) setRevealed(null);
81+
const revealKey = revealed !== null && revealed === typedKey;
7982
const change = (patch: Partial<AgentDraft>) => {
8083
const next = { ...draft, ...patch };
8184
const nextProvider = isGoose(next.command)
@@ -152,9 +155,7 @@ export function AgentSettingsFields({
152155
}
153156
size="sm"
154157
disabled={disabled}
155-
onClick={() =>
156-
setRevealed(revealKey ? null : apiKey.env)
157-
}
158+
onClick={() => setRevealed(revealKey ? null : typedKey)}
158159
/>
159160
) : undefined
160161
}

0 commit comments

Comments
 (0)