Skip to content

Commit 9b25d8b

Browse files
jasnelltargos
authored andcommitted
lib: move isLegalPort to validators, refactor
isLegalPort was used multiple places in the same way -- to validate the port and throw if necessary. Moved into internal/validators. PR-URL: nodejs#31851 Reviewed-By: Richard Lau <riclau@uk.ibm.com>
1 parent ea17cf6 commit 9b25d8b

10 files changed

+68
-80
lines changed

lib/dgram.js

+4-20
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,11 @@ const {
3535
newHandle,
3636
} = require('internal/dgram');
3737
const { guessHandleType } = internalBinding('util');
38-
const {
39-
isLegalPort,
40-
} = require('internal/net');
4138
const {
4239
ERR_INVALID_ARG_TYPE,
4340
ERR_MISSING_ARGS,
4441
ERR_SOCKET_ALREADY_BOUND,
4542
ERR_SOCKET_BAD_BUFFER_SIZE,
46-
ERR_SOCKET_BAD_PORT,
4743
ERR_SOCKET_BUFFER_SIZE,
4844
ERR_SOCKET_CANNOT_SEND,
4945
ERR_SOCKET_DGRAM_IS_CONNECTED,
@@ -54,7 +50,8 @@ const {
5450
const {
5551
isInt32,
5652
validateString,
57-
validateNumber
53+
validateNumber,
54+
validatePort,
5855
} = require('internal/validators');
5956
const { Buffer } = require('buffer');
6057
const { deprecate } = require('internal/util');
@@ -352,21 +349,8 @@ Socket.prototype.bind = function(port_, address_ /* , callback */) {
352349
return this;
353350
};
354351

355-
356-
function validatePort(port) {
357-
const legal = isLegalPort(port);
358-
if (legal)
359-
port = port | 0;
360-
361-
if (!legal || port === 0)
362-
throw new ERR_SOCKET_BAD_PORT(port);
363-
364-
return port;
365-
}
366-
367-
368352
Socket.prototype.connect = function(port, address, callback) {
369-
port = validatePort(port);
353+
port = validatePort(port, 'Port', { allowZero: false });
370354
if (typeof address === 'function') {
371355
callback = address;
372356
address = '';
@@ -612,7 +596,7 @@ Socket.prototype.send = function(buffer,
612596
}
613597

614598
if (!connected)
615-
port = validatePort(port);
599+
port = validatePort(port, 'Port', { allowZero: false });
616600

617601
// Normalize callback so it's either a function or undefined but not anything
618602
// else.

lib/dns.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ const {
2929

3030
const cares = internalBinding('cares_wrap');
3131
const { toASCII } = require('internal/idna');
32-
const { isIP, isLegalPort } = require('internal/net');
32+
const { isIP } = require('internal/net');
3333
const { customPromisifyArgs } = require('internal/util');
3434
const errors = require('internal/errors');
3535
const {
@@ -45,9 +45,11 @@ const {
4545
ERR_INVALID_CALLBACK,
4646
ERR_INVALID_OPT_VALUE,
4747
ERR_MISSING_ARGS,
48-
ERR_SOCKET_BAD_PORT
4948
} = errors.codes;
50-
const { validateString } = require('internal/validators');
49+
const {
50+
validatePort,
51+
validateString,
52+
} = require('internal/validators');
5153

5254
const {
5355
GetAddrInfoReqWrap,
@@ -171,8 +173,7 @@ function lookupService(address, port, callback) {
171173
if (isIP(address) === 0)
172174
throw new ERR_INVALID_OPT_VALUE('address', address);
173175

174-
if (!isLegalPort(port))
175-
throw new ERR_SOCKET_BAD_PORT(port);
176+
validatePort(port);
176177

177178
if (typeof callback !== 'function')
178179
throw new ERR_INVALID_CALLBACK(callback);

lib/internal/cluster/master.js

+2-5
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,12 @@ const RoundRobinHandle = require('internal/cluster/round_robin_handle');
1414
const SharedHandle = require('internal/cluster/shared_handle');
1515
const Worker = require('internal/cluster/worker');
1616
const { internal, sendHelper } = require('internal/cluster/utils');
17-
const { ERR_SOCKET_BAD_PORT } = require('internal/errors').codes;
1817
const cluster = new EventEmitter();
1918
const intercom = new EventEmitter();
2019
const SCHED_NONE = 1;
2120
const SCHED_RR = 2;
22-
const { isLegalPort } = require('internal/net');
2321
const [ minPort, maxPort ] = [ 1024, 65535 ];
22+
const { validatePort } = require('internal/validators');
2423

2524
module.exports = cluster;
2625

@@ -118,9 +117,7 @@ function createWorkerProcess(id, env) {
118117
else
119118
inspectPort = cluster.settings.inspectPort;
120119

121-
if (!isLegalPort(inspectPort)) {
122-
throw new ERR_SOCKET_BAD_PORT(inspectPort);
123-
}
120+
validatePort(inspectPort);
124121
} else {
125122
inspectPort = process.debugPort + debugPortOffset;
126123
if (inspectPort > maxPort)

lib/internal/dns/promises.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const {
1414
} = require('internal/dns/utils');
1515
const { codes, dnsException } = require('internal/errors');
1616
const { toASCII } = require('internal/idna');
17-
const { isIP, isLegalPort } = require('internal/net');
17+
const { isIP } = require('internal/net');
1818
const {
1919
getaddrinfo,
2020
getnameinfo,
@@ -27,10 +27,11 @@ const {
2727
ERR_INVALID_ARG_TYPE,
2828
ERR_INVALID_OPT_VALUE,
2929
ERR_MISSING_ARGS,
30-
ERR_SOCKET_BAD_PORT
3130
} = codes;
32-
const { validateString } = require('internal/validators');
33-
31+
const {
32+
validatePort,
33+
validateString
34+
} = require('internal/validators');
3435

3536
function onlookup(err, addresses) {
3637
if (err) {
@@ -162,8 +163,7 @@ function lookupService(address, port) {
162163
if (isIP(address) === 0)
163164
throw new ERR_INVALID_OPT_VALUE('address', address);
164165

165-
if (!isLegalPort(port))
166-
throw new ERR_SOCKET_BAD_PORT(port);
166+
validatePort(port);
167167

168168
return createLookupServicePromise(address, +port);
169169
}

lib/internal/errors.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1274,7 +1274,7 @@ E('ERR_SOCKET_ALREADY_BOUND', 'Socket is already bound', Error);
12741274
E('ERR_SOCKET_BAD_BUFFER_SIZE',
12751275
'Buffer size must be a positive integer', TypeError);
12761276
E('ERR_SOCKET_BAD_PORT',
1277-
'Port should be >= 0 and < 65536. Received %s.', RangeError);
1277+
'%s should be >= 0 and < 65536. Received %s.', RangeError);
12781278
E('ERR_SOCKET_BAD_TYPE',
12791279
'Bad socket type specified. Valid types are: udp4, udp6', TypeError);
12801280
E('ERR_SOCKET_BUFFER_SIZE',

lib/internal/net.js

-10
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,6 @@ function isIP(s) {
4141
return 0;
4242
}
4343

44-
// Check that the port number is not NaN when coerced to a number,
45-
// is an integer and that it falls within the legal range of port numbers.
46-
function isLegalPort(port) {
47-
if ((typeof port !== 'number' && typeof port !== 'string') ||
48-
(typeof port === 'string' && port.trim().length === 0))
49-
return false;
50-
return +port === (+port >>> 0) && port <= 0xFFFF;
51-
}
52-
5344
function makeSyncWrite(fd) {
5445
return function(chunk, enc, cb) {
5546
if (enc !== 'buffer')
@@ -74,7 +65,6 @@ module.exports = {
7465
isIP,
7566
isIPv4,
7667
isIPv6,
77-
isLegalPort,
7868
makeSyncWrite,
7969
normalizedArgsSymbol: Symbol('normalizedArgs')
8070
};

lib/internal/validators.js

+19-4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const {
99
const {
1010
hideStackFrames,
1111
codes: {
12+
ERR_SOCKET_BAD_PORT,
1213
ERR_INVALID_ARG_TYPE,
1314
ERR_INVALID_ARG_VALUE,
1415
ERR_OUT_OF_RANGE,
@@ -144,15 +145,29 @@ const validateBuffer = hideStackFrames((buffer, name = 'buffer') => {
144145
}
145146
});
146147

148+
// Check that the port number is not NaN when coerced to a number,
149+
// is an integer and that it falls within the legal range of port numbers.
150+
function validatePort(port, name = 'Port', { allowZero = true } = {}) {
151+
if ((typeof port !== 'number' && typeof port !== 'string') ||
152+
(typeof port === 'string' && port.trim().length === 0) ||
153+
+port !== (+port >>> 0) ||
154+
port > 0xFFFF ||
155+
(port === 0 && !allowZero)) {
156+
throw new ERR_SOCKET_BAD_PORT(name, port);
157+
}
158+
return port | 0;
159+
}
160+
147161
module.exports = {
148162
isInt32,
149163
isUint32,
150164
parseMode,
151165
validateBuffer,
152-
validateInteger,
153166
validateInt32,
154-
validateUint32,
155-
validateString,
167+
validateInteger,
156168
validateNumber,
157-
validateSignalName
169+
validatePort,
170+
validateSignalName,
171+
validateString,
172+
validateUint32,
158173
};

lib/net.js

+7-9
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ const {
4141
isIP,
4242
isIPv4,
4343
isIPv6,
44-
isLegalPort,
4544
normalizedArgsSymbol,
4645
makeSyncWrite
4746
} = require('internal/net');
@@ -92,15 +91,18 @@ const {
9291
ERR_INVALID_OPT_VALUE,
9392
ERR_SERVER_ALREADY_LISTEN,
9493
ERR_SERVER_NOT_RUNNING,
95-
ERR_SOCKET_BAD_PORT,
9694
ERR_SOCKET_CLOSED
9795
},
9896
errnoException,
9997
exceptionWithHostPort,
10098
uvExceptionWithHostPort
10199
} = require('internal/errors');
102100
const { isUint8Array } = require('internal/util/types');
103-
const { validateInt32, validateString } = require('internal/validators');
101+
const {
102+
validateInt32,
103+
validatePort,
104+
validateString
105+
} = require('internal/validators');
104106
const kLastWriteQueueSize = Symbol('lastWriteQueueSize');
105107
const {
106108
DTRACE_NET_SERVER_CONNECTION,
@@ -997,9 +999,7 @@ function lookupAndConnect(self, options) {
997999
throw new ERR_INVALID_ARG_TYPE('options.port',
9981000
['number', 'string'], port);
9991001
}
1000-
if (!isLegalPort(port)) {
1001-
throw new ERR_SOCKET_BAD_PORT(port);
1002-
}
1002+
validatePort(port);
10031003
}
10041004
port |= 0;
10051005

@@ -1436,9 +1436,7 @@ Server.prototype.listen = function(...args) {
14361436
// or if options.port is normalized as 0 before
14371437
let backlog;
14381438
if (typeof options.port === 'number' || typeof options.port === 'string') {
1439-
if (!isLegalPort(options.port)) {
1440-
throw new ERR_SOCKET_BAD_PORT(options.port);
1441-
}
1439+
validatePort(options.port, 'options.port');
14421440
backlog = options.backlog || backlogFromArgs;
14431441
// start TCP server listening on host:port
14441442
if (options.host) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Flags: --expose-internals
2+
'use strict';
3+
4+
require('../common');
5+
const assert = require('assert');
6+
const { validatePort } = require('internal/validators');
7+
8+
for (let n = 0; n <= 0xFFFF; n++) {
9+
validatePort(n);
10+
validatePort(`${n}`);
11+
validatePort(`0x${n.toString(16)}`);
12+
validatePort(`0o${n.toString(8)}`);
13+
validatePort(`0b${n.toString(2)}`);
14+
}
15+
16+
[
17+
-1, 'a', {}, [], false, true,
18+
0xFFFF + 1, Infinity, -Infinity, NaN,
19+
undefined, null, '', ' ', 1.1, '0x',
20+
'-0x1', '-0o1', '-0b1', '0o', '0b'
21+
].forEach((i) => assert.throws(() => validatePort(i), {
22+
code: 'ERR_SOCKET_BAD_PORT'
23+
}));

test/parallel/test-net-internal.js

-20
This file was deleted.

0 commit comments

Comments
 (0)