feat(security): encrypt API keys at rest via safeStorage#629
Open
gabrielste1n wants to merge 3 commits intomainfrom
Open
feat(security): encrypt API keys at rest via safeStorage#629gabrielste1n wants to merge 3 commits intomainfrom
gabrielste1n wants to merge 3 commits intomainfrom
Conversation
Closes #532. Migrates the 12 secret-class env vars (7 AI API keys + 5 AWS/Azure/Vertex credentials) from plaintext .env / localStorage to per-key encrypted files in userData/secure-keys/. Non-secret preferences (regions, endpoints, hotkeys, flags) continue to live in .env. Main process: - EnvironmentManager.init() runs once post-whenReady. First launch migrates any existing plaintext secrets with round-trip verification before stripping .env; a sentinel file makes the migration idempotent and re-tryable on partial failure. - _saveKey routes SECRET_KEYS through safeStorage.encryptString with atomic temp+rename writes; process.env is updated synchronously so in-process reads see the new value immediately. - saveAllKeysToEnvFile excludes secrets from .env output whenever safeStorage.isEncryptionAvailable() is true; falls back to plaintext on Linux systems without a keyring (matches Electron's default behavior). - Corrupt .enc files (e.g., keychain reset, userData copied between machines) log an error and leave process.env unset rather than crashing. Renderer: - Secret setters drop localStorage writes and debounce IPC saves at 250ms to avoid per-keystroke encryption + disk writes. - initializeSettings hydrates all 12 secrets in parallel from the main process and removes stale plaintext copies from localStorage (self- healing on every startup). - byokDetection reads Zustand state instead of localStorage; the cloudTranscriptionMode BYOK default is promoted to post-hydration. - Removes apiKeyPreview debug log leaks in audioManager and ReasoningService.
- Replace 40-line switch for secret IPC savers with a simple lookup map. - Inline single-use _rewriteEnvFileWithoutSecrets helper. - Shorten explanatory comments to keep only the non-obvious WHYs.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
API keys (OpenAI, Anthropic, Gemini, Groq, Mistral, custom endpoints, AWS Bedrock, Azure OpenAI, GCP Vertex) have been sitting in plaintext — half in
userData/.env, half in rendererlocalStorage. Anyone on the machine reading either file pulled usable secrets.What changes
Secrets now live in per-key encrypted blobs under
userData/secure-keys/via Electron'ssafeStorage(Keychain on macOS, DPAPI on Windows, libsecret/kwallet on Linux). Non-secret preferences stay in.envwhere they belong. The renderer holds secrets only in Zustand memory and hydrates from main on startup.On first launch after upgrade, any existing plaintext secrets migrate automatically — round-trip verified before
.envis rewritten, with a sentinel file so partial failures are retried cleanly rather than silently half-completing. StalelocalStoragecopies self-heal on every boot.Also removed a few long-standing
apiKeyPreviewdebug-log leaks that printed the first 8 chars of secrets — effectively leaking the full key for short-prefix providers like Groq.Compatibility
No IPC surface changes, no new deps, no UI changes. Existing users with plaintext
.envsecrets migrate transparently on first run. Linux systems without a configured keyring fall back to plaintext (matching Electron's own behavior) and log a warning; SECURITY.md will reflect that gap.Closes #532.