Skip to content

Commit 52322aa

Browse files
jazellylpinca
andauthored
net: validate host name for server listen
Fixes: #54441 Co-authored-by: Luigi Pinca <luigipinca@gmail.com> PR-URL: #54470 Reviewed-By: Paolo Insogna <paolo@cowtech.it> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Jake Yuesong Li <jake.yuesong@gmail.com>
1 parent 7fea010 commit 52322aa

2 files changed

Lines changed: 20 additions & 0 deletions

File tree

‎lib/net.js‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ const {
3535
NumberParseInt,
3636
ObjectDefineProperty,
3737
ObjectSetPrototypeOf,
38+
RegExp,
39+
RegExpPrototypeExec,
3840
Symbol,
3941
SymbolAsyncDispose,
4042
SymbolDispose,
@@ -143,6 +145,8 @@ const { kTimeout } = require('internal/timers');
143145
constDEFAULT_IPV4_ADDR='0.0.0.0';
144146
constDEFAULT_IPV6_ADDR='::';
145147

148+
constHOST_REGEXP=newRegExp('^[a-zA-Z0-9-:%.]+$');
149+
146150
constnoop=()=>{};
147151

148152
constkPerfHooksNetConnectContext=Symbol('kPerfHooksNetConnectContext');
@@ -2020,6 +2024,10 @@ Server.prototype.listen = function(...args) {
20202024
toNumber(args.length>2&&args[2]);// (port, host, backlog)
20212025

20222026
options=options._handle||options.handle||options;
2027+
if(typeofoptions.host==='string'&&RegExpPrototypeExec(HOST_REGEXP,options.host)===null){
2028+
thrownewERR_INVALID_ARG_VALUE('host',options.host);
2029+
}
2030+
20232031
constflags=getFlags(options.ipv6Only);
20242032
// Refresh the id to make the previous call invalid
20252033
this._listeningId++;

‎test/parallel/test-net-server-listen-options.js‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ function close() { this.close(); }
1515
// Test listen({port})
1616
net.createServer().listen({port: 0})
1717
.on('listening',common.mustCall(close));
18+
// Test listen(host, port}) on ipv4
19+
net.createServer().listen({host: '127.0.0.1',port: '3000'}).on('listening',common.mustCall(close));
20+
// Test listen(host, port}) on ipv6
21+
net.createServer().listen({host: '::',port: '3001'}).on('listening',common.mustCall(close));
1822
}
1923

2024
// Test listen(port, cb) and listen({ port }, cb) combinations
@@ -66,6 +70,13 @@ const listenOnPort = [
6670
name: 'TypeError',
6771
message: /^Theargument'options'musthavetheproperty"port"or"path"\.Received.+$/,
6872
});
73+
}elseif(typeofoptions.host==='string'&&!options.host.match(/^[a-zA-Z0-9-:%.]+$/)){
74+
assert.throws(fn,
75+
{
76+
code: 'ERR_INVALID_ARG_VALUE',
77+
name: 'TypeError',
78+
message: /^Theargument'host'isinvalid\.Received.+$/,
79+
});
6980
}else{
7081
assert.throws(fn,
7182
{
@@ -91,4 +102,5 @@ const listenOnPort = [
91102
shouldFailToListen({host: 'localhost:3000'});
92103
shouldFailToListen({host: {port: 3000}});
93104
shouldFailToListen({exclusive: true});
105+
shouldFailToListen({host: '[::]',port: 3000});
94106
}

0 commit comments

Comments
 (0)