Skip to content

Commit 391dc74

Browse files
authored
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 <matteo.collina@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
1 parent 73c0564 commit 391dc74

4 files changed

Lines changed: 34 additions & 23 deletions

File tree

‎lib/_http_server.js‎

Lines changed: 8 additions & 9 deletions
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
constrequestTimeout=options.requestTimeout;
440439
if(requestTimeout!==undefined){
441440
validateInteger(requestTimeout,'requestTimeout',0);
@@ -502,17 +501,17 @@ function Server(options, requestListener) {
502501

503502
if(typeofoptions==='function'){
504503
requestListener=options;
505-
options={};
506-
}elseif(options==null||typeofoptions==='object'){
507-
options={ ...options};
504+
options=kEmptyObject;
505+
}elseif(options==null){
506+
options=kEmptyObject;
508507
}else{
509-
thrownewERR_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‎

Lines changed: 16 additions & 10 deletions
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
functionServer(opts,requestListener){
5859
if(!(thisinstanceofServer))returnnewServer(opts,requestListener);
5960

6061
if(typeofopts==='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+
}elseif(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‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,7 @@ const http = require('http');
2727
consturl=require('url');
2828
constqs=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-
constinvalid_options=['foo',42,true];
30+
constinvalid_options=['foo',42,true,[]];
3431

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

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

Lines changed: 9 additions & 0 deletions
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+
constinvalid_options=['foo',42,true,[]];
48+
invalid_options.forEach((option)=>{
49+
assert.throws(()=>{
50+
newhttps.Server(option);
51+
},{
52+
code: 'ERR_INVALID_ARG_TYPE',
53+
});
54+
});
55+
4756
constserver=https.createServer(options,serverCallback);
4857

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

0 commit comments

Comments
 (0)