Skip to content
Myr

fix: throw on reads of a revoked Map or Set draft (#1298)

Landed as 8848a5b

Once produce or finishDraft returns, the draft is revoked and any further use is meant to fail loudly. Object and array drafts get that from Proxy.revocable, but Map and Set drafts are subclasses guarded by a manual revoked_ flag, and several read paths never checked it: const map = new Map([["a", 1]]) let draft produce({map}, d => { draft = d.map }) draft.has("a") // true, should throw draft.size // 1, should throw [...draft.keys()] // ["a"], should throw draft.get("a") // throws, as expected DraftSet.has already calls assertUnrevoked, so the same read on a Set draft throws while the Map one silently returns stale data. This is the exact async-leak case error 3 is meant to catch, so it should not depend on which accessor the caller happens to use. Add assertUnrevoked to DraftMap.has, DraftMap.size, DraftMap.keys, DraftMap.forEach and DraftSet.size. DraftMap.values and DraftMap.entries build on keys, so they now fail when they are called rather than on the first next(). Nothing outside the plugin reads these accessors after revocation, and the full suite is unchanged.

authored by Bao Nguyen · committed by the platform · parent 02e87e7 · Browse files · 2 files+33 added−4 removed
This landing

State

Landed as 8848a5b

By oscabriel

Diff stats

2 files+33 added−4 removed

  • tests:+20 added−0 removed
  • other:+13 added−4 removed

2 files+33 added−4 removed

modified:__tests__/base.js+20 added
2423 unmodified lines
2424 expect(() => m.get("x")).toThrowErrorMatchingSnapshot()2425 expect(() => m.set("x", 3)).toThrowErrorMatchingSnapshot()2426 })24272428 it("revokes map proxies for reads that do not go through get", () => {2429 let m2430 produce(baseState, s => {2431 m = s.aMap2432 })2433 const revoked = /has been revoked|minified error nr: 3/2434 expect(() => m.has("jedi")).toThrow(revoked)2435 expect(() => m.size).toThrow(revoked)2436 expect(() => m.keys()).toThrow(revoked)2437 expect(() => m.forEach(() => {})).toThrow(revoked)2438 })24392440 it("does not draft map keys", () => {2441 // anything else would be terribly confusing2442 const key = {a: 1}2443 const map = new Map([[key, 2]])
296 unmodified lines
2740 expect(() => m.has("x")).toThrowErrorMatchingSnapshot()2741 expect(() => m.add("x")).toThrowErrorMatchingSnapshot()2742 })27432744 it("revokes set size reads", () => {2745 let m2746 produce(baseState, s => {2747 m = s.aSet2748 })2749 expect(() => m.size).toThrow(/has been revoked|minified error nr: 3/)2750 })27512752 it("does support instanceof Set", () => {2753 const set = new Set()2754 produce(set, d => {2755 expect(d instanceof Set).toBeTruthy()
1660 unmodified lines
modified:src/plugins/mapset.ts+13 added-4 removed
52 unmodified lines
53 }54 }5556 get size(): number {57 const state: MapState = this[DRAFT_STATE]58 assertUnrevoked(state)57 return latest(this[DRAFT_STATE]).size59 return latest(state).size60 }6162 has(key: any): boolean {63 const state: MapState = this[DRAFT_STATE]64 assertUnrevoked(state)61 return latest(this[DRAFT_STATE]).has(key)65 return latest(state).has(key)66 }6768 set(key: any, value: any) {69 const state: MapState = this[DRAFT_STATE]
42 unmodified lines
112 }113114 forEach(cb: (value: any, key: any, self: any) => void, thisArg?: any) {115 const state: MapState = this[DRAFT_STATE]116 assertUnrevoked(state)117 latest(state).forEach((_value: any, key: any, _map: any) => {118 cb.call(thisArg, this.get(key), key, this)119 })120 }
15 unmodified lines
136 return draft137 }138139 keys(): IterableIterator<any> {140 const state: MapState = this[DRAFT_STATE]141 assertUnrevoked(state)135 return latest(this[DRAFT_STATE]).keys()142 return latest(state).keys()143 }144145 values(): IterableIterator<any> {146 const iterator = this.keys()
84 unmodified lines
231 }232 }233234 get size(): number {235 const state: SetState = this[DRAFT_STATE]236 assertUnrevoked(state)228 return latest(this[DRAFT_STATE]).size237 return latest(state).size238 }239240 has(value: any): boolean {241 const state: SetState = this[DRAFT_STATE]
124 unmodified lines