Skip to content

Commit 859a003

Browse files
committed
net: validate host name with square brackets
1 parent 9e83853 commit 859a003

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

lib/net.js

+5
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ const {
3535
NumberParseInt,
3636
ObjectDefineProperty,
3737
ObjectSetPrototypeOf,
38+
RegExpPrototypeExec,
3839
Symbol,
3940
SymbolAsyncDispose,
4041
SymbolDispose,
@@ -2019,6 +2020,10 @@ Server.prototype.listen = function(...args) {
20192020
toNumber(args.length > 2 && args[2]); // (port, host, backlog)
20202021

20212022
options = options._handle || options.handle || options;
2023+
if (typeof options.host === 'string' && RegExpPrototypeExec(/\[.*\]/, options.host) !== null) {
2024+
throw new ERR_INVALID_ARG_VALUE('host', options.host, 'must not contain square brackets');
2025+
}
2026+
20222027
const flags = getFlags(options.ipv6Only);
20232028
// Refresh the id to make the previous call invalid
20242029
this._listeningId++;

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

+8
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,13 @@ const listenOnPort = [
6666
name: 'TypeError',
6767
message: /^The argument 'options' must have the property "port" or "path"\. Received .+$/,
6868
});
69+
} else if (options.host && options.host.match(/\[.*\]/)) {
70+
assert.throws(fn,
71+
{
72+
code: 'ERR_INVALID_ARG_VALUE',
73+
name: 'TypeError',
74+
message: /^The argument 'host' must not contain square brackets\. Received .+$/,
75+
});
6976
} else {
7077
assert.throws(fn,
7178
{
@@ -91,4 +98,5 @@ const listenOnPort = [
9198
shouldFailToListen({ host: 'localhost:3000' });
9299
shouldFailToListen({ host: { port: 3000 } });
93100
shouldFailToListen({ exclusive: true });
101+
shouldFailToListen({ host: '[::]', port: 3000 });
94102
}

0 commit comments

Comments
 (0)