net: Validate port in createServer().listen()

Make sure we validate the port number in all kinds of `listen()` calls.

Fixes: https://github.com/nodejs/node/issues/5727
PR-URL: https://github.com/nodejs/node/pull/5732
Reviewed-By: Evan Lucas <evanlucas@me.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
Dirceu Pereira Tiegs 2016-03-15 20:34:19 -03:00 committed by James M Snell
parent 7dc1a87a7b
commit 02ac302b6d
5 changed files with 39 additions and 20 deletions

View file

@ -1,6 +1,6 @@
'use strict';
module.exports = { isLegalPort };
module.exports = { isLegalPort, assertPort };
// Check that the port number is not NaN when coerced to a number,
// is an integer and that it falls within the legal range of port numbers.
@ -10,3 +10,9 @@ function isLegalPort(port) {
return false;
return +port === (+port >>> 0) && port <= 0xFFFF;
}
function assertPort(port) {
if (typeof port !== 'undefined' && !isLegalPort(port))
throw new RangeError('"port" argument must be >= 0 and < 65536');
}