Skip to content

Commit 4310742

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

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed

lib/parse.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ var parseObject = function parseObjectRecursive(chain, val, options) {
6868
) {
6969
obj = [];
7070
obj[index] = parseObject(chain, val, options);
71-
} else {
71+
} else if (cleanRoot !== '__proto__') {
7272
obj[cleanRoot] = parseObject(chain, val, options);
7373
}
7474
}

test/parse.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,66 @@ test('parse()', function (t) {
487487
st.end();
488488
});
489489

490+
t.test('dunder proto is ignored', function (st) {
491+
var payload = 'categories[__proto__]=login&categories[__proto__]&categories[length]=42';
492+
var result = qs.parse(payload, { allowPrototypes: true });
493+
494+
st.deepEqual(
495+
result,
496+
{
497+
categories: {
498+
length: '42'
499+
}
500+
},
501+
'silent [[Prototype]] payload'
502+
);
503+
504+
var plainResult = qs.parse(payload, { allowPrototypes: true, plainObjects: true });
505+
506+
st.deepEqual(
507+
plainResult,
508+
{
509+
__proto__: null,
510+
categories: {
511+
__proto__: null,
512+
length: '42'
513+
}
514+
},
515+
'silent [[Prototype]] payload: plain objects'
516+
);
517+
518+
var query = qs.parse('categories[__proto__]=cats&categories[__proto__]=dogs&categories[some][json]=toInject', { allowPrototypes: true });
519+
520+
st.notOk(Array.isArray(query.categories), 'is not an array');
521+
st.notOk(query.categories instanceof Array, 'is not instanceof an array');
522+
st.deepEqual(query.categories, { some: { json: 'toInject' } });
523+
st.equal(JSON.stringify(query.categories), '{"some":{"json":"toInject"}}', 'stringifies as a non-array');
524+
525+
st.deepEqual(
526+
qs.parse('foo[__proto__][hidden]=value&foo[bar]=stuffs', { allowPrototypes: true }),
527+
{
528+
foo: {
529+
bar: 'stuffs'
530+
}
531+
},
532+
'hidden values'
533+
);
534+
535+
st.deepEqual(
536+
qs.parse('foo[__proto__][hidden]=value&foo[bar]=stuffs', { allowPrototypes: true, plainObjects: true }),
537+
{
538+
__proto__: null,
539+
foo: {
540+
__proto__: null,
541+
bar: 'stuffs'
542+
}
543+
},
544+
'hidden values: plain objects'
545+
);
546+
547+
st.end();
548+
});
549+
490550
t.test('can return null objects', { skip: !Object.create }, function (st) {
491551
var expected = Object.create(null);
492552
expected.a = Object.create(null);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy