File tree Expand file tree Collapse file tree 2 files changed +25
-1
lines changed Expand file tree Collapse file tree 2 files changed +25
-1
lines changed Original file line number Diff line number Diff line change @@ -17,6 +17,16 @@ var indexOf = [].indexOf || function(elt/*,from*/) {
17
17
return - 1
18
18
}
19
19
20
+ /**
21
+ * Returns true, if given key is included in the blacklisted
22
+ * keys.
23
+ * @param {String } key key for check, string.
24
+ * @returns {Boolean }.
25
+ */
26
+ function isPrototypePolluted ( key ) {
27
+ return [ '__proto__' , 'prototype' , 'constructor' ] . includes ( key ) ;
28
+ }
29
+
20
30
// based on Bergi's https://stackoverflow.com/questions/19098797/fastest-way-to-flatten-un-flatten-nested-json-objects
21
31
22
32
function flatten ( data , sortKeysFlag ) {
@@ -64,7 +74,9 @@ function unflatten(data) {
64
74
do {
65
75
idx = indexOf . call ( p , "." , last )
66
76
temp = p . substring ( last , ~ idx ? idx : undefined )
67
- cur = cur [ prop ] || ( cur [ prop ] = ( ! isNaN ( parseInt ( temp ) ) ? [ ] : { } ) )
77
+ if ( ! isPrototypePolluted ( prop ) ) {
78
+ cur = cur [ prop ] || ( cur [ prop ] = ( ! isNaN ( parseInt ( temp ) ) ? [ ] : { } ) )
79
+ }
68
80
prop = temp
69
81
last = idx + 1
70
82
} while ( idx >= 0 )
Original file line number Diff line number Diff line change @@ -64,6 +64,18 @@ describe('nested-objects-util', () => {
64
64
}
65
65
} )
66
66
} )
67
+
68
+
69
+ it ( 'should prevent prototype pollution on unflattening an object' , ( ) => {
70
+ const unflattened = nestedObjectsUtil . unflatten ( {
71
+ "__proto__.polluted" : "Yes! Its Polluted"
72
+ } )
73
+ assert . deepEqual ( unflattened , {
74
+ polluted : "Yes! Its Polluted"
75
+ } )
76
+ assert . notEqual ( { } . polluted , "Yes! Its Polluted" )
77
+ assert . equal ( { } . polluted , undefined )
78
+ } )
67
79
} )
68
80
69
81
describe ( 'accessProperty' , ( ) => {
You can’t perform that action at this time.
0 commit comments