GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — [Fix] `utils`: `isBuffer`: do not invoke a non-callable `constructor.… · ljharb/qs@e83d321

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • lib
    • utils.js
  • test
    • stringify.js
    • utils.js
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎lib/utils.js‎

Copy file name to clipboardExpand all lines: lib/utils.js
+1-1Lines 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) {
Collapse file

‎test/stringify.js‎

Copy file name to clipboardExpand all lines: test/stringify.js
+27Lines 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();
Collapse file

‎test/utils.js‎

Copy file name to clipboardExpand all lines: test/utils.js
+12Lines 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 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.