Skip to content

Commit 82f44f8

Browse files
fix(webview): keep view-local profile credentials out of the shared surface
getValues() is the public view of settings: an API client reads it through getConfiguration and can hand the same object straight back to setConfiguration. It merged the view-local overlay whole, so a credential bearing profile reached the caller inside apiConfiguration, and a payload then wrote that object into ordinary global state through contextProxy.setValues. The public getter now reports the overlay without its secret keys, and the shared write drops the overlay: its place is the per-view record and secret storage. The buffer is never mutated, so the view still serves its own profile. broadcastResetToAllInstances cleared each buffer inside the same loop that awaited sibling posts, so a stalled post left the views after it serving selections the reset had already thrown away; the clears now run first. resetState skipped the broadcast when a later reset step rejected after the shared reset had already landed, leaving live views holding the discarded modes, profiles and credentials; they are invalidated and refreshed on that path, and the original failure is what the caller sees.
1 parent 39a38b2 commit 82f44f8

2 files changed

Lines changed: 215 additions & 11 deletions

File tree

‎src/core/webview/ClineProvider.ts‎

Lines changed: 63 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ import {
7777
modelIdKeys,
7878
providerIdentifiers,
7979
PROVIDER_SETTINGS_KEYS,
80+
isSecretStateKey,
8081
} from "@roo-code/types"
8182
import { RateLimitClock, createRateLimitClock } from "../task/RateLimitClock"
8283
import { TaskRegistry } from "../task/TaskRegistry"
@@ -5501,7 +5502,23 @@ export class ClineProvider
55015502
}
55025503

55035504
public getValues() {
5504-
return { ...this.contextProxy.getValues(), ...this.viewLocalState }
5505+
// The view-local overlay can carry this view profile credentials, and this is the public
5506+
// view of settings: an API client reads it through getConfiguration and can hand the same
5507+
// object straight back to setConfiguration. The credentials are dropped from the copy only
5508+
// - the buffer keeps them, and the internal consumers read viewLocalState directly: getState
5509+
// merges the buffer below, and the profile rollback path reads the stored record rather
5510+
// than this getter.
5511+
const { apiConfiguration } = this.viewLocalState
5512+
if (apiConfiguration === undefined) {
5513+
return { ...this.contextProxy.getValues(), ...this.viewLocalState }
5514+
}
5515+
const publicOverlay: Partial<ExtensionState> = {
5516+
...this.viewLocalState,
5517+
apiConfiguration: Object.fromEntries(
5518+
Object.entries(apiConfiguration).filter(([key]) => !isSecretStateKey(key)),
5519+
) as ProviderSettings,
5520+
}
5521+
return { ...this.contextProxy.getValues(), ...publicOverlay }
55055522
}
55065523

55075524
public async setValues(values: RooCodeSettings, options?: { savedProfileName?: string }) {
@@ -5540,16 +5557,23 @@ export class ClineProvider
55405557
await this.snapshotSiblingOverlaysBeforeSharedSettingsWrite(options?.savedProfileName)
55415558
}
55425559

5560+
// The profile overlay is view-local: it is applied to this view buffer below, and its
5561+
// credentials belong in secret storage. Handing it to the shared write would put a profile
5562+
// whole settings, credentials included, into ordinary global state - which is where an API
5563+
// client that passed getConfiguration back to setConfiguration would leave it.
5564+
const sharedValues: Partial<RooCodeSettings> & Partial<ExtensionState> = { ...sanitizedValues }
5565+
delete sharedValues.apiConfiguration
55435566
const previousSharedValues = Object.fromEntries(
5544-
Object.keys(sanitizedValues).map((key) => [key, this.contextProxy.getValue(key as keyof RooCodeSettings)]),
5567+
Object.keys(sharedValues).map((key) => [key, this.contextProxy.getValue(key as keyof RooCodeSettings)]),
55455568
) as RooCodeSettings
55465569
try {
5547-
await this.contextProxy.setValues(sanitizedValues)
5570+
await this.contextProxy.setValues(sharedValues)
55485571
} catch (error) {
55495572
// Same scope rule as setValue: a multi-key write that fails partway has
55505573
// already persisted a durable subset, so the restore must cover the
5551-
// initial shared write, not only the per-view persistence below.
5552-
await this._restoreSharedValuesAfterFailedViewWrite(previousSharedValues, sanitizedValues)
5574+
// initial shared write, not only the per-view persistence below. Compared against the
5575+
// shared subset, because that is what the shared layer was written.
5576+
await this._restoreSharedValuesAfterFailedViewWrite(previousSharedValues, sharedValues)
55535577
throw error
55545578
}
55555579
try {
@@ -5694,9 +5718,14 @@ export class ClineProvider
56945718
)
56955719
}
56965720

