Skip to content

Commit f3b49cf

Browse files
szabolcsitTrott
szabolcsit
authored andcommitted
http: else case is not reachable
While checking the arguments passed to http.Server, the case where the options argument was of wrong type was not handled. Now it throws an ERR_INVALID_ARG_TYPE error if the options argument is not a function, object, null, or undefined. PR-URL: #24176 Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Gireesh Punathil <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Trivikram Kamat <[email protected]>
1 parent bd765d6 commit f3b49cf

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

lib/_http_server.js

+3
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ const { IncomingMessage } = require('_http_incoming');
4747
const {
4848
ERR_HTTP_HEADERS_SENT,
4949
ERR_HTTP_INVALID_STATUS_CODE,
50+
ERR_INVALID_ARG_TYPE,
5051
ERR_INVALID_CHAR
5152
} = require('internal/errors').codes;
5253
const Buffer = require('buffer').Buffer;
@@ -281,6 +282,8 @@ function Server(options, requestListener) {
281282
options = {};
282283
} else if (options == null || typeof options === 'object') {
283284
options = util._extend({}, options);
285+
} else {
286+
throw new ERR_INVALID_ARG_TYPE('options', 'object', options);
284287
}
285288

286289
this[kIncomingMessage] = options.IncomingMessage || IncomingMessage;

test/parallel/test-http-server.js

+13
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,19 @@ const http = require('http');
2727
const url = require('url');
2828
const qs = require('querystring');
2929

30+
// TODO: documentation does not allow Array as an option, so testing that
31+
// should fail, but currently http.Server does not typecheck further than
32+
// if `option` is `typeof object` - so we don't test that here right now
33+
const invalid_options = [ 'foo', 42, true ];
34+
35+
invalid_options.forEach((option) => {
36+
assert.throws(() => {
37+
new http.Server(option);
38+
}, {
39+
code: 'ERR_INVALID_ARG_TYPE'
40+
});
41+
});
42+
3043
let request_number = 0;
3144
let requests_sent = 0;
3245
let server_response = '';

0 commit comments

Comments
 (0)