Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
net: add a port validation to connect
Browse files Browse the repository at this point in the history
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
  • Loading branch information
mmalecki authored and bnoordhuis committed Sep 19, 2013
1 parent a1cf3ad commit d80d131
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
14 changes: 10 additions & 4 deletions lib/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
6 changes: 6 additions & 0 deletions test/simple/test-net-create-connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit d80d131

Please sign in to comment.