Skip to content

Commit

Permalink
fix(platform): fixed emulated browser detection in node.js environmen…
Browse files Browse the repository at this point in the history
…t; (#6055)
  • Loading branch information
DigitalBrainJS committed Nov 7, 2023
1 parent f7adacd commit 3dc8369
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 56 deletions.
4 changes: 2 additions & 2 deletions lib/adapters/xhr.js
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion lib/helpers/cookies.js
Expand Up @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion lib/helpers/isURLSameOrigin.js
Expand Up @@ -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.
Expand Down
51 changes: 0 additions & 51 deletions lib/platform/browser/index.js
Expand Up @@ -2,63 +2,12 @@ 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: {
URLSearchParams,
FormData,
Blob
},
isStandardBrowserEnv,
isStandardBrowserWebWorkerEnv,
protocols: ['http', 'https', 'file', 'blob', 'url', 'data']
};
47 changes: 47 additions & 0 deletions 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
}
6 changes: 5 additions & 1 deletion 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
}

0 comments on commit 3dc8369

Please sign in to comment.