5721+
// Clear every live buffer before awaiting any post: a sibling whose state post stalls - it
5722+
// reads through customModesManager and reaches the settings file - must not leave the views
5723+
// after it serving selections this reset already threw away.
56975724
for (const instance of allInstances) {
56985725
instance._clearViewLocalState()
5726+
}
56995727

5728+
for (const instance of allInstances) {
57005729
if (instance !== this) {
57015730
// A sibling's post can throw mid-reset (state generation reaches the
57025731
// settings file through customModesManager.getCustomModes): the failure
@@ -5754,9 +5783,35 @@ export class ClineProvider
57545783
// a per-view clear here would only add a failure point that can strand the
57555784
// reset between the shared and provider resets.
57565785

5757-
await this.providerSettingsManager.resetAllConfigs()
5758-
await this.customModesManager.resetCustomModes()
5759-
await this.removeClineFromStack()
5786+
try {
5787+
await this.providerSettingsManager.resetAllConfigs()
5788+
await this.customModesManager.resetCustomModes()
5789+
await this.removeClineFromStack()
5790+
} catch (resetError) {
5791+
// The shared reset already landed when this rejected, so the live views still hold the
5792+
// modes, profiles and credentials the reset threw away and a new task would run on them.
5793+
// Invalidate the live buffers and refresh the views before the error surfaces, and keep
5794+
// the original failure as the one the caller sees.
5795+
try {
5796+
await this.broadcastResetToAllInstances()
5797+
} catch (invalidateError) {
5798+
this.log(
5799+
`[resetState] failed to invalidate live views after a failed reset step: ${
5800+
invalidateError instanceof Error ? invalidateError.message : String(invalidateError)
5801+
}`,
5802+
)
5803+
}
5804+
try {
5805+
await this.postStateToWebview()
5806+
} catch (postError) {
5807+
this.log(
5808+
`[resetState] failed to refresh this view after a failed reset step: ${
5809+
postError instanceof Error ? postError.message : String(postError)
5810+
}`,
5811+
)
5812+
}
5813+
throw resetError
5814+
}
57605815

57615816
// Clear durable and in-memory per-view state across live instances so parallel tabs don't keep stale state.
57625817
await this.broadcastResetToAllInstances()

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

Lines changed: 152 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4314,6 +4314,147 @@ describe("ClineProvider", () => {
43144314
})
43154315
})
43164316

