From d80d131c75d7defa06845d7daa634c4d51515e0c Mon Sep 17 00:00:00 2001 From: =?utf8?q?Maciej=20Ma=C5=82ecki?= Date: Wed, 4 Sep 2013 18:57:43 -0400 Subject: [PATCH] net: add a port validation to `connect` Fix "Assertion failed" when trying to connect to non-int ports: Assertion failed: (args[2]->Uint32Value()), function Connect, file ../src/tcp_wrap.cc, line 379. Abort trap: 6 --- lib/net.js | 14 ++++++++++---- test/simple/test-net-create-connection.js | 6 ++++++ 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/lib/net.js b/lib/net.js index 52f171b..4331a6b 100644 --- a/lib/net.js +++ b/lib/net.js @@ -794,10 +794,16 @@ function connect(self, address, port, addressType, localAddress) { } var req = { oncomplete: afterConnect }; - if (addressType == 6) { - err = self._handle.connect6(req, address, port); - } else if (addressType == 4) { - err = self._handle.connect(req, address, port); + if (addressType === 6 || addressType === 4) { + port = port | 0; + if (port <= 0 || port > 65535) + throw new RangeError('Port should be > 0 and < 65536'); + + if (addressType === 6) { + err = self._handle.connect6(req, address, port); + } else if (addressType === 4) { + err = self._handle.connect(req, address, port); + } } else { err = self._handle.connect(req, address, afterConnect); } diff --git a/test/simple/test-net-create-connection.js b/test/simple/test-net-create-connection.js index c21be15..12f7f0b 100644 --- a/test/simple/test-net-create-connection.js +++ b/test/simple/test-net-create-connection.js @@ -42,6 +42,12 @@ server.listen(tcpPort, 'localhost', function() { net.createConnection(tcpPort, 'localhost').on('connect', cb); net.createConnection(tcpPort, cb); net.createConnection(tcpPort, 'localhost', cb); + + assert.throws(function () { + net.createConnection({ + port: 'invalid!' + }, cb); + }); }); process.on('exit', function() { -- 2.7.4