Skip to content

Commit f945393

Browse files
committed
[Fix] parse: ignore __proto__ keys (#428)
1 parent a8d5286 commit f945393

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed

lib/parse.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ var parseObject = function (chain, val, options, valuesParsed) {
146146
) {
147147
obj = [];
148148
obj[index] = leaf;
149-
} else {
149+
} else if (cleanRoot !== '__proto__') {
150150
obj[cleanRoot] = leaf;
151151
}
152152
}

test/parse.js

+60
Original file line numberDiff line numberDiff line change
@@ -632,6 +632,66 @@ test('parse()', function (t) {
632632
st.end();
633633
});
634634

635+
t.test('dunder proto is ignored', function (st) {
636+
var payload = 'categories[__proto__]=login&categories[__proto__]&categories[length]=42';
637+
var result = qs.parse(payload, { allowPrototypes: true });
638+
639+
st.deepEqual(
640+
result,
641+
{
642+
categories: {
643+
length: '42'
644+
}
645+
},
646+
'silent [[Prototype]] payload'
647+
);
648+
649+
var plainResult = qs.parse(payload, { allowPrototypes: true, plainObjects: true });
650+
651+
st.deepEqual(
652+
plainResult,
653+
{
654+
__proto__: null,
655+
categories: {
656+
__proto__: null,
657+
length: '42'
658+
}
659+
},
660+
'silent [[Prototype]] payload: plain objects'
661+
);
662+
663+
var query = qs.parse('categories[__proto__]=cats&categories[__proto__]=dogs&categories[some][json]=toInject', { allowPrototypes: true });
664+
665+
st.notOk(Array.isArray(query.categories), 'is not an array');
666+
st.notOk(query.categories instanceof Array, 'is not instanceof an array');
667+
st.deepEqual(query.categories, { some: { json: 'toInject' } });
668+
st.equal(JSON.stringify(query.categories), '{"some":{"json":"toInject"}}', 'stringifies as a non-array');
669+
670+
st.deepEqual(
671+
qs.parse('foo[__proto__][hidden]=value&foo[bar]=stuffs', { allowPrototypes: true }),
672+
{
673+
foo: {
674+
bar: 'stuffs'
675+
}
676+
},
677+
'hidden values'
678+
);
679+
680+
st.deepEqual(
681+
qs.parse('foo[__proto__][hidden]=value&foo[bar]=stuffs', { allowPrototypes: true, plainObjects: true }),
682+
{
683+
__proto__: null,
684+
foo: {
685+
__proto__: null,
686+
bar: 'stuffs'
687+
}
688+
},
689+
'hidden values: plain objects'
690+
);
691+
692+
st.end();
693+
});
694+
635695
t.test('can return null objects', { skip: !Object.create }, function (st) {
636696
var expected = Object.create(null);
637697
expected.a = Object.create(null);

0 commit comments

Comments
 (0)