From 3dc8369e505e32a4e12c22f154c55fd63ac67fbb Mon Sep 17 00:00:00 2001 From: Dmitriy Mozgovoy Date: Tue, 7 Nov 2023 22:16:15 +0200 Subject: [PATCH] fix(platform): fixed emulated browser detection in node.js environment; (#6055) --- lib/adapters/xhr.js | 4 +-- lib/helpers/cookies.js | 2 +- lib/helpers/isURLSameOrigin.js | 2 +- lib/platform/browser/index.js | 51 ---------------------------------- lib/platform/common/utils.js | 47 +++++++++++++++++++++++++++++++ lib/platform/index.js | 6 +++- 6 files changed, 56 insertions(+), 56 deletions(-) create mode 100644 lib/platform/common/utils.js diff --git a/lib/adapters/xhr.js b/lib/adapters/xhr.js index 7864b48178..d52d4b29bc 100644 --- a/lib/adapters/xhr.js +++ b/lib/adapters/xhr.js @@ -64,7 +64,7 @@ export default isXHRAdapterSupported && function (config) { let contentType; if (utils.isFormData(requestData)) { - if (platform.isStandardBrowserEnv || platform.isStandardBrowserWebWorkerEnv) { + if (platform.hasStandardBrowserEnv || platform.hasStandardBrowserWebWorkerEnv) { requestHeaders.setContentType(false); // Let the browser set it } else if(!requestHeaders.getContentType(/^\s*multipart\/form-data/)){ requestHeaders.setContentType('multipart/form-data'); // mobile/desktop app frameworks @@ -186,7 +186,7 @@ export default isXHRAdapterSupported && function (config) { // Add xsrf header // This is only done if running in a standard browser environment. // Specifically not if we're in a web worker, or react-native. - if (platform.isStandardBrowserEnv) { + if (platform.hasStandardBrowserEnv) { // Add xsrf header // regarding CVE-2023-45857 config.withCredentials condition was removed temporarily const xsrfValue = isURLSameOrigin(fullPath) && config.xsrfCookieName && cookies.read(config.xsrfCookieName); diff --git a/lib/helpers/cookies.js b/lib/helpers/cookies.js index 361493a3a4..2a0520037e 100644 --- a/lib/helpers/cookies.js +++ b/lib/helpers/cookies.js @@ -3,7 +3,7 @@ import utils from './../utils.js'; import platform from '../platform/index.js'; -export default platform.isStandardBrowserEnv ? +export default platform.hasStandardBrowserEnv ? // Standard browser envs support document.cookie (function standardBrowserEnv() { diff --git a/lib/helpers/isURLSameOrigin.js b/lib/helpers/isURLSameOrigin.js index 18db03b3c5..cab47d9cd1 100644 --- a/lib/helpers/isURLSameOrigin.js +++ b/lib/helpers/isURLSameOrigin.js @@ -3,7 +3,7 @@ import utils from './../utils.js'; import platform from '../platform/index.js'; -export default platform.isStandardBrowserEnv ? +export default platform.hasStandardBrowserEnv ? // Standard browser envs have full support of the APIs needed to test // whether the request URL is of the same origin as current location. diff --git a/lib/platform/browser/index.js b/lib/platform/browser/index.js index 4d2203f4bb..08c206f3c7 100644 --- a/lib/platform/browser/index.js +++ b/lib/platform/browser/index.js @@ -2,55 +2,6 @@ import URLSearchParams from './classes/URLSearchParams.js' import FormData from './classes/FormData.js' import Blob from './classes/Blob.js' -/** - * Determine if we're running in a standard browser environment - * - * This allows axios to run in a web worker, and react-native. - * Both environments support XMLHttpRequest, but not fully standard globals. - * - * web workers: - * typeof window -> undefined - * typeof document -> undefined - * - * react-native: - * navigator.product -> 'ReactNative' - * nativescript - * navigator.product -> 'NativeScript' or 'NS' - * - * @returns {boolean} - */ -const isStandardBrowserEnv = (() => { - let product; - if (typeof navigator !== 'undefined' && ( - (product = navigator.product) === 'ReactNative' || - product === 'NativeScript' || - product === 'NS') - ) { - return false; - } - - return typeof window !== 'undefined' && typeof document !== 'undefined'; -})(); - -/** - * Determine if we're running in a standard browser webWorker environment - * - * Although the `isStandardBrowserEnv` method indicates that - * `allows axios to run in a web worker`, the WebWorker will still be - * filtered out due to its judgment standard - * `typeof window !== 'undefined' && typeof document !== 'undefined'`. - * This leads to a problem when axios post `FormData` in webWorker - */ - const isStandardBrowserWebWorkerEnv = (() => { - return ( - typeof WorkerGlobalScope !== 'undefined' && - // eslint-disable-next-line no-undef - self instanceof WorkerGlobalScope && - typeof self.importScripts === 'function' - ); -})(); - - export default { isBrowser: true, classes: { @@ -58,7 +9,5 @@ export default { FormData, Blob }, - isStandardBrowserEnv, - isStandardBrowserWebWorkerEnv, protocols: ['http', 'https', 'file', 'blob', 'url', 'data'] }; diff --git a/lib/platform/common/utils.js b/lib/platform/common/utils.js new file mode 100644 index 0000000000..56fe79ab79 --- /dev/null +++ b/lib/platform/common/utils.js @@ -0,0 +1,47 @@ +const hasBrowserEnv = typeof window !== 'undefined' && typeof document !== 'undefined'; + +/** + * Determine if we're running in a standard browser environment + * + * This allows axios to run in a web worker, and react-native. + * Both environments support XMLHttpRequest, but not fully standard globals. + * + * web workers: + * typeof window -> undefined + * typeof document -> undefined + * + * react-native: + * navigator.product -> 'ReactNative' + * nativescript + * navigator.product -> 'NativeScript' or 'NS' + * + * @returns {boolean} + */ +const hasStandardBrowserEnv = ( + (product) => { + return hasBrowserEnv && ['ReactNative', 'NativeScript', 'NS'].indexOf(product) < 0 + })(typeof navigator !== 'undefined' && navigator.product); + +/** + * Determine if we're running in a standard browser webWorker environment + * + * Although the `isStandardBrowserEnv` method indicates that + * `allows axios to run in a web worker`, the WebWorker will still be + * filtered out due to its judgment standard + * `typeof window !== 'undefined' && typeof document !== 'undefined'`. + * This leads to a problem when axios post `FormData` in webWorker + */ +const hasStandardBrowserWebWorkerEnv = (() => { + return ( + typeof WorkerGlobalScope !== 'undefined' && + // eslint-disable-next-line no-undef + self instanceof WorkerGlobalScope && + typeof self.importScripts === 'function' + ); +})(); + +export { + hasBrowserEnv, + hasStandardBrowserWebWorkerEnv, + hasStandardBrowserEnv +} diff --git a/lib/platform/index.js b/lib/platform/index.js index 5e9d005f3b..860ba21a1b 100644 --- a/lib/platform/index.js +++ b/lib/platform/index.js @@ -1,3 +1,7 @@ import platform from './node/index.js'; +import * as utils from './common/utils.js'; -export {platform as default} +export default { + ...utils, + ...platform +}