Skip to content

Commit 04586c5

Browse files
committed
fix: normalize empty auth credentials
1 parent b24a31f commit 04586c5

4 files changed

Lines changed: 40 additions & 3 deletions

File tree

lib/adapters/http.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -375,8 +375,8 @@ module.exports = function httpAdapter(config) {
375375
var auth = undefined;
376376
var configAuth = utils.hasOwnProperty(config, 'auth') ? config.auth : undefined;
377377
if (configAuth) {
378-
var username = utils.hasOwnProperty(configAuth, 'username') ? configAuth.username : '';
379-
var password = utils.hasOwnProperty(configAuth, 'password') ? configAuth.password : '';
378+
var username = utils.hasOwnProperty(configAuth, 'username') ? configAuth.username || '' : '';
379+
var password = utils.hasOwnProperty(configAuth, 'password') ? configAuth.password || '' : '';
380380
auth = username + ':' + password;
381381
}
382382

lib/adapters/xhr.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ module.exports = function xhrAdapter(config) {
4242
// HTTP basic authentication
4343
var configAuth = utils.hasOwnProperty(config, 'auth') ? config.auth : undefined;
4444
if (configAuth) {
45-
var username = utils.hasOwnProperty(configAuth, 'username') ? configAuth.username : '';
45+
var username = utils.hasOwnProperty(configAuth, 'username') ? configAuth.username || '' : '';
4646
var password = utils.hasOwnProperty(configAuth, 'password') && configAuth.password
4747
? unescape(encodeURIComponent(configAuth.password))
4848
: '';

test/specs/__helpers.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,22 @@ setupBasicAuthTest = function setupBasicAuthTest() {
9191
}, 100);
9292
});
9393

94+
it('should normalize nullish own HTTP Basic auth credentials to empty strings', function (done) {
95+
axios('/foo', {
96+
auth: {
97+
username: undefined,
98+
password: null
99+
}
100+
});
101+
102+
setTimeout(function () {
103+
var request = jasmine.Ajax.requests.mostRecent();
104+
105+
expect(request.requestHeaders['Authorization']).toEqual('Basic Og==');
106+
done();
107+
}, 100);
108+
});
109+
94110
it('should accept HTTP Basic auth credentials with non-Latin1 characters in password', function (done) {
95111
axios('/foo', {
96112
auth: {

test/unit/adapters/http.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -767,6 +767,27 @@ describe("supports http with nodejs", function () {
767767
});
768768
});
769769

770+
it("should normalize nullish own basic auth credentials to empty strings", function (done) {
771+
server = http
772+
.createServer(function (req, res) {
773+
res.end(req.headers.authorization);
774+
})
775+
.listen(4444, function () {
776+
axios
777+
.get("http://localhost:4444/", {
778+
auth: {
779+
username: undefined,
780+
password: null,
781+
},
782+
})
783+
.then(function (res) {
784+
assert.equal(res.data, "Basic " + Buffer.from(":", "utf8").toString("base64"));
785+
done();
786+
})
787+
.catch(done);
788+
});
789+
});
790+
770791
it("should not use inherited basic auth credentials after config cloning", function (done) {
771792
Object.prototype.username = "polluted-user";
772793
Object.prototype.password = "polluted-pass";

0 commit comments

Comments
 (0)