forked from lukeed/dset
/
pollution.js
99 lines (79 loc) · 2.4 KB
/
pollution.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import { suite } from 'uvu';
import * as assert from 'uvu/assert';
export default function (dset) {
const pollution = suite('pollution');
pollution('should protect against "__proto__" assignment', () => {
let input = { abc: 123 };
let before = input.__proto__;
dset(input, '__proto__.hello', 123);
assert.equal(input.__proto__, before);
assert.equal(input, {
abc: 123
});
assert.is.not({}.hello, 123);
assert.is.not((new Object).hello, 123);
assert.is.not(Object.create(null).hello, 123);
});
pollution('should protect against "__proto__" assignment :: nested', () => {
let input = { abc: 123 };
let before = input.__proto__;
dset(input, ['xyz', '__proto__', 'hello'], 123);
assert.equal(input.__proto__, before);
assert.equal(input, {
abc: 123,
xyz: {
// empty
}
});
assert.is({}.hello, undefined);
assert.is(input.hello, undefined);
assert.is((new Object).hello, undefined);
assert.is(Object.create(null).hello, undefined);
});
pollution('should ignore "prototype" assignment', () => {
let input = { a: 123 };
dset(input, 'a.prototype.hello', 'world');
assert.is(input.a.prototype, undefined);
assert.is(input.a.hello, undefined);
assert.equal(input, {
a: {
// converted, then aborted
}
});
assert.is(
JSON.stringify(input),
'{"a":{}}'
);
});
pollution('should ignore "constructor" assignment :: direct', () => {
let input = { a: 123 };
function Custom() {
//
}
dset(input, 'a.constructor', Custom);
assert.is.not(input.a.constructor, Custom);
assert.not.instance(input.a, Custom);
assert.instance(input.a.constructor, Object, '~> 123 -> {}');
assert.is(input.a.hasOwnProperty('constructor'), false);
assert.equal(input, { a: {} });
});
pollution('should ignore "constructor" assignment :: nested', () => {
let input = {};
dset(input, 'constructor.prototype.hello', 'world');
assert.is(input.hasOwnProperty('constructor'), false);
assert.is(input.hasOwnProperty('hello'), false);
assert.equal(input, {
// empty
});
});
// Test for CVE-2022-25645 - CWE-1321
pollution('should ignore JSON.parse crafted object with "__proto__" key', () => {
let a = { b: { c: 1 } };
assert.is(a.polluted, undefined);
assert.is({}.polluted, undefined);
dset(a, "b", JSON.parse('{"__proto__":{"polluted":"Yes!"}}'));
assert.is(a.polluted, undefined);
assert.is({}.polluted, undefined);
});
pollution.run();
}