Skip to content

feat: store AI service API keys in the OS secret store - #2799

Open
drewster99 wants to merge 3 commits into
sourcegit-scm:masterfrom
drewster99:fix/ai-apikey-secret-store
Open

drewster99 wants to merge 3 commits into
sourcegit-scm:masterfrom
drewster99:fix/ai-apikey-secret-store

Conversation

@drewster99

Copy link
Copy Markdown

Addresses #2798.

Problem

Each AI service (src/AI/Service.cs) holds an ApiKey, and the whole Preferences object is serialized to preference.json in cleartext, so the key is written to disk unprotected. These keys are usually billable.

Change

Adds a small secret-store abstraction to the Native backend (OS.SupportsSecretStorage/ReadSecret/WriteSecret/DeleteSecret):

  • macOS: stores keys as generic-password items in the login Keychain via Security.framework (src/Native/MacOSKeychain.cs), keyed by a stable per-service Id. The key is never written to preference.json.
  • Windows / Linux: clearly marked stubs that report no secret store, so those platforms keep today's behavior until a maintainer backs them with Credential Manager / libsecret. (I can only build and test the macOS backend; flagging the other two for someone who can.)

Service keeps the effective key in memory (bound by the UI) and persists an empty value under "ApiKey" where a secret store exists. The env-var-name mode is unchanged, since an env var name is not itself a secret. On startup, any existing plaintext key is migrated into the secret store and scrubbed from preference.json (Preferences.MigrateAISecrets, called after preferences become modifiable). Deleting a service removes its stored key.

Backward compatible: the on-disk JSON key stays "ApiKey", so existing configs load and are migrated transparently.

Tests / verification

  • tools/keychain-roundtrip-test (added) exercises the macOS backend: store / read / update / delete with quotes, backslashes and non-ASCII, asserting an exact round-trip. Passes.
  • Service-level behavior (new key scrubbed from JSON + Id persisted + key in store; reload resolves from the store; legacy plaintext migrates and scrubs; env-var name still persists; delete removes the stored key) was validated against the real Service code and a live Keychain with a local integration harness. All checks pass.
  • dotnet build -c Debug and dotnet publish -c Release -r osx-arm64 (NativeAOT) both succeed with no new warnings.
  • dotnet format --verify-no-changes src/SourceGit.csproj is clean.

Note

On macOS, rebuilding with a different code signature can make the Keychain prompt once to allow access to an existing item; this is expected macOS behavior for a re-signed app.

API keys for AI services were serialized into preference.json in
cleartext, so anything able to read the user's config directory could
read them. These keys are usually billable.

Add a secret-store abstraction to the Native backend:

- macOS: store keys as generic-password items in the login Keychain
  (Security.framework), keyed by a stable per-service Id. The key is
  never written to preference.json.
- Windows and Linux: clearly marked stubs that report no secret store,
  so those platforms keep the current behavior until a maintainer backs
  them with Credential Manager / libsecret.

Service keeps the effective key in memory (bound by the UI) and persists
an empty value on platforms with a secret store; the env-var-name mode
is unchanged, since the name is not itself a secret. On startup any
existing plaintext key is migrated into the secret store and scrubbed
from preference.json. Deleting a service removes its stored key.

Add tools/keychain-roundtrip-test covering the macOS backend.
PersistedApiKey previously returned empty on any platform with a secret
store, so if WriteSecret failed the key was scrubbed from preference.json
yet never stored, losing it. Track whether the key is confirmed in the
store and only omit it from disk then; otherwise fall back to persisting
it, so a failed write cannot lose the key.
- Clear a service's API key field when the user switches it to read the
  key from an environment variable, so a real secret that was in the
  secret store is never written back to preference.json in plaintext.
  Handled on the checkbox Click (user action only), so loading a saved
  service is unaffected.
- ResolveSecret now reports "needs save to scrub" only when the store
  write actually succeeded, so a failed write no longer re-saves the
  plaintext copy on every launch.
- MacOSKeychain: route all CFRelease through a NULL-guarded helper and
  bail from CreateDictionary if any component handle is null, removing
  CFRetain(NULL)/CFRelease(NULL) crash paths; fall back to update on
  errSecDuplicateItem; drop a dead empty branch and an unused constant.

This branch has not been deployed

No deployments
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.

1 participant