Skip to content

Commit 2b4e39b

Browse files
committed
fix(plugin-api): address review findings on KeystoreSecretStore
All ten findings from the review at 8f739ca. Both IMPORTANT ones were in the published contract of a class three plugins are about to adopt, so they are fixed rather than documented. encrypt's @throws(GeneralSecurityException) did not cover every way it can fail: KeyStore.load(null) declares IOException and AndroidKeyStore keygen raises the unchecked ProviderException, so a plugin catching exactly what the KDoc documents still took an uncaught throwable - in the host's process, so it surfaced as an IDE crash. Anything that is not already a GeneralSecurityException is now wrapped in one, cause preserved; a KeyStoreException still passes through unwrapped. The invalidated-key recovery was a delete-then-create-then-encrypt compound with no serialization, so two concurrent callers could leave one holding a ciphertext nothing could ever decrypt. readAndMigrate was a read-modify-write against SharedPreferences with the same gap, so a write committing mid-upgrade was silently reverted. Both now run under one alias-scoped, process-wide monitor shared with write - the same scope AndroidKeystoreSource already uses for its own read-then-generate race, and for the same reason. Also from the review: - Stored.Value overrides toString, so a Stored reaching a log line or a crash breadcrumb can no longer print the decrypted secret. - decrypt no longer collapses "the key is lost" into "the Keystore would not answer". A new Stored.Unavailable reports the transient case, so a keystore that is not ready yet does not tell the user to retype an intact credential. Free to add now: Stored is sealed and unreleased with no consumers, which is exactly why the changelog entry now says a later case needs a breaking row. - AndroidKeystoreSource logs the alias and the unexpected entry class before replacing a non-SecretKeyEntry, instead of silently overwriting whatever a colliding plugin left there. Test harness: - The two raw NUL bytes are now unicode escapes. Identical bytes once compiled, but the file is text to git again, so the suite has a reviewable diff on GitHub (568 added lines vs origin/stage, previously Bin 0 -> 14025 bytes). - FakeKeySource.delete() mints a fresh key instead of only counting, and invalidation is modelled on the key rather than the next call - a next-call-only flag models a failure that cannot happen and is what made the recovery race untestable. - Three new regression tests, each confirmed to fail against the unfixed code for the reason it is named for: concurrent recovery (deleted=2, was 1), a write racing a legacy upgrade (reverted to legacy-secret), and the non-GeneralSecurityException wrap (raw IOException escaped). The Unavailable split and the toString redaction were verified the same way. 36 tests, 0 failures, the two concurrency cases repeated 6x. Whole module: 76 tests, 0 failures. plugin-api.api regenerated: 43 additions, 0 deletions vs origin/stage, apiCheck green. Docs: the 26.36 entry claimed later additions to KeystoreSecretStore are additive. That holds for the class, which plugins instantiate, but not for the sealed Stored the same entry introduces - a new case breaks every plugin's exhaustive when at compile time and throws NoWhenBranchMatchedException in an already-built .cgp. The entry now says a new Stored case needs a breaking row, including the enc:v2: state its own future-proofing note leaves room for, and plugin-api.md lists Stored among the sealed types plugins reference. CI, the last finding: debug.yml ran only :plugin-api:apiCheck, so the suite never ran on a PR at all (analyze.yml is schedule/dispatch only). Adds a step beside apiCheck, running testDebugUnitTest rather than testV8DebugUnitTest because this module declares no product flavors. jacocoAggregateReport had the matching gap: it collected only testV8DebugUnitTest and its class and exec paths were v8Debug-only, so mapNotNull returned nothing for a flavorless module and dropped it from the report entirely. It now carries a (variant, task) pair list, v8 first, and collects both shapes. Verified by diffing the generated report against the old wiring: 286 -> 292 packages (the six com.itsaky.androidide.plugins.* packages were absent altogether), line 1957/79722 -> 2147/80459, branch 312/44837 -> 361/44961, with KeystoreSecretStore itself at 99% line / 93% branch. The task graph gains :plugin-api:testDebugUnitTest, which the old wiring did not contain (0 occurrences in --dry-run before, present after). sourceDirectories is deliberately left naming src/main/java only, though nine modules keep sources under src/main/kotlin: it is inert for this task. Emptying it outright still renders full Kotlin source in the HTML and leaves the XML byte-identical, so adding the Kotlin roots buys nothing. Measured rather than assumed, and commented in place so the next reader does not "fix" it.
1 parent bf46e02 commit 2b4e39b

