Skip to content

YC (S26) | Open Computer History | Continuously record your company computer work, map your workflows, help you find work worth automating, and power your agents' context

fix(ws-auth): stop wiping IPC-loaded api key when settings.apiKey is empty

Louis Beaumontce8374893f19
diff --git a/apps/screenpipe-app-tauri/lib/api.ts b/apps/screenpipe-app-tauri/lib/api.ts
index 7cbbc2a2c..c99ba3998 100644
--- a/apps/screenpipe-app-tauri/lib/api.ts
+++ b/apps/screenpipe-app-tauri/lib/api.ts
@@ -178,14 +178,22 @@ export function configureApi(opts: {
authEnabled?: boolean;
}) {
if (opts.port !== undefined) _port = opts.port;
- if (opts.apiKey !== undefined) _apiKey = opts.apiKey || null;
+ // Only overwrite the cached key when the caller has a real value to set.
+ // settings.apiKey is empty for users on the auto-generated server key, and
+ // wiping _apiKey to null here would race with `ensureInitialized` and break
+ // every WS connection (cookie auth + ?token= both come from this same var).
+ if (opts.apiKey) _apiKey = opts.apiKey;
if (opts.authEnabled !== undefined) _authEnabled = opts.authEnabled;
_initialized = true;
- // Update auth cookie
+ // Update auth cookie. Only clear when auth is explicitly disabled — if it's
+ // enabled but _apiKey is momentarily null (init not finished), leave any
+ // existing cookie alone so ensureInitialized can rewrite it once the IPC
+ // returns. Clearing on every settings reload was the root cause of the WS
+ // 403 storm reported by users on April 21 2026.
if (_authEnabled && _apiKey) {
document.cookie = `screenpipe_auth=${_apiKey}; path=/; SameSite=Strict`;
- } else {
+ } else if (!_authEnabled) {
document.cookie = "screenpipe_auth=; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT";
}
}
diff --git a/apps/screenpipe-app-tauri/lib/hooks/use-settings.tsx b/apps/screenpipe-app-tauri/lib/hooks/use-settings.tsx
index 688b4c720..048591a72 100644
--- a/apps/screenpipe-app-tauri/lib/hooks/use-settings.tsx
+++ b/apps/screenpipe-app-tauri/lib/hooks/use-settings.tsx
@@ -685,11 +685,18 @@ export const SettingsProvider: React.FC<{ children: React.ReactNode }> = ({ chil
setIsSettingsLoaded(true);
setLoadingError(null);
- // Configure the API module — single source of truth for port + auth
+ // Configure the API module — single source of truth for port + auth.
+ // `apiKey` is intentionally NOT passed: `ensureInitialized` in
+ // lib/api.ts loads the canonical key from the server via IPC
+ // (`get_local_api_config`). settings.apiKey is a user preference
+ // fed to the server's auth resolver; the server then exposes the
+ // resolved key via that IPC. Passing it here would race with the
+ // IPC and overwrite a good key with `null` for the majority of
+ // users (who never set a custom api key) — which silently breaks
+ // every WebSocket auth path.
const { configureApi } = await import("@/lib/api");
configureApi({
port: loadedSettings.port ?? 3030,
- apiKey: loadedSettings.apiKey || null,
authEnabled: loadedSettings.apiAuth ?? true,
});
} catch (error) {
@@ -853,13 +860,18 @@ export const SettingsProvider: React.FC<{ children: React.ReactNode }> = ({ chil
await settingsStore.set(updates);
// Settings will be updated via the listener
- // Reconfigure API module if auth/port settings changed
+ // Reconfigure API module if auth/port settings changed.
+ // `apiKey` deliberately omitted: the server is the source of truth and
+ // the frontend learns the key via IPC (`get_local_api_config`). When
+ // the user changes their api_key preference here, the server picks it
+ // up on its next restart and the frontend will re-fetch the resolved
+ // value — passing `apiKey: null` here when settings.apiKey is empty
+ // would race with that IPC and wipe the cached key.
if ("port" in updates || "apiKey" in updates || "apiAuth" in updates) {
const { configureApi } = await import("@/lib/api");
const merged = { ...settings, ...updates };
configureApi({
port: merged.port ?? 3030,
- apiKey: merged.apiKey || null,
authEnabled: merged.apiAuth ?? true,
});
}