Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: don't assume broadcast traffic is unfiltered #220

Closed
wants to merge 23 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
4dc660e
build: do not generate support for libuv's probes
Dec 3, 2014
20a7088
deps: update libuv to 1.0.2
saghul Dec 9, 2014
8708c7a
test: mark more tests as flaky
orangemocha Dec 10, 2014
946cec7
lib,src: fix spawnSync ignoring its 'env' option
juamedgod Oct 14, 2014
4bba870
test: add test for spawnSync() env option
cjihrig Dec 9, 2014
5b9e5bd
doc: clarify create{Read,Write}Stream fd option
benjamincburns Jul 26, 2014
6f6a979
zlib: support concatenated gzip files
eendeego Sep 9, 2014
e93ff4f
debugger: fix unhandled error in setBreakpoint
bajtos Nov 4, 2013
93533e9
src: fix windows build error
bnoordhuis Nov 11, 2014
9158666
stream: switch _writableState.buffer to queue
chrisdickinson Dec 4, 2014
890baa0
doc: add details for http res/req end callback
JacksonTian Dec 10, 2014
6a03fce
url: improve parsing speed
CGavrila Oct 28, 2014
d8586ea
lib: introduce process module
Dec 30, 2014
8b04161
doc: util: document --trace-deprecation
bnoordhuis Dec 21, 2014
d5c7a97
doc: added TC meeting minutes 2014-12-17
rvagg Dec 17, 2014
a308395
build: i18n: add icu config options
srl295 Nov 13, 2014
261706e
doc: added TC meeting minutes 2014-12-30
rvagg Jan 2, 2015
8cfbeed
docs: update to authors file
srl295 Jan 3, 2015
b636ba8
net: make connect() input validation synchronous
cjihrig Jan 3, 2015
372a2f5
smalloc: fix bad assert for zero length data
trevnorris Jan 5, 2015
94e1475
Merge remote-tracking branch 'joyent/v0.12' into v1.x
bnoordhuis Jan 5, 2015
eaed2a1
deps: update libuv to 1.2.0
bnoordhuis Jan 5, 2015
52e600a
test: don't assume broadcast traffic is unfiltered
bnoordhuis Dec 29, 2014
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
net: make connect() input validation synchronous
Socket.prototype.connect() sometimes throws on bad inputs
after an asynchronous operation. This commit makes the input
validation synchronous. This commit also removes some hard
coded IP addresses.

PR-URL: nodejs/node-v0.x-archive#8180
Fixes: nodejs/node-v0.x-archive#8140
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
Reviewed-By: Fedor Indutny <fedor@indutny.com>
Reviewed-By: Timothy J Fontaine <tjfontaine@gmail.com>
  • Loading branch information