8 files changed

Lines changed: 249 additions & 99 deletions

File tree

‎.github/workflows/debug.yml‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,13 @@ jobs:
197197
run: |
198198
flox activate -d flox/base -- ./gradlew :plugin-api:apiCheck --no-daemon
199199
200+
# testDebugUnitTest, not testV8DebugUnitTest: :plugin-api declares no product flavors, so the
201+
# v8 task every other module has does not exist here. Named as its own step rather than left
202+
# to jacocoAggregateReport, which is not on the PR path at all.
203+
- name: Run plugin-api unit tests
204+
run: |
205+
flox activate -d flox/base -- ./gradlew :plugin-api:testDebugUnitTest --no-daemon
206+
200207
# Combined with a compile task on purpose: spotlessCheck alone never pulls a
201208
# source-tree-writing task into the graph, so the implicit-dependency failure ADFA-5244 fixed
202209
# cannot reproduce in a standalone run. Without this step a regression of that exclude is

‎build.gradle.kts‎

Lines changed: 44 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -462,11 +462,24 @@ tasks.named("sonarqube") {
462462
tasks.register<JacocoReport>("jacocoAggregateReport") {
463463
val excludedProjects = emptySet<String>()
464464

465-
// Depend only on testV8DebugUnitTest tasks in subprojects
465+
// (variant directory, unit-test task) pairs, v8 first so a flavored module keeps using it. Two
466+
// entries because the module set is not uniform: every flavored module builds `v8Debug`, but a
467+
// flavorless one such as `:plugin-api` only builds `debug`. Collecting the v8 names alone dropped
468+
// such a module from this report silently - `mapNotNull` returned nothing for it, so its tests
469+
// never ran here and its classes counted for nothing.
470+
val coverageVariants =
471+
listOf(
472+
"v8Debug" to "testV8DebugUnitTest",
473+
"debug" to "testDebugUnitTest",
474+
)
475+
476+
// One unit-test task per subproject, not one per variant: a module has only ever one of these.
466477
dependsOn(
467478
subprojects
468479
.filterNot { it.name in excludedProjects }
469-
.mapNotNull { it.tasks.findByName("testV8DebugUnitTest") },
480+
.mapNotNull { subproj ->
481+
coverageVariants.firstNotNullOfOrNull { (_, task) -> subproj.tasks.findByName(task) }
482+
},
470483
)
471484

472485
reports {
@@ -483,41 +496,49 @@ tasks.register<JacocoReport>("jacocoAggregateReport") {
483496
"**/*Test*.*",
484497
)
485498

486-
// Collect kotlin and java class directories for v8Debug and v8DebugUnitTest variant
499+
// Kotlin and java class directories, for each variant a module might have built. Absent
500+
// directories contribute nothing, so listing both shapes per module is safe.
487501
val classDirs =
488502
subprojects
489503
.filterNot { it.name in excludedProjects }
490504
.flatMap { subproj ->
491-
listOf(
492-
fileTree(subproj.layout.buildDirectory.dir("tmp/kotlin-classes/v8Debug")) {
493-
exclude(fileFilter)
494-
},
495-
fileTree(subproj.layout.buildDirectory.dir("tmp/kotlin-classes/v8DebugUnitTest")) {
496-
exclude(fileFilter)
497-
},
498-
fileTree(subproj.layout.buildDirectory.dir("classes/java/v8Debug")) {
499-
exclude(fileFilter)
500-
},
501-
fileTree(subproj.layout.buildDirectory.dir("intermediates/javac/v8DebugUnitTest/classes")) {
502-
exclude(fileFilter)
503-
},
504-
)
505+
coverageVariants.flatMap { (variant, _) ->
506+
listOf(
507+
fileTree(subproj.layout.buildDirectory.dir("tmp/kotlin-classes/$variant")) {
508+
exclude(fileFilter)
509+
},
510+
fileTree(subproj.layout.buildDirectory.dir("tmp/kotlin-classes/${variant}UnitTest")) {
511+
exclude(fileFilter)
512+
},
513+
fileTree(subproj.layout.buildDirectory.dir("classes/java/$variant")) {
514+
exclude(fileFilter)
515+
},
516+
fileTree(subproj.layout.buildDirectory.dir("intermediates/javac/${variant}UnitTest/classes")) {
517+
exclude(fileFilter)
518+
},
519+
)
520+
}
505521
}
506522

507-
// Collect source directories
523+
// Untouched on purpose, though it names only `src/main/java` while nine modules keep sources under
524+
// `src/main/kotlin`: this property is inert for this task. Emptying it outright still renders full
525+
// Kotlin source in the HTML and leaves the XML byte-identical, so adding the Kotlin roots here
526+
// buys nothing. Measured, not assumed - do not "fix" it without re-measuring.
508527
val sourceDirs =
509528
subprojects
510529
.filterNot { it.name in excludedProjects }
511530
.map { it.file("src/main/java") }
512531

513-
// Collect execution data (.exec files)
532+
// Collect execution data (.exec files), for each variant a module might have run.
514533
val execFiles =
515534
subprojects
516535
.filterNot { it.name in excludedProjects }
517-
.map { subproj ->
518-
subproj.layout.buildDirectory.file(
519-
"outputs/unit_test_code_coverage/v8DebugUnitTest/testV8DebugUnitTest.exec",
520-
)
536+
.flatMap { subproj ->
537+
coverageVariants.map { (variant, task) ->
538+
subproj.layout.buildDirectory.file(
539+
"outputs/unit_test_code_coverage/${variant}UnitTest/$task.exec",
540+
)
541+
}
521542
}
522543

523544
classDirectories.setFrom(classDirs)

‎docs/PLUGIN_API_CHANGELOG.md‎

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,24 @@ milestone. **[verified]** = read from the checked-in ABI dump. **[reconstructed]
6767
source into each `.cgp` would not fix that: `compileOnly` against the host means one
6868
implementation in the process, loaded by the host's class loader.
6969
`KeystoreSecretStore(alias)` (`encrypt`, `decrypt`, `write`, `readAndMigrate`) and
70-
`KeystoreSecretStore.Stored` / `.Absent` / `.Value` / `.Unreadable`. The `enc:v1:` marker
71-
it writes is deliberately **not** part of the surface: the on-disk format is the store's
72-
business, both formats are handled for the caller, and keeping it private is what leaves
73-
room for an `enc:v2:` later.
74-
A class a plugin **instantiates**, not an interface it implements, so later additions to
75-
it are additive. The `alias` is a constructor parameter and must stay **distinct per
70+
`KeystoreSecretStore.Stored` / `.Absent` / `.Value` / `.Unreadable` / `.Unavailable`. The
71+
`enc:v1:` marker it writes is deliberately **not** part of the surface: the on-disk format
72+
is the store's business, both formats are handled for the caller, and keeping it private is
73+
what leaves room for an `enc:v2:` later.
74+
Additions to the **class** are additive, because a plugin instantiates it rather than
75+
implementing it. `Stored` is the exception, and the one part of this entry that cannot grow
76+
quietly: it is a public **sealed** interface, so a fifth case makes every plugin's exhaustive
77+
`when` fail to compile, and a `.cgp` already built against four throws
78+
`NoWhenBranchMatchedException` at runtime. Any new `Stored` case needs a `breaking` row —
79+
including the `enc:v2:` state the paragraph above leaves room for, if it ever surfaces.
80+
`encrypt` throws `GeneralSecurityException` and nothing else: whatever the Keystore actually
81+
raises (`IOException` from `KeyStore.load`, the unchecked `ProviderException` from keygen) is
82+
wrapped in one, so catching the documented type is enough — an unwrapped throwable here
83+
reaches the host's crash handler as an IDE crash, not your plugin's error path.
84+
`readAndMigrate` keeps a lost key (`Unreadable` — ask the user for the secret again) apart
85+
from a Keystore that would not answer (`Unavailable` — retry), so a transient failure does
86+
not cost the user a credential that is still perfectly readable.
87+
The `alias` is a constructor parameter and must stay **distinct per
7688
plugin**: plugins share the host's process, UID and therefore its Keystore, so a shared
7789
alias would let one plugin's invalidated-key recovery (`deleteEntry`) destroy another's
7890
stored secret. It must also stay stable across releases, since a secret encrypted under

‎docs/plugin-api.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ The surface a plugin binds to is broader than one module. All of the following a
1515
- Cross-plugin service interfaces, where **one plugin implements what another calls** (via `SharedServices`): `LlmInferenceService` — implemented by ai-core, called by every AI plugin — together with the types nested in it that a *backend* plugin implements (`LlmBackend`, `HistoryCapableBackend`, `ToolCallingBackend`, `CancellableBackend`, `ConfigurableBackend`) and the value types either side constructs (`ChatMessage`, `LlmConfig`, `LlmResponse`, `SystemPromptRequest`, `ToolDefinition`, `ToolCallRequest`). Also `ToolSourceRegistry` — implemented by ai-core, called by any plugin contributing tools to the agent — with `ToolSource` and `ToolSpec`, which a *contributing* plugin implements, `ToolInvocation`, which ai-core constructs and passes to `ToolSource.invoke`, and `ToolOutcome`, which the source returns.
1616
- Utility classes plugins **instantiate**: `KeystoreSecretStore` (AES/GCM over the Android Keystore, alias supplied by the caller). Host-side implementation rather than an interface, so plugins share one copy in the process instead of compiling their own.
1717
- Data classes plugins **construct** (e.g. `MenuItem`, `TabItem`, `EditorTabItem`, `NavigationItem`, `ToolbarAction`, `FabAction`, `PluginBuildAction`, `SnippetContribution`, `PluginTooltipEntry`, `PluginSettingsEntry`).
18-
- Enums / sealed types plugins **reference**: `PluginPermission`, `ShowAsAction`, `ArchiveFormat`, `BuildActionCategory`, `ToolbarActionIds`, `CommandSpec`, `CommandResult`, `ExtractResult`.
18+
- Enums / sealed types plugins **reference**: `PluginPermission`, `ShowAsAction`, `ArchiveFormat`, `BuildActionCategory`, `ToolbarActionIds`, `CommandSpec`, `CommandResult`, `ExtractResult`, `KeystoreSecretStore.Stored`. Sealed, so a plugin `when`s over the cases exhaustively — adding one is a **breaking** change, not an additive one.
1919
- **Wire/format contracts outside the module:**
2020
- Manifest `<meta-data>` keys — `plugin.id`, `plugin.name`, `plugin.version`, `plugin.description`, `plugin.author`, `plugin.main_class`, `plugin.min_ide_version`, `plugin.max_ide_version`, `plugin.permissions`, `plugin.sidebar_items`, `plugin.icon_day`, `plugin.icon_night`. Matched **by string** — a rename silently breaks every plugin.
2121
- Permission **key strings** (`filesystem.read`, `filesystem.write`, `network.access`, `system.commands`, `ide.settings`, `project.structure`, `native.code`, `ide.environment.write`) — also matched by string.

‎plugin-api/api/plugin-api.api‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1231,6 +1231,13 @@ public final class com/itsaky/androidide/plugins/security/KeystoreSecretStore$St
12311231
public fun toString ()Ljava/lang/String;
12321232
}
12331233

1234+
public final class com/itsaky/androidide/plugins/security/KeystoreSecretStore$Stored$Unavailable : com/itsaky/androidide/plugins/security/KeystoreSecretStore$Stored {
1235+
public static final field INSTANCE Lcom/itsaky/androidide/plugins/security/KeystoreSecretStore$Stored$Unavailable;
1236+
public fun equals (Ljava/lang/Object;)Z
1237+
public fun hashCode ()I
1238+
public fun toString ()Ljava/lang/String;
1239+
}
1240+
12341241
public final class com/itsaky/androidide/plugins/security/KeystoreSecretStore$Stored$Unreadable : com/itsaky/androidide/plugins/security/KeystoreSecretStore$Stored {
12351242
public static final field INSTANCE Lcom/itsaky/androidide/plugins/security/KeystoreSecretStore$Stored$Unreadable;
12361243
public fun equals (Ljava/lang/Object;)Z

0 commit comments

Comments
 (0)