|
1 | 1 | import addedDiff from "../src/added";
|
| 2 | +import updatedDiff from "../src/updated"; |
| 3 | +import diff from "../src/diff"; |
| 4 | +import deletedDiff from "../src/deleted"; |
2 | 5 |
|
3 | 6 | describe("Prototype pollution", () => {
|
4 |
| - test("Demonstrate prototype pollution globally across all objects", () => { |
5 |
| - const a = {}; |
6 |
| - const b = new Object(); |
7 |
| - |
8 |
| - expect(a.hello).toBeUndefined(); |
9 |
| - expect(b.hello).toBeUndefined(); |
10 |
| - expect({}.hello).toBeUndefined(); |
11 |
| - |
12 |
| - b.__proto__.hello = "world"; |
13 |
| - |
14 |
| - expect(a.hello).toBe("world"); |
15 |
| - expect(b.hello).toBe("world"); |
16 |
| - expect({}.hello).toBe("world"); |
| 7 | + describe("diff", () => { |
| 8 | + test("should not pollute returned diffs prototype", () => { |
| 9 | + const l = { role: "user" }; |
| 10 | + const r = JSON.parse('{ "role": "user", "__proto__": { "role": "admin" } }'); |
| 11 | + const difference = diff(l, r); |
| 12 | + |
| 13 | + expect(l.role).toBe("user"); |
| 14 | + expect(r.role).toBe("user"); |
| 15 | + expect(difference.role).toBeUndefined(); |
| 16 | + }); |
| 17 | + |
| 18 | + test("should not pollute returned diffs prototype on nested diffs", () => { |
| 19 | + const l = { about: { role: "user" } }; |
| 20 | + const r = JSON.parse('{ "about": { "__proto__": { "role": "admin" } } }'); |
| 21 | + const difference = addedDiff(l, r); |
| 22 | + |
| 23 | + expect(l.about.role).toBe("user"); |
| 24 | + expect(r.about.role).toBeUndefined(); |
| 25 | + expect(difference.about.role).toBeUndefined(); |
| 26 | + }); |
17 | 27 | });
|
18 | 28 |
|
19 |
| - test("addedDiff does not pollute global prototype when running diff with added `__proto__` key", () => { |
20 |
| - const a = { role: "user" }; |
21 |
| - const b = JSON.parse('{ "__proto__": { "role": "admin" } }'); |
22 |
| - |
23 |
| - expect(a.role).toBe("user"); |
24 |
| - expect(a.__proto__.role).toBeUndefined(); |
25 |
| - expect(b.role).toBeUndefined(); |
26 |
| - expect(b.__proto__.role).toBe("admin"); |
27 |
| - expect({}.role).toBeUndefined(); |
28 |
| - expect({}.__proto__role).toBeUndefined(); |
29 |
| - |
30 |
| - const difference = addedDiff(a, b); |
31 |
| - |
32 |
| - expect(a.role).toBe("user"); |
33 |
| - expect(a.__proto__.role).toBeUndefined(); |
34 |
| - expect(b.__proto__.role).toBe("admin"); |
35 |
| - expect(b.role).toBeUndefined(); |
36 |
| - expect({}.role).toBeUndefined(); |
37 |
| - expect({}.__proto__role).toBeUndefined(); |
38 |
| - |
39 |
| - expect(difference).toEqual({ __proto__: { role: "admin" } }); |
| 29 | + describe("addedDiff", () => { |
| 30 | + test("addedDiff should not pollute returned diffs prototype", () => { |
| 31 | + const l = { role: "user" }; |
| 32 | + const r = JSON.parse('{ "__proto__": { "role": "admin" } }'); |
| 33 | + const difference = addedDiff(l, r); |
| 34 | + |
| 35 | + expect(l.role).toBe("user"); |
| 36 | + expect(r.role).toBeUndefined(); |
| 37 | + expect(difference.role).toBeUndefined(); |
| 38 | + }); |
| 39 | + |
| 40 | + test("should not pollute returned diffs prototype on nested diffs", () => { |
| 41 | + const l = { about: { role: "user" } }; |
| 42 | + const r = JSON.parse('{ "about": { "__proto__": { "role": "admin" } } }'); |
| 43 | + const difference = addedDiff(l, r); |
| 44 | + |
| 45 | + expect(l.about.role).toBe("user"); |
| 46 | + expect(r.about.role).toBeUndefined(); |
| 47 | + expect(difference.about.role).toBeUndefined(); |
| 48 | + }); |
40 | 49 | });
|
41 | 50 |
|
42 |
| - test("addedDiff does not pollute global prototype when running diff with added `__proto__` key generated from JSON.parse and mutating original left hand object", () => { |
43 |
| - let a = { role: "user" }; |
44 |
| - // Note: Don't trust `JSON.parse`!!! |
45 |
| - const b = JSON.parse('{ "__proto__": { "role": "admin" } }'); |
46 |
| - |
47 |
| - expect(a.role).toBe("user"); |
48 |
| - expect(a.__proto__.role).toBeUndefined(); |
49 |
| - expect(b.role).toBeUndefined(); |
50 |
| - expect(b.__proto__.role).toBe("admin"); |
51 |
| - expect({}.role).toBeUndefined(); |
52 |
| - expect({}.__proto__role).toBeUndefined(); |
53 |
| - |
54 |
| - // Note: although this does not pollute the global proto, it does pollute the original object. (Don't mutate kids!) |
55 |
| - a = addedDiff(a, b); |
| 51 | + test("updatedDiff should not pollute returned diffs prototype", () => { |
| 52 | + const l = { role: "user" }; |
| 53 | + const r = JSON.parse('{ "role": "user", "__proto__": { "role": "admin" } }'); |
| 54 | + const difference = updatedDiff(l, r); |
56 | 55 |
|
57 |
| - expect(a.role).toBe("admin"); |
58 |
| - expect(a.__proto__.role).toBe("admin"); |
59 |
| - expect(b.__proto__.role).toBe("admin"); |
60 |
| - expect(b.role).toBeUndefined(); |
61 |
| - expect({}.role).toBeUndefined(); |
62 |
| - expect({}.__proto__role).toBeUndefined(); |
| 56 | + expect(l.role).toBe("user"); |
| 57 | + expect(r.role).toBe("user"); |
| 58 | + expect(difference.role).toBeUndefined(); |
63 | 59 | });
|
64 | 60 |
|
65 |
| - test("addedDiff does not pollute global prototype or original object when running diff with added `__proto__` key", () => { |
66 |
| - let a = { role: "user" }; |
67 |
| - const b = { __proto__: { role: "admin" } }; |
68 |
| - |
69 |
| - expect(a.role).toBe("user"); |
70 |
| - expect(a.__proto__.role).toBeUndefined(); |
71 |
| - expect(b.role).toBe("admin"); |
72 |
| - expect(b.__proto__.role).toBe("admin"); |
73 |
| - expect({}.role).toBeUndefined(); |
74 |
| - expect({}.__proto__role).toBeUndefined(); |
75 |
| - |
76 |
| - a = addedDiff(a, b); |
| 61 | + test("deletedDiff should not pollute returned diffs prototype", () => { |
| 62 | + const l = { role: "user" }; |
| 63 | + const r = JSON.parse('{ "__proto__": { "role": "admin" } }'); |
| 64 | + const difference = deletedDiff(l, r); |
77 | 65 |
|
78 |
| - expect(a.role).toBeUndefined(); |
79 |
| - expect(a.__proto__.role).toBeUndefined(); |
80 |
| - expect(b.role).toBe("admin"); |
81 |
| - expect(b.__proto__.role).toBe("admin"); |
82 |
| - expect({}.role).toBeUndefined(); |
83 |
| - expect({}.__proto__role).toBeUndefined(); |
| 66 | + expect(l.role).toBe("user"); |
| 67 | + expect(r.role).toBeUndefined(); |
| 68 | + expect(difference.role).toBeUndefined(); |
84 | 69 | });
|
85 | 70 | });
|
0 commit comments