Skip to content

Commit

Permalink
fix(object#set): enforce key types
Browse files Browse the repository at this point in the history
  • Loading branch information
marstamm authored and fake-join[bot] committed Jan 17, 2022
1 parent 59451ec commit 2c6689e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ export function set(target, path, value) {

forEach(path, function(key, idx) {

if (typeof key !== 'number' && typeof key !== 'string') {
throw new Error('illegal key type: ' + typeof key + '. Key should be of type number or string.');
}

if (key === 'constructor') {
throw new Error('illegal key: constructor');
}

if (key === '__proto__') {
throw new Error('illegal key: __proto__');
}
Expand Down
21 changes: 21 additions & 0 deletions test/object.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,27 @@ describe('object', function() {
}).to.throw(/illegal key/);
});


it('should not allow prototype polution via constructor', function() {
expect(function() {
set({}, ['constructor', 'prototype', 'polluted'], 'success');
}).to.throw(/illegal key/);
});


it('should not allow array as key', function() {
expect(function() {
set({}, [['__proto__'], 'polluted'], 'success');
}).to.throw(/illegal key type/);
});


it('should not allow object as key', function() {
expect(function() {
set({}, [{}, 'polluted'], 'success');
}).to.throw(/illegal key type/);
});

});


Expand Down

0 comments on commit 2c6689e

Please sign in to comment.