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: https://github.com/nodejs/node/pull/24176
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
This commit is contained in:
szabolcsit 2018-11-06 17:07:59 +01:00 committed by Rich Trott
parent bd765d61d7
commit f3b49cfa7b
2 changed files with 16 additions and 0 deletions

View File

@ -47,6 +47,7 @@ const { IncomingMessage } = require('_http_incoming');
const { const {
ERR_HTTP_HEADERS_SENT, ERR_HTTP_HEADERS_SENT,
ERR_HTTP_INVALID_STATUS_CODE, ERR_HTTP_INVALID_STATUS_CODE,
ERR_INVALID_ARG_TYPE,
ERR_INVALID_CHAR ERR_INVALID_CHAR
} = require('internal/errors').codes; } = require('internal/errors').codes;
const Buffer = require('buffer').Buffer; const Buffer = require('buffer').Buffer;
@ -281,6 +282,8 @@ function Server(options, requestListener) {
options = {}; options = {};
} else if (options == null || typeof options === 'object') { } else if (options == null || typeof options === 'object') {
options = util._extend({}, options); options = util._extend({}, options);
} else {
throw new ERR_INVALID_ARG_TYPE('options', 'object', options);
} }
this[kIncomingMessage] = options.IncomingMessage || IncomingMessage; this[kIncomingMessage] = options.IncomingMessage || IncomingMessage;

View File

@ -27,6 +27,19 @@ const http = require('http');
const url = require('url'); const url = require('url');
const qs = require('querystring'); const qs = require('querystring');
// TODO: documentation does not allow Array as an option, so testing that
// should fail, but currently http.Server does not typecheck further than
// if `option` is `typeof object` - so we don't test that here right now
const invalid_options = [ 'foo', 42, true ];
invalid_options.forEach((option) => {
assert.throws(() => {
new http.Server(option);
}, {
code: 'ERR_INVALID_ARG_TYPE'
});
});
let request_number = 0; let request_number = 0;
let requests_sent = 0; let requests_sent = 0;
let server_response = ''; let server_response = '';