4317+
describe("view-local secrets and reset invalidation", () => {
4318+
it("clears every live buffer before awaiting any sibling post", async () => {
4319+
const provider = new ClineProvider(
4320+
mockContext,
4321+
mockOutputChannel,
4322+
"sidebar",
4323+
new ContextProxy(mockContext),
4324+
new WebviewFocusTracker(),
4325+
)
4326+
const firstSibling = new ClineProvider(
4327+
mockContext,
4328+
mockOutputChannel,
4329+
"editor",
4330+
new ContextProxy(mockContext),
4331+
new WebviewFocusTracker(),
4332+
)
4333+
const secondSibling = new ClineProvider(
4334+
mockContext,
4335+
mockOutputChannel,
4336+
"sidebar",
4337+
new ContextProxy(mockContext),
4338+
new WebviewFocusTracker(),
4339+
)
4340+
provider["viewLocalState"] = { mode: "architect" }
4341+
firstSibling["viewLocalState"] = { mode: "plan" }
4342+
secondSibling["viewLocalState"] = { mode: "code" }
4343+
4344+
// The first sibling stalls and then fails; what it can still see is the point.
4345+
let secondBufferAtFirstPost: unknown
4346+
vi.spyOn(firstSibling, "postStateToWebview").mockImplementation(async () => {
4347+
secondBufferAtFirstPost = secondSibling["viewLocalState"].mode
4348+
throw new Error("sibling post stalled")
4349+
})
4350+
const secondPost = vi.spyOn(secondSibling, "postStateToWebview").mockResolvedValue(undefined)
4351+
4352+
await provider.broadcastResetToAllInstances()
4353+
4354+
// Clearing happens before any await, so a stalled sibling cannot leave a later view
4355+
// serving a selection this reset already threw away, and the failure does not stop the
4356+
// reset from reaching the remaining view.
4357+
expect(secondBufferAtFirstPost).toBeUndefined()
4358+
expect(provider["viewLocalState"]).toEqual({})
4359+
expect(firstSibling["viewLocalState"]).toEqual({})
4360+
expect(secondSibling["viewLocalState"]).toEqual({})
4361+
expect(secondPost).toHaveBeenCalledTimes(1)
4362+
4363+
await provider.dispose()
4364+
await firstSibling.dispose()
4365+
await secondSibling.dispose()
4366+
})
4367+
4368+
it("invalidates and refreshes live views when a reset step rejects after the shared reset", async () => {
4369+
const provider = new ClineProvider(
4370+
mockContext,
4371+
mockOutputChannel,
4372+
"sidebar",
4373+
new ContextProxy(mockContext),
4374+
new WebviewFocusTracker(),
4375+
)
4376+
provider["viewLocalState"] = { mode: "architect" }
4377+
const broadcast = vi.spyOn(provider, "broadcastResetToAllInstances").mockResolvedValue(undefined)
4378+
const post = vi.spyOn(provider, "postStateToWebview").mockResolvedValue(undefined)
4379+
vi.spyOn(provider["providerSettingsManager"], "resetAllConfigs").mockRejectedValue(
4380+
new Error("profile reset failed"),
4381+
)
4382+
// @ts-ignore - the real resetCustomModes writes to disk; this path must not reach it.
4383+
provider.customModesManager = { resetCustomModes: vi.fn().mockResolvedValue(undefined), dispose: vi.fn() }
4384+
vi.mocked(vscode.window.showInformationMessage).mockResolvedValue(
4385+
t("common:answers.yes") as unknown as vscode.MessageItem,
4386+
)
4387+
4388+
await expect(provider.resetState()).rejects.toThrow("profile reset failed")
4389+
4390+
// The shared reset had already landed, so the live views had to be invalidated and
4391+
// refreshed rather than left serving the discarded modes and credentials; the original
4392+
// failure is still what the caller sees.
4393+
expect(broadcast).toHaveBeenCalledTimes(1)
4394+
expect(post).toHaveBeenCalledTimes(1)
4395+
4396+
await provider.dispose()
4397+
})
4398+
4399+
it("drops the overlay credentials from the public values without touching the buffer", async () => {
4400+
const provider = new ClineProvider(
4401+
mockContext,
4402+
mockOutputChannel,
4403+
"sidebar",
4404+
new ContextProxy(mockContext),
4405+
new WebviewFocusTracker(),
4406+
)
4407+
provider["viewLocalState"] = { apiConfiguration: { apiKey: "sk-view-local", apiModelId: "m1" } }
4408+
4409+
const values = provider.getValues()
4410+
4411+
// getValues is the surface getConfiguration exposes to an API client, and that client can
4412+
// hand the same object back to setConfiguration: the profile credential must not ride
4413+
// along. The buffer keeps it, because this view still has to serve its own profile.
4414+
expect(values.apiConfiguration).toEqual({ apiModelId: "m1" })
4415+
expect(provider["viewLocalState"].apiConfiguration).toEqual({
4416+
apiKey: "sk-view-local",
4417+
apiModelId: "m1",
4418+
})
4419+
4420+
await provider.dispose()
4421+
})
4422+
4423+
it("keeps a credential-bearing overlay out of the shared write", async () => {
4424+
const contextProxy = new ContextProxy(mockContext)
4425+
const provider = new ClineProvider(
4426+
mockContext,
4427+
mockOutputChannel,
4428+
"sidebar",
4429+
contextProxy,
4430+
new WebviewFocusTracker(),
4431+
)
4432+
const sharedWrites: Record<string, unknown>[] = []
4433+
vi.spyOn(contextProxy, "setValues").mockImplementation(async (values) => {
4434+
sharedWrites.push({ ...values })
4435+
})
4436+
4437+
// An API payload shape: the overlay key is not part of RooCodeSettings, but the
4438+
// mutation path accepts it and that is how a client passing getConfiguration back
4439+
// delivers it.
4440+
const payload: Partial<RooCodeSettings> & Partial<ExtensionState> = {
4441+
apiConfiguration: { apiKey: "sk-overlay", apiModelId: "m2" },
4442+
}
4443+
await provider.setValues(payload)
4444+
4445+
// The overlay is view-local: its place is the per-view record and secret storage, not the
4446+
// shared slot, where an API payload would persist the profile credential in global state.
4447+
expect(sharedWrites).toHaveLength(1)
4448+
expect(sharedWrites[0]).not.toHaveProperty("apiConfiguration")
4449+
expect(provider["viewLocalState"].apiConfiguration).toEqual({
4450+
apiKey: "sk-overlay",
4451+
apiModelId: "m2",
4452+
})
4453+
4454+
await provider.dispose()
4455+
})
4456+
})
4457+
43174458
describe("setViewStateId", () => {
43184459
it('should ignore "__proto__" and keep the temporary viewId', async () => {
43194460
const provider = new ClineProvider(
@@ -5357,9 +5498,10 @@ describe("ClineProvider", () => {
53575498
apiProvider: providerIdentifiers.openrouter,
53585499
apiKey: "stale-key",
53595500
})
5501+
// The public getter is the API surface: it reports the overlay without its credential,
5502+
// which still identifies which source serves the view, and the buffer keeps the credential.
53605503
expect(provider.getValues().apiConfiguration).toEqual({
53615504
apiProvider: providerIdentifiers.openrouter,
5362-
apiKey: "stale-key",
53635505
})
53645506

53655507
await provider.activateProviderProfile({ name: "new-profile" })
@@ -5524,9 +5666,10 @@ describe("ClineProvider", () => {
55245666
apiProvider: providerIdentifiers.anthropic,
55255667
apiKey: "stale-key",
55265668
})
5669+
// The public getter is the API surface: it reports the overlay without its credential,
5670+
// which still identifies which source serves the view, and the buffer keeps the credential.
55275671
expect(provider.getValues().apiConfiguration).toEqual({
55285672
apiProvider: providerIdentifiers.anthropic,
5529-
apiKey: "stale-key",
55305673
})
55315674

55325675
await provider.upsertProviderProfile("fresh-profile", { apiProvider: providerIdentifiers.openrouter })
@@ -5930,9 +6073,10 @@ describe("ClineProvider", () => {
59306073
apiProvider: providerIdentifiers.openrouter,
59316074
apiKey: "stale-key",
59326075
})
6076+
// The public getter is the API surface: it reports the overlay without its credential,
6077+
// which still identifies which source serves the view, and the buffer keeps the credential.
59336078
expect(provider.getValues().apiConfiguration).toEqual({
59346079
apiProvider: providerIdentifiers.openrouter,
5935-
apiKey: "stale-key",
59366080
})
59376081

59386082
await provider.deleteProviderProfile(oldProfile)
@@ -6811,8 +6955,13 @@ describe("ClineProvider", () => {
68116955

68126956
expect(values.mode).toBe("architect")
68136957
expect(values.currentApiConfigName).toBe("view-profile")
6958+
// The credential stays in the buffer; the public getter reports the overlay without it,
6959+
// so the merge claim is carried by the provider identity.
68146960
expect(values.apiConfiguration).toEqual({
68156961
apiProvider: providerIdentifiers.openrouter,
6962+
})
6963+
expect(provider["viewLocalState"].apiConfiguration).toEqual({
6964+
apiProvider: providerIdentifiers.openrouter,
68166965
openRouterApiKey: "view-key",
68176966
})
68186967
expect(values.customModePrompts).toEqual({ code: { roleDefinition: "shared" } })

0 commit comments

Comments
 (0)