Skip to content

Commit 75add35

Browse files
fix(webview): drop a caller-supplied viewStates map at the settings write boundary
The registered viewStates map is host-owned: ClineProvider rewrites it from a full read (savePersistedViewState, clearPersistedViewState, prunePersistedViewStates), so a map handed in through a settings write - the webview newTask taskConfiguration, or the extension API's createTask configuration - replaced every other view's durable selection in one write, and an explicit unset erased the map outright. Entries arriving that way are validated nowhere on the path, so the key is dropped rather than sanitised; the rest of the settings object is the caller's to set. mode, currentApiConfigName and apiConfiguration are untouched: those are the acting view's own selections and already route through the per-view buffer. The generic updateSettings loop writes through ContextProxy directly, bypassing setValues, so it drops the same key before any side effect is run for it. provider.setValue is not guarded: the only handler caller of it passes defaultModeSlug, measured at the call site. Tests: ClineProvider.spec.ts pins the setValues door against a seeded sibling entry, and webviewMessageHandler.spec.ts pins the updateSettings door. Both are red at a639813 without the guards - the crafted map replaced the seeded one, and the loop called contextProxy.setValue("viewStates", ...) - and green with them. Lanes at this head: ClineProvider 475, webviewMessageHandler 175, sticky-mode 29, sticky-profile 25, ContextProxy 64, importExport 55, api-start-new-task 6, api-start-task- logging 8, all passing. Lint, check-types, code-qa-ci, solheim-smoke and agent-evals types, lifecycle and webview-recovery model-checks, and the MCP OAuth check are green; the two solheim record.sh suites need ffprobe (absent here) and config.test.ts asserts POSIX path separators, so those two are box-local only.
1 parent a639813 commit 75add35

4 files changed

Lines changed: 71 additions & 0 deletions

File tree

‎src/core/webview/ClineProvider.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5553,6 +5553,19 @@ export class ClineProvider
55535553
),
55545554
}
55555555

5556+
if ("viewStates" in sanitizedValues) {
5557+
// The registered viewStates map is host-owned: this provider rewrites it from a full read of
5558+
// the map (savePersistedViewState, clearPersistedViewState, prunePersistedViewStates), so a map
5559+
// handed to a settings write by a caller - the webview's newTask configuration, or the extension
5560+
// API's - would replace every other view's durable selection in one write, and an explicit unset
5561+
// would erase the map outright. Entries arriving this way are validated nowhere, so the key is
5562+
// dropped rather than sanitised; the rest of the settings object is the caller's to set.
5563+
this.log(
5564+
"[setValues] Ignored a caller-supplied viewStates map; the registered map is written by the extension host.",
5565+
)
5566+
delete sanitizedValues.viewStates
5567+
}
5568+
55565569
if (sanitizedValues.mode !== undefined) {
55575570
// An unknown or non-string mode (e.g. from an API payload) must not be persisted:
55585571
// a new Task would read it from getState() and persist it into task history.

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9019,6 +9019,30 @@ describe("ClineProvider", () => {
90199019

90209020
expect(setValues).toHaveBeenCalledWith({ mode: "code" })
90219021
})
9022+
9023+
it("drops a caller-supplied viewStates map so another view's selection survives the write", async () => {
9024+
// The registered viewStates map is host-owned: this provider rewrites it from a full read of the
9025+
// map (savePersistedViewState, clearPersistedViewState, prunePersistedViewStates), so a map that a
9026+
// caller hands to a settings write - the webview's newTask configuration, or the extension API's -
9027+
// would replace every view's durable selection in one write, and an unset would erase it outright.
9028+
// The key is dropped; the rest of the settings object is the caller's to set.
9029+
const log = vi.spyOn(provider, "log").mockImplementation(() => {})
9030+
await provider.contextProxy.setValue("viewStates", {
9031+
"stable-editor-view": { mode: "code", updatedAt: 2 },
9032+
})
9033+
9034+
await provider.setValues(
9035+
hand({ viewStates: { "crafted-view": { mode: "ask", updatedAt: 3 } }, customInstructions: "keep me" }),
9036+
)
9037+
9038+
expect(mockContext.globalState.get<RooCodeSettings["viewStates"]>("viewStates")).toEqual({
9039+
"stable-editor-view": { mode: "code", updatedAt: 2 },
9040+
})
9041+
expect(mockContext.globalState.get("customInstructions")).toBe("keep me")
9042+
expect(log).toHaveBeenCalledWith(
9043+
expect.stringContaining("[setValues] Ignored a caller-supplied viewStates map"),
9044+
)
9045+
})
90229046
})
90239047

90249048
test("getState returns the saved destructive command guard setting", async () => {

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2116,6 +2116,28 @@ it("persists alwaysDenyUnapprovedCommands through the generic updateSettings loo
21162116
expect(mockClineProvider.postStateToWebview).toHaveBeenCalledTimes(1)
21172117
})
21182118

2119+
// The registered viewStates map is host-owned: ClineProvider.setValues drops a caller-supplied map
2120+
// because the provider rewrites that map from a full read of it. This loop writes through ContextProxy
2121+
// directly, so the same key has to be dropped here too - a webview-supplied map would replace every
2122+
// view's durable selection, not just this view's, and an unset would erase the map.
2123+
it("ignores a webview-supplied viewStates map in the generic updateSettings loop", async () => {
2124+
vi.clearAllMocks()
2125+
2126+
await webviewMessageHandler(mockClineProvider, {
2127+
type: "updateSettings",
2128+
updatedSettings: {
2129+
viewStates: { "crafted-view": { mode: "ask" } },
2130+
alwaysDenyUnapprovedCommands: true,
2131+
},
2132+
})
2133+
2134+
expect(mockClineProvider.contextProxy.setValue).not.toHaveBeenCalledWith("viewStates", expect.anything())
2135+
expect(mockClineProvider.contextProxy.setValue).toHaveBeenCalledWith("alwaysDenyUnapprovedCommands", true)
2136+
expect(mockClineProvider.log).toHaveBeenCalledWith(
2137+
expect.stringContaining("Ignored a webview-supplied viewStates map"),
2138+
)
2139+
})
2140+
21192141
// Both allowlists are normalized by the same branch, so both are held to the
21202142
// same contract.
21212143
describe.each(["allowedReadFiles", "allowedWriteFiles"] as const)("webviewMessageHandler - %s", (key) => {

‎src/core/webview/webviewMessageHandler.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -782,6 +782,18 @@ export const webviewMessageHandler = async (
782782
continue
783783
}
784784

785+
// The registered viewStates map is host-owned. ClineProvider.setValues drops a caller-supplied
786+
// map because the provider rewrites that map from a full read of it; this loop writes through
787+
// ContextProxy directly, so the same key has to be dropped here too - a webview-supplied map
788+
// would replace every view's durable selection, not just this view's, and an unset would erase
789+
// the map. The key is dropped before any side effect is run for it.
790+
if (key === "viewStates") {
791+
provider.log(
792+
"[updateSettings] Ignored a webview-supplied viewStates map; the registered map is written by the extension host.",
793+
)
794+
continue
795+
}
796+
785797
let newValue = value
786798

787799
if (key === "language") {

0 commit comments

Comments
 (0)