Repository navigation
feat: store AI service API keys in the OS secret store - #2799
Open
drewster99 wants to merge 3 commits into
Open
drewster99 wants to merge 3 commits into
drewster99 wants to merge 3 commits into
Conversation
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
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.
Addresses #2798.
Problem
Each AI service (
src/AI/Service.cs) holds anApiKey, and the wholePreferencesobject is serialized topreference.jsonin cleartext, so the key is written to disk unprotected. These keys are usually billable.Change
Adds a small secret-store abstraction to the
Nativebackend (OS.SupportsSecretStorage/ReadSecret/WriteSecret/DeleteSecret):Security.framework(src/Native/MacOSKeychain.cs), keyed by a stable per-serviceId. The key is never written topreference.json.Servicekeeps 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 frompreference.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.Idpersisted + 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 realServicecode and a live Keychain with a local integration harness. All checks pass.dotnet build -c Debuganddotnet publish -c Release -r osx-arm64(NativeAOT) both succeed with no new warnings.dotnet format --verify-no-changes src/SourceGit.csprojis 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.