diff --git a/lib/net.js b/lib/net.js index 4de6f9c5f6f23a..409db6feb92fa2 100644 --- a/lib/net.js +++ b/lib/net.js @@ -35,6 +35,7 @@ const { NumberParseInt, ObjectDefineProperty, ObjectSetPrototypeOf, + RegExpPrototypeExec, Symbol, SymbolAsyncDispose, SymbolDispose, @@ -2019,6 +2020,10 @@ Server.prototype.listen = function(...args) { toNumber(args.length > 2 && args[2]); // (port, host, backlog) options = options._handle || options.handle || options; + if (typeof options.host === 'string' && RegExpPrototypeExec(/\[.*\]/, options.host) !== null) { + throw new ERR_INVALID_ARG_VALUE('host', options.host, 'must not contain sqaure brackets'); + } + const flags = getFlags(options.ipv6Only); // Refresh the id to make the previous call invalid this._listeningId++; diff --git a/test/parallel/test-net-server-listen-options.js b/test/parallel/test-net-server-listen-options.js index 7e306af8ab082f..c0b2e6d8f37c89 100644 --- a/test/parallel/test-net-server-listen-options.js +++ b/test/parallel/test-net-server-listen-options.js @@ -66,6 +66,13 @@ const listenOnPort = [ name: 'TypeError', message: /^The argument 'options' must have the property "port" or "path"\. Received .+$/, }); + } else if (options.host && options.host.match(/\[.*\]/)) { + assert.throws(fn, + { + code: 'ERR_INVALID_ARG_VALUE', + name: 'TypeError', + message: /^The argument 'host' must not contain sqaure brackets\. Received .+$/, + }); } else { assert.throws(fn, { @@ -91,4 +98,5 @@ const listenOnPort = [ shouldFailToListen({ host: 'localhost:3000' }); shouldFailToListen({ host: { port: 3000 } }); shouldFailToListen({ exclusive: true }); + shouldFailToListen({ host: '[::]', port: 3000 }); }