Skip to content

Commit 0b6b934

Browse files
committed
fix(core): derive the delete purge keep-set from the surviving provider's ownership
What: - Both delete branches now pass an ownership-derived keep-set to purgeDeletedProfileSecrets, via a new getSurvivingProviderSecretKeys helper that resolves the surviving profile's provider from the manager and keeps the deleted profile's keys that provider owns. - Exports providerOwnsSecretKey from ContextProxy. - Replaces the active branch's blob-derived keep-set with the same helper, and retitles the round-5 same-provider spec to assert the corrected semantics. - Adds three inactive-branch specs: same-provider preservation, cross-provider purge, and the unreadable-survivor purge fallback. Why: The inactive branch called purgeDeletedProfileSecrets with no keep-set, so deleting a profile whose blob declared a key the surviving active profile also uses deleted the live credential. The e2e profile test's finally deletes subtask-child-profile and subtask-parent-profile while default is active; all three are OpenRouter, so they share the single openRouterApiKey SecretStorage slot. The purge deleted default's credential and every rehydrated task hung until its 30 s timeout, which is the deterministic 9/3 mocked-e2e failure. A blob-derived keep-set cannot fix this: the api.getConfiguration() round-trip rewrites a profile without its secret fields while keeping apiProvider, so the surviving profile's blob is secret-less exactly when the live credential needs protecting. Provider ownership comes from the schema, which no round-trip can strip. The active branch had the same hazard and is aligned. Impact: Fixes the deterministic e2e-mock failure: the local mocked subtasks suite is 12/12. Preserves the S-3 security closure, because a cross-provider delete keeps nothing (the survivor owns none of the deleted keys) and an unreadable survivor falls back to purging every key the deleted profile declared.
1 parent 99a95bc commit 0b6b934

3 files changed

Lines changed: 158 additions & 30 deletions

File tree

