patch.js
This file
Path
__tests__/patch.js
1,258 lines · 23.3 KiB · JavaScript
Last changed by
fix: Fixed security issue #738: prototype pollution possible when applying patches CVE-2020-28477Michel Weststratebefore adoptionda2bd4f
History of this fileTake it
1"use strict"2import produce, {3 setUseProxies,4 applyPatches,5 produceWithPatches,6 enableAllPlugins,7 isDraft,8 immerable9} from "../src/immer"1011enableAllPlugins()1213jest.setTimeout(1000)1415const isProd = process.env.NODE_ENV === "production"1617function runPatchTest(base, producer, patches, inversePathes) {18 let resultProxies, resultEs51920 function runPatchTestHelper() {21 let recordedPatches22 let recordedInversePatches23 const res = produce(base, producer, (p, i) => {24 recordedPatches = p25 recordedInversePatches = i26 })2728 test("produces the correct patches", () => {29 expect(recordedPatches).toEqual(patches)30 if (inversePathes) expect(recordedInversePatches).toEqual(inversePathes)31 })3233 test("patches are replayable", () => {34 expect(applyPatches(base, recordedPatches)).toEqual(res)35 })3637 test("patches can be reversed", () => {38 expect(applyPatches(res, recordedInversePatches)).toEqual(base)39 })4041 return res42 }4344 describe(`proxy`, () => {45 setUseProxies(true)46 resultProxies = runPatchTestHelper()47 })4849 describe(`es5`, () => {50 setUseProxies(false)51 resultEs5 = runPatchTestHelper()52 test("ES5 and Proxy implementation yield same result", () => {53 expect(resultEs5).toEqual(resultProxies)54 })55 })5657 return resultProxies58}5960describe("applyPatches", () => {61 it("mutates the base state when it is a draft", () => {62 produce({a: 1}, draft => {63 const result = applyPatches(draft, [64 {op: "replace", path: ["a"], value: 2}65 ])66 expect(result).toBe(draft)67 expect(draft.a).toBe(2)68 })69 })70 it("produces a copy of the base state when not a draft", () => {71 const base = {a: 1}72 const result = applyPatches(base, [{op: "replace", path: ["a"], value: 2}])73 expect(result).not.toBe(base)74 expect(result.a).toBe(2)75 expect(base.a).toBe(1)76 })77 it('throws when `op` is not "add", "replace", nor "remove"', () => {78 expect(() => {79 const patch = {op: "copy", from: [0], path: [1]}80 applyPatches([2], [patch])81 }).toThrowErrorMatchingSnapshot()82 })83 it("throws when `path` cannot be resolved", () => {84 // missing parent85 expect(() => {86 const patch = {op: "add", path: ["a", "b"], value: 1}87 applyPatches({}, [patch])88 }).toThrowErrorMatchingSnapshot()8990 // missing grand-parent91 expect(() => {92 const patch = {op: "add", path: ["a", "b", "c"], value: 1}93 applyPatches({}, [patch])94 }).toThrowErrorMatchingSnapshot()95 })96 it("applied patches cannot be modified", () => {97 // see also: https://github.com/immerjs/immer/issues/41198 const s0 = {99 items: [1]100 }101102 const [s1, p1] = produceWithPatches(s0, draft => {103 draft.items = []104 })105106 const replaceValueBefore = p1[0].value.slice()107108 const [s2, p2] = produceWithPatches(s1, draft => {109 draft.items.push(2)110 })111112 applyPatches(s0, [...p1, ...p2])113114 const replaceValueAfter = p1[0].value.slice()115116 expect(replaceValueAfter).toStrictEqual(replaceValueBefore)117 })118})119120describe("simple assignment - 1", () => {121 runPatchTest(122 {x: 3},123 d => {124 d.x++125 },126 [{op: "replace", path: ["x"], value: 4}]127 )128})129130describe("simple assignment - 2", () => {131 runPatchTest(132 {x: {y: 4}},133 d => {134 d.x.y++135 },136 [{op: "replace", path: ["x", "y"], value: 5}]137 )138})139140describe("simple assignment - 3", () => {141 runPatchTest(142 {x: [{y: 4}]},143 d => {144 d.x[0].y++145 },146 [{op: "replace", path: ["x", 0, "y"], value: 5}]147 )148})149150describe("simple assignment - 4", () => {151 runPatchTest(152 new Map([["x", {y: 4}]]),153 d => {154 d.get("x").y++155 },156 [{op: "replace", path: ["x", "y"], value: 5}],157 [{op: "replace", path: ["x", "y"], value: 4}]158 )159})160161describe("simple assignment - 5", () => {162 runPatchTest(163 {x: new Map([["y", 4]])},164 d => {165 d.x.set("y", 5)166 },167 [{op: "replace", path: ["x", "y"], value: 5}],168 [{op: "replace", path: ["x", "y"], value: 4}]169 )170})171172describe("simple assignment - 6", () => {173 runPatchTest(174 new Map([["x", 1]]),175 d => {176 // Map.prototype.set should return the Map itself177 const res = d.set("x", 2)178 res.set("y", 3)179 },180 [181 {op: "replace", path: ["x"], value: 2},182 {op: "add", path: ["y"], value: 3}183 ],184 [185 {op: "replace", path: ["x"], value: 1},186 {op: "remove", path: ["y"]}187 ]188 )189})190191describe("simple assignment - 7", () => {192 const key1 = {prop: "val1"}193 const key2 = {prop: "val2"}194 runPatchTest(195 {x: new Map([[key1, 4]])},196 d => {197 d.x.set(key1, 5)198 d.x.set(key2, 6)199 },200 [201 {op: "replace", path: ["x", key1], value: 5},202 {op: "add", path: ["x", key2], value: 6}203 ],204 [205 {op: "replace", path: ["x", key1], value: 4},206 {op: "remove", path: ["x", key2]}207 ]208 )209})210211describe("delete 1", () => {212 runPatchTest(213 {x: {y: 4}},214 d => {215 delete d.x216 },217 [{op: "remove", path: ["x"]}]218 )219})220221describe("delete 2", () => {222 runPatchTest(223 new Map([["x", 1]]),224 d => {225 d.delete("x")226 },227 [{op: "remove", path: ["x"]}],228 [{op: "add", path: ["x"], value: 1}]229 )230})231232describe("delete 3", () => {233 runPatchTest(234 {x: new Map([["y", 1]])},235 d => {236 d.x.delete("y")237 },238 [{op: "remove", path: ["x", "y"]}],239 [{op: "add", path: ["x", "y"], value: 1}]240 )241})242243describe("delete 5", () => {244 const key1 = {prop: "val1"}245 const key2 = {prop: "val2"}246 runPatchTest(247 {248 x: new Map([249 [key1, 1],250 [key2, 2]251 ])252 },253 d => {254 d.x.delete(key1)255 d.x.delete(key2)256 },257 [258 {op: "remove", path: ["x", key1]},259 {op: "remove", path: ["x", key2]}260 ],261 [262 {op: "add", path: ["x", key1], value: 1},263 {op: "add", path: ["x", key2], value: 2}264 ]265 )266})267268describe("delete 6", () => {269 runPatchTest(270 new Set(["x", 1]),271 d => {272 d.delete("x")273 },274 [{op: "remove", path: [0], value: "x"}],275 [{op: "add", path: [0], value: "x"}]276 )277})278279describe("delete 7", () => {280 runPatchTest(281 {x: new Set(["y", 1])},282 d => {283 d.x.delete("y")284 },285 [{op: "remove", path: ["x", 0], value: "y"}],286 [{op: "add", path: ["x", 0], value: "y"}]287 )288})289290describe("renaming properties", () => {291 describe("nested object (no changes)", () => {292 runPatchTest(293 {a: {b: 1}},294 d => {295 d.x = d.a296 delete d.a297 },298 [299 {op: "add", path: ["x"], value: {b: 1}},300 {op: "remove", path: ["a"]}301 ]302 )303 })304305 describe("nested change in object", () => {306 runPatchTest(307 {308 a: {b: 1}309 },310 d => {311 d.a.b++312 },313 [{op: "replace", path: ["a", "b"], value: 2}],314 [{op: "replace", path: ["a", "b"], value: 1}]315 )316 })317318 describe("nested change in map", () => {319 runPatchTest(320 new Map([["a", new Map([["b", 1]])]]),321 d => {322 d.get("a").set("b", 2)323 },324 [{op: "replace", path: ["a", "b"], value: 2}],325 [{op: "replace", path: ["a", "b"], value: 1}]326 )327 })328329 describe("nested change in array", () => {330 runPatchTest(331 [[{b: 1}]],332 d => {333 d[0][0].b++334 },335 [{op: "replace", path: [0, 0, "b"], value: 2}],336 [{op: "replace", path: [0, 0, "b"], value: 1}]337 )338 })339340 describe("nested map (no changes)", () => {341 runPatchTest(342 new Map([["a", new Map([["b", 1]])]]),343 d => {344 d.set("x", d.get("a"))345 d.delete("a")346 },347 [348 {op: "add", path: ["x"], value: new Map([["b", 1]])},349 {op: "remove", path: ["a"]}350 ],351 [352 {op: "remove", path: ["x"]},353 {op: "add", path: ["a"], value: new Map([["b", 1]])}354 ]355 )356 })357358 describe("nested object (with changes)", () => {359 runPatchTest(360 {a: {b: 1, c: 1}},361 d => {362 let a = d.a363 a.b = 2 // change364 delete a.c // delete365 a.y = 2 // add366367 // rename368 d.x = a369 delete d.a370 },371 [372 {op: "add", path: ["x"], value: {b: 2, y: 2}},373 {op: "remove", path: ["a"]}374 ]375 )376 })377378 describe("nested map (with changes)", () => {379 runPatchTest(380 new Map([381 [382 "a",383 new Map([384 ["b", 1],385 ["c", 1]386 ])387 ]388 ]),389 d => {390 let a = d.get("a")391 a.set("b", 2) // change392 a.delete("c") // delete393 a.set("y", 2) // add394395 // rename396 d.set("x", a)397 d.delete("a")398 },399 [400 {401 op: "add",402 path: ["x"],403 value: new Map([404 ["b", 2],405 ["y", 2]406 ])407 },408 {op: "remove", path: ["a"]}409 ],410 [411 {op: "remove", path: ["x"]},412 {413 op: "add",414 path: ["a"],415 value: new Map([416 ["b", 1],417 ["c", 1]418 ])419 }420 ]421 )422 })423424 describe("deeply nested object (with changes)", () => {425 runPatchTest(426 {a: {b: {c: 1, d: 1}}},427 d => {428 let b = d.a.b429 b.c = 2 // change430 delete b.d // delete431 b.y = 2 // add432433 // rename434 d.a.x = b435 delete d.a.b436 },437 [438 {op: "add", path: ["a", "x"], value: {c: 2, y: 2}},439 {op: "remove", path: ["a", "b"]}440 ]441 )442 })443444 describe("deeply nested map (with changes)", () => {445 runPatchTest(446 new Map([447 [448 "a",449 new Map([450 [451 "b",452 new Map([453 ["c", 1],454 ["d", 1]455 ])456 ]457 ])458 ]459 ]),460 d => {461 let b = d.get("a").get("b")462 b.set("c", 2) // change463 b.delete("d") // delete464 b.set("y", 2) // add465466 // rename467 d.get("a").set("x", b)468 d.get("a").delete("b")469 },470 [471 {472 op: "add",473 path: ["a", "x"],474 value: new Map([475 ["c", 2],476 ["y", 2]477 ])478 },479 {op: "remove", path: ["a", "b"]}480 ],481 [482 {op: "remove", path: ["a", "x"]},483 {484 op: "add",485 path: ["a", "b"],486 value: new Map([487 ["c", 1],488 ["d", 1]489 ])490 }491 ]492 )493 })494})495496describe("minimum amount of changes", () => {497 runPatchTest(498 {x: 3, y: {a: 4}, z: 3},499 d => {500 d.y.a = 4501 d.y.b = 5502 Object.assign(d, {x: 4, y: {a: 2}})503 },504 [505 {op: "replace", path: ["x"], value: 4},506 {op: "replace", path: ["y"], value: {a: 2}}507 ]508 )509})510511describe("arrays - prepend", () => {512 runPatchTest(513 {x: [1, 2, 3]},514 d => {515 d.x.unshift(4)516 },517 [518 {op: "replace", path: ["x", 0], value: 4},519 {op: "replace", path: ["x", 1], value: 1},520 {op: "replace", path: ["x", 2], value: 2},521 {op: "add", path: ["x", 3], value: 3}522 ]523 )524})525526describe("arrays - multiple prepend", () => {527 runPatchTest(528 {x: [1, 2, 3]},529 d => {530 d.x.unshift(4)531 d.x.unshift(5)532 // 4,5,1,2,3533 },534 [535 {op: "replace", path: ["x", 0], value: 5},536 {op: "replace", path: ["x", 1], value: 4},537 {op: "replace", path: ["x", 2], value: 1},538 {op: "add", path: ["x", 3], value: 2},539 {op: "add", path: ["x", 4], value: 3}540 ]541 )542})543544describe("arrays - splice middle", () => {545 runPatchTest(546 {x: [1, 2, 3]},547 d => {548 d.x.splice(1, 1)549 },550 [551 {op: "replace", path: ["x", 1], value: 3},552 {op: "replace", path: ["x", "length"], value: 2}553 ]554 )555})556557describe("arrays - multiple splice", () => {558 runPatchTest(559 [0, 1, 2, 3, 4, 5, 0],560 d => {561 d.splice(4, 2, 3)562 // [0,1,2,3,3,0]563 d.splice(1, 2, 3)564 // [0,3,3,3,0]565 expect(d.slice()).toEqual([0, 3, 3, 3, 0])566 },567 [568 {op: "replace", path: [1], value: 3},569 {op: "replace", path: [2], value: 3},570 {op: "replace", path: [4], value: 0},571 {op: "replace", path: ["length"], value: 5}572 ]573 )574})575576describe("arrays - modify and shrink", () => {577 runPatchTest(578 {x: [1, 2, 3]},579 d => {580 d.x[0] = 4581 d.x.length = 2582 // [0, 2]583 },584 [585 {op: "replace", path: ["x", 0], value: 4},586 {op: "replace", path: ["x", "length"], value: 2}587 ],588 [589 {op: "replace", path: ["x", 0], value: 1},590 {op: "add", path: ["x", 2], value: 3}591 ]592 )593})594595describe("arrays - prepend then splice middle", () => {596 runPatchTest(597 {x: [1, 2, 3]},598 d => {599 d.x.unshift(4)600 d.x.splice(2, 1)601 // 4, 1, 3602 },603 [604 {op: "replace", path: ["x", 0], value: 4},605 {op: "replace", path: ["x", 1], value: 1}606 ]607 )608})609610describe("arrays - splice middle then prepend", () => {611 runPatchTest(612 {x: [1, 2, 3]},613 d => {614 d.x.splice(1, 1)615 d.x.unshift(4)616 // [4, 1, 3]617 },618 [619 {op: "replace", path: ["x", 0], value: 4},620 {op: "replace", path: ["x", 1], value: 1}621 ]622 )623})624625describe("arrays - truncate", () => {626 runPatchTest(627 {x: [1, 2, 3]},628 d => {629 d.x.length -= 2630 },631 [{op: "replace", path: ["x", "length"], value: 1}],632 [633 {op: "add", path: ["x", 1], value: 2},634 {op: "add", path: ["x", 2], value: 3}635 ]636 )637})638639describe("arrays - pop twice", () => {640 runPatchTest(641 {x: [1, 2, 3]},642 d => {643 d.x.pop()644 d.x.pop()645 },646 [{op: "replace", path: ["x", "length"], value: 1}]647 )648})649650describe("arrays - push multiple", () => {651 // These patches were more optimal pre immer 7, but not always correct652 runPatchTest(653 {x: [1, 2, 3]},654 d => {655 d.x.push(4, 5)656 },657 [658 {op: "add", path: ["x", 3], value: 4},659 {op: "add", path: ["x", 4], value: 5}660 ],661 [{op: "replace", path: ["x", "length"], value: 3}]662 )663})664665describe("arrays - splice (expand)", () => {666 // These patches were more optimal pre immer 7, but not always correct667 runPatchTest(668 {x: [1, 2, 3]},669 d => {670 d.x.splice(1, 1, 4, 5, 6) // [1,4,5,6,3]671 },672 [673 {op: "replace", path: ["x", 1], value: 4},674 {op: "replace", path: ["x", 2], value: 5},675 {op: "add", path: ["x", 3], value: 6},676 {op: "add", path: ["x", 4], value: 3}677 ],678 [679 {op: "replace", path: ["x", 1], value: 2},680 {op: "replace", path: ["x", 2], value: 3},681 {op: "replace", path: ["x", "length"], value: 3}682 ]683 )684})685686describe("arrays - splice (shrink)", () => {687 // These patches were more optimal pre immer 7, but not always correct688 runPatchTest(689 {x: [1, 2, 3, 4, 5]},690 d => {691 d.x.splice(1, 3, 6) // [1, 6, 5]692 },693 [694 {op: "replace", path: ["x", 1], value: 6},695 {op: "replace", path: ["x", 2], value: 5},696 {op: "replace", path: ["x", "length"], value: 3}697 ],698 [699 {op: "replace", path: ["x", 1], value: 2},700 {op: "replace", path: ["x", 2], value: 3},701 {op: "add", path: ["x", 3], value: 4},702 {op: "add", path: ["x", 4], value: 5}703 ]704 )705})706707describe("arrays - delete", () => {708 runPatchTest(709 {710 x: [711 {a: 1, b: 2},712 {c: 3, d: 4}713 ]714 },715 d => {716 delete d.x[1].c717 },718 [{op: "remove", path: ["x", 1, "c"]}]719 )720})721722describe("sets - add - 1", () => {723 runPatchTest(724 new Set([1]),725 d => {726 d.add(2)727 },728 [{op: "add", path: [1], value: 2}],729 [{op: "remove", path: [1], value: 2}]730 )731})732733describe("sets - add, delete, add - 1", () => {734 runPatchTest(735 new Set([1]),736 d => {737 d.add(2)738 d.delete(2)739 d.add(2)740 },741 [{op: "add", path: [1], value: 2}],742 [{op: "remove", path: [1], value: 2}]743 )744})745746describe("sets - add, delete, add - 2", () => {747 runPatchTest(748 new Set([2, 1]),749 d => {750 d.add(2)751 d.delete(2)752 d.add(2)753 },754 [],755 []756 )757})758759describe("sets - mutate - 1", () => {760 const findById = (set, id) => {761 for (const item of set) {762 if (item.id === id) return item763 }764 }765 runPatchTest(766 new Set([767 {id: 1, val: "We"},768 {id: 2, val: "will"}769 ]),770 d => {771 const obj1 = findById(d, 1)772 const obj2 = findById(d, 2)773 obj1.val = "rock"774 obj2.val = "you"775 },776 [777 {op: "remove", path: [0], value: {id: 1, val: "We"}},778 {op: "remove", path: [1], value: {id: 2, val: "will"}},779 {op: "add", path: [0], value: {id: 1, val: "rock"}},780 {op: "add", path: [1], value: {id: 2, val: "you"}}781 ],782 [783 {op: "remove", path: [1], value: {id: 2, val: "you"}},784 {op: "remove", path: [0], value: {id: 1, val: "rock"}},785 {op: "add", path: [1], value: {id: 2, val: "will"}},786 {op: "add", path: [0], value: {id: 1, val: "We"}}787 ]788 )789})790791describe("arrays - splice should should result in remove op.", () => {792 // These patches were more optimal pre immer 7, but not always correct793 runPatchTest(794 [1, 2],795 d => {796 d.splice(1, 1)797 },798 [{op: "replace", path: ["length"], value: 1}],799 [{op: "add", path: [1], value: 2}]800 )801})802803describe("arrays - NESTED splice should should result in remove op.", () => {804 // These patches were more optimal pre immer 7, but not always correct805 runPatchTest(806 {a: {b: {c: [1, 2]}}},807 d => {808 d.a.b.c.splice(1, 1)809 },810 [{op: "replace", path: ["a", "b", "c", "length"], value: 1}],811 [{op: "add", path: ["a", "b", "c", 1], value: 2}]812 )813})814815describe("simple replacement", () => {816 runPatchTest({x: 3}, _d => 4, [{op: "replace", path: [], value: 4}])817})818819describe("same value replacement - 1", () => {820 runPatchTest(821 {x: {y: 3}},822 d => {823 const a = d.x824 d.x = a825 },826 []827 )828})829830describe("same value replacement - 2", () => {831 runPatchTest(832 {x: {y: 3}},833 d => {834 const a = d.x835 d.x = 4836 d.x = a837 },838 []839 )840})841842describe("same value replacement - 3", () => {843 runPatchTest(844 {x: 3},845 d => {846 d.x = 3847 },848 []849 )850})851852describe("same value replacement - 4", () => {853 runPatchTest(854 {x: 3},855 d => {856 d.x = 4857 d.x = 3858 },859 []860 )861})862863describe("same value replacement - 5", () => {864 runPatchTest(865 new Map([["x", 3]]),866 d => {867 d.set("x", 4)868 d.set("x", 3)869 },870 [],871 []872 )873})874875describe("same value replacement - 6", () => {876 runPatchTest(877 new Set(["x", 3]),878 d => {879 d.delete("x")880 d.add("x")881 },882 [],883 []884 )885})886887describe("simple delete", () => {888 runPatchTest(889 {x: 2},890 d => {891 delete d.x892 },893 [894 {895 op: "remove",896 path: ["x"]897 }898 ]899 )900})901902describe("patch compressions yields correct results", () => {903 let p1, p2904 runPatchTest(905 {},906 d => {907 d.x = {test: true}908 },909 (p1 = [910 {911 op: "add",912 path: ["x"],913 value: {914 test: true915 }916 }917 ])918 )919 runPatchTest(920 {x: {test: true}},921 d => {922 delete d.x923 },924 (p2 = [925 {926 op: "remove",927 path: ["x"]928 }929 ])930 )931 const res = runPatchTest(932 {},933 d => {934 applyPatches(d, [...p1, ...p2])935 },936 []937 )938939 expect(res).toEqual({})940})941942describe("change then delete property", () => {943 const res = runPatchTest(944 {945 x: 1946 },947 d => {948 d.x = 2949 delete d.x950 },951 [952 {953 op: "remove",954 path: ["x"]955 }956 ]957 )958 test("valid result", () => {959 expect(res).toEqual({})960 })961})962963test("replaying patches with interweaved replacements should work correctly", () => {964 const patches = []965 const s0 = {x: 1}966967 const s1 = produce(968 s0,969 draft => {970 draft.x = 2971 },972 p => {973 patches.push(...p)974 }975 )976977 const s2 = produce(978 s1,979 draft => {980 return {x: 0}981 },982 p => {983 patches.push(...p)984 }985 )986987 const s3 = produce(988 s2,989 draft => {990 draft.x--991 },992 p => {993 patches.push(...p)994 }995 )996997 expect(s3).toEqual({x: -1}) // correct result998 expect(applyPatches(s0, patches)).toEqual({x: -1}) // correct replay9991000 // manual replay on a draft should also be correct1001 expect(1002 produce(s0, draft => {1003 return applyPatches(draft, patches)1004 })1005 ).toEqual({x: -1})1006})10071008describe("#468", () => {1009 function run() {1010 const item = {id: 1}1011 const state = [item]1012 const [nextState, patches] = produceWithPatches(state, draft => {1013 draft[0].id = 21014 draft[1] = item1015 })10161017 expect(nextState).toEqual([{id: 2}, {id: 1}])1018 expect(patches).toEqual([1019 {1020 op: "replace",1021 path: [0, "id"],1022 value: 21023 },1024 {1025 op: "add",1026 path: [1],1027 value: {1028 id: 11029 }1030 }1031 ])10321033 const final = applyPatches(state, patches)1034 expect(final).toEqual(nextState)1035 }10361037 test("es5", () => {1038 setUseProxies(false)1039 run()1040 })10411042 test("proxy", () => {1043 setUseProxies(true)1044 run()1045 })1046})10471048test("#521", () => {1049 const state = new Map()10501051 const [nextState, patches] = produceWithPatches(state, draft => {1052 draft.set("hello", new Set(["world"]))1053 })10541055 let patchedState = applyPatches(state, patches)1056 expect(patchedState).toEqual(nextState)10571058 const [nextStateV2, patchesV2] = produceWithPatches(nextState, draft => {1059 draft.get("hello").add("immer")1060 })10611062 expect(applyPatches(nextState, patchesV2)).toEqual(1063 new Map([["hello", new Set(["world", "immer"])]])1064 )1065})10661067test("#559 patches works in a nested reducer with proxies", () => {1068 setUseProxies(true)10691070 const state = {1071 x: 1,1072 sub: {1073 y: [{a: 0}, {a: 1}]1074 }1075 }10761077 const changes = []1078 const inverseChanges = []10791080 const newState = produce(state, draft => {1081 draft.sub = produce(1082 draft.sub,1083 draft => {1084 draft.y.pop()1085 },1086 (patches, inversePatches) => {1087 expect(isDraft(inversePatches[0].value)).toBeFalsy()1088 expect(inversePatches[0].value).toMatchObject({a: 1})1089 changes.push(...patches)1090 inverseChanges.push(...inversePatches)1091 }1092 )1093 })10941095 const reversedSubState = applyPatches(newState.sub, inverseChanges)10961097 expect(reversedSubState).toMatchObject(state.sub)1098})10991100describe("#588", () => {1101 const reference = {value: {num: 53}}11021103 class Base {1104 [immerable] = true1105 get nested() {1106 return reference.value1107 }1108 set nested(value) {}1109 }11101111 let base = new Base()11121113 runPatchTest(1114 base,1115 vdraft => {1116 reference.value = vdraft1117 produce(base, bdraft => {1118 bdraft.nested.num = 421119 })1120 },1121 [{op: "add", path: ["num"], value: 42}]1122 )1123})11241125test("#676 patching Date objects", () => {1126 class Test {1127 constructor() {1128 this.test = true1129 }1130 perform() {1131 return "tested!"1132 }1133 }11341135 const [nextState, patches] = produceWithPatches({}, function(draft) {1136 draft.date = new Date("2020-11-10T08:08:08.003Z")1137 draft.test = new Test()1138 })11391140 expect(nextState.date.toJSON()).toMatchInlineSnapshot(1141 `"2020-11-10T08:08:08.003Z"`1142 )1143 expect(nextState.test.perform()).toBe("tested!")11441145 const rebuilt = applyPatches({}, patches)1146 expect(rebuilt.date).toBeInstanceOf(Date)1147 expect(rebuilt.date.toJSON()).toMatchInlineSnapshot(1148 `"2020-11-10T08:08:08.003Z"`1149 )1150 expect(rebuilt.date).toEqual(new Date("2020-11-10T08:08:08.003Z"))1151})11521153test("do not allow __proto__ polution - 738", () => {1154 const obj = {}11551156 // @ts-ignore1157 expect(obj.polluted).toBe(undefined)1158 expect(() => {1159 applyPatches({}, [1160 {op: "add", path: ["__proto__", "polluted"], value: "yes"}1161 ])1162 }).toThrow(1163 isProd1164 ? "24"1165 : "Patching reserved attributes like __proto__, prototype and constructor is not allowed"1166 )1167 // @ts-ignore1168 expect(obj.polluted).toBe(undefined)1169})11701171test("do not allow __proto__ polution using arrays - 738", () => {1172 const obj = {}1173 const ar = []11741175 // @ts-ignore1176 expect(obj.polluted).toBe(undefined)1177 // @ts-ignore1178 expect(ar.polluted).toBe(undefined)1179 expect(() => {1180 applyPatches(1181 [],1182 [{op: "add", path: ["__proto__", "polluted"], value: "yes"}]1183 )1184 }).toThrow(1185 isProd1186 ? "24"1187 : "Patching reserved attributes like __proto__, prototype and constructor is not allowed"1188 )1189 // @ts-ignore1190 expect(obj.polluted).toBe(undefined)1191 // @ts-ignore1192 expect(ar.polluted).toBe(undefined)1193})11941195test("do not allow prototype polution - 738", () => {1196 const obj = {}11971198 // @ts-ignore1199 expect(obj.polluted).toBe(undefined)1200 expect(() => {1201 applyPatches(Object, [1202 {op: "add", path: ["prototype", "polluted"], value: "yes"}1203 ])1204 }).toThrow(1205 isProd1206 ? "24"1207 : "Patching reserved attributes like __proto__, prototype and constructor is not allowed"1208 )1209 // @ts-ignore1210 expect(obj.polluted).toBe(undefined)1211})12121213test("do not allow constructor polution - 738", () => {1214 const obj = {}12151216 // @ts-ignore1217 expect(obj.polluted).toBe(undefined)1218 const t = {}1219 applyPatches(t, [{op: "replace", path: ["constructor"], value: "yes"}])1220 expect(typeof t.constructor).toBe("function")1221 // @ts-ignore1222 expect(Object.polluted).toBe(undefined)1223})12241225test("do not allow constructor.prototype polution - 738", () => {1226 const obj = {}12271228 // @ts-ignore1229 expect(obj.polluted).toBe(undefined)1230 expect(() => {1231 applyPatches({}, [1232 {op: "add", path: ["constructor", "prototype", "polluted"], value: "yes"}1233 ])1234 }).toThrow(1235 isProd1236 ? "24"1237 : "Patching reserved attributes like __proto__, prototype and constructor is not allowed"1238 )1239 // @ts-ignore1240 expect(Object.polluted).toBe(undefined)1241})12421243test("maps can store __proto__, prototype and constructor props", () => {1244 const obj = {}1245 const map = new Map()1246 map.set("__proto__", {})1247 map.set("constructor", {})1248 map.set("prototype", {})1249 const newMap = applyPatches(map, [1250 {op: "add", path: ["__proto__", "polluted"], value: "yes"},1251 {op: "add", path: ["constructor", "polluted"], value: "yes"},1252 {op: "add", path: ["prototype", "polluted"], value: "yes"}1253 ])1254 expect(newMap.get("__proto__").polluted).toBe("yes")1255 expect(newMap.get("constructor").polluted).toBe("yes")1256 expect(newMap.get("prototype").polluted).toBe("yes")1257 expect(obj.polluted).toBe(undefined)1258})