Skip to content

Commit c32c889

Browse files
dirceujasnell
authored andcommitted
net: Validate port in createServer().listen()
Make sure we validate the port number in all kinds of `listen()` calls. Fixes: #5727 PR-URL: #5732 Reviewed-By: Evan Lucas <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Trevor Norris <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 451f4fc commit c32c889

File tree

5 files changed

+39
-20
lines changed

5 files changed

+39
-20
lines changed

lib/internal/net.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
module.exports = { isLegalPort };
3+
module.exports = { isLegalPort, assertPort };
44

55
// Check that the port number is not NaN when coerced to a number,
66
// is an integer and that it falls within the legal range of port numbers.
@@ -10,3 +10,9 @@ function isLegalPort(port) {
1010
return false;
1111
return +port === (+port >>> 0) && port <= 0xFFFF;
1212
}
13+
14+
15+
function assertPort(port) {
16+
if (typeof port !== 'undefined' && !isLegalPort(port))
17+
throw new RangeError('"port" argument must be >= 0 and < 65536');
18+
}

lib/net.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ var cluster;
2424
const errnoException = util._errnoException;
2525
const exceptionWithHostPort = util._exceptionWithHostPort;
2626
const isLegalPort = internalNet.isLegalPort;
27+
const assertPort = internalNet.assertPort;
2728

2829
function noop() {}
2930

@@ -1352,9 +1353,7 @@ Server.prototype.listen = function() {
13521353
(typeof h.port === 'undefined' && 'port' in h)) {
13531354
// Undefined is interpreted as zero (random port) for consistency
13541355
// with net.connect().
1355-
if (typeof h.port !== 'undefined' && !isLegalPort(h.port))
1356-
throw new RangeError('"port" option should be >= 0 and < 65536: ' +
1357-
h.port);
1356+
assertPort(h.port);
13581357
if (h.host)
13591358
listenAfterLookup(h.port | 0, h.host, backlog, h.exclusive);
13601359
else
@@ -1375,10 +1374,12 @@ Server.prototype.listen = function() {
13751374
typeof arguments[1] === 'function' ||
13761375
typeof arguments[1] === 'number') {
13771376
// The first argument is the port, no IP given.
1377+
assertPort(port);
13781378
listen(self, null, port, 4, backlog);
13791379

13801380
} else {
13811381
// The first argument is the port, the second an IP.
1382+
assertPort(port);
13821383
listenAfterLookup(port, arguments[1], backlog);
13831384
}
13841385

test/parallel/test-net-listen-port-option.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ net.Server().listen({ port: '' + common.PORT }, close);
1818
].forEach(function(port) {
1919
assert.throws(function() {
2020
net.Server().listen({ port: port }, assert.fail);
21-
}, /"port" option should be >= 0 and < 65536/i);
21+
}, /"port" argument must be >= 0 and < 65536/i);
2222
});
2323

2424
[null, true, false].forEach(function(port) {

test/parallel/test-regress-GH-5727.js

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
'use strict';
2+
const common = require('../common');
3+
const assert = require('assert');
4+
const net = require('net');
5+
6+
const invalidPort = -1 >>> 0;
7+
const errorMessage = /"port" argument must be \>= 0 and \< 65536/;
8+
9+
net.Server().listen(common.PORT, function() {
10+
assert.equal(this._connectionKey, '6::::' + common.PORT);
11+
this.close();
12+
});
13+
14+
// The first argument is a configuration object
15+
assert.throws(() => {
16+
net.Server().listen({ port: invalidPort }, common.fail);
17+
}, errorMessage);
18+
19+
// The first argument is the port, no IP given.
20+
assert.throws(() => {
21+
net.Server().listen(invalidPort, common.fail);
22+
}, errorMessage);
23+
24+
// The first argument is the port, the second an IP.
25+
assert.throws(() => {
26+
net.Server().listen(invalidPort, '0.0.0.0', common.fail);
27+
}, errorMessage);

test/sequential/test-net-server-address.js

-15
Original file line numberDiff line numberDiff line change
@@ -87,18 +87,3 @@ server3.listen(0, function() {
8787
assert.strictEqual(address.family, family_ipv6);
8888
server3.close();
8989
});
90-
91-
// Test without hostname, but with port -1
92-
var server4 = net.createServer();
93-
94-
server4.on('error', function(e) {
95-
console.log('Error on ip socket: ' + e.toString());
96-
});
97-
98-
// Specify -1 as port number
99-
server4.listen(-1, function() {
100-
var address = server4.address();
101-
assert.strictEqual(address.address, anycast_ipv6);
102-
assert.strictEqual(address.family, family_ipv6);
103-
server4.close();
104-
});

0 commit comments

Comments
 (0)