-
Notifications
You must be signed in to change notification settings - Fork 30k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 <matteo.collina@gmail.com> Reviewed-By: Refael Ackermann <refack@gmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
- Loading branch information
Showing
3 changed files
with
69 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
'use strict'; | ||
// Flags: --expose-internals | ||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const net = require('net'); | ||
const { normalizedArgsSymbol } = require('internal/net'); | ||
|
||
function validateNormalizedArgs(input, output) { | ||
const args = net._normalizeArgs(input); | ||
|
||
assert.deepStrictEqual(args, output); | ||
assert.strictEqual(args[normalizedArgsSymbol], true); | ||
} | ||
|
||
// Test creation of normalized arguments. | ||
validateNormalizedArgs([], [{}, null]); | ||
validateNormalizedArgs([{ port: 1234 }], [{ port: 1234 }, null]); | ||
validateNormalizedArgs([{ port: 1234 }, assert.fail], | ||
[{ port: 1234 }, assert.fail]); | ||
|
||
// Connecting to the server should fail with a standard array. | ||
{ | ||
const server = net.createServer(common.mustNotCall('should not connect')); | ||
|
||
server.listen(common.mustCall(() => { | ||
const possibleErrors = ['ECONNREFUSED', 'EADDRNOTAVAIL']; | ||
const port = server.address().port; | ||
const socket = new net.Socket(); | ||
|
||
socket.on('error', common.mustCall((err) => { | ||
assert(possibleErrors.includes(err.code)); | ||
assert(possibleErrors.includes(err.errno)); | ||
assert.strictEqual(err.syscall, 'connect'); | ||
server.close(); | ||
})); | ||
|
||
socket.connect([{ port }, assert.fail]); | ||
})); | ||
} | ||
|
||
// Connecting to the server should succeed with a normalized array. | ||
{ | ||
const server = net.createServer(common.mustCall((connection) => { | ||
connection.end(); | ||
server.close(); | ||
})); | ||
|
||
server.listen(common.mustCall(() => { | ||
const port = server.address().port; | ||
const socket = new net.Socket(); | ||
const args = net._normalizeArgs([{ port }, common.mustCall()]); | ||
|
||
socket.connect(args); | ||
})); | ||
} |