Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ba84464

Browse files
Add prototype pollution tests
1 parent ae43d2c commit ba84464

File tree

1 file changed

+85
-0
lines changed

1 file changed

+85
-0
lines changed

‎test/pollution.test.js

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import addedDiff from "../src/added";
2+
3+
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");
17+
});
18+
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" } });
40+
});
41+
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);
56+
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();
63+
});
64+
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);
77+
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();
84+
});
85+
});

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /