Skip to content

Commit 8e89ee0

Browse files
committed
fix(webview): apply extension-side normalization to persisted view-state ids
getViewStateId now trims and rewrites unsafe characters before reuse, mirroring ClineProvider.setViewStateId, and rejects whitespace-only and __proto__ values by generating a fresh id. Regression coverage: normalized reuse, whitespace-only, and __proto__.
1 parent d5054f2 commit 8e89ee0

2 files changed

Lines changed: 60 additions & 2 deletions

File tree

‎webview-ui/src/utils/__tests__/vscode.spec.ts‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,58 @@ describe("VSCodeAPIWrapper", () => {
214214
expect(JSON.parse(storage.getItem("vscodeState")!)).toEqual({ viewStateId: "replaced-string-view" })
215215
})
216216

217+
it("returns the normalized id when the persisted id needs the extension-side cleanup", () => {
218+
const randomUUID = vi.fn(() => "should-not-be-generated")
219+
Object.defineProperty(globalThis, "crypto", {
220+
configurable: true,
221+
value: { randomUUID },
222+
})
223+
// ClineProvider.setViewStateId trims and rewrites unsafe characters, so the
224+
// webview must report the same normalized id instead of the raw stored value.
225+
const storage = createMockStorage({ vscodeState: JSON.stringify({ viewStateId: " side 1 " }) })
226+
Object.defineProperty(globalThis, "localStorage", {
227+
configurable: true,
228+
value: storage,
229+
})
230+
const wrapper = new VSCodeAPIWrapper()
231+
232+
expect(wrapper.getViewStateId()).toBe("side_1")
233+
expect(wrapper.getViewStateId()).toBe("side_1")
234+
expect(randomUUID).not.toHaveBeenCalled()
235+
})
236+
237+
it("generates a fresh id when the persisted id is whitespace-only", () => {
238+
Object.defineProperty(globalThis, "crypto", {
239+
configurable: true,
240+
value: { randomUUID: vi.fn(() => "ws-refilled-view") },
241+
})
242+
const storage = createMockStorage({ vscodeState: JSON.stringify({ viewStateId: " " }) })
243+
Object.defineProperty(globalThis, "localStorage", {
244+
configurable: true,
245+
value: storage,
246+
})
247+
const wrapper = new VSCodeAPIWrapper()
248+
249+
expect(wrapper.getViewStateId()).toBe("ws-refilled-view")
250+
expect(JSON.parse(storage.getItem("vscodeState")!)).toEqual({ viewStateId: "ws-refilled-view" })
251+
})
252+
253+
it("generates a fresh id when the persisted id is the __proto__ string", () => {
254+
Object.defineProperty(globalThis, "crypto", {
255+
configurable: true,
256+
value: { randomUUID: vi.fn(() => "proto-refused-view") },
257+
})
258+
const storage = createMockStorage({ vscodeState: JSON.stringify({ viewStateId: "__proto__" }) })
259+
Object.defineProperty(globalThis, "localStorage", {
260+
configurable: true,
261+
value: storage,
262+
})
263+
const wrapper = new VSCodeAPIWrapper()
264+
265+
expect(wrapper.getViewStateId()).toBe("proto-refused-view")
266+
expect(JSON.parse(storage.getItem("vscodeState")!)).toEqual({ viewStateId: "proto-refused-view" })
267+
})
268+
217269
it("keeps one generated id when a write fails while stale persisted state stays readable", () => {
218270
const randomUUID = vi.fn().mockReturnValueOnce("gen-one").mockReturnValueOnce("gen-two")
219271
Object.defineProperty(globalThis, "crypto", {

‎webview-ui/src/utils/vscode.ts‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,14 @@ export class VSCodeAPIWrapper {
5252
: {}
5353
const existingViewStateId = stateObject.viewStateId
5454

55-
if (typeof existingViewStateId === "string" && existingViewStateId.length > 0) {
56-
return existingViewStateId
55+
// Mirror the ClineProvider.setViewStateId normalization so the webview never
56+
// registers an id the extension would rewrite or reject: trim, replace unsafe
57+
// characters, and drop whitespace-only and "__proto__" values.
58+
const normalizedViewStateId =
59+
typeof existingViewStateId === "string" ? existingViewStateId.trim().replace(/[^A-Za-z0-9_-]/g, "_") : ""
60+
61+
if (normalizedViewStateId && normalizedViewStateId !== "__proto__") {
62+
return normalizedViewStateId
5763
}
5864

5965
const viewStateId = this.createViewStateId()

0 commit comments

Comments
 (0)