From 6ef867e684adf7fb2343e3b29a79078a3c76dc29 Mon Sep 17 00:00:00 2001 From: Gabriel Quaresma Date: Wed, 12 Nov 2025 16:04:39 -0300 Subject: [PATCH] fix: unclear error message is thrown when specifying an empty proxy authorization (#6314) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix: add AxiosError to Invalid proxy authorization * fix: minor update * Update test/unit/adapters/http.js Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * chore: remove redundant check Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * chore: code style Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * chore: style Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * chore: correct assert Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --------- Co-authored-by: João Gabriel Quaresma de Almeida Co-authored-by: Jay Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- lib/adapters/http.js | 12 ++++++++---- test/unit/adapters/http.js | 23 +++++++++++++++++++++++ 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/lib/adapters/http.js b/lib/adapters/http.js index ccc89be..6a12570 100755 --- a/lib/adapters/http.js +++ b/lib/adapters/http.js @@ -193,12 +193,16 @@ function setProxy(options, configProxy, location) { if (proxy.auth) { // Support proxy auth object form - if (proxy.auth.username || proxy.auth.password) { + const validProxyAuth = Boolean(proxy.auth.username || proxy.auth.password); + + if (validProxyAuth) { proxy.auth = (proxy.auth.username || '') + ':' + (proxy.auth.password || ''); + } else if (typeof proxy.auth === 'object') { + throw new AxiosError('Invalid proxy authorization', AxiosError.ERR_BAD_OPTION, { proxy }); } - const base64 = Buffer - .from(proxy.auth, 'utf8') - .toString('base64'); + + const base64 = Buffer.from(proxy.auth, 'utf8').toString('base64'); + options.headers['Proxy-Authorization'] = 'Basic ' + base64; } diff --git a/test/unit/adapters/http.js b/test/unit/adapters/http.js index 864d29a..3e087a2 100644 --- a/test/unit/adapters/http.js +++ b/test/unit/adapters/http.js @@ -1361,6 +1361,29 @@ describe('supports http with nodejs', function () { }); }); + describe('when invalid proxy options are provided', function () { + it('should throw error', function () { + const proxy = { + protocol: "http:", + host: "hostname.abc.xyz", + port: 3300, + auth: { + username: "", + password: "", + } + }; + + return axios.get('https://test-domain.abc', {proxy}) + .then(function(){ + assert.fail('Does not throw'); + }, function (error) { + assert.strictEqual(error.message, 'Invalid proxy authorization'); + assert.strictEqual(error.code, 'ERR_BAD_OPTION'); + assert.deepStrictEqual(error.config.proxy, proxy); + }) + }); + }); + context('different options for direct proxy configuration (without env variables)', () => { const destination = 'www.example.com';