Skip to content

Commit 391dc74

Browse files
authoredJan 25, 2023
http: throw error if options of http.Server is array
If options of http.Server is array, throw ERR_INVALID_ARG_TYPE. Refs: #24176 PR-URL: #46283 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: Luigi Pinca <[email protected]>
1 parent 73c0564 commit 391dc74

File tree

4 files changed

+34
-23
lines changed

4 files changed

+34
-23
lines changed
 

‎lib/_http_server.js

+8-9
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,12 @@ const {
7575
ERR_HTTP_HEADERS_SENT,
7676
ERR_HTTP_INVALID_STATUS_CODE,
7777
ERR_HTTP_SOCKET_ENCODING,
78-
ERR_INVALID_ARG_TYPE,
7978
ERR_INVALID_ARG_VALUE,
8079
ERR_INVALID_CHAR
8180
} = codes;
81+
const {
82+
kEmptyObject,
83+
} = require('internal/util');
8284
const {
8385
validateInteger,
8486
validateBoolean,
@@ -433,9 +435,6 @@ function storeHTTPOptions(options) {
433435
validateBoolean(insecureHTTPParser, 'options.insecureHTTPParser');
434436
this.insecureHTTPParser = insecureHTTPParser;
435437

436-
if (options.noDelay === undefined)
437-
options.noDelay = true;
438-
439438
const requestTimeout = options.requestTimeout;
440439
if (requestTimeout !== undefined) {
441440
validateInteger(requestTimeout, 'requestTimeout', 0);
@@ -502,17 +501,17 @@ function Server(options, requestListener) {
502501

503502
if (typeof options === 'function') {
504503
requestListener = options;
505-
options = {};
506-
} else if (options == null || typeof options === 'object') {
507-
options = { ...options };
504+
options = kEmptyObject;
505+
} else if (options == null) {
506+
options = kEmptyObject;
508507
} else {
509-
throw new ERR_INVALID_ARG_TYPE('options', 'object', options);
508+
validateObject(options, 'options');
510509
}
511510

512511
storeHTTPOptions.call(this, options);
513512
net.Server.call(
514513
this,
515-
{ allowHalfOpen: true, noDelay: options.noDelay,
514+
{ allowHalfOpen: true, noDelay: options.noDelay ?? true,
516515
keepAlive: options.keepAlive,
517516
keepAliveInitialDelay: options.keepAliveInitialDelay });
518517

‎lib/https.js

+16-10
Original file line numberDiff line numberDiff line change
@@ -53,25 +53,31 @@ let debug = require('internal/util/debuglog').debuglog('https', (fn) => {
5353
debug = fn;
5454
});
5555
const { URL, urlToHttpOptions, searchParamsSymbol } = require('internal/url');
56+
const { validateObject } = require('internal/validators');
5657

5758
function Server(opts, requestListener) {
5859
if (!(this instanceof Server)) return new Server(opts, requestListener);
5960

6061
if (typeof opts === 'function') {
6162
requestListener = opts;
62-
opts = undefined;
63-
}
64-
opts = { ...opts };
65-
66-
if (!opts.ALPNProtocols) {
67-
// http/1.0 is not defined as Protocol IDs in IANA
68-
// https://www.iana.org/assignments/tls-extensiontype-values
69-
// /tls-extensiontype-values.xhtml#alpn-protocol-ids
70-
opts.ALPNProtocols = ['http/1.1'];
63+
opts = kEmptyObject;
64+
} else if (opts == null) {
65+
opts = kEmptyObject;
66+
} else {
67+
validateObject(opts, 'options');
7168
}
7269

7370
FunctionPrototypeCall(storeHTTPOptions, this, opts);
74-
FunctionPrototypeCall(tls.Server, this, opts, _connectionListener);
71+
FunctionPrototypeCall(tls.Server, this,
72+
{
73+
noDelay: true,
74+
// http/1.0 is not defined as Protocol IDs in IANA
75+
// https://www.iana.org/assignments/tls-extensiontype-values
76+
// /tls-extensiontype-values.xhtml#alpn-protocol-ids
77+
ALPNProtocols: ['http/1.1'],
78+
...opts,
79+
},
80+
_connectionListener);
7581

7682
this.httpAllowHalfOpen = false;
7783

‎test/parallel/test-http-server.js

+1-4
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,7 @@ 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 ];
30+
const invalid_options = [ 'foo', 42, true, [] ];
3431

3532
invalid_options.forEach((option) => {
3633
assert.throws(() => {

‎test/parallel/test-https-simple.js

+9
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,15 @@ const serverCallback = common.mustCall(function(req, res) {
4444
res.end(body);
4545
});
4646

47+
const invalid_options = [ 'foo', 42, true, [] ];
48+
invalid_options.forEach((option) => {
49+
assert.throws(() => {
50+
new https.Server(option);
51+
}, {
52+
code: 'ERR_INVALID_ARG_TYPE',
53+
});
54+
});
55+
4756
const server = https.createServer(options, serverCallback);
4857

4958
server.listen(0, common.mustCall(() => {

0 commit comments

Comments
 (0)
Please sign in to comment.