From 6201a8236fd5c46de003da5932a906c8f014be54 Mon Sep 17 00:00:00 2001 From: DavidMetcalfe <80915+DavidMetcalfe@users.noreply.github.com> Date: Sun, 13 Sep 2026 23:00:46 -0700 Subject: [PATCH] fix(desktop): drop unsaved credential edits when the settings target changes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The shared Settings "Applies to" target re-fetches `vars`, but the in-flight edit and revealed maps are keyed by var name alone and were not reset with it. A value typed while targeting one profile therefore survived a switch to another, where the still-live Save button wrote it into the profile then being targeted — the credential landed in the wrong profile. --- .../src/app/settings/env-credentials.tsx | 7 +++ .../src/app/settings/keys-settings.test.tsx | 51 +++++++++++++++++++ 2 files changed, 58 insertions(+) diff --git a/apps/desktop/src/app/settings/env-credentials.tsx b/apps/desktop/src/app/settings/env-credentials.tsx index 490d08c066..8be974dc68 100644 --- a/apps/desktop/src/app/settings/env-credentials.tsx +++ b/apps/desktop/src/app/settings/env-credentials.tsx @@ -68,7 +68,14 @@ export function useEnvCredentials(profile?: string): UseEnvCredentials { useEffect(() => { let cancelled = false + // Everything keyed by var name is dropped together with the reload: the + // cached `vars`, plus any in-flight edit or revealed value. Those maps are + // keyed by name alone, so leaving a draft behind after the target profile + // changed left its Save button live — writing the value into the profile + // now being targeted instead of the one it was typed for. setVars(null) + setEdits({}) + setRevealed({}) void (async () => { try { diff --git a/apps/desktop/src/app/settings/keys-settings.test.tsx b/apps/desktop/src/app/settings/keys-settings.test.tsx index 699fd0e4a7..4d3f4b0dc9 100644 --- a/apps/desktop/src/app/settings/keys-settings.test.tsx +++ b/apps/desktop/src/app/settings/keys-settings.test.tsx @@ -126,4 +126,55 @@ describe('KeysSettings', () => { }) expect(screen.getByText('Crawl and extract websites.')).toBeTruthy() }) + + it('drops an unsaved credential edit when the settings target switches profile', async () => { + // Regression: `vars` is re-fetched when the shared "Applies to" target + // changes, but the in-flight edit map was not reset with it. A value typed + // while targeting profile-b survived the switch to profile-c, where the + // still-live Save would persist it into the WRONG profile. + const { $settingsScopeOverride } = await import('@/store/settings-scope') + + $settingsScopeOverride.set('profile-b') + getEnvVars.mockResolvedValue({ + WIDGET_API_KEY: envVar('tool', { description: 'Widget key.', is_set: true, redacted_value: '••••••' }) + }) + + try { + const { KeysSettings } = await import('./keys-settings') + + const { container } = render( + + + + ) + + expect(await screen.findByText('WIDGET')).toBeTruthy() + await waitFor(() => expect(getEnvVars).toHaveBeenCalledWith('profile-b')) + + // Open the field and type a value without saving it. + fireEvent.focus(container.querySelector('input[readonly]') as HTMLInputElement) + fireEvent.change(container.querySelector('input[type="password"]') as HTMLInputElement, { + target: { value: 'typed-secret' } + }) + + expect(screen.getByDisplayValue('typed-secret')).toBeTruthy() + + // Re-target Settings at another profile. This is where the leak + // manifested: the draft stayed live, so the (still-rendered) Save would + // dispatch it through setEnvVar against the NEW target. + await act(async () => { + $settingsScopeOverride.set('profile-c') + }) + await waitFor(() => expect(getEnvVars).toHaveBeenCalledWith('profile-c')) + + // The draft belonged to the previous target: it is gone, and so is the + // Save control that would have dispatched it — no path is left that can + // write the stale value into the profile now being targeted. + expect(screen.queryByDisplayValue('typed-secret')).toBeNull() + expect(screen.queryByRole('button', { name: 'Save' })).toBeNull() + } finally { + cleanup() + $settingsScopeOverride.set(null) + } + }) })