Skip to content

Commit bd36f7b

Browse files
committedJan 17, 2020
Fix basic auth options type issue
1 parent 7c09c23 commit bd36f7b

File tree

2 files changed

+140
-2
lines changed

2 files changed

+140
-2
lines changed
 

‎lib/http-server.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,11 @@ function HttpServer(options) {
108108
// an attacker knowledge of whether the username is correct via a timing
109109
// attack.
110110
if (credentials) {
111-
var usernameEqual = secureCompare(options.username, credentials.name);
112-
var passwordEqual = secureCompare(options.password, credentials.pass);
111+
// since the `name` and `pass` attributes of `credentials` are always string type
112+
// https://github.com/DefinitelyTyped/DefinitelyTyped/blob/HEAD/types/basic-auth/index.d.ts#L15-L16
113+
// so we use `.toString()` to fix https://github.com/http-party/http-server/issues/583
114+
var usernameEqual = secureCompare(options.username.toString(), credentials.name);
115+
var passwordEqual = secureCompare(options.password.toString(), credentials.pass);
113116
if (usernameEqual && passwordEqual) {
114117
return res.emit('next');
115118
}

‎test/http-server-test.js

+135
Original file line numberDiff line numberDiff line change
@@ -379,5 +379,140 @@ vows.describe('http-server').addBatch({
379379
teardown: function (server) {
380380
server.close();
381381
}
382+
},
383+
'When http-server is listening on 8086 with username "good_username" and Number type password 123456': {
384+
topic: function () {
385+
var server = httpServer.createServer({
386+
root: root,
387+
robots: true,
388+
headers: {
389+
'Access-Control-Allow-Origin': '*',
390+
'Access-Control-Allow-Credentials': 'true'
391+
},
392+
username: 'good_username',
393+
password: 123456
394+
});
395+
396+
server.listen(8086);
397+
this.callback(null, server);
398+
},
399+
'and the user requests an existent file with no auth details': {
400+
topic: function () {
401+
request('http://127.0.0.1:8086/file', this.callback);
402+
},
403+
'status code should be 401': function (res) {
404+
assert.equal(res.statusCode, 401);
405+
},
406+
'and file content': {
407+
topic: function (res, body) {
408+
var self = this;
409+
fs.readFile(path.join(root, 'file'), 'utf8', function (err, data) {
410+
self.callback(err, data, body);
411+
});
412+
},
413+
'should be a forbidden message': function (err, file, body) {
414+
assert.equal(body, 'Access denied');
415+
}
416+
}
417+
},
418+
'and the user requests an existent file with incorrect username': {
419+
topic: function () {
420+
request('http://127.0.0.1:8086/file', {
421+
auth: {
422+
user: 'wrong_username',
423+
pass: '123456'
424+
}
425+
}, this.callback);
426+
},
427+
'status code should be 401': function (res) {
428+
assert.equal(res.statusCode, 401);
429+
},
430+
'and file content': {
431+
topic: function (res, body) {
432+
var self = this;
433+
fs.readFile(path.join(root, 'file'), 'utf8', function (err, data) {
434+
self.callback(err, data, body);
435+
});
436+
},
437+
'should be a forbidden message': function (err, file, body) {
438+
assert.equal(body, 'Access denied');
439+
}
440+
}
441+
},
442+
'and the user requests an existent file with incorrect password': {
443+
topic: function () {
444+
request('http://127.0.0.1:8086/file', {
445+
auth: {
446+
user: 'good_username',
447+
pass: '654321'
448+
}
449+
}, this.callback);
450+
},
451+
'status code should be 401': function (res) {
452+
assert.equal(res.statusCode, 401);
453+
},
454+
'and file content': {
455+
topic: function (res, body) {
456+
var self = this;
457+
fs.readFile(path.join(root, 'file'), 'utf8', function (err, data) {
458+
self.callback(err, data, body);
459+
});
460+
},
461+
'should be a forbidden message': function (err, file, body) {
462+
assert.equal(body, 'Access denied');
463+
}
464+
}
465+
},
466+
'and the user requests a non-existent file with incorrect password': {
467+
topic: function () {
468+
request('http://127.0.0.1:8086/404', {
469+
auth: {
470+
user: 'good_username',
471+
pass: '654321'
472+
}
473+
}, this.callback);
474+
},
475+
'status code should be 401': function (res) {
476+
assert.equal(res.statusCode, 401);
477+
},
478+
'and file content': {
479+
topic: function (res, body) {
480+
var self = this;
481+
fs.readFile(path.join(root, 'file'), 'utf8', function (err, data) {
482+
self.callback(err, data, body);
483+
});
484+
},
485+
'should be a forbidden message': function (err, file, body) {
486+
assert.equal(body, 'Access denied');
487+
}
488+
}
489+
},
490+
'and the user requests an existent file with correct auth details': {
491+
topic: function () {
492+
request('http://127.0.0.1:8086/file', {
493+
auth: {
494+
user: 'good_username',
495+
pass: '123456'
496+
}
497+
}, this.callback);
498+
},
499+
'status code should be 200': function (res) {
500+
assert.equal(res.statusCode, 200);
501+
},
502+
'and file content': {
503+
topic: function (res, body) {
504+
var self = this;
505+
fs.readFile(path.join(root, 'file'), 'utf8', function (err, data) {
506+
self.callback(err, data, body);
507+
});
508+
},
509+
'should match content of served file': function (err, file, body) {
510+
assert.equal(body.trim(), file.trim());
511+
}
512+
}
513+
},
514+
teardown: function (server) {
515+
server.close();
516+
}
382517
}
383518
}).export(module);

0 commit comments

Comments
 (0)
Please sign in to comment.