Skip to content

Commit 7bdf4ab

Browse files
committedAug 27, 2021
Fix prototype pollution when path components are not strings
1 parent ebc5e2c commit 7bdf4ab

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed
 

‎index.js

+3
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,9 @@
111111
return set(obj, path.split('.').map(getKey), value, doNotReplace);
112112
}
113113
var currentPath = path[0];
114+
if (typeof currentPath !== 'string' && typeof currentPath !== 'number') {
115+
currentPath = String(currentPath)
116+
}
114117
var currentValue = getShallowProperty(obj, currentPath);
115118
if (options.includeInheritedProps && (currentPath === '__proto__' ||
116119
(currentPath === 'constructor' && typeof currentValue === 'function'))) {

‎test.js

+16-2
Original file line numberDiff line numberDiff line change
@@ -241,12 +241,18 @@ describe('set', function () {
241241
objectPath.set({}, '__proto__.injected', 'this is bad')
242242
expect(Object.prototype.injected).to.be.undefined
243243

244+
objectPath.set({}, [['__proto__'], 'injected'], 'this is bad')
245+
expect(Object.prototype.injected).to.be.undefined
246+
244247
function Clazz() {}
245248
Clazz.prototype.test = 'original'
246249

247250
objectPath.set(new Clazz(), '__proto__.test', 'this is bad')
248251
expect(Clazz.prototype.test).to.be.equal('original')
249252

253+
objectPath.set(new Clazz(), [['__proto__'], 'test'], 'this is bad')
254+
expect(Clazz.prototype.test).to.be.equal('original')
255+
250256
objectPath.set(new Clazz(), 'constructor.prototype.test', 'this is bad')
251257
expect(Clazz.prototype.test).to.be.equal('original')
252258
})
@@ -256,6 +262,11 @@ describe('set', function () {
256262
.to.throw('For security reasons')
257263
expect(Object.prototype.injected).to.be.undefined
258264

265+
expect(function() {
266+
objectPath.withInheritedProps.set({}, [['__proto__'], 'injected'], 'this is bad')
267+
expect(Object.prototype.injected).to.be.undefined
268+
}).to.throw('For security reasons')
269+
259270
function Clazz() {}
260271
Clazz.prototype.test = 'original'
261272

@@ -267,8 +278,11 @@ describe('set', function () {
267278
.to.throw('For security reasons')
268279
expect(Clazz.prototype.test).to.be.equal('original')
269280

270-
const obj = {}
271-
expect(function() {objectPath.withInheritedProps.set(obj, 'constructor.prototype.injected', 'this is OK')})
281+
expect(function() {objectPath.withInheritedProps.set({}, 'constructor.prototype.injected', 'this is OK')})
282+
.to.throw('For security reasons')
283+
expect(Object.prototype.injected).to.be.undefined
284+
285+
expect(function() {objectPath.withInheritedProps.set({}, [['constructor'], 'prototype', 'injected'], 'this is bad')})
272286
.to.throw('For security reasons')
273287
expect(Object.prototype.injected).to.be.undefined
274288
})

0 commit comments

Comments
 (0)
Please sign in to comment.