Skip to content

Commit 6ef867e

Browse files
joaoGabriel55jasonsaaymanCopilot
authored
fix: unclear error message is thrown when specifying an empty proxy authorization (#6314)
* fix: add AxiosError to Invalid proxy authorization * fix: minor update * Update test/unit/adapters/http.js Co-authored-by: Copilot <[email protected]> * chore: remove redundant check Co-authored-by: Copilot <[email protected]> * chore: code style Co-authored-by: Copilot <[email protected]> * chore: style Co-authored-by: Copilot <[email protected]> * chore: correct assert Co-authored-by: Copilot <[email protected]> --------- Co-authored-by: João Gabriel Quaresma de Almeida <joaoGabriel55> Co-authored-by: Jay <[email protected]> Co-authored-by: Copilot <[email protected]>
1 parent 15bf956 commit 6ef867e

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

lib/adapters/http.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -193,12 +193,16 @@ function setProxy(options, configProxy, location) {
193193

194194
if (proxy.auth) {
195195
// Support proxy auth object form
196-
if (proxy.auth.username || proxy.auth.password) {
196+
const validProxyAuth = Boolean(proxy.auth.username || proxy.auth.password);
197+
198+
if (validProxyAuth) {
197199
proxy.auth = (proxy.auth.username || '') + ':' + (proxy.auth.password || '');
200+
} else if (typeof proxy.auth === 'object') {
201+
throw new AxiosError('Invalid proxy authorization', AxiosError.ERR_BAD_OPTION, { proxy });
198202
}
199-
const base64 = Buffer
200-
.from(proxy.auth, 'utf8')
201-
.toString('base64');
203+
204+
const base64 = Buffer.from(proxy.auth, 'utf8').toString('base64');
205+
202206
options.headers['Proxy-Authorization'] = 'Basic ' + base64;
203207
}
204208

test/unit/adapters/http.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1361,6 +1361,29 @@ describe('supports http with nodejs', function () {
13611361
});
13621362
});
13631363

1364+
describe('when invalid proxy options are provided', function () {
1365+
it('should throw error', function () {
1366+
const proxy = {
1367+
protocol: "http:",
1368+
host: "hostname.abc.xyz",
1369+
port: 3300,
1370+
auth: {
1371+
username: "",
1372+
password: "",
1373+
}
1374+
};
1375+
1376+
return axios.get('https://test-domain.abc', {proxy})
1377+
.then(function(){
1378+
assert.fail('Does not throw');
1379+
}, function (error) {
1380+
assert.strictEqual(error.message, 'Invalid proxy authorization');
1381+
assert.strictEqual(error.code, 'ERR_BAD_OPTION');
1382+
assert.deepStrictEqual(error.config.proxy, proxy);
1383+
})
1384+
});
1385+
});
1386+
13641387
context('different options for direct proxy configuration (without env variables)', () => {
13651388
const destination = 'www.example.com';
13661389

0 commit comments

Comments
 (0)