Skip to content

Commit 51664fc

Browse files
committed
net: add symbol to normalized connect() args
This commit attaches a Symbol to the result of net._normalizeArgs(). This prevents normal arrays from being passed to the internal Socket.prototype.connect() bypass logic. PR-URL: #13069 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Refael Ackermann <[email protected]> Reviewed-By: Joyee Cheung <[email protected]>
1 parent 6b1819c commit 51664fc

File tree

3 files changed

+69
-5
lines changed

3 files changed

+69
-5
lines changed

lib/internal/net.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@ function isLegalPort(port) {
1010
}
1111

1212
module.exports = {
13-
isLegalPort
13+
isLegalPort,
14+
normalizedArgsSymbol: Symbol('normalizedArgs')
1415
};

lib/net.js

+12-4
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ var dns;
4949
const errnoException = util._errnoException;
5050
const exceptionWithHostPort = util._exceptionWithHostPort;
5151
const isLegalPort = internalNet.isLegalPort;
52+
const normalizedArgsSymbol = internalNet.normalizedArgsSymbol;
5253

5354
function noop() {}
5455

@@ -118,8 +119,12 @@ function connect() {
118119
// For Server.prototype.listen(), the [...] part is [, backlog]
119120
// but will not be handled here (handled in listen())
120121
function normalizeArgs(args) {
122+
var arr;
123+
121124
if (args.length === 0) {
122-
return [{}, null];
125+
arr = [{}, null];
126+
arr[normalizedArgsSymbol] = true;
127+
return arr;
123128
}
124129

125130
const arg0 = args[0];
@@ -140,9 +145,12 @@ function normalizeArgs(args) {
140145

141146
var cb = args[args.length - 1];
142147
if (typeof cb !== 'function')
143-
return [options, null];
148+
arr = [options, null];
144149
else
145-
return [options, cb];
150+
arr = [options, cb];
151+
152+
arr[normalizedArgsSymbol] = true;
153+
return arr;
146154
}
147155

148156

@@ -947,7 +955,7 @@ Socket.prototype.connect = function() {
947955
// already been normalized (so we don't normalize more than once). This has
948956
// been solved before in https://github.com/nodejs/node/pull/12342, but was
949957
// reverted as it had unintended side effects.
950-
if (arguments.length === 1 && Array.isArray(arguments[0])) {
958+
if (Array.isArray(arguments[0]) && arguments[0][normalizedArgsSymbol]) {
951959
normalized = arguments[0];
952960
} else {
953961
var args = new Array(arguments.length);
+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
'use strict';
2+
// Flags: --expose-internals
3+
const common = require('../common');
4+
const assert = require('assert');
5+
const net = require('net');
6+
const { normalizedArgsSymbol } = require('internal/net');
7+
8+
function validateNormalizedArgs(input, output) {
9+
const args = net._normalizeArgs(input);
10+
11+
assert.deepStrictEqual(args, output);
12+
assert.strictEqual(args[normalizedArgsSymbol], true);
13+
}
14+
15+
// Test creation of normalized arguments.
16+
validateNormalizedArgs([], [{}, null]);
17+
validateNormalizedArgs([{ port: 1234 }], [{ port: 1234 }, null]);
18+
validateNormalizedArgs([{ port: 1234 }, assert.fail],
19+
[{ port: 1234 }, assert.fail]);
20+
21+
// Connecting to the server should fail with a standard array.
22+
{
23+
const server = net.createServer(common.mustNotCall('should not connect'));
24+
25+
server.listen(common.mustCall(() => {
26+
const possibleErrors = ['ECONNREFUSED', 'EADDRNOTAVAIL'];
27+
const port = server.address().port;
28+
const socket = new net.Socket();
29+
30+
socket.on('error', common.mustCall((err) => {
31+
assert(possibleErrors.includes(err.code));
32+
assert(possibleErrors.includes(err.errno));
33+
assert.strictEqual(err.syscall, 'connect');
34+
server.close();
35+
}));
36+
37+
socket.connect([{ port }, assert.fail]);
38+
}));
39+
}
40+
41+
// Connecting to the server should succeed with a normalized array.
42+
{
43+
const server = net.createServer(common.mustCall((connection) => {
44+
connection.end();
45+
server.close();
46+
}));
47+
48+
server.listen(common.mustCall(() => {
49+
const port = server.address().port;
50+
const socket = new net.Socket();
51+
const args = net._normalizeArgs([{ port }, common.mustCall()]);
52+
53+
socket.connect(args);
54+
}));
55+
}

0 commit comments

Comments
 (0)