patch.js
This file
Path
__tests__/patch.js
1,305 lines · 24.3 KiB · JavaScript
Last changed by
fix(security): Follow up on CVE-2020-28477 where `path: [["__proto__"], "x"]` could still pollute the prototypeMichel Weststratebefore adoptionfa671e5
History of this fileTake it
1"use strict"2import produce, {3 setUseProxies,4 applyPatches,5 produceWithPatches,6 enableAllPlugins,7 isDraft,8 immerable,9 nothing10} from "../src/immer"1112enableAllPlugins()1314jest.setTimeout(1000)1516const isProd = process.env.NODE_ENV === "production"1718function runPatchTest(base, producer, patches, inversePathes) {19 let resultProxies, resultEs52021 function runPatchTestHelper() {22 let recordedPatches23 let recordedInversePatches24 const res = produce(base, producer, (p, i) => {25 recordedPatches = p26 recordedInversePatches = i27 })2829 test("produces the correct patches", () => {30 expect(recordedPatches).toEqual(patches)31 if (inversePathes) expect(recordedInversePatches).toEqual(inversePathes)32 })3334 test("patches are replayable", () => {35 expect(applyPatches(base, recordedPatches)).toEqual(res)36 })3738 test("patches can be reversed", () => {39 expect(applyPatches(res, recordedInversePatches)).toEqual(base)40 })4142 return res43 }4445 describe(`proxy`, () => {46 setUseProxies(true)47 resultProxies = runPatchTestHelper()48 })4950 describe(`es5`, () => {51 setUseProxies(false)52 resultEs5 = runPatchTestHelper()53 test("ES5 and Proxy implementation yield same result", () => {54 expect(resultEs5).toEqual(resultProxies)55 })56 })5758 return resultProxies59}6061describe("applyPatches", () => {62 it("mutates the base state when it is a draft", () => {63 produce({a: 1}, draft => {64 const result = applyPatches(draft, [65 {op: "replace", path: ["a"], value: 2}66 ])67 expect(result).toBe(draft)68 expect(draft.a).toBe(2)69 })70 })71 it("produces a copy of the base state when not a draft", () => {72 const base = {a: 1}73 const result = applyPatches(base, [{op: "replace", path: ["a"], value: 2}])74 expect(result).not.toBe(base)75 expect(result.a).toBe(2)76 expect(base.a).toBe(1)77 })78 it('throws when `op` is not "add", "replace", nor "remove"', () => {79 expect(() => {80 const patch = {op: "copy", from: [0], path: [1]}81 applyPatches([2], [patch])82 }).toThrowErrorMatchingSnapshot()83 })84 it("throws when `path` cannot be resolved", () => {85 // missing parent86 expect(() => {87 const patch = {op: "add", path: ["a", "b"], value: 1}88 applyPatches({}, [patch])89 }).toThrowErrorMatchingSnapshot()9091 // missing grand-parent92 expect(() => {93 const patch = {op: "add", path: ["a", "b", "c"], value: 1}94 applyPatches({}, [patch])95 }).toThrowErrorMatchingSnapshot()96 })97 it("applied patches cannot be modified", () => {98 // see also: https://github.com/immerjs/immer/issues/41199 const s0 = {100 items: [1]101 }102103 const [s1, p1] = produceWithPatches(s0, draft => {104 draft.items = []105 })106107 const replaceValueBefore = p1[0].value.slice()108109 const [s2, p2] = produceWithPatches(s1, draft => {110 draft.items.push(2)111 })112113 applyPatches(s0, [...p1, ...p2])114115 const replaceValueAfter = p1[0].value.slice()116117 expect(replaceValueAfter).toStrictEqual(replaceValueBefore)118 })119})120121describe("simple assignment - 1", () => {122 runPatchTest(123 {x: 3},124 d => {125 d.x++126 },127 [{op: "replace", path: ["x"], value: 4}]128 )129})130131describe("simple assignment - 2", () => {132 runPatchTest(133 {x: {y: 4}},134 d => {135 d.x.y++136 },137 [{op: "replace", path: ["x", "y"], value: 5}]138 )139})140141describe("simple assignment - 3", () => {142 runPatchTest(143 {x: [{y: 4}]},144 d => {145 d.x[0].y++146 },147 [{op: "replace", path: ["x", 0, "y"], value: 5}]148 )149})150151describe("simple assignment - 4", () => {152 runPatchTest(153 new Map([["x", {y: 4}]]),154 d => {155 d.get("x").y++156 },157 [{op: "replace", path: ["x", "y"], value: 5}],158 [{op: "replace", path: ["x", "y"], value: 4}]159 )160})161162describe("simple assignment - 5", () => {163 runPatchTest(164 {x: new Map([["y", 4]])},165 d => {166 d.x.set("y", 5)167 },168 [{op: "replace", path: ["x", "y"], value: 5}],169 [{op: "replace", path: ["x", "y"], value: 4}]170 )171})172173describe("simple assignment - 6", () => {174 runPatchTest(175 new Map([["x", 1]]),176 d => {177 // Map.prototype.set should return the Map itself178 const res = d.set("x", 2)179 res.set("y", 3)180 },181 [182 {op: "replace", path: ["x"], value: 2},183 {op: "add", path: ["y"], value: 3}184 ],185 [186 {op: "replace", path: ["x"], value: 1},187 {op: "remove", path: ["y"]}188 ]189 )190})191192describe("simple assignment - 7", () => {193 const key1 = {prop: "val1"}194 const key2 = {prop: "val2"}195 runPatchTest(196 {x: new Map([[key1, 4]])},197 d => {198 d.x.set(key1, 5)199 d.x.set(key2, 6)200 },201 [202 {op: "replace", path: ["x", key1], value: 5},203 {op: "add", path: ["x", key2], value: 6}204 ],205 [206 {op: "replace", path: ["x", key1], value: 4},207 {op: "remove", path: ["x", key2]}208 ]209 )210})211212describe("delete 1", () => {213 runPatchTest(214 {x: {y: 4}},215 d => {216 delete d.x217 },218 [{op: "remove", path: ["x"]}]219 )220})221222describe("delete 2", () => {223 runPatchTest(224 new Map([["x", 1]]),225 d => {226 d.delete("x")227 },228 [{op: "remove", path: ["x"]}],229 [{op: "add", path: ["x"], value: 1}]230 )231})232233describe("delete 3", () => {234 runPatchTest(235 {x: new Map([["y", 1]])},236 d => {237 d.x.delete("y")238 },239 [{op: "remove", path: ["x", "y"]}],240 [{op: "add", path: ["x", "y"], value: 1}]241 )242})243244describe("delete 5", () => {245 const key1 = {prop: "val1"}246 const key2 = {prop: "val2"}247 runPatchTest(248 {249 x: new Map([250 [key1, 1],251 [key2, 2]252 ])253 },254 d => {255 d.x.delete(key1)256 d.x.delete(key2)257 },258 [259 {op: "remove", path: ["x", key1]},260 {op: "remove", path: ["x", key2]}261 ],262 [263 {op: "add", path: ["x", key1], value: 1},264 {op: "add", path: ["x", key2], value: 2}265 ]266 )267})268269describe("delete 6", () => {270 runPatchTest(271 new Set(["x", 1]),272 d => {273 d.delete("x")274 },275 [{op: "remove", path: [0], value: "x"}],276 [{op: "add", path: [0], value: "x"}]277 )278})279280describe("delete 7", () => {281 runPatchTest(282 {x: new Set(["y", 1])},283 d => {284 d.x.delete("y")285 },286 [{op: "remove", path: ["x", 0], value: "y"}],287 [{op: "add", path: ["x", 0], value: "y"}]288 )289})290291describe("renaming properties", () => {292 describe("nested object (no changes)", () => {293 runPatchTest(294 {a: {b: 1}},295 d => {296 d.x = d.a297 delete d.a298 },299 [300 {op: "add", path: ["x"], value: {b: 1}},301 {op: "remove", path: ["a"]}302 ]303 )304 })305306 describe("nested change in object", () => {307 runPatchTest(308 {309 a: {b: 1}310 },311 d => {312 d.a.b++313 },314 [{op: "replace", path: ["a", "b"], value: 2}],315 [{op: "replace", path: ["a", "b"], value: 1}]316 )317 })318319 describe("nested change in map", () => {320 runPatchTest(321 new Map([["a", new Map([["b", 1]])]]),322 d => {323 d.get("a").set("b", 2)324 },325 [{op: "replace", path: ["a", "b"], value: 2}],326 [{op: "replace", path: ["a", "b"], value: 1}]327 )328 })329330 describe("nested change in array", () => {331 runPatchTest(332 [[{b: 1}]],333 d => {334 d[0][0].b++335 },336 [{op: "replace", path: [0, 0, "b"], value: 2}],337 [{op: "replace", path: [0, 0, "b"], value: 1}]338 )339 })340341 describe("nested map (no changes)", () => {342 runPatchTest(343 new Map([["a", new Map([["b", 1]])]]),344 d => {345 d.set("x", d.get("a"))346 d.delete("a")347 },348 [349 {op: "add", path: ["x"], value: new Map([["b", 1]])},350 {op: "remove", path: ["a"]}351 ],352 [353 {op: "remove", path: ["x"]},354 {op: "add", path: ["a"], value: new Map([["b", 1]])}355 ]356 )357 })358359 describe("nested object (with changes)", () => {360 runPatchTest(361 {a: {b: 1, c: 1}},362 d => {363 let a = d.a364 a.b = 2 // change365 delete a.c // delete366 a.y = 2 // add367368 // rename369 d.x = a370 delete d.a371 },372 [373 {op: "add", path: ["x"], value: {b: 2, y: 2}},374 {op: "remove", path: ["a"]}375 ]376 )377 })378379 describe("nested map (with changes)", () => {380 runPatchTest(381 new Map([382 [383 "a",384 new Map([385 ["b", 1],386 ["c", 1]387 ])388 ]389 ]),390 d => {391 let a = d.get("a")392 a.set("b", 2) // change393 a.delete("c") // delete394 a.set("y", 2) // add395396 // rename397 d.set("x", a)398 d.delete("a")399 },400 [401 {402 op: "add",403 path: ["x"],404 value: new Map([405 ["b", 2],406 ["y", 2]407 ])408 },409 {op: "remove", path: ["a"]}410 ],411 [412 {op: "remove", path: ["x"]},413 {414 op: "add",415 path: ["a"],416 value: new Map([417 ["b", 1],418 ["c", 1]419 ])420 }421 ]422 )423 })424425 describe("deeply nested object (with changes)", () => {426 runPatchTest(427 {a: {b: {c: 1, d: 1}}},428 d => {429 let b = d.a.b430 b.c = 2 // change431 delete b.d // delete432 b.y = 2 // add433434 // rename435 d.a.x = b436 delete d.a.b437 },438 [439 {op: "add", path: ["a", "x"], value: {c: 2, y: 2}},440 {op: "remove", path: ["a", "b"]}441 ]442 )443 })444445 describe("deeply nested map (with changes)", () => {446 runPatchTest(447 new Map([448 [449 "a",450 new Map([451 [452 "b",453 new Map([454 ["c", 1],455 ["d", 1]456 ])457 ]458 ])459 ]460 ]),461 d => {462 let b = d.get("a").get("b")463 b.set("c", 2) // change464 b.delete("d") // delete465 b.set("y", 2) // add466467 // rename468 d.get("a").set("x", b)469 d.get("a").delete("b")470 },471 [472 {473 op: "add",474 path: ["a", "x"],475 value: new Map([476 ["c", 2],477 ["y", 2]478 ])479 },480 {op: "remove", path: ["a", "b"]}481 ],482 [483 {op: "remove", path: ["a", "x"]},484 {485 op: "add",486 path: ["a", "b"],487 value: new Map([488 ["c", 1],489 ["d", 1]490 ])491 }492 ]493 )494 })495})496497describe("minimum amount of changes", () => {498 runPatchTest(499 {x: 3, y: {a: 4}, z: 3},500 d => {501 d.y.a = 4502 d.y.b = 5503 Object.assign(d, {x: 4, y: {a: 2}})504 },505 [506 {op: "replace", path: ["x"], value: 4},507 {op: "replace", path: ["y"], value: {a: 2}}508 ]509 )510})511512describe("arrays - prepend", () => {513 runPatchTest(514 {x: [1, 2, 3]},515 d => {516 d.x.unshift(4)517 },518 [519 {op: "replace", path: ["x", 0], value: 4},520 {op: "replace", path: ["x", 1], value: 1},521 {op: "replace", path: ["x", 2], value: 2},522 {op: "add", path: ["x", 3], value: 3}523 ]524 )525})526527describe("arrays - multiple prepend", () => {528 runPatchTest(529 {x: [1, 2, 3]},530 d => {531 d.x.unshift(4)532 d.x.unshift(5)533 // 4,5,1,2,3534 },535 [536 {op: "replace", path: ["x", 0], value: 5},537 {op: "replace", path: ["x", 1], value: 4},538 {op: "replace", path: ["x", 2], value: 1},539 {op: "add", path: ["x", 3], value: 2},540 {op: "add", path: ["x", 4], value: 3}541 ]542 )543})544545describe("arrays - splice middle", () => {546 runPatchTest(547 {x: [1, 2, 3]},548 d => {549 d.x.splice(1, 1)550 },551 [552 {op: "replace", path: ["x", 1], value: 3},553 {op: "replace", path: ["x", "length"], value: 2}554 ]555 )556})557558describe("arrays - multiple splice", () => {559 runPatchTest(560 [0, 1, 2, 3, 4, 5, 0],561 d => {562 d.splice(4, 2, 3)563 // [0,1,2,3,3,0]564 d.splice(1, 2, 3)565 // [0,3,3,3,0]566 expect(d.slice()).toEqual([0, 3, 3, 3, 0])567 },568 [569 {op: "replace", path: [1], value: 3},570 {op: "replace", path: [2], value: 3},571 {op: "replace", path: [4], value: 0},572 {op: "replace", path: ["length"], value: 5}573 ]574 )575})576577describe("arrays - modify and shrink", () => {578 runPatchTest(579 {x: [1, 2, 3]},580 d => {581 d.x[0] = 4582 d.x.length = 2583 // [0, 2]584 },585 [586 {op: "replace", path: ["x", 0], value: 4},587 {op: "replace", path: ["x", "length"], value: 2}588 ],589 [590 {op: "replace", path: ["x", 0], value: 1},591 {op: "add", path: ["x", 2], value: 3}592 ]593 )594})595596describe("arrays - prepend then splice middle", () => {597 runPatchTest(598 {x: [1, 2, 3]},599 d => {600 d.x.unshift(4)601 d.x.splice(2, 1)602 // 4, 1, 3603 },604 [605 {op: "replace", path: ["x", 0], value: 4},606 {op: "replace", path: ["x", 1], value: 1}607 ]608 )609})610611describe("arrays - splice middle then prepend", () => {612 runPatchTest(613 {x: [1, 2, 3]},614 d => {615 d.x.splice(1, 1)616 d.x.unshift(4)617 // [4, 1, 3]618 },619 [620 {op: "replace", path: ["x", 0], value: 4},621 {op: "replace", path: ["x", 1], value: 1}622 ]623 )624})625626describe("arrays - truncate", () => {627 runPatchTest(628 {x: [1, 2, 3]},629 d => {630 d.x.length -= 2631 },632 [{op: "replace", path: ["x", "length"], value: 1}],633 [634 {op: "add", path: ["x", 1], value: 2},635 {op: "add", path: ["x", 2], value: 3}636 ]637 )638})639640describe("arrays - pop twice", () => {641 runPatchTest(642 {x: [1, 2, 3]},643 d => {644 d.x.pop()645 d.x.pop()646 },647 [{op: "replace", path: ["x", "length"], value: 1}]648 )649})650651describe("arrays - push multiple", () => {652 // These patches were more optimal pre immer 7, but not always correct653 runPatchTest(654 {x: [1, 2, 3]},655 d => {656 d.x.push(4, 5)657 },658 [659 {op: "add", path: ["x", 3], value: 4},660 {op: "add", path: ["x", 4], value: 5}661 ],662 [{op: "replace", path: ["x", "length"], value: 3}]663 )664})665666describe("arrays - splice (expand)", () => {667 // These patches were more optimal pre immer 7, but not always correct668 runPatchTest(669 {x: [1, 2, 3]},670 d => {671 d.x.splice(1, 1, 4, 5, 6) // [1,4,5,6,3]672 },673 [674 {op: "replace", path: ["x", 1], value: 4},675 {op: "replace", path: ["x", 2], value: 5},676 {op: "add", path: ["x", 3], value: 6},677 {op: "add", path: ["x", 4], value: 3}678 ],679 [680 {op: "replace", path: ["x", 1], value: 2},681 {op: "replace", path: ["x", 2], value: 3},682 {op: "replace", path: ["x", "length"], value: 3}683 ]684 )685})686687describe("arrays - splice (shrink)", () => {688 // These patches were more optimal pre immer 7, but not always correct689 runPatchTest(690 {x: [1, 2, 3, 4, 5]},691 d => {692 d.x.splice(1, 3, 6) // [1, 6, 5]693 },694 [695 {op: "replace", path: ["x", 1], value: 6},696 {op: "replace", path: ["x", 2], value: 5},697 {op: "replace", path: ["x", "length"], value: 3}698 ],699 [700 {op: "replace", path: ["x", 1], value: 2},701 {op: "replace", path: ["x", 2], value: 3},702 {op: "add", path: ["x", 3], value: 4},703 {op: "add", path: ["x", 4], value: 5}704 ]705 )706})707708describe("arrays - delete", () => {709 runPatchTest(710 {711 x: [712 {a: 1, b: 2},713 {c: 3, d: 4}714 ]715 },716 d => {717 delete d.x[1].c718 },719 [{op: "remove", path: ["x", 1, "c"]}]720 )721})722723describe("sets - add - 1", () => {724 runPatchTest(725 new Set([1]),726 d => {727 d.add(2)728 },729 [{op: "add", path: [1], value: 2}],730 [{op: "remove", path: [1], value: 2}]731 )732})733734describe("sets - add, delete, add - 1", () => {735 runPatchTest(736 new Set([1]),737 d => {738 d.add(2)739 d.delete(2)740 d.add(2)741 },742 [{op: "add", path: [1], value: 2}],743 [{op: "remove", path: [1], value: 2}]744 )745})746747describe("sets - add, delete, add - 2", () => {748 runPatchTest(749 new Set([2, 1]),750 d => {751 d.add(2)752 d.delete(2)753 d.add(2)754 },755 [],756 []757 )758})759760describe("sets - mutate - 1", () => {761 const findById = (set, id) => {762 for (const item of set) {763 if (item.id === id) return item764 }765 }766 runPatchTest(767 new Set([768 {id: 1, val: "We"},769 {id: 2, val: "will"}770 ]),771 d => {772 const obj1 = findById(d, 1)773 const obj2 = findById(d, 2)774 obj1.val = "rock"775 obj2.val = "you"776 },777 [778 {op: "remove", path: [0], value: {id: 1, val: "We"}},779 {op: "remove", path: [1], value: {id: 2, val: "will"}},780 {op: "add", path: [0], value: {id: 1, val: "rock"}},781 {op: "add", path: [1], value: {id: 2, val: "you"}}782 ],783 [784 {op: "remove", path: [1], value: {id: 2, val: "you"}},785 {op: "remove", path: [0], value: {id: 1, val: "rock"}},786 {op: "add", path: [1], value: {id: 2, val: "will"}},787 {op: "add", path: [0], value: {id: 1, val: "We"}}788 ]789 )790})791792describe("arrays - splice should should result in remove op.", () => {793 // These patches were more optimal pre immer 7, but not always correct794 runPatchTest(795 [1, 2],796 d => {797 d.splice(1, 1)798 },799 [{op: "replace", path: ["length"], value: 1}],800 [{op: "add", path: [1], value: 2}]801 )802})803804describe("arrays - NESTED splice should should result in remove op.", () => {805 // These patches were more optimal pre immer 7, but not always correct806 runPatchTest(807 {a: {b: {c: [1, 2]}}},808 d => {809 d.a.b.c.splice(1, 1)810 },811 [{op: "replace", path: ["a", "b", "c", "length"], value: 1}],812 [{op: "add", path: ["a", "b", "c", 1], value: 2}]813 )814})815816describe("simple replacement", () => {817 runPatchTest({x: 3}, _d => 4, [{op: "replace", path: [], value: 4}])818})819820describe("same value replacement - 1", () => {821 runPatchTest(822 {x: {y: 3}},823 d => {824 const a = d.x825 d.x = a826 },827 []828 )829})830831describe("same value replacement - 2", () => {832 runPatchTest(833 {x: {y: 3}},834 d => {835 const a = d.x836 d.x = 4837 d.x = a838 },839 []840 )841})842843describe("same value replacement - 3", () => {844 runPatchTest(845 {x: 3},846 d => {847 d.x = 3848 },849 []850 )851})852853describe("same value replacement - 4", () => {854 runPatchTest(855 {x: 3},856 d => {857 d.x = 4858 d.x = 3859 },860 []861 )862})863864describe("same value replacement - 5", () => {865 runPatchTest(866 new Map([["x", 3]]),867 d => {868 d.set("x", 4)869 d.set("x", 3)870 },871 [],872 []873 )874})875876describe("same value replacement - 6", () => {877 runPatchTest(878 new Set(["x", 3]),879 d => {880 d.delete("x")881 d.add("x")882 },883 [],884 []885 )886})887888describe("simple delete", () => {889 runPatchTest(890 {x: 2},891 d => {892 delete d.x893 },894 [895 {896 op: "remove",897 path: ["x"]898 }899 ]900 )901})902903describe("patch compressions yields correct results", () => {904 let p1, p2905 runPatchTest(906 {},907 d => {908 d.x = {test: true}909 },910 (p1 = [911 {912 op: "add",913 path: ["x"],914 value: {915 test: true916 }917 }918 ])919 )920 runPatchTest(921 {x: {test: true}},922 d => {923 delete d.x924 },925 (p2 = [926 {927 op: "remove",928 path: ["x"]929 }930 ])931 )932 const res = runPatchTest(933 {},934 d => {935 applyPatches(d, [...p1, ...p2])936 },937 []938 )939940 expect(res).toEqual({})941})942943describe("change then delete property", () => {944 const res = runPatchTest(945 {946 x: 1947 },948 d => {949 d.x = 2950 delete d.x951 },952 [953 {954 op: "remove",955 path: ["x"]956 }957 ]958 )959 test("valid result", () => {960 expect(res).toEqual({})961 })962})963964test("replaying patches with interweaved replacements should work correctly", () => {965 const patches = []966 const s0 = {x: 1}967968 const s1 = produce(969 s0,970 draft => {971 draft.x = 2972 },973 p => {974 patches.push(...p)975 }976 )977978 const s2 = produce(979 s1,980 draft => {981 return {x: 0}982 },983 p => {984 patches.push(...p)985 }986 )987988 const s3 = produce(989 s2,990 draft => {991 draft.x--992 },993 p => {994 patches.push(...p)995 }996 )997998 expect(s3).toEqual({x: -1}) // correct result999 expect(applyPatches(s0, patches)).toEqual({x: -1}) // correct replay10001001 // manual replay on a draft should also be correct1002 expect(1003 produce(s0, draft => {1004 return applyPatches(draft, patches)1005 })1006 ).toEqual({x: -1})1007})10081009describe("#468", () => {1010 function run() {1011 const item = {id: 1}1012 const state = [item]1013 const [nextState, patches] = produceWithPatches(state, draft => {1014 draft[0].id = 21015 draft[1] = item1016 })10171018 expect(nextState).toEqual([{id: 2}, {id: 1}])1019 expect(patches).toEqual([1020 {1021 op: "replace",1022 path: [0, "id"],1023 value: 21024 },1025 {1026 op: "add",1027 path: [1],1028 value: {1029 id: 11030 }1031 }1032 ])10331034 const final = applyPatches(state, patches)1035 expect(final).toEqual(nextState)1036 }10371038 test("es5", () => {1039 setUseProxies(false)1040 run()1041 })10421043 test("proxy", () => {1044 setUseProxies(true)1045 run()1046 })1047})10481049test("#521", () => {1050 const state = new Map()10511052 const [nextState, patches] = produceWithPatches(state, draft => {1053 draft.set("hello", new Set(["world"]))1054 })10551056 let patchedState = applyPatches(state, patches)1057 expect(patchedState).toEqual(nextState)10581059 const [nextStateV2, patchesV2] = produceWithPatches(nextState, draft => {1060 draft.get("hello").add("immer")1061 })10621063 expect(applyPatches(nextState, patchesV2)).toEqual(1064 new Map([["hello", new Set(["world", "immer"])]])1065 )1066})10671068test("#559 patches works in a nested reducer with proxies", () => {1069 setUseProxies(true)10701071 const state = {1072 x: 1,1073 sub: {1074 y: [{a: 0}, {a: 1}]1075 }1076 }10771078 const changes = []1079 const inverseChanges = []10801081 const newState = produce(state, draft => {1082 draft.sub = produce(1083 draft.sub,1084 draft => {1085 draft.y.pop()1086 },1087 (patches, inversePatches) => {1088 expect(isDraft(inversePatches[0].value)).toBeFalsy()1089 expect(inversePatches[0].value).toMatchObject({a: 1})1090 changes.push(...patches)1091 inverseChanges.push(...inversePatches)1092 }1093 )1094 })10951096 const reversedSubState = applyPatches(newState.sub, inverseChanges)10971098 expect(reversedSubState).toMatchObject(state.sub)1099})11001101describe("#588", () => {1102 const reference = {value: {num: 53}}11031104 class Base {1105 [immerable] = true1106 get nested() {1107 return reference.value1108 }1109 set nested(value) {}1110 }11111112 let base = new Base()11131114 runPatchTest(1115 base,1116 vdraft => {1117 reference.value = vdraft1118 produce(base, bdraft => {1119 bdraft.nested.num = 421120 })1121 },1122 [{op: "add", path: ["num"], value: 42}]1123 )1124})11251126test("#676 patching Date objects", () => {1127 class Test {1128 constructor() {1129 this.test = true1130 }1131 perform() {1132 return "tested!"1133 }1134 }11351136 const [nextState, patches] = produceWithPatches({}, function(draft) {1137 draft.date = new Date("2020-11-10T08:08:08.003Z")1138 draft.test = new Test()1139 })11401141 expect(nextState.date.toJSON()).toMatchInlineSnapshot(1142 `"2020-11-10T08:08:08.003Z"`1143 )1144 expect(nextState.test.perform()).toBe("tested!")11451146 const rebuilt = applyPatches({}, patches)1147 expect(rebuilt.date).toBeInstanceOf(Date)1148 expect(rebuilt.date.toJSON()).toMatchInlineSnapshot(1149 `"2020-11-10T08:08:08.003Z"`1150 )1151 expect(rebuilt.date).toEqual(new Date("2020-11-10T08:08:08.003Z"))1152})11531154test("do not allow __proto__ polution - 738", () => {1155 const obj = {}11561157 // @ts-ignore1158 expect(obj.polluted).toBe(undefined)1159 expect(() => {1160 applyPatches({}, [1161 {op: "add", path: ["__proto__", "polluted"], value: "yes"}1162 ])1163 }).toThrow(1164 isProd1165 ? "24"1166 : "Patching reserved attributes like __proto__, prototype and constructor is not allowed"1167 )1168 // @ts-ignore1169 expect(obj.polluted).toBe(undefined)1170})11711172test("do not allow __proto__ polution using arrays - 738", () => {1173 const obj = {}1174 const ar = []11751176 // @ts-ignore1177 expect(obj.polluted).toBe(undefined)1178 // @ts-ignore1179 expect(ar.polluted).toBe(undefined)1180 expect(() => {1181 applyPatches(1182 [],1183 [{op: "add", path: ["__proto__", "polluted"], value: "yes"}]1184 )1185 }).toThrow(1186 isProd1187 ? "24"1188 : "Patching reserved attributes like __proto__, prototype and constructor is not allowed"1189 )1190 // @ts-ignore1191 expect(obj.polluted).toBe(undefined)1192 // @ts-ignore1193 expect(ar.polluted).toBe(undefined)1194})11951196test("do not allow prototype polution - 738", () => {1197 const obj = {}11981199 // @ts-ignore1200 expect(obj.polluted).toBe(undefined)1201 expect(() => {1202 applyPatches(Object, [1203 {op: "add", path: ["prototype", "polluted"], value: "yes"}1204 ])1205 }).toThrow(1206 isProd1207 ? "24"1208 : "Patching reserved attributes like __proto__, prototype and constructor is not allowed"1209 )1210 // @ts-ignore1211 expect(obj.polluted).toBe(undefined)1212})12131214test("do not allow constructor polution - 738", () => {1215 const obj = {}12161217 // @ts-ignore1218 expect(obj.polluted).toBe(undefined)1219 const t = {}1220 applyPatches(t, [{op: "replace", path: ["constructor"], value: "yes"}])1221 expect(typeof t.constructor).toBe("function")1222 // @ts-ignore1223 expect(Object.polluted).toBe(undefined)1224})12251226test("do not allow constructor.prototype polution - 738", () => {1227 const obj = {}12281229 // @ts-ignore1230 expect(obj.polluted).toBe(undefined)1231 expect(() => {1232 applyPatches({}, [1233 {op: "add", path: ["constructor", "prototype", "polluted"], value: "yes"}1234 ])1235 }).toThrow(1236 isProd1237 ? "24"1238 : "Patching reserved attributes like __proto__, prototype and constructor is not allowed"1239 )1240 // @ts-ignore1241 expect(Object.polluted).toBe(undefined)1242})12431244test("maps can store __proto__, prototype and constructor props", () => {1245 const obj = {}1246 const map = new Map()1247 map.set("__proto__", {})1248 map.set("constructor", {})1249 map.set("prototype", {})1250 const newMap = applyPatches(map, [1251 {op: "add", path: ["__proto__", "polluted"], value: "yes"},1252 {op: "add", path: ["constructor", "polluted"], value: "yes"},1253 {op: "add", path: ["prototype", "polluted"], value: "yes"}1254 ])1255 expect(newMap.get("__proto__").polluted).toBe("yes")1256 expect(newMap.get("constructor").polluted).toBe("yes")1257 expect(newMap.get("prototype").polluted).toBe("yes")1258 expect(obj.polluted).toBe(undefined)1259})12601261test("CVE-2020-28477 (https://snyk.io/vuln/SNYK-JS-IMMER-1019369) follow up", () => {1262 const obj = {}12631264 // @ts-ignore1265 expect(obj.polluted).toBe(undefined)1266 expect(() => {1267 applyPatches({}, [1268 {op: "add", path: [["__proto__"], "polluted"], value: "yes"}1269 ])1270 }).toThrow(1271 isProd1272 ? "24"1273 : "Patching reserved attributes like __proto__, prototype and constructor is not allowed"1274 )1275 // @ts-ignore1276 expect(obj.polluted).toBe(undefined)1277})12781279test("#648 assigning object to itself should not change patches", () => {1280 const input = {1281 obj: {1282 value: 2001283 }1284 }12851286 const [nextState, patches] = produceWithPatches(input, draft => {1287 draft.obj.value = 11288 draft.obj = draft.obj1289 })12901291 expect(patches).toEqual([1292 {1293 op: "replace",1294 path: ["obj", "value"],1295 value: 11296 }1297 ])1298})12991300test("#791 patch for nothing is stored as undefined", () => {1301 const [newState, patches] = produceWithPatches({abc: 123}, draft => nothing)1302 expect(patches).toEqual([{op: "replace", path: [], value: undefined}])13031304 expect(applyPatches({}, patches)).toEqual(undefined)1305})