Skip to content

Commit 06aab59

Browse files
fix(webview): refuse a stored view id the host would not accept
getViewStateId() reused any stored string that survived normalization, so a persisted id longer than 64 characters came back on every launch while the extension refused to store it: the "Ignoring an oversized viewStateId" log (ClineProvider.ts:945-947) meant this view stayed on its pre-launch temporary id forever, which is the stranded state the reuse path exists to avoid. The wrapper now declares MAX_VIEW_STATE_ID_LENGTH = 64 - the host keeps its own copy private and the webview cannot import it - and refuses an id above it, while still reusing the largest legal id rather than minting a new one away from real per-view state. The comment also claimed the host rejects every Object.prototype member name. It does not: setViewStateId refuses only "__proto__" by name (ClineProvider.ts:938). The webview still refuses every prototype member, deliberately stricter than the host rather than a mirror: those names are useless keys on this side, because writing them into the state object goes through the prototype setter and the later spread drops them. The length cap is the part that mirrors the host, and the comment now says which is which. The same overstated claim in the prototype-member case comment was corrected too. Test first: both cases were written against the constant before the cap existed, and the oversize case failed exactly as expected - 1 failed / 17 passed, returning the stored 65-character id. Control with only the length disjunct removed: 1 failed / 17 passed, failing only "generates and persists a fresh id when the stored id is longer than the host limit". vscode.spec.ts is 16 passed before this commit and 18 after. The whole webview-ui suite is green: 188 files / 2508 tests. tsc clean (root typescript against the webview-ui project), eslint clean on both files, prettier clean; src/ is untouched here so src/eslint-suppressions.json is unchanged. Answers #1560 review row 4477606394.
1 parent 4fb086f commit 06aab59

2 files changed

Lines changed: 58 additions & 9 deletions

File tree

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

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,10 @@ describe("VSCodeAPIWrapper", () => {
6666
})
6767
const wrapper = new VSCodeAPIWrapper()
6868

69-
// The host's setViewStateId rejects every Object.prototype member name; accepting
70-
// "constructor" here would make every launch message rejected and strand the
69+
// The host refuses only "__proto__" by name (ClineProvider.ts:938), so "constructor" survives
70+
// its normalization. This side refuses every Object.prototype member anyway - stricter than
71+
// the host on purpose - because such an id is a useless key: writing it into the state object
72+
// goes through the prototype setter and the later spread drops it, stranding this view's
7173
// pre-launch entry under the temporary id forever.
7274
expect(wrapper.getViewStateId()).toBe("generated-view")
7375
expect(JSON.parse(storage.getItem("vscodeState")!)).toMatchObject({ viewStateId: "generated-view" })
@@ -285,6 +287,40 @@ describe("VSCodeAPIWrapper", () => {
285287
expect(JSON.parse(storage.getItem("vscodeState")!)).toEqual({ viewStateId: "proto-refused-view" })
286288
})
287289

