Skip to content

feat: encrypt user AI API keys at rest - #69

Closed
hacksynth wants to merge 1 commit into
mainfrom
claude/issue-53-20260502-1634
Closed

hacksynth wants to merge 1 commit into
mainfrom
claude/issue-53-20260502-1634

Conversation

@hacksynth

Copy link
Copy Markdown
Owner

Closes #53

Summary

  • Add encrypted API key fields and AES-256-GCM helpers.
  • Require OSE_ENCRYPTION_KEY for saving new user-provided API keys; reject unsafe writes instead of falling back to plaintext.
  • Keep legacy plaintext columns only for compatibility/lazy migration, and document key setup/backup guidance.

Review notes

  • Opened from Claude branch claude/issue-53-20260502-1634 after checking the write path rejects missing/invalid encryption config for new key saves.

- Add src/lib/crypto/secrets.ts: AES-256-GCM helpers (encryptSecret,
  decryptSecret, resolveSecret, isEncryptionEnabled, isEncryptedSecret)
- Add apiKeyEncrypted / imageApiKeyEncrypted columns to UserAISettings
  via migration 20260502140000_add_encrypted_api_key_fields
- PUT /api/profile/ai-settings: reject new key submissions with HTTP 503
  when OSE_ENCRYPTION_KEY is absent or invalid; never write plaintext
- GET /api/profile/ai-settings: resolve hasApiKey/masked from encrypted
  columns first, falling back to legacy plaintext for display only
- Read paths (resolveAIConfig, resolveAIImageConfig,
  resolveAIImageConfigFromRequest, resolveAIConfigFromRequest): decrypt
  via resolveSecret; lazy-migrate any legacy plaintext key to encrypted
  storage on first use when OSE_ENCRYPTION_KEY is available
- Without OSE_ENCRYPTION_KEY, legacy plaintext keys are treated as
  unconfigured (not used); users must set the env var and re-enter keys
- Add tests: src/test/crypto-secrets.test.ts (round-trip, unicode,
  tamper resistance, resolveSecret fallback behaviour)
- Update .env.example and docs/self-hosting.md with key setup,
  rotation, and backup guidance

Closes #53

Co-authored-by: hacksynth <hacksynth@users.noreply.github.com>
@hacksynth

Copy link
Copy Markdown
Owner Author

Superseded by #72, which merged the strict API-key encryption fix from #53 together with the remaining issue fixes. Closing this conflicted Claude PR.

@hacksynth hacksynth closed this May 2, 2026
@hacksynth
hacksynth deleted the claude/issue-53-20260502-1634 branch May 3, 2026 01:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Encrypt stored user AI API keys at rest

1 participant