8000 Merge pull request #2 from 418sec/1-npm-nested-objects-util · BonneVoyager/nested-objects-util@46d3fa5 · GitHub
[go: up one dir, main page]

Skip to content

Commit 46d3fa5

Browse files
authored
Merge pull request #2 from 418sec/1-npm-nested-objects-util
Security Fix for Prototype Pollution - huntr.dev
2 parents b5e668e + 53bb28b commit 46d3fa5

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

index.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,16 @@ var indexOf = [].indexOf || function(elt/*,from*/) {
1717
return -1
1818
}
1919

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+
2030
// based on Bergi's https://stackoverflow.com/questions/19098797/fastest-way-to-flatten-un-flatten-nested-json-objects
2131

2232
function flatten(data, sortKeysFlag) {
@@ -64,7 +74,9 @@ function unflatten(data) {
6474
do {
6575
idx = indexOf.call(p, ".", last)
6676
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+
}
6880
prop = temp
6981
last = idx + 1
7082
} while(idx >= 0)

index.spec.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,18 @@ describe('nested-objects-util', () => {
6464
}
6565
})
6666
})
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+
})
6779
})
6880

6981
describe('accessProperty', () => {

0 commit comments

Comments
 (0)
0