Skip to content

armgit / mimik

Public Imported from GitHub
0

🎬 A browser extension and desktop app that turns your clicks into step-by-step guides with annotated screenshots πŸ“–

fix(ai): time out key validation on an unresponsive host

Luis Ortizdedcf968b015
diff --git a/src/core/capture/ai/__tests__/validate.test.ts b/src/core/capture/ai/__tests__/validate.test.ts
index ea4ef86..47927d8 100644
--- a/src/core/capture/ai/__tests__/validate.test.ts
+++ b/src/core/capture/ai/__tests__/validate.test.ts
@@ -59,6 +59,13 @@ describe('validateApiKey', () => {
expect(init.headers.Authorization).toBe('Bearer gsk-key');
});
+ it('gives up rather than spinning forever when a host never answers', async () => {
+ fetchMock.mockResolvedValue({ ok: true, status: 200 });
+ await validateApiKey('openai', 'sk-key');
+ const [, init] = fetchMock.mock.calls[0];
+ expect(init.signal).toBeInstanceOf(AbortSignal);
+ });
+
it('never sends a key to an unknown provider, and does not call it rejected', async () => {
expect(await validateApiKey('mystery', 'secret')).toEqual({ valid: false, reason: 'network' });
expect(fetchMock).not.toHaveBeenCalled();
diff --git a/src/core/capture/ai/validate.ts b/src/core/capture/ai/validate.ts
index 9fc2ae2..1aa7f09 100644
--- a/src/core/capture/ai/validate.ts
+++ b/src/core/capture/ai/validate.ts
@@ -2,6 +2,8 @@ import { logger } from '@/lib/logger';
export type KeyValidation = { valid: true } | { valid: false; reason: 'rejected' | 'network' };
+const REQUEST_TIMEOUT_MS = 10_000;
+
const ENDPOINTS: Record<string, { url: string; headers: (key: string) => Record<string, string> }> = {
openai: {
url: 'https://api.openai.com/v1/models',
@@ -28,7 +30,10 @@ export async function validateApiKey(provider: string, apiKey: string): Promise<
return { valid: false, reason: 'network' };
}
try {
- const res = await fetch(endpoint.url, { headers: endpoint.headers(apiKey) });
+ const res = await fetch(endpoint.url, {
+ headers: endpoint.headers(apiKey),
+ signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS),
+ });
if (res.ok) return { valid: true };
if (res.status === 401 || res.status === 403) return { valid: false, reason: 'rejected' };
return { valid: false, reason: 'network' };