Skip to content

Commit 9532969

Browse files
committed
[Robustness] utils.merge / utils.assign: avoid invoking __proto__ setter when copying own properties
When object-valued input to `parse` contains a nested object whose own keys include `__proto__` (e.g. from `JSON.parse('{"__proto__":...}')`, which creates an own data property via CreateDataProperty), the `acc[key] = value` assignment in `merge`/`assign` invoked the `Object.prototype.__proto__` accessor and mutated the per-call returned sub-object's [[Prototype]]. This is not `Object.prototype` pollution - it affects only the returned sub-object, and downstream consumers can guard with `hasOwnProperty` or `plainObjects: true` - but the setter firing as a side effect of property copying is a footgun that the string-input path has long guarded against (see `parseObject`'s `decodedRoot !== '__proto__'` check). Use `Object.defineProperty` for `__proto__` keys so an own data property is created instead.
1 parent 9aca407 commit 9532969

3 files changed

Lines changed: 34 additions & 3 deletions

File tree

‎lib/utils.js‎

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
var formats = require('./formats');
44
var getSideChannel = require('side-channel');
5+
var defineProperty = require('es-define-property');
56

67
var has = Object.prototype.hasOwnProperty;
78
var isArray = Array.isArray;
@@ -66,6 +67,19 @@ var arrayToObject = function arrayToObject(source, options) {
6667
return obj;
6768
};
6869

70+
var setProperty = function setProperty(obj, key, value) {
71+
if (key === '__proto__' && defineProperty) {
72+
defineProperty(obj, key, {
73+
configurable: true,
74+
enumerable: true,
75+
value: value,
76+
writable: true
77+
});
78+
} else {
79+
obj[key] = value;
80+
}
81+
};
82+
6983
var merge = function merge(target, source, options) {
7084
/* eslint no-param-reassign: 0 */
7185
if (!source) {
@@ -145,9 +159,9 @@ var merge = function merge(target, source, options) {
145159
var value = source[key];
146160

147161
if (has.call(acc, key)) {
148-
acc[key] = merge(acc[key], value, options);
162+
setProperty(acc, key, merge(acc[key], value, options));
149163
} else {
150-
acc[key] = value;
164+
setProperty(acc, key, value);
151165
}
152166

153167
if (isOverflow(source) && !isOverflow(acc)) {
@@ -166,7 +180,7 @@ var merge = function merge(target, source, options) {
166180

167181
var assign = function assignSingleSource(target, source) {
168182
return Object.keys(source).reduce(function (acc, key) {
169-
acc[key] = source[key];
183+
setProperty(acc, key, source[key]);
170184
return acc;
171185
}, target);
172186
};

‎package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"node": ">=0.6"
3232
},
3333
"dependencies": {
34+
"es-define-property": "^1.0.1",
3435
"side-channel": "^1.1.0"
3536
},
3637
"devDependencies": {

‎test/parse.js‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -959,6 +959,22 @@ test('parse()', function (t) {
959959
st.end();
960960
});
961961

962+
t.test('object-valued input with own `__proto__` does not mutate sub-object [[Prototype]]', function (st) {
963+
// JSON.parse creates own data `__proto__` properties (via CreateDataProperty),
964+
// which would trigger the Object.prototype.__proto__ accessor if merged via `acc[key] = value`.
965+
var out = qs.parse({
966+
'user[name]': 'alice',
967+
user: JSON.parse('{"__proto__":{"isAdmin":true}}')
968+
}, { allowPrototypes: false });
969+
970+
st.equal(out.user.name, 'alice', 'name from bracket key is preserved');
971+
st.equal(out.user.isAdmin, undefined, 'attacker-controlled inherited property is not exposed');
972+
st.equal(Object.getPrototypeOf(out.user), Object.prototype, 'sub-object [[Prototype]] is unchanged');
973+
st.equal(Object.prototype.isAdmin, undefined, 'Object.prototype is not polluted');
974+
975+
st.end();
976+
});
977+
962978
t.test('can return null objects', { skip: !hasProto }, function (st) {
963979
var expected = {
964980
__proto__: null,

0 commit comments

Comments
 (0)