@@ -4,11 +4,59 @@ import deepmerge from './deepmerge';
44
55describe ( 'deepmerge' , ( ) => {
66 // https://snyk.io/blog/after-three-years-of-silence-a-new-jquery-prototype-pollution-vulnerability-emerges-once-again/
7- it ( 'should not be subject to prototype pollution' , ( ) => {
8- deepmerge ( { } , JSON . parse ( '{ "myProperty": "a", "__proto__" : { "isAdmin" : true } }' ) , {
9- clone : false ,
10- } ) ;
7+ it ( 'should not be subject to prototype pollution via __proto__' , ( ) => {
8+ const result = deepmerge (
9+ { } ,
10+ JSON . parse ( '{ "myProperty": "a", "__proto__" : { "isAdmin" : true } }' ) ,
11+ {
12+ clone : false ,
13+ } ,
14+ ) ;
15+
16+ // @ts -expect-error __proto__ is not on this object type
17+ // eslint-disable-next-line no-proto
18+ expect ( result . __proto__ ) . to . have . property ( 'isAdmin' ) ;
19+ expect ( { } ) . not . to . have . property ( 'isAdmin' ) ;
20+ } ) ;
21+
22+ // https://cwe.mitre.org/data/definitions/915.html
23+ it ( 'should not be subject to prototype pollution via constructor' , ( ) => {
24+ const result = deepmerge (
25+ { } ,
26+ JSON . parse ( '{ "myProperty": "a", "constructor" : { "prototype": { "isAdmin" : true } } }' ) ,
27+ {
28+ clone : true ,
29+ } ,
30+ ) ;
31+
32+ expect ( result . constructor . prototype ) . to . have . property ( 'isAdmin' ) ;
33+ expect ( { } ) . not . to . have . property ( 'isAdmin' ) ;
34+ } ) ;
35+
36+ // https://cwe.mitre.org/data/definitions/915.html
37+ it ( 'should not be subject to prototype pollution via prototype' , ( ) => {
38+ const result = deepmerge (
39+ { } ,
40+ JSON . parse ( '{ "myProperty": "a", "prototype": { "isAdmin" : true } }' ) ,
41+ {
42+ clone : false ,
43+ } ,
44+ ) ;
45+
46+ // @ts -expect-error prototype is not on this object type
47+ expect ( result . prototype ) . to . have . property ( 'isAdmin' ) ;
48+ expect ( { } ) . not . to . have . property ( 'isAdmin' ) ;
49+ } ) ;
50+
51+ it ( 'should appropriately copy the fields without prototype pollution' , ( ) => {
52+ const result = deepmerge (
53+ { } ,
54+ JSON . parse ( '{ "myProperty": "a", "__proto__" : { "isAdmin" : true } }' ) ,
55+ ) ;
1156
57+ // @ts -expect-error __proto__ is not on this object type
58+ // eslint-disable-next-line no-proto
59+ expect ( result . __proto__ ) . to . have . property ( 'isAdmin' ) ;
1260 expect ( { } ) . not . to . have . property ( 'isAdmin' ) ;
1361 } ) ;
1462
0 commit comments