Skip to content

Commit 55f06d7

Browse files
committed
fix prototype pollution vulnerability
1 parent 48b246e commit 55f06d7

File tree

4 files changed

+43
-9
lines changed

4 files changed

+43
-9
lines changed

CHANGELOG.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
<a name="Unreleased"></a>
1+
<a name="1.0.1"></a>
22

3-
## Unreleased (2018-02-25)
3+
## <small>1.0.1 (2020-07-25)</small>
44

55
* add prettier and `npm run build` ([1f34461](https://github.com/skratchdot/object-path-set/commit/1f34461))
66
* adding contributors ([5bf6e83](https://github.com/skratchdot/object-path-set/commit/5bf6e83))
7+
* Bump eslint from 4.18.1 to 4.18.2 ([1756583](https://github.com/skratchdot/object-path-set/commit/1756583))
8+
* fix prototype pollution vulnerability ([e3108fe](https://github.com/skratchdot/object-path-set/commit/e3108fe))
79
* rename tonic to runkit ([9c2f1ea](https://github.com/skratchdot/object-path-set/commit/9c2f1ea))
8-
* small readme tweaks ([883d400](https://github.com/skratchdot/object-path-set/commit/883d400))
10+
* small readme tweaks ([9750b7a](https://github.com/skratchdot/object-path-set/commit/9750b7a))
911
* travis runs node 6+ ([ac1969b](https://github.com/skratchdot/object-path-set/commit/ac1969b))
1012
* update travis config ([9c5a14e](https://github.com/skratchdot/object-path-set/commit/9c5a14e))
1113
* updating changelog and changelog generator ([216d2e7](https://github.com/skratchdot/object-path-set/commit/216d2e7))

index.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
'use strict';
22

3+
// https://github.com/jonschlinkert/assign-deep/commit/90bf1c551d05940898168d04066bbf15060f50cc
4+
var isValidKey = function(key) {
5+
return key !== '__proto__' && key !== 'constructor' && key !== 'prototype';
6+
};
7+
38
var setPath = function(obj, path, value, delimiter) {
49
var arr;
510
var key;
@@ -12,11 +17,13 @@ var setPath = function(obj, path, value, delimiter) {
1217
if (Array.isArray(path) && path.length > 0) {
1318
arr = path;
1419
key = arr[0];
15-
if (arr.length > 1) {
16-
arr.shift();
17-
obj[key] = setPath(obj[key], arr, value, delimiter);
18-
} else {
19-
obj[key] = value;
20+
if (isValidKey(key)) {
21+
if (arr.length > 1) {
22+
arr.shift();
23+
obj[key] = setPath(obj[key], arr, value, delimiter);
24+
} else {
25+
obj[key] = value;
26+
}
2027
}
2128
}
2229
return obj;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "object-path-set",
3-
"version": "1.0.0",
3+
"version": "1.0.1",
44
"description": "set values in javascript objects by specifying a path",
55
"main": "index.js",
66
"scripts": {

test.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,4 +100,29 @@ describe('object-path-set', function() {
100100
obj2[''] = defaultValue;
101101
expect(setPath(obj, '', defaultValue)).toEqual(obj2);
102102
});
103+
it('should not pollute __proto__', function() {
104+
var obj = {};
105+
expect(obj.polluted).toBeUndefined();
106+
setPath(obj, '__proto__.polluted', 'yes');
107+
var obj2 = {};
108+
expect(obj.polluted).toBeUndefined();
109+
expect(obj2.polluted).toBeUndefined();
110+
});
111+
it('should not pollute constructor', function() {
112+
var obj = {};
113+
expect(obj.polluted).toBeUndefined();
114+
setPath(obj, 'constructor.polluted', 'yes');
115+
var obj2 = {};
116+
expect(obj.polluted).toBeUndefined();
117+
expect(obj2.polluted).toBeUndefined();
118+
});
119+
it('should not pollute prototype', function() {
120+
var obj = {};
121+
expect(obj.polluted).toBeUndefined();
122+
setPath(obj, 'prototype.polluted', 'yes');
123+
// eslint-disable-next-line
124+
var obj2 = new Object();
125+
expect(obj.polluted).toBeUndefined();
126+
expect(obj2.polluted).toBeUndefined();
127+
});
103128
});

0 commit comments

Comments
 (0)