fix(desktop): drop unsaved credential edits when the settings target changes

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.
This commit is contained in:
DavidMetcalfe
2026-09-13 23:00:46 -07:00
committed by Teknium
parent 7c47e5517b
commit 6201a8236f
2 changed files with 58 additions and 0 deletions
@@ -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 {
@@ -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(
<MemoryRouter initialEntries={['/settings']}>
<KeysSettings view="tools" />
</MemoryRouter>
)
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)
}
})
})