cjihrig committed Jan 4, 2015
commit b636ba8186d191c52ee36f2f2b1aebbbb4d95575
77 changes: 29 additions & 48 deletions lib/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -785,34 +785,18 @@ function connect(self, address, port, addressType, localAddress, localPort) {
assert.ok(self._connecting);

var err;
if (localAddress || localPort) {
if (localAddress && !exports.isIP(localAddress))
err = new TypeError(
'localAddress should be a valid IP: ' + localAddress);

if (localPort && !util.isNumber(localPort))
err = new TypeError('localPort should be a number: ' + localPort);

if (localAddress || localPort) {
var bind;

switch (addressType) {
case 4:
if (!localAddress)
localAddress = '0.0.0.0';
bind = self._handle.bind;
break;
case 6:
if (!localAddress)
localAddress = '::';
bind = self._handle.bind6;
break;
default:
err = new TypeError('Invalid addressType: ' + addressType);
break;
}

if (err) {
self._destroy(err);
if (addressType === 4) {
localAddress = localAddress || '0.0.0.0';
bind = self._handle.bind;
} else if (addressType === 6) {
localAddress = localAddress || '::';
bind = self._handle.bind6;
} else {
self._destroy(new TypeError('Invalid addressType: ' + addressType));
return;
}

Expand All @@ -832,15 +816,12 @@ function connect(self, address, port, addressType, localAddress, localPort) {
if (addressType === 6 || addressType === 4) {
var req = new TCPConnectWrap();
req.oncomplete = afterConnect;
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) {
if (addressType === 4)
err = self._handle.connect(req, address, port);
}
else
err = self._handle.connect6(req, address, port);

} else {
var req = new PipeConnectWrap();
req.oncomplete = afterConnect;
Expand Down Expand Up @@ -898,19 +879,26 @@ Socket.prototype.connect = function(options, cb) {
if (pipe) {
connect(self, options.path);

} else if (!options.host) {
debug('connect: missing host');
self._host = '127.0.0.1';
connect(self, self._host, options.port, 4);

} else {
var dns = require('dns');
var host = options.host;
var host = options.host || 'localhost';
var port = options.port | 0;
var localAddress = options.localAddress;
var localPort = options.localPort;
var dnsopts = {
family: options.family,
hints: 0
};

if (localAddress && !exports.isIP(localAddress))
throw new TypeError('localAddress must be a valid IP: ' + localAddress);

if (localPort && !util.isNumber(localPort))
throw new TypeError('localPort should be a number: ' + localPort);

if (port <= 0 || port > 65535)
throw new RangeError('port should be > 0 and < 65536: ' + port);

if (dnsopts.family !== 4 && dnsopts.family !== 6)
dnsopts.hints = dns.ADDRCONFIG | dns.V4MAPPED;

Expand All @@ -936,19 +924,12 @@ Socket.prototype.connect = function(options, cb) {
});
} else {
timers._unrefActive(self);

addressType = addressType || 4;

// node_net.cc handles null host names graciously but user land
// expects remoteAddress to have a meaningful value
ip = ip || (addressType === 4 ? '127.0.0.1' : '0:0:0:0:0:0:0:1');

connect(self,
ip,
options.port,
port,
addressType,
options.localAddress,
options.localPort);
localAddress,
localPort);
}
});
}
Expand Down
53 changes: 22 additions & 31 deletions test/simple/test-net-localerror.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,39 +23,30 @@ var common = require('../common');
var assert = require('assert');
var net = require('net');

var server = net.createServer(function(socket) {
assert.ok(false, 'no clients should connect');
}).listen(common.PORT).on('listening', function() {
server.unref();
connect({
host: 'localhost',
port: common.PORT,
localPort: 'foobar',
}, 'localPort should be a number: foobar');

function test1(next) {
connect({
host: '127.0.0.1',
port: common.PORT,
localPort: 'foobar',
},
'localPort should be a number: foobar',
next);
}
connect({
host: 'localhost',
port: common.PORT,
localAddress: 'foobar',
}, 'localAddress should be a valid IP: foobar');

function test2(next) {
connect({
host: '127.0.0.1',
port: common.PORT,
localAddress: 'foobar',
},
'localAddress should be a valid IP: foobar',
next)
}
connect({
host: 'localhost',
port: 65536
}, 'port should be > 0 and < 65536: 65536');

test1(test2);
})
connect({
host: 'localhost',
port: 0
}, 'port should be > 0 and < 65536: 0');

function connect(opts, msg, cb) {
var client = net.connect(opts).on('connect', function() {
assert.ok(false, 'we should never connect');
}).on('error', function(err) {
assert.strictEqual(err.message, msg);
if (cb) cb();
});
function connect(opts, msg) {
assert.throws(function() {
var client = net.connect(opts);
}, msg);
}
16 changes: 14 additions & 2 deletions test/simple/test-process-active-wraps.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,16 @@ var handles = [];
})();

(function() {
function onlookup() {
setImmediate(function() {
assert.equal(process._getActiveRequests().length, 0);
});
};

expect(1, 0);
var conn = net.createConnection(common.PORT);
conn.on('error', function() { /* ignore */ });
conn.on('lookup', onlookup);
conn.on('error', function() { assert(false); });
expect(2, 1);
conn.destroy();
expect(2, 1); // client handle doesn't shut down until next tick
Expand All @@ -52,10 +59,15 @@ var handles = [];

(function() {
var n = 0;

handles.forEach(function(handle) {
handle.once('close', onclose);
});
function onclose() {
if (++n === handles.length) setImmediate(expect, 0, 0);
if (++n === handles.length) {
setImmediate(function() {
assert.equal(process._getActiveHandles().length, 0);
});
}
}
})();