GeezerVison is showing github.com — [Fix] `utils`: `isBuffer`: do not invoke a non-callable `constructor.… · ljharb/qs@e83d321
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/utils.js +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -329,7 +329,7 @@ var isBuffer = function isBuffer(obj) { | 329 | 329 | return false; | 330 | 330 | } | 331 | 331 |
| 332 | | - return !!(obj.constructor && obj.constructor.isBuffer && obj.constructor.isBuffer(obj)); | | 332 | + return !!(obj.constructor && typeof obj.constructor.isBuffer === 'function' && obj.constructor.isBuffer(obj)); | 333 | 333 | }; | 334 | 334 |
| 335 | 335 | var combine = function combine(a, b, arrayLimit, plainObjects, throwOnLimitExceeded) { | |
Collapse file Copy file name to clipboardExpand all lines: test/stringify.js +27Lines changed: 27 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -932,6 +932,33 @@ test('stringify()', function (t) { | 932 | 932 | st.end(); | 933 | 933 | }); | 934 | 934 |
| | 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 | + | 935 | 962 | t.test('stringifies an object using an alternative delimiter', function (st) { | 936 | 963 | st.equal(qs.stringify({ a: 'b', c: 'd' }, { delimiter: ';' }), 'a=b;c=d'); | 937 | 964 | st.end(); | |
Collapse file Copy file name to clipboardExpand all lines: test/utils.js +12Lines changed: 12 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -604,6 +604,18 @@ test('isBuffer()', function (t) { | 604 | 604 | var fakeBuffer = { constructor: Buffer }; | 605 | 605 | t.equal(utils.isBuffer(fakeBuffer), false, 'fake buffer is not a buffer'); | 606 | 606 |
| | 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 | + | 607 | 619 | var saferBuffer = SaferBuffer.from('abc'); | 608 | 620 | t.equal(utils.isBuffer(saferBuffer), true, 'SaferBuffer instance is a buffer'); | 609 | 621 |
| |
|