Skip to content

Commit 29f4078

Browse files
committed
fix(webview): validate the webview-supplied view state id before adopting it
The stable view state id arrives over the webview postMessage channel, so the declared string type is not a guarantee: a crafted message could carry a non-string value (which the sanitizer would crash on), an unbounded string (unbounded growth in the shared viewStates map), or an id another live view already owns (adopting it would load that view's pinned profile, overlay and mode here, and let this webview overwrite that view's persisted entry). setViewStateId now rejects non-strings, bounds the key length, and refuses ids claimed by another live instance; a refused view stays on its temporary id and keeps working against the shared state. Proofs: ClineProvider.sticky-mode.spec 32/32 (3 new tests: non-string, oversized, claimed-by-another-view); negative controls: disabling the ownership check reddens exactly the claim test, widening the length bound reddens exactly the oversized test, restores byte-exact (hash match); eslint --max-warnings=0 clean; tsc clean except the two known local telemetry-dist noise lines; prettier ALL-MATCHED on touched files.
1 parent 3643cea commit 29f4078

2 files changed

Lines changed: 71 additions & 1 deletion

File tree

‎src/core/webview/ClineProvider.ts‎

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ export class ClineProvider
216216
private static activeInstances: Set<ClineProvider> = new Set()
217217
private static nextViewId = 0
218218
private static readonly MAX_PERSISTED_VIEW_STATES = 50
219+
private static readonly MAX_VIEW_STATE_ID_LENGTH = 64
219220
private static persistedViewStateWriteQueue: Promise<void> = Promise.resolve()
220221
private disposables: vscode.Disposable[] = []
221222
private webviewDisposables: vscode.Disposable[] = []
@@ -707,7 +708,18 @@ export class ClineProvider
707708
* The identifier is sanitized so it remains a safe object key in the shared viewStates map.
708709
*/
709710
public async setViewStateId(viewStateId: string | undefined): Promise<void> {
710-
const normalizedViewStateId = viewStateId?.trim().replace(/[^A-Za-z0-9_-]/g, "_")
711+
// The webview generates this id on purpose - it has to survive a webview reload,
712+
// which recreates the view on both sides - so the host cannot mint it instead.
713+
// What the host owns is which view may hold a given persisted key, so everything
714+
// below validates the supplied value before it becomes one.
715+
if (typeof viewStateId !== "string") {
716+
// A crafted postMessage can carry any JSON value here even though the message
717+
// type declares a string; the persisted key must stay a string.
718+
this.log(`[setViewStateId] Ignoring a non-string viewStateId for view ${this.viewId}.`)
719+
return
720+
}
721+
722+
const normalizedViewStateId = viewStateId.trim().replace(/[^A-Za-z0-9_-]/g, "_")
711723

712724
if (
713725
!normalizedViewStateId ||
@@ -721,6 +733,29 @@ export class ClineProvider
721733
return
722734
}
723735

736+
// Bound the key: the webview generates a UUID, so a much longer value is not a
737+
// view id, and an unbounded key is unbounded growth in the shared viewStates map.
738+
if (normalizedViewStateId.length > ClineProvider.MAX_VIEW_STATE_ID_LENGTH) {
739+
this.log(
740+
`[setViewStateId] Ignoring an oversized viewStateId (${normalizedViewStateId.length} chars) for view ${this.viewId}.`,
741+
)
742+
return
743+
}
744+
745+
const claimant = ClineProvider.getAllInstances().find(
746+
(instance) => instance !== this && instance.viewStateId === normalizedViewStateId,
747+
)
748+
if (claimant) {
749+
// Ownership check: a key another live view holds must not be adopted. Doing so
750+
// would load that view's pinned profile, nested overlay, and mode into this
751+
// provider, and this webview could then overwrite that view's persisted entry.
752+
// Staying on the temporary id keeps this view working against the shared state.
753+
this.log(
754+
`[setViewStateId] Refusing viewStateId ${normalizedViewStateId} for view ${this.viewId}: already claimed by live view ${claimant.viewId}.`,
755+
)
756+
return
757+
}
758+
724759
const previousViewStateId = this.viewStateId
725760

726761
this.viewStateId = normalizedViewStateId

‎src/core/webview/__tests__/ClineProvider.sticky-mode.spec.ts‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,8 @@ vi.mock("@roo-code/cloud", () => ({
121121
get instance() {
122122
return {
123123
isAuthenticated: vi.fn().mockReturnValue(false),
124+
on: vi.fn(),
125+
off: vi.fn(),
124126
}
125127
},
126128
},
@@ -1678,4 +1680,37 @@ describe("ClineProvider - Sticky Mode", () => {
16781680
})
16791681
})
16801682
})
1683+
1684+
describe("setViewStateId input validation", () => {
1685+
it("ignores a non-string viewStateId from a crafted message", async () => {
1686+
const before = provider["viewStateId"]
1687+
// A crafted postMessage can carry any JSON value despite the declared type.
1688+
await provider.setViewStateId(42 as unknown as string)
1689+
expect(provider["viewStateId"]).toBe(before)
1690+
})
1691+
1692+
it("ignores an oversized viewStateId", async () => {
1693+
const before = provider["viewStateId"]
1694+
await provider.setViewStateId("x".repeat(200))
1695+
expect(provider["viewStateId"]).toBe(before)
1696+
})
1697+
1698+
it("refuses a viewStateId already claimed by another live view", async () => {
1699+
const other = new ClineProvider(
1700+
mockContext,
1701+
mockOutputChannel,
1702+
"sidebar",
1703+
new ContextProxy(mockContext),
1704+
new WebviewFocusTracker(),
1705+
)
1706+
try {
1707+
await other.setViewStateId("claimed-by-other-view")
1708+
await provider.setViewStateId("claimed-by-other-view")
1709+
expect(provider["viewStateId"]).not.toBe("claimed-by-other-view")
1710+
expect(other["viewStateId"]).toBe("claimed-by-other-view")
1711+
} finally {
1712+
await other.dispose()
1713+
}
1714+
})
1715+
})
16811716
})

0 commit comments

Comments
 (0)