Skip to content

Commit e83d321

Browse files
committed
[Fix] utils: isBuffer: do not invoke a non-callable constructor.isBuffer
`isBuffer` duck-types buffers via `obj.constructor.isBuffer(obj)` (since 3768a75 / v2.2.5, so that it works without referencing the `Buffer` global and recognizes polyfill and foreign-realm buffers), but only checked that the property was truthy before calling it. An object with an own `constructor` key whose `isBuffer` member is a non-function, as produced by `qs.parse` under `plainObjects` or `allowPrototypes`, or by `JSON.parse`, made `stringify` throw `TypeError: obj.constructor.isBuffer is not a function`. Require it to be callable before invoking it. Real `Buffer`, `safer-buffer`, and browserify `buffer` polyfill instances serialize exactly as before.
1 parent 7e87a07 commit e83d321

3 files changed

Lines changed: 40 additions & 1 deletion

File tree

‎lib/utils.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ var isBuffer = function isBuffer(obj) {
329329
return false;
330330
}
331331

332-
return !!(obj.constructor && obj.constructor.isBuffer && obj.constructor.isBuffer(obj));
332+
return !!(obj.constructor && typeof obj.constructor.isBuffer === 'function' && obj.constructor.isBuffer(obj));
333333
};
334334

335335
var combine = function combine(a, b, arrayLimit, plainObjects, throwOnLimitExceeded) {

‎test/stringify.js‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -932,6 +932,33 @@ test('stringify()', function (t) {
932932
st.end();
933933
});
934934

935+
t.test('does not throw on a non-callable `constructor.isBuffer` key', function (st) {
936+
var input = { a: { constructor: { isBuffer: 'x' } } };
937+
st.doesNotThrow(function () { qs.stringify(input); });
938+
st.equal(qs.stringify(input), 'a%5Bconstructor%5D%5BisBuffer%5D=x');
939+
st.equal(qs.stringify({ a: { constructor: { isBuffer: { b: 'c' } } } }), 'a%5Bconstructor%5D%5BisBuffer%5D%5Bb%5D=c');
940+
st.equal(qs.stringify({ a: { constructor: { isBuffer: ['b'] } } }), 'a%5Bconstructor%5D%5BisBuffer%5D%5B0%5D=b');
941+
st.equal(qs.stringify({ a: { constructor: { isBuffer: true } } }), 'a%5Bconstructor%5D%5BisBuffer%5D=true');
942+
st.end();
943+
});
944+
945+
t.test('round-trips a parsed `constructor[isBuffer]` key', function (st) {
946+
var str = 'x%5Bconstructor%5D%5BisBuffer%5D=y';
947+
948+
var plain = qs.parse(str, { plainObjects: true });
949+
st.deepEqual(plain, { __proto__: null, x: { __proto__: null, constructor: { __proto__: null, isBuffer: 'y' } } }, 'plainObjects keeps the key');
950+
st.doesNotThrow(function () { qs.stringify(plain); });
951+
st.equal(qs.stringify(plain), str, 'plainObjects round-trips');
952+
953+
var proto = qs.parse(str, { allowPrototypes: true });
954+
st.deepEqual(proto, { x: { constructor: { isBuffer: 'y' } } }, 'allowPrototypes keeps the key');
955+
st.doesNotThrow(function () { qs.stringify(proto); });
956+
st.equal(qs.stringify(proto), str, 'allowPrototypes round-trips');
957+
958+
st.equal(qs.stringify(qs.parse(str)), '', 'default options drop the key');
959+
st.end();
960+
});
961+
935962
t.test('stringifies an object using an alternative delimiter', function (st) {
936963
st.equal(qs.stringify({ a: 'b', c: 'd' }, { delimiter: ';' }), 'a=b;c=d');
937964
st.end();

‎test/utils.js‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,18 @@ test('isBuffer()', function (t) {
604604
var fakeBuffer = { constructor: Buffer };
605605
t.equal(utils.isBuffer(fakeBuffer), false, 'fake buffer is not a buffer');
606606

607+
forEach(['x', 1, true, {}, [], { isBuffer: true }], function (notCallable) {
608+
var obj = { constructor: { isBuffer: notCallable } };
609+
t.doesNotThrow(function () { utils.isBuffer(obj); }, 'non-callable `constructor.isBuffer` (' + inspect(notCallable) + ') does not throw');
610+
t.equal(utils.isBuffer(obj), false, 'non-callable `constructor.isBuffer` (' + inspect(notCallable) + ') is not a buffer');
611+
});
612+
613+
var nullObject = { __proto__: null, constructor: { __proto__: null, isBuffer: 'y' } };
614+
t.equal(utils.isBuffer(nullObject), false, 'null object with non-callable `constructor.isBuffer` is not a buffer');
615+
616+
var duckBuffer = { constructor: { isBuffer: function () { return true; } } };
617+
t.equal(utils.isBuffer(duckBuffer), true, 'callable `constructor.isBuffer` is still honored');
618+
607619
var saferBuffer = SaferBuffer.from('abc');
608620
t.equal(utils.isBuffer(saferBuffer), true, 'SaferBuffer instance is a buffer');
609621

0 commit comments

Comments
 (0)