Skip to content

7.4 billion tokens per month. 34 free LLM providers. 635 free model endpoints. All behind one /v1 endpoint, plus any custom OpenAI-compatible endpoint. Smart routing, automatic failover, encrypted keys. Personal experimentation only.

TypeScript

fix(keys): two-step confirm before revoking the unified API key (#1316)

tashdroid40d3b18d4e5f
diff --git a/client/src/components/keys/unified-key-section.test.tsx b/client/src/components/keys/unified-key-section.test.tsx
new file mode 100644
index 00000000..aad807ff
--- /dev/null
+++ b/client/src/components/keys/unified-key-section.test.tsx
@@ -0,0 +1,129 @@
+// @vitest-environment jsdom
+import { afterEach, beforeAll, beforeEach, expect, it, vi } from 'vitest'
+import { act } from 'react'
+import { createRoot, type Root } from 'react-dom/client'
+import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
+import { I18nProvider } from '@/i18n'
+import { apiFetch } from '@/lib/api'
+import { UnifiedKeySection } from './unified-key-section'
+
+vi.mock('@/lib/api', () => ({ apiFetch: vi.fn() }))
+vi.mock('@/lib/toast', () => ({ toast: { success: vi.fn(), error: vi.fn() } }))
+
+let root: Root
+let container: HTMLDivElement
+
+beforeAll(() => {
+ ;(globalThis as unknown as { IS_REACT_ACT_ENVIRONMENT: boolean }).IS_REACT_ACT_ENVIRONMENT = true
+ // The component builds its dev-mode base URL from the vite-injected port.
+ ;(globalThis as unknown as Record<string, unknown>).__SERVER_PORT__ = '3001'
+})
+
+beforeEach(() => {
+ vi.mocked(apiFetch).mockReset()
+ container = document.createElement('div')
+ document.body.appendChild(container)
+ root = createRoot(container)
+})
+
+afterEach(() => {
+ act(() => root.unmount())
+ container.remove()
+})
+
+async function flush() {
+ for (let i = 0; i < 6; i++) await act(async () => { await new Promise(r => setTimeout(r, 0)) })
+}
+
+function mount() {
+ const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false }, mutations: { retry: false } } })
+ act(() => root.render(
+ <QueryClientProvider client={queryClient}>
+ <I18nProvider initialLocale="en"><UnifiedKeySection /></I18nProvider>
+ </QueryClientProvider>,
+ ))
+ return flush()
+}
+
+function findButton(label: string): HTMLButtonElement | undefined {
+ return [...container.querySelectorAll('button')].find(b => b.textContent?.trim() === label)
+}
+
+async function click(el: HTMLElement) {
+ await act(async () => { el.dispatchEvent(new MouseEvent('click', { bubbles: true })) })
+ await flush()
+}
+
+it('does not regenerate the key on a single click', async () => {
+ vi.mocked(apiFetch).mockImplementation(async path =>
+ path === '/api/settings/api-key' ? { apiKey: 'sk-fre…e-key' } : { ok: true })
+ await mount()
+
+ await click(findButton('Regenerate')!)
+ // One click only ARMS the button (the dashboard's destructive idiom); the
+ // endpoint that revokes the live key must not have been hit yet.
+ expect(apiFetch).not.toHaveBeenCalledWith('/api/settings/api-key/regenerate', expect.anything())
+ expect(findButton('Confirm')).toBeTruthy()
+})
+
+it('regenerates on the confirming click and reveals the fresh key', async () => {
+ const calls = { regenerate: 0 }
+ vi.mocked(apiFetch).mockImplementation(async path => {
+ if (path === '/api/settings/api-key') {
+ return { apiKey: calls.regenerate ? 'sk-fresh-key-after-regen' : 'sk-old-key-before-regen' }
+ }
+ if (path === '/api/settings/api-key/regenerate') { calls.regenerate++; return { apiKey: 'sk-fresh-key-after-regen' } }
+ return { ok: true }
+ })
+ await mount()
+
+ await click(findButton('Regenerate')!)
+ await click(findButton('Confirm')!)
+ expect(calls.regenerate).toBe(1)
+
+ // The fresh key is shown unmasked right after: the user is about to paste
+ // it into every app that still holds the revoked one.
+ const code = container.querySelector('code.select-all')!
+ expect(code.textContent).toContain('sk-fresh-key-after-regen')
+})
+
+it('keeps the key masked when the user disarms the confirmation', async () => {
+ vi.mocked(apiFetch).mockImplementation(async path =>
+ path === '/api/settings/api-key' ? { apiKey: 'sk-secret-value-123' } : { ok: true })
+ await mount()
+
+ await click(findButton('Regenerate')!)
+ // Escape disarms the armed button without firing anything.
+ await act(async () => {
+ findButton('Confirm')!.dispatchEvent(new KeyboardEvent('keydown', { key: 'Escape', bubbles: true }))
+ })
+ await flush()
+ expect(apiFetch).not.toHaveBeenCalledWith('/api/settings/api-key/regenerate', expect.anything())
+ expect(container.querySelector('code.select-all')!.textContent).not.toContain('sk-secret-value-123')
+})
+
+it('never shows the revoked key unmasked while the new one loads', async () => {
+ // The GET refetch never resolves after regeneration: the fresh key must
+ // come straight from the regenerate response, not from a refetch.
+ let regenerated = false
+ vi.mocked(apiFetch).mockImplementation(async path => {
+ if (path === '/api/settings/api-key') {
+ return regenerated ? new Promise(() => {}) : { apiKey: 'sk-old-key-before-regen' }
+ }
+ if (path === '/api/settings/api-key/regenerate') { regenerated = true; return { apiKey: 'sk-fresh-key-after-regen' } }
+ return { ok: true }
+ })
+ await mount()
+
+ const seen: string[] = []
+ const code = () => container.querySelector('code.select-all')!
+ const observer = new MutationObserver(() => seen.push(code().textContent ?? ''))
+ observer.observe(container, { subtree: true, characterData: true, childList: true })
+
+ await click(findButton('Regenerate')!)
+ await click(findButton('Confirm')!)
+ observer.disconnect()
+
+ expect(seen.some(text => text.includes('sk-old-key-before-regen'))).toBe(false)
+ expect(code().textContent).toContain('sk-fresh-key-after-regen')
+})
diff --git a/client/src/components/keys/unified-key-section.tsx b/client/src/components/keys/unified-key-section.tsx
index c37b5da0..fd05dc73 100644
--- a/client/src/components/keys/unified-key-section.tsx
+++ b/client/src/components/keys/unified-key-section.tsx
@@ -4,6 +4,7 @@ import { apiFetch } from '@/lib/api'
import { copyText } from '@/lib/clipboard'
import { toast } from '@/lib/toast'
import { Button } from '@/components/ui/button'
+import { ConfirmButton } from '@/components/confirm-button'
import { useI18n } from '@/i18n'
export function UnifiedKeySection() {
@@ -18,8 +19,18 @@ export function UnifiedKeySection() {
})
const regenerate = useMutation({
- mutationFn: () => apiFetch('/api/settings/api-key/regenerate', { method: 'POST' }),
- onSuccess: () => queryClient.invalidateQueries({ queryKey: ['unified-key'] }),
+ mutationFn: () => apiFetch<{ apiKey: string }>('/api/settings/api-key/regenerate', { method: 'POST' }),
+ onSuccess: (res) => {
+ // Write the fresh key into the cache BEFORE unmasking: invalidating
+ // alone would leave the old, now-revoked key in the cache until the
+ // refetch lands, and it would flash unmasked in the meantime.
+ queryClient.setQueryData<{ apiKey: string }>(['unified-key'], { apiKey: res.apiKey })
+ // Reveal the fresh key right away: the whole point of regenerating is
+ // to move apps to the new value, and a masked box hides exactly the
+ // string they need to copy next. Showing it doubles as the success
+ // feedback, so no toast (and no new key in 60 locale files) is needed.
+ setShowKey(true)
+ },
})
const apiKey = data?.apiKey ?? ''
@@ -46,14 +57,19 @@ export function UnifiedKeySection() {
{t('keys.unifiedKeyDescBefore')}<code className="font-mono">api_key</code>{t('keys.unifiedKeyDescAfter')}
</p>
</div>
- <Button
+ {/* Regenerating revokes the key every running app authenticates with,
+ so it goes through the dashboard's two-step destructive idiom
+ (ConfirmButton) instead of firing on a single click. */}
+ <ConfirmButton
variant="ghost"
size="sm"
- onClick={() => regenerate.mutate()}
+ // Armed label stays the shared "Confirm" — the same destructive
+ // idiom every other ConfirmButton on the dashboard uses.
disabled={regenerate.isPending || isError}
+ onConfirm={() => regenerate.mutate()}
>
{t('keys.regenerate')}
- </Button>
+ </ConfirmButton>
</div>
{isError ? (