‎src/core/config/ContextProxy.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ for (const branch of providerSettingsSchemaDiscriminated.options) {
8484
* provider, whose branch is not in the union), so callers keep the old
8585
* presence-based semantics instead of guessing.
8686
*/
87-
const providerOwnsSecretKey = (apiProvider: unknown, key: string): boolean | undefined => {
87+
export const providerOwnsSecretKey = (apiProvider: unknown, key: string): boolean | undefined => {
8888
if (typeof apiProvider !== "string" || !isProviderName(apiProvider)) {
8989
return undefined
9090
}

‎src/core/webview/ClineProvider.ts‎

Lines changed: 53 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ import { t } from "../../i18n"
112112
import { buildApiHandler } from "../../api"
113113
import { forceFullModelDetailsLoad, hasLoadedFullDetails } from "../../api/providers/fetchers/lmstudio"
114114

115-
import { ContextProxy, getProfileSecretKeys } from "../config/ContextProxy"
115+
import { ContextProxy, getProfileSecretKeys, providerOwnsSecretKey } from "../config/ContextProxy"
116116
import { ProviderSettingsManager } from "../config/ProviderSettingsManager"
117117
import { CustomModesManager } from "../config/CustomModesManager"
118118
import { PendingActionSettlementError, Task } from "../task/Task"
@@ -2261,7 +2261,18 @@ export class ClineProvider
22612261
// clear-out: without an explicit purge the deleted profile's secrets stay in the
22622262
// cache and in SecretStorage until some unrelated future write happens to name a
22632263
// provider that does not own them, which may never happen.
2264-
await this.purgeDeletedProfileSecrets(undoConfig)
2264+
//
2265+
// The keep-set comes from the *surviving* profile's provider, not from its blob.
2266+
// A blob-derived set is empty exactly when the live credential needs protecting:
2267+
// the `api.getConfiguration()` round-trip rewrites a profile without its secret
2268+
// fields while keeping `apiProvider`, so a surviving OpenRouter profile's blob
2269+
// declares no `openRouterApiKey` even though that SecretStorage slot holds its
2270+
// live value. When the deleted profile and the survivor share a provider and a key
2271+
// name, the slot they would purge *is* the survivor's credential.
2272+
await this.purgeDeletedProfileSecrets(
2273+
undoConfig,
2274+
await this.getSurvivingProviderSecretKeys(globalSettings.currentApiConfigName, undoConfig),
2275+
)
22652276

22662277
await this.contextProxy.setValues({
22672278
listApiConfigMeta: this.getProviderProfileEntries().filter(
@@ -2323,12 +2334,13 @@ export class ClineProvider
23232334
// the replacement's name. The delete is explicit intent, so the deleted profile's own
23242335
// keys are removed here, after the activation has written the replacement's slice.
23252336
//
2326-
// A key the replacement's own blob declares is excluded: the replacement wrote its
2327-
// value through the activation above, and purging by name would delete the live
2328-
// profile's credential, which is the round-4 data-loss shape.
2337+
// The keep-set is ownership-derived for the same reason as the inactive branch: a
2338+
// blob-derived set is empty when the replacement's blob was written from an
2339+
// `api.getConfiguration()` snapshot (secrets stripped), and purging then deletes the
2340+
// replacement's live credential from the shared SecretStorage slot.
23292341
await this.purgeDeletedProfileSecrets(
23302342
deletedProfileConfig,
2331-
await this.getReplacementSecretKeys(profileToActivate),
2343+
await this.getSurvivingProviderSecretKeys(profileToActivate, deletedProfileConfig),
23322344
)
23332345

23342346
// The activation above only rebinds the *current* mode, so every other mode that
@@ -2364,20 +2376,44 @@ export class ClineProvider
23642376
}
23652377

23662378
/**
2367-
* The secret keys the profile that replaced a deleted one declares.
2379+
* The deleted profile's secret keys that the *surviving* profile's provider also owns.
2380+
*
2381+
* Both delete branches purge the deleted profile's credentials explicitly, and both need the
2382+
* same answer: is a key about to be deleted still in use by the profile that survives? That
2383+
* cannot be answered from the surviving profile's blob. The `api.getConfiguration()` round-trip
2384+
* rewrites a profile without its secret fields while keeping `apiProvider`, so a surviving
2385+
* OpenRouter profile's blob declares no `openRouterApiKey` even though that SecretStorage slot
2386+
* holds its live value. Provider ownership answers it from the schema instead, which no
2387+
* round-trip can strip.
2388+
*
2389+
* A same-provider succession (the deleted profile and the survivor both OpenRouter) shares one
2390+
* SecretStorage slot per key name, so "the deleted profile's key" *is* the survivor's credential:
2391+
* keeping it is the correct semantics, not a leak, because nothing is orphaned to purge. A
2392+
* cross-provider delete keeps nothing, because the survivor owns none of the deleted keys.
23682393
*
2369-
* Used to keep the delete purge from removing a credential the replacement wrote itself. A
2370-
* manager that cannot produce the profile (it was just activated, so it should exist) yields an
2371-
* empty set: the purge then falls back to removing every key the deleted profile declared, which
2372-
* is the confidentiality-safe direction.
2394+
* An unreadable survivor yields an empty set, so the purge removes every key the deleted profile
2395+
* declared. That is the confidentiality-safe direction.
23732396
*/
2374-
private async getReplacementSecretKeys(name: string): Promise<Set<string>> {
2397+
private async getSurvivingProviderSecretKeys(
2398+
survivingProfileName: string | undefined,
2399+
deletedConfig: (ProviderSettingsWithId & { name: string }) | undefined,
2400+
): Promise<Set<string>> {
2401+
if (!survivingProfileName || !deletedConfig) {
2402+
return new Set()
2403+
}
2404+
2405+
let survivingProvider: unknown
2406+
23752407
try {
2376-
const { name: _name, ...config } = await this.providerSettingsManager.getProfile({ name })
2377-
return new Set(getProfileSecretKeys(config))
2408+
const { apiProvider } = await this.providerSettingsManager.getProfile({ name: survivingProfileName })
2409+
survivingProvider = apiProvider
23782410
} catch {
23792411
return new Set()
23802412
}
2413+
2414+
return new Set(
2415+
getProfileSecretKeys(deletedConfig).filter((key) => providerOwnsSecretKey(survivingProvider, key) === true),
2416+
)
23812417
}
23822418

23832419
/**
@@ -2388,8 +2424,9 @@ export class ClineProvider
23882424
* same-provider succession is exactly the case ownership preserves. A delete is explicit
23892425
* intent, so the deleted profile's keys are removed by name here instead of being inferred.
23902426
*
2391-
* `keep` names the keys the replacement profile declares: those carry the replacement's own
2392-
* value, so removing them would delete a live credential.
2427+
* `keep` names the keys the surviving profile's provider still owns: those carry a live
2428+
* credential (a same-provider succession shares one SecretStorage slot per key name), so
2429+
* removing them would delete the surviving profile's own key.
23932430
*
23942431
* Best-effort: the profile is already gone from the manager, so a failure here must not turn
23952432
* a successful delete into an error. The keys are logged by name only, never by value.

‎src/core/webview/__tests__/ClineProvider.spec.ts‎

Lines changed: 104 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3813,17 +3813,108 @@ describe("ClineProvider", () => {
38133813
expect(provider.getProviderProfileEntries()).toEqual([otherProfile])
38143814
})
38153815

3816-
test("purges the deleted profile's secret when the replacement is the same provider", async () => {
3817-
// Ownership deliberately preserves a secret the *writing* provider owns, so a
3818-
// same-provider succession (Anthropic -> Anthropic) keeps the deleted profile's `apiKey`
3819-
// alive under the replacement's name. A delete is explicit intent: the deleted profile's
3820-
// own keys go, whether or not the replacement shares its provider.
3816+
test("keeps the surviving profile's credential on an inactive same-provider delete", async () => {
3817+
// The exact e2e trio shape: both profiles are OpenRouter, so they share the single
3818+
// `openRouterApiKey` SecretStorage slot, and the surviving `default` profile's blob was
3819+
// written from an `api.getConfiguration()` snapshot, which strips secrets. The inactive
3820+
// branch activates no replacement, so nothing rewrites the slot: purging the deleted
3821+
// profile's key would delete the live credential and every rehydrated task would hang.
3822+
const { profiles } = mockProviderSettingsManager()
3823+
3824+
// Both profiles are OpenRouter with the same key name; the survivor's blob is secret-less.
3825+
vi.mocked(provider.providerSettingsManager.getProfile).mockImplementation(async (params) => {
3826+
const name = "name" in params ? params.name : ""
3827+
3828+
if (name === "other-config") {
3829+
return { ...otherProfile, name: "other-config", apiProvider: providerIdentifiers.openrouter }
3830+
}
3831+
3832+
return {
3833+
...currentProfile,
3834+
name: "current-config",
3835+
apiProvider: providerIdentifiers.openrouter,
3836+
openRouterApiKey: "live-key",
3837+
}
3838+
})
3839+
3840+
// The survivor is active, so deleting the other profile is an inactive deletion.
3841+
await provider.contextProxy.setValue("currentApiConfigName", "other-config")
3842+
await provider.contextProxy.setProviderSettings({
3843+
apiProvider: providerIdentifiers.openrouter,
3844+
openRouterApiKey: "live-key",
3845+
})
3846+
expect(provider.contextProxy.getSecret("openRouterApiKey")).toBe("live-key")
3847+
3848+
await provider.deleteProviderProfile(currentProfile)
3849+
3850+
// The slot is the surviving OpenRouter profile's credential, so it must survive.
3851+
expect(provider.contextProxy.getSecret("openRouterApiKey")).toBe("live-key")
3852+
expect(mockContext.secrets.delete).not.toHaveBeenCalledWith("openRouterApiKey")
3853+
expect(profiles.map(({ name }) => name)).toEqual(["other-config"])
3854+
})
3855+
3856+
test("still purges on an inactive cross-provider delete, which owns none of the deleted keys", async () => {
3857+
// The confidentiality case S-3 exists for: the survivor is OpenRouter, the deleted profile
3858+
// is Anthropic. The survivor owns none of the deleted profile's keys, so the keep-set is
3859+
// empty and the purge removes the orphaned credential.
3860+
mockProviderSettingsManager()
3861+
3862+
await provider.contextProxy.setValue("currentApiConfigName", "other-config")
3863+
await provider.contextProxy.setProviderSettings({
3864+
apiProvider: providerIdentifiers.anthropic,
3865+
apiKey: "orphaned-key",
3866+
})
3867+
expect(provider.contextProxy.getSecret("apiKey")).toBe("orphaned-key")
3868+
3869+
await provider.deleteProviderProfile(currentProfile)
3870+
3871+
expect(provider.contextProxy.getSecret("apiKey")).toBeUndefined()
3872+
expect(provider.contextProxy.getValues().apiKey).toBeUndefined()
3873+
expect(mockContext.secrets.delete).toHaveBeenCalledWith("apiKey")
3874+
})
3875+
3876+
test("purges on an inactive delete when the surviving profile cannot be read", async () => {
3877+
// Confidentiality-safe fallback: an unreadable survivor yields an empty keep-set, so the
3878+
// purge removes every key the deleted profile declared instead of silently retaining them.
3879+
mockProviderSettingsManager()
3880+
3881+
await provider.contextProxy.setValue("currentApiConfigName", "other-config")
3882+
await provider.contextProxy.setProviderSettings({
3883+
apiProvider: providerIdentifiers.openrouter,
3884+
openRouterApiKey: "orphaned-key",
3885+
})
3886+
3887+
vi.mocked(provider.providerSettingsManager.getProfile).mockImplementation(async (params) => {
3888+
const name = "name" in params ? params.name : ""
3889+
3890+
if (name === "other-config") {
3891+
throw new Error("Failed to read provider profiles from secrets: storage error")
3892+
}
3893+
3894+
return {
3895+
...currentProfile,
3896+
name: "current-config",
3897+
apiProvider: providerIdentifiers.openrouter,
3898+
openRouterApiKey: "orphaned-key",
3899+
}
3900+
})
3901+
3902+
await provider.deleteProviderProfile(currentProfile)
3903+
3904+
expect(provider.contextProxy.getSecret("openRouterApiKey")).toBeUndefined()
3905+
expect(mockContext.secrets.delete).toHaveBeenCalledWith("openRouterApiKey")
3906+
})
3907+
3908+
test("keeps the shared slot when the replacement is the same provider and declares no secret", async () => {
3909+
// A same-provider succession where the replacement's blob is secret-less: the normal
3910+
// shape of a profile written from an `api.getConfiguration()` snapshot, which strips
3911+
// secrets. Two profiles of one provider necessarily share the same SecretStorage slot
3912+
// (each secret key has exactly one owning provider), so the slot holds the *replacement's*
3913+
// live credential. Purging it would delete the surviving profile's key, which is the
3914+
// round-4 data-loss shape and the deterministic e2e failure. Nothing is orphaned here, so
3915+
// keeping the slot is the correct semantics rather than a leak.
38213916
const { profiles } = mockProviderSettingsManager()
38223917

3823-
// The replacement is Anthropic too, and its blob is secret-less: the normal shape of a
3824-
// profile written from an `api.getConfiguration()` snapshot, which strips secrets. The
3825-
// ownership rule reads that omission as a partial write and preserves `apiKey`, so only
3826-
// an explicit purge removes the deleted profile's credential.
38273918
vi.mocked(provider.providerSettingsManager.activateProfile).mockImplementationOnce(async () => {
38283919
return { ...otherProfile, apiProvider: providerIdentifiers.anthropic }
38293920
})
@@ -3845,10 +3936,10 @@ describe("ClineProvider", () => {
38453936

38463937
await provider.deleteProviderProfile(currentProfile)
38473938

3848-
// The deleted profile's credential is gone rather than surviving under the replacement.
3849-
expect(provider.contextProxy.getSecret("apiKey")).toBeUndefined()
3850-
expect(provider.contextProxy.getValues().apiKey).toBeUndefined()
3851-
expect(mockContext.secrets.delete).toHaveBeenCalledWith("apiKey")
3939+
// The slot survives because the surviving Anthropic profile owns `apiKey`: it is that
3940+
// profile's credential, not the deleted profile's orphan.
3941+
expect(provider.contextProxy.getSecret("apiKey")).toBe("deleted-key")
3942+
expect(mockContext.secrets.delete).not.toHaveBeenCalledWith("apiKey")
38523943
expect(profiles.map(({ name }) => name)).toEqual(["other-config"])
38533944
})
38543945

0 commit comments

Comments
 (0)