From 66337fc6fc238286231afd3e5ea385a10e15236a Mon Sep 17 00:00:00 2001 From: Karan Mangtani Date: Tue, 28 Apr 2026 19:54:48 +0530 Subject: [PATCH] fix: replace deprecated unescape() with modern UTF-8 encoding (#7378) - Replace deprecated unescape(encodeURIComponent()) pattern with a regex-based encodeUTF8() helper function in resolveConfig.js - Fix incorrect JSDoc comment for isFileList in utils.js (said 'File' instead of 'FileList') The new encodeUTF8 function produces identical output to the deprecated pattern, verified against existing basicAuth.spec.js test expectations. Co-authored-by: Jay --- lib/helpers/resolveConfig.js | 23 +++++++++++++++-------- lib/utils.js | 2 +- 2 files changed, 16 insertions(+), 9 deletions(-) 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');