diff --git a/lib/helpers/resolveConfig.js b/lib/helpers/resolveConfig.js index f5ff1898..4d931c64 100644 --- a/lib/helpers/resolveConfig.js +++ b/lib/helpers/resolveConfig.js @@ -7,6 +7,19 @@ import mergeConfig from '../core/mergeConfig.js'; import AxiosHeaders from '../core/AxiosHeaders.js'; import buildURL from './buildURL.js'; +/** + * Encode a UTF-8 string to a Latin-1 byte string for use with btoa(). + * This is a modern replacement for the deprecated unescape(encodeURIComponent(str)) pattern. + * + * @param {string} str The string to encode + * + * @returns {string} UTF-8 bytes as a Latin-1 string + */ +const encodeUTF8 = (str) => encodeURIComponent(str).replace( + /%([0-9A-F]{2})/gi, + (_, hex) => String.fromCharCode(parseInt(hex, 16)) +); + export default (config) => { const newConfig = mergeConfig({}, config); @@ -34,14 +47,8 @@ export default (config) => { // HTTP basic authentication if (auth) { - headers.set( - 'Authorization', - 'Basic ' + - btoa( - (auth.username || '') + - ':' + - (auth.password ? unescape(encodeURIComponent(auth.password)) : '') - ) + headers.set('Authorization', 'Basic ' + + btoa((auth.username || '') + ':' + (auth.password ? encodeUTF8(auth.password) : '')) ); } diff --git a/lib/utils.js b/lib/utils.js index 77f395e9..ae2a440b 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -225,7 +225,7 @@ const isBlob = kindOfTest('Blob'); * * @param {*} val The value to test * - * @returns {boolean} True if value is a File, otherwise false + * @returns {boolean} True if value is a FileList, otherwise false */ const isFileList = kindOfTest('FileList');