Skip to content

Commit d2c7a5f

Browse files
authored
fix: fail fast on --password without --user (#937)
* fix: fail fast on `--password` without `--user` When passing a password without a username, http-server will launch successfully but the first request with an Authorization header will crash the server. This behavior is very inconvenient. This commit makes it fail fast instead so you know there's a problem before you log out of SSH or walk out of the house or office. Also alised `--user` to `--username` for added convenience. Resolves #935 * doc: update documentation for `--username` flag
1 parent 09c4e4d commit d2c7a5f

File tree

4 files changed

+10
-5
lines changed

4 files changed

+10
-5
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ with the provided Dockerfile.
8181
|`-P` or `--proxy` |Proxies all requests which can't be resolved locally to the given url. e.g.: -P http://someurl.com | |
8282
|`--proxy-all` |Forward every request to the proxy target instead of serving local files|`false`|
8383
|`--proxy-options` |Pass proxy [options](https://github.com/http-party/node-http-proxy#options) using nested dotted objects. e.g.: --proxy-options.secure false |
84-
|`--username` |Username for basic authentication | |
84+
|`--user` or `--username` |Username for basic authentication | |
8585
|`--password` |Password for basic authentication | |
8686
|`-S`, `--tls` or `--ssl` |Enable secure request serving with TLS/SSL (HTTPS)|`false`|
8787
|`-C` or `--cert` |Path to ssl cert file |`cert.pem` |

bin/http-server

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ var chalk = require('chalk'),
1313
var argv = require('minimist')(process.argv.slice(2), {
1414
alias: {
1515
tls: 'ssl',
16-
header: 'H'
16+
header: 'H',
17+
user: 'username',
1718
},
1819
boolean: ['proxy-all']
1920
});
@@ -68,8 +69,8 @@ if (argv.h || argv.help) {
6869
' --proxy-options Pass options to proxy using nested dotted objects. e.g.: --proxy-options.secure false',
6970
' --websocket Enable websocket proxy',
7071
'',
71-
' --username Username for basic authentication [none]',
72-
' Can also be specified with the env variable NODE_HTTP_SERVER_USERNAME',
72+
' --user --username Username for basic authentication [none]',
73+
' Can also be specified with the env variable NODE_HTTP_SERVER_USERNAME',
7374
' --password Password for basic authentication [none]',
7475
' Can also be specified with the env variable NODE_HTTP_SERVER_PASSWORD',
7576
'',

doc/http-server.1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ Requires \-\-proxy.
118118
Pass proxy options using nested dotted objects.
119119

120120
.TP
121-
.BI \-\-username " " \fIUSERNAME\fR
121+
.BI \-\-user ", " \-\-username " " \fIUSERNAME\fR
122122
Username for basic authentication.
123123
Can also be specified with the environment variable NODE_HTTP_SERVER_USERNAME.
124124
Defaults to none.

lib/http-server.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,10 @@ function HttpServer(options) {
9393
}
9494

9595
if (options.username || options.password) {
96+
if (!options.username || !options.password) {
97+
throw new Error('Basic authentication requires both username and password to be specified');
98+
}
99+
96100
before.push(function (req, res) {
97101
var credentials = auth(req);
98102

0 commit comments

Comments
 (0)