-
-
Notifications
You must be signed in to change notification settings - Fork 5
ADFA-5270 | Adopt shared secret store, fix terminal-tool dedup, and cover MCP flows #80
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jatezzz
wants to merge
7
commits into
main
Choose a base branch
from
refactor/ADFA-5270-adopt-shared-secret-store
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
af606a7
refactor(ai): centralize plugin crypto on the host KeystoreSecretStore
jatezzz 03a81e2
fix(ai): stop Unavailable secrets being treated as lost or overwritable
jatezzz efe0f60
fix(ai): keep Unavailable secrets out of the test and second-save paths
jatezzz 3dfeabf
Merge branch 'main' into refactor/ADFA-5270-adopt-shared-secret-store
jatezzz 8a06a9a
fix(ai): correct the IDE floor and stop unread headers being replaced
jatezzz b3af7f6
Merge branch 'main' into refactor/ADFA-5270-adopt-shared-secret-store
jatezzz 154803f
fix(ai): keep the agent's answer and stop the token field speaking fo…
jatezzz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
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
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
151 changes: 11 additions & 140 deletions
151
...src/main/kotlin/com/itsaky/androidide/plugins/aiagentgemini/security/SecureApiKeyStore.kt
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,146 +1,17 @@ | ||
| package com.itsaky.androidide.plugins.aiagentgemini.security | ||
|
|
||
| import android.content.SharedPreferences | ||
| import android.security.keystore.KeyGenParameterSpec | ||
| import android.security.keystore.KeyPermanentlyInvalidatedException | ||
| import android.security.keystore.KeyProperties | ||
| import android.util.Base64 | ||
| import android.util.Log | ||
| import com.itsaky.androidide.plugins.aiagentgemini.logging.LOG_PREFIX | ||
| import java.security.GeneralSecurityException | ||
| import java.security.KeyStore | ||
| import javax.crypto.Cipher | ||
| import javax.crypto.KeyGenerator | ||
| import javax.crypto.SecretKey | ||
| import javax.crypto.spec.GCMParameterSpec | ||
| import com.itsaky.androidide.plugins.security.KeystoreSecretStore | ||
|
|
||
| /** Unique to this plugin and fixed across releases; see [KeystoreSecretStore] for why both matter. */ | ||
| private const val ALIAS = "cotg_ai_gemini_key_v1" | ||
|
|
||
| /** | ||
| * AES/GCM encryption for sensitive settings (currently the Gemini API key), | ||
| * keyed by a hardware-backed Android Keystore secret. Only ciphertext is | ||
| * written to SharedPreferences, so a copied prefs file (root, `adb backup`, | ||
| * forensic dump) is useless without this device's Keystore. | ||
| * This plugin's binding of [KeystoreSecretStore]: its API key, encrypted under this plugin's own | ||
| * Keystore alias. | ||
| * | ||
| * The [ALIAS] must stay stable across releases: a key encrypted under one | ||
| * alias cannot be read under another, so changing it silently invalidates | ||
| * every stored key. It is also what lets a key written before the AI plugins | ||
| * were reorganised still decrypt today — every plugin runs in the host app's | ||
| * process and UID, so they all share one Android Keystore. | ||
| * The store is the IDE's, from plugin-api, and callers use it directly. A forwarding object per | ||
| * method would only be a second copy of its contract to keep in step — and one that had to pick a | ||
| * single answer for "absent" and "no longer decryptable", which callers here do not share. The | ||
| * thing this file owns is the alias. | ||
| */ | ||
| object SecureApiKeyStore { | ||
| private const val TAG = "$LOG_PREFIX.SecureApiKeyStore" | ||
| private const val KEYSTORE = "AndroidKeyStore" | ||
| private const val ALIAS = "cotg_ai_gemini_key_v1" | ||
| private const val TRANSFORM = "AES/GCM/NoPadding" | ||
| private const val IV_LEN = 12 | ||
| private const val TAG_BITS = 128 | ||
|
|
||
| /** Marks a stored value as ciphertext; anything without it is treated as legacy plaintext. */ | ||
| const val ENC_PREFIX = "enc:v1:" | ||
|
|
||
| private fun getOrCreateKey(): SecretKey { | ||
| val ks = KeyStore.getInstance(KEYSTORE).apply { load(null) } | ||
| (ks.getEntry(ALIAS, null) as? KeyStore.SecretKeyEntry)?.let { return it.secretKey } | ||
| val generator = KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES, KEYSTORE) | ||
| generator.init( | ||
| KeyGenParameterSpec.Builder( | ||
| ALIAS, | ||
| KeyProperties.PURPOSE_ENCRYPT or KeyProperties.PURPOSE_DECRYPT | ||
| ) | ||
| .setBlockModes(KeyProperties.BLOCK_MODE_GCM) | ||
| .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_NONE) | ||
| .build() | ||
| ) | ||
| return generator.generateKey() | ||
| } | ||
|
|
||
| private fun deleteKey() { | ||
| try { | ||
| KeyStore.getInstance(KEYSTORE).apply { load(null) }.deleteEntry(ALIAS) | ||
| } catch (e: Exception) { | ||
| Log.w(TAG, "Failed to delete Keystore alias $ALIAS", e) | ||
| } | ||
| } | ||
|
|
||
| private fun encryptWith(key: SecretKey, plain: String): String { | ||
| val cipher = Cipher.getInstance(TRANSFORM) | ||
| cipher.init(Cipher.ENCRYPT_MODE, key) | ||
| val iv = cipher.iv | ||
| val ciphertext = cipher.doFinal(plain.toByteArray(Charsets.UTF_8)) | ||
| val combined = ByteArray(iv.size + ciphertext.size) | ||
| System.arraycopy(iv, 0, combined, 0, iv.size) | ||
| System.arraycopy(ciphertext, 0, combined, iv.size, ciphertext.size) | ||
| return ENC_PREFIX + Base64.encodeToString(combined, Base64.NO_WRAP) | ||
| } | ||
|
|
||
| /** | ||
| * Encrypt [plain] into a self-describing string: [ENC_PREFIX] + base64(iv | ciphertext). | ||
| * | ||
| * The key is not auth-bound, so a credential change does not invalidate it; an alias an | ||
| * OEM Keystore drops anyway is regenerated once before retrying. | ||
| * | ||
| * @param plain the value to encrypt | ||
| * @throws GeneralSecurityException on any other Keystore/cipher failure, so the caller can | ||
| * inform the user instead of crashing the IDE on Save | ||
| */ | ||
| @Throws(GeneralSecurityException::class) | ||
| fun encrypt(plain: String): String { | ||
| return try { | ||
| encryptWith(getOrCreateKey(), plain) | ||
| } catch (e: KeyPermanentlyInvalidatedException) { | ||
| Log.w(TAG, "Keystore key invalidated; regenerating and retrying encrypt", e) | ||
| deleteKey() | ||
| encryptWith(getOrCreateKey(), plain) | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Return the plaintext for a stored value, handling both formats transparently: | ||
| * an [ENC_PREFIX] value is decrypted; anything else is returned unchanged as | ||
| * legacy plaintext (use [readAndMigrate] to upgrade it in place). Returns | ||
| * null if a ciphertext value can't be decrypted — e.g. the Keystore key was | ||
| * lost or invalidated — in which case the user must re-enter the key. | ||
| */ | ||
| fun decrypt(stored: String?): String? { | ||
| if (stored == null) return null | ||
| if (!stored.startsWith(ENC_PREFIX)) return stored | ||
| return try { | ||
| val combined = Base64.decode(stored.removePrefix(ENC_PREFIX), Base64.NO_WRAP) | ||
| val iv = combined.copyOfRange(0, IV_LEN) | ||
| val ciphertext = combined.copyOfRange(IV_LEN, combined.size) | ||
| val cipher = Cipher.getInstance(TRANSFORM) | ||
| cipher.init(Cipher.DECRYPT_MODE, getOrCreateKey(), GCMParameterSpec(TAG_BITS, iv)) | ||
| String(cipher.doFinal(ciphertext), Charsets.UTF_8) | ||
| } catch (e: Exception) { | ||
| Log.w(TAG, "Failed to decrypt stored API key", e) | ||
| null | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Read [key] from [prefs], upgrading a legacy plaintext value to ciphertext in place. | ||
| * | ||
| * Keys written before this store existed are still plaintext on disk, and [decrypt] alone | ||
| * hands them back unchanged forever — so an install that configured its key earlier would | ||
| * never actually gain encryption. Re-encrypting on the first read closes that gap without | ||
| * making the user re-enter the key. | ||
| * | ||
| * The value is trimmed on migration, so the stored, displayed and sent forms all agree. | ||
| * | ||
| * Keystore IPC + AES/GCM, so call this off the main thread. | ||
| * | ||
| * @return the trimmed plaintext value, or null when nothing is stored or decryption failed. | ||
| */ | ||
| fun readAndMigrate(prefs: SharedPreferences?, key: String): String? { | ||
| val stored = prefs?.getString(key, null) ?: return null | ||
| if (stored.startsWith(ENC_PREFIX)) return decrypt(stored) | ||
| val plain = stored.trim() | ||
| if (plain.isEmpty()) return plain | ||
| try { | ||
| prefs.edit().putString(key, encrypt(plain)).apply() | ||
| Log.i(TAG, "Upgraded legacy plaintext value for '$key' to ciphertext") | ||
| } catch (e: Exception) { | ||
| Log.w(TAG, "Could not upgrade legacy plaintext value for '$key' to ciphertext", e) | ||
| } | ||
| return plain | ||
| } | ||
| } | ||
| val secureApiKeyStore = KeystoreSecretStore(ALIAS) |
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.