Skip to content

Commit 114d7fb

Browse files
committed
fix: follow allowPrototypes during merge
1 parent 556ee0a commit 114d7fb

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed

lib/utils.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ exports.merge = function (target, source, options) {
3131
if (Array.isArray(target)) {
3232
target.push(source);
3333
} else if (typeof target === 'object') {
34-
target[source] = true;
34+
if (options.plainObjects || options.allowPrototypes || !has.call(Object.prototype, source)) {
35+
target[source] = true;
36+
}
3537
} else {
3638
return [target, source];
3739
}

test/parse.js

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,6 @@ test('parse()', function (t) {
152152
st.end();
153153
});
154154

155-
t.deepEqual(qs.parse('a[b]=c&a=d'), { a: { b: 'c', d: true } }, 'can add keys to objects');
156-
157155
t.test('correctly prunes undefined values when converting an array to an object', function (st) {
158156
st.deepEqual(qs.parse('a[2]=b&a[99999999]=c'), { a: { 2: 'b', 99999999: 'c' } });
159157
st.end();
@@ -444,6 +442,44 @@ test('parse()', function (t) {
444442
st.end();
445443
});
446444

445+
t.test('params starting with a starting bracket', function (st) {
446+
st.deepEqual(qs.parse('[=toString'), {});
447+
st.end();
448+
});
449+
450+
t.test('params starting with a starting bracket', function (st) {
451+
st.deepEqual(qs.parse('[=toString'), {});
452+
st.end();
453+
});
454+
455+
t.test('add keys to objects', function (st) {
456+
st.deepEqual(
457+
qs.parse('a[b]=c&a=d'),
458+
{ a: { b: 'c', d: true } },
459+
'can add keys to objects'
460+
);
461+
462+
st.deepEqual(
463+
qs.parse('a[b]=c&a=toString'),
464+
{ a: { b: 'c' } },
465+
'can not overwrite prototype'
466+
);
467+
468+
st.deepEqual(
469+
qs.parse('a[b]=c&a=toString', { allowPrototypes: true }),
470+
{ a: { b: 'c', toString: true } },
471+
'can overwrite prototype with allowPrototypes true'
472+
);
473+
474+
st.deepEqual(
475+
qs.parse('a[b]=c&a=toString', { plainObjects: true }),
476+
{ a: { b: 'c', toString: true } },
477+
'can overwrite prototype with plainObjects true'
478+
);
479+
480+
st.end();
481+
});
482+
447483
t.test('can return null objects', { skip: !Object.create }, function (st) {
448484
var expected = Object.create(null);
449485
expected.a = Object.create(null);

0 commit comments

Comments
 (0)