From aa372f7306295dfd1100c1c2c77ce95c95808e76 Mon Sep 17 00:00:00 2001 From: Dmitriy Mozgovoy Date: Wed, 19 Apr 2023 20:14:32 +0300 Subject: [PATCH 1/2] fix(utils): make isFormData detection logic stricter to avoid unnecessary calling of the `toString` method on the target; (#5661) --- lib/utils.js | 14 +++++++++----- test/unit/utils/utils.js | 31 +++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 5 deletions(-) diff --git a/lib/utils.js b/lib/utils.js index 6a388721c1..80ae34c0c4 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -188,12 +188,16 @@ const isStream = (val) => isObject(val) && isFunction(val.pipe); * @returns {boolean} True if value is an FormData, otherwise false */ const isFormData = (thing) => { - const pattern = '[object FormData]'; + let kind; return thing && ( - (typeof FormData === 'function' && thing instanceof FormData) || - toString.call(thing) === pattern || - (isFunction(thing.toString) && thing.toString() === pattern) - ); + (typeof FormData === 'function' && thing instanceof FormData) || ( + isFunction(thing.append) && ( + (kind = kindOf(thing)) === 'formdata' || + // detect form-data instance + (kind === 'object' && isFunction(thing.toString) && thing.toString() === '[object FormData]') + ) + ) + ) } /** diff --git a/test/unit/utils/utils.js b/test/unit/utils/utils.js index 73f378878b..8d40250318 100644 --- a/test/unit/utils/utils.js +++ b/test/unit/utils/utils.js @@ -22,6 +22,37 @@ describe('utils', function (){ }); assert.equal(utils.isFormData(new FormData()), true); }); + + it('should not call toString method on built-in objects instances', () => { + const buf = Buffer.from('123'); + + buf.toString = () => assert.fail('should not be called'); + + assert.equal(utils.isFormData(buf), false); + }); + + it('should not call toString method on built-in objects instances, even if append method exists', () => { + const buf = Buffer.from('123'); + + buf.append = () => {}; + + buf.toString = () => assert.fail('should not be called'); + + assert.equal(utils.isFormData(buf), false); + }); + + it('should detect custom FormData instances by toStringTag signature and append method presence', () => { + class FormData { + append(){ + + } + + get [Symbol.toStringTag]() { + return 'FormData'; + } + } + assert.equal(utils.isFormData(new FormData()), true); + }); }); describe('toJSON', function (){ From 1b8cc3b02b13f5d16ad988460edbda463113177e Mon Sep 17 00:00:00 2001 From: Dmitriy Mozgovoy Date: Wed, 19 Apr 2023 21:14:02 +0300 Subject: [PATCH 2/2] chore(template): improve issue template; (#5665) --- .github/ISSUE_TEMPLATE/BUG_REPORT.yml | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/.github/ISSUE_TEMPLATE/BUG_REPORT.yml b/.github/ISSUE_TEMPLATE/BUG_REPORT.yml index e79dda8c27..133996f324 100644 --- a/.github/ISSUE_TEMPLATE/BUG_REPORT.yml +++ b/.github/ISSUE_TEMPLATE/BUG_REPORT.yml @@ -26,7 +26,10 @@ body: id: reproduce attributes: label: 'To Reproduce' - description: Code snippet to reproduce, ideally that will work by pasting into something like https://npm.runkit.com/axios, a hosted solution, or a repository that illustrates the issue. **If your problem is not reproducible, please file under Support or Usage Question** + description: | + Code snippet to reproduce, ideally if you can provide a live example in https://codesandbox.io/ sandbox or a repository that illustrates the issue. + (You can use https://codesandbox.io/p/sandbox/zen-knuth-9hvhzq as a node sandbox template, or https://codesandbox.io/s/axios-browser-issue-2l8jec as a browser template) + **If your problem is not reproducible, please file under Support or Usage Question** validations: required: false - type: textarea