290+
it("generates and persists a fresh id when the stored id is longer than the host limit", () => {
291+
Object.defineProperty(globalThis, "crypto", {
292+
configurable: true,
293+
value: { randomUUID: vi.fn(() => "generated-after-oversize") },
294+
})
295+
const oversized = "a".repeat(VSCodeAPIWrapper.MAX_VIEW_STATE_ID_LENGTH + 1)
296+
const storage = createMockStorage({ vscodeState: JSON.stringify({ viewStateId: oversized }) })
297+
Object.defineProperty(globalThis, "localStorage", {
298+
configurable: true,
299+
value: storage,
300+
})
301+
const wrapper = new VSCodeAPIWrapper()
302+
303+
// The host logs "Ignoring an oversized viewStateId" and returns without storing the id
304+
// (ClineProvider.ts:945-947), so reusing an oversized value here would strand this view on
305+
// its pre-launch id at every launch - the exact state this reuse path exists to avoid.
306+
expect(wrapper.getViewStateId()).toBe("generated-after-oversize")
307+
expect(JSON.parse(storage.getItem("vscodeState")!)).toEqual({ viewStateId: "generated-after-oversize" })
308+
})
309+
310+
it("reuses a stored id that sits exactly on the host limit", () => {
311+
const atLimit = "b".repeat(VSCodeAPIWrapper.MAX_VIEW_STATE_ID_LENGTH)
312+
const storage = createMockStorage({ vscodeState: JSON.stringify({ viewStateId: atLimit }) })
313+
Object.defineProperty(globalThis, "localStorage", {
314+
configurable: true,
315+
value: storage,
316+
})
317+
const wrapper = new VSCodeAPIWrapper()
318+
319+
// The host rejects values strictly longer than 64, so 64 must still be reused rather than
320+
// minted away: a cap that also drops the largest legal id would discard real per-view state.
321+
expect(wrapper.getViewStateId()).toBe(atLimit)
322+
expect(JSON.parse(storage.getItem("vscodeState")!)).toEqual({ viewStateId: atLimit })
323+
})
288324
it("keeps one generated id when a write fails while stale persisted state stays readable", () => {
289325
const randomUUID = vi.fn().mockReturnValueOnce("gen-one").mockReturnValueOnce("gen-two")
290326
Object.defineProperty(globalThis, "crypto", {

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

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@ export class VSCodeAPIWrapper {
1919
// for getState() until a write succeeds again.
2020
private storageWriteFailed: boolean = false
2121

22+
// The extension host caps a persisted view id at 64 characters: ClineProvider declares
23+
// MAX_VIEW_STATE_ID_LENGTH = 64 and refuses a longer value with the "Ignoring an oversized
24+
// viewStateId" log instead of storing it, so a longer value cannot become this view's id.
25+
// The host keeps its own copy private and the webview cannot import it, so the number is
26+
// restated here; vscode.spec.ts pins both the limit and the reuse of the largest legal id.
27+
public static readonly MAX_VIEW_STATE_ID_LENGTH = 64
28+
2229
constructor() {
2330
// Check if the acquireVsCodeApi function exists in the current development
2431
// context (i.e. VS Code development window or web browser)
@@ -52,16 +59,22 @@ export class VSCodeAPIWrapper {
5259
: {}
5360
const existingViewStateId = stateObject.viewStateId
5461

55-
// Mirror the ClineProvider.setViewStateId validation so the webview never
56-
// registers an id the extension would reject: trim, replace unsafe
57-
// characters, and drop whitespace-only values plus every Object.prototype
58-
// member name. "__proto__" alone is not enough: ids like "constructor" or
59-
// "toString" survive the host's normalization and would be refused there
60-
// forever, stranding this view's pre-launch entry under the temporary id.
62+
// Match the extension host's setViewStateId so this view never registers an id the
63+
// extension would refuse: same trim and same unsafe-character replacement, drop
64+
// whitespace-only values, and refuse an id over MAX_VIEW_STATE_ID_LENGTH, which the
65+
// host logs and refuses without storing (ClineProvider.ts:945-947). The host rejects
66+
// only "__proto__" by name (ClineProvider.ts:938) while this side refuses every
67+
// Object.prototype member - deliberately stricter, not a mirror: those names are
68+
// useless keys here because writing them into the state object goes through the
69+
// prototype setter and the later spread drops them. The length cap is the mirror.
6170
const normalizedViewStateId =
6271
typeof existingViewStateId === "string" ? existingViewStateId.trim().replace(/[^A-Za-z0-9_-]/g, "_") : ""
6372

64-
if (normalizedViewStateId && !Object.prototype.hasOwnProperty.call(Object.prototype, normalizedViewStateId)) {
73+
if (
74+
normalizedViewStateId &&
75+
normalizedViewStateId.length <= VSCodeAPIWrapper.MAX_VIEW_STATE_ID_LENGTH &&
76+
!Object.prototype.hasOwnProperty.call(Object.prototype, normalizedViewStateId)
77+
) {
6578
return normalizedViewStateId
6679
}
6780

0 commit comments

Comments
 (0)