Skip to content

Commit c112986

Browse files
committed
better fix for prototype pollution vulnerability
cheers Idan Digmi of Snyk Security
1 parent 49d19b0 commit c112986

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

index.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,17 @@ function isNonNegativeInteger (i) {
2222
function set (obj, path, value) {
2323
if(!obj) throw new Error('libnested.set: first arg must be an object')
2424
if(isBasic(path)) return obj[path] = value
25-
for(var i = 0; i < path.length; i++)
25+
for(var i = 0; i < path.length; i++) {
26+
if (isPrototypePolluted(path[i]))
27+
continue
28+
2629
if(i === path.length - 1)
2730
obj[path[i]] = value
2831
else if(null == obj[path[i]])
2932
obj = (obj[path[i]] = isNonNegativeInteger(path[i+1]) ? [] : {})
30-
else if (!(isPrototypePolluted(path[i])))
33+
else
3134
obj = obj[path[i]]
35+
}
3236
return value
3337
}
3438

@@ -92,7 +96,7 @@ function clone (obj) {
9296
}
9397

9498
function isPrototypePolluted(key) {
95-
return ['__proto__', 'constructor', 'prototype'].includes(key)
99+
return ['__proto__', 'constructor', 'prototype'].includes(key.toString())
96100
}
97101

98102
exports.get = get

test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,3 +151,14 @@ tape('clone does not leave an array reference', function (t) {
151151

152152
t.end()
153153
})
154+
155+
tape('prototype pollution', function (t) {
156+
t.notEqual({}.polluted, 'yes')
157+
R.set({}, ['__proto__','polluted'], 'yes');
158+
t.notEqual({}.polluted, 'yes')
159+
R.set({}, [['__proto__'], 'polluted'], 'yes')
160+
t.notEqual({}.polluted, 'yes')
161+
R.set({}, [['constructor', 'prototype'], 'polluted'], 'yes')
162+
t.notEqual({}.polluted, 'yes')
163+
t.end()
164+
})

0 commit comments

Comments
 (0)