From 097aea402bf6ee13e9d9b431794268c4071e581d Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Fri, 16 Jun 2017 21:47:48 +0200 Subject: [PATCH 1/5] net: fix abort on bad address input --- lib/net.js | 4 +++ .../test-net-better-error-messages-path.js | 25 +++++++++++++------ 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/lib/net.js b/lib/net.js index 1533843dbab981..614b440e0e7d1c 100644 --- a/lib/net.js +++ b/lib/net.js @@ -872,6 +872,10 @@ function internalConnect( var err; + if (typeof address !== 'string') { + throw new TypeError('Invalid address: ' + address); + } + if (localAddress || localPort) { debug('binding to localAddress: %s and localPort: %d (addressType: %d)', localAddress, localPort, addressType); diff --git a/test/parallel/test-net-better-error-messages-path.js b/test/parallel/test-net-better-error-messages-path.js index f4d00c7aebf055..4cb402f3f1afe4 100644 --- a/test/parallel/test-net-better-error-messages-path.js +++ b/test/parallel/test-net-better-error-messages-path.js @@ -2,12 +2,23 @@ const common = require('../common'); const net = require('net'); const assert = require('assert'); -const fp = '/tmp/fadagagsdfgsdf'; -const c = net.connect(fp); -c.on('connect', common.mustNotCall()); +{ + const fp = '/tmp/fadagagsdfgsdf'; + const c = net.connect(fp); -c.on('error', common.mustCall(function(e) { - assert.strictEqual(e.code, 'ENOENT'); - assert.strictEqual(e.message, `connect ENOENT ${fp}`); -})); + c.on('connect', common.mustNotCall()); + c.on('error', common.mustCall(function(e) { + assert.strictEqual(e.code, 'ENOENT'); + assert.strictEqual(e.message, `connect ENOENT ${fp}`); + })); +} + +{ + try { + net.createConnection({ path: {} }); + throw new Error('UNREACHABLE'); + } catch (e) { + assert.strictEqual(e.message, 'Invalid address: [object Object]'); + } +} From a4d753f34ad5d8e7bcd344e8491ef304eb972633 Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Fri, 16 Jun 2017 22:35:28 +0200 Subject: [PATCH 2/5] fixup --- lib/net.js | 3 ++- test/parallel/test-net-better-error-messages-path.js | 7 ++----- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/lib/net.js b/lib/net.js index 614b440e0e7d1c..3f4d215b3b643a 100644 --- a/lib/net.js +++ b/lib/net.js @@ -42,6 +42,7 @@ const WriteWrap = process.binding('stream_wrap').WriteWrap; const async_id_symbol = process.binding('async_wrap').async_id_symbol; const { newUid, setInitTriggerId } = require('async_hooks'); const nextTick = require('internal/process/next_tick').nextTick; +const errors = require('internal/errors'); var cluster; var dns; @@ -873,7 +874,7 @@ function internalConnect( var err; if (typeof address !== 'string') { - throw new TypeError('Invalid address: ' + address); + throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'address', 'string'); } if (localAddress || localPort) { diff --git a/test/parallel/test-net-better-error-messages-path.js b/test/parallel/test-net-better-error-messages-path.js index 4cb402f3f1afe4..ec7b508777ce6f 100644 --- a/test/parallel/test-net-better-error-messages-path.js +++ b/test/parallel/test-net-better-error-messages-path.js @@ -15,10 +15,7 @@ const assert = require('assert'); } { - try { + assert.throws(() => { net.createConnection({ path: {} }); - throw new Error('UNREACHABLE'); - } catch (e) { - assert.strictEqual(e.message, 'Invalid address: [object Object]'); - } + }, TypeError); } From dcb6cde8aac3542f06f6f7e07164cbac78b34f67 Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Fri, 16 Jun 2017 23:38:29 +0200 Subject: [PATCH 3/5] move check to connect function --- lib/net.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/net.js b/lib/net.js index 3f4d215b3b643a..7f00ce68b47f2c 100644 --- a/lib/net.js +++ b/lib/net.js @@ -873,10 +873,6 @@ function internalConnect( var err; - if (typeof address !== 'string') { - throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'address', 'string'); - } - if (localAddress || localPort) { debug('binding to localAddress: %s and localPort: %d (addressType: %d)', localAddress, localPort, addressType); @@ -969,8 +965,9 @@ Socket.prototype.connect = function() { this._sockname = null; } - var pipe = !!options.path; - debug('pipe', pipe, options.path); + const path = options.path; + var pipe = !!path; + debug('pipe', pipe, path); if (!this._handle) { this._handle = pipe ? new Pipe() : new TCP(); @@ -987,7 +984,10 @@ Socket.prototype.connect = function() { this.writable = true; if (pipe) { - internalConnect(this, options.path); + if (typeof path !== 'string') { + throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'options.path', 'string', path); + } + internalConnect(this, path); } else { lookupAndConnect(this, options); } From 8ee82af985fe0dfcd8c5295ad466d86ee69dcbf0 Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Sat, 17 Jun 2017 00:14:36 +0200 Subject: [PATCH 4/5] fix line length --- lib/net.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/net.js b/lib/net.js index 7f00ce68b47f2c..278153d3bdffc7 100644 --- a/lib/net.js +++ b/lib/net.js @@ -985,7 +985,10 @@ Socket.prototype.connect = function() { if (pipe) { if (typeof path !== 'string') { - throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'options.path', 'string', path); + throw new errors.TypeError('ERR_INVALID_ARG_TYPE', + 'options.path', + 'string', + path); } internalConnect(this, path); } else { From 7a6f4144cfd4034567b6ce3c5a32d5c55471c4fb Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Sat, 17 Jun 2017 00:20:53 +0200 Subject: [PATCH 5/5] address comments --- .../parallel/test-net-better-error-messages-path.js | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/test/parallel/test-net-better-error-messages-path.js b/test/parallel/test-net-better-error-messages-path.js index ec7b508777ce6f..bb2256637a0d30 100644 --- a/test/parallel/test-net-better-error-messages-path.js +++ b/test/parallel/test-net-better-error-messages-path.js @@ -8,14 +8,15 @@ const assert = require('assert'); const c = net.connect(fp); c.on('connect', common.mustNotCall()); - c.on('error', common.mustCall(function(e) { - assert.strictEqual(e.code, 'ENOENT'); - assert.strictEqual(e.message, `connect ENOENT ${fp}`); + c.on('error', common.expectsError({ + code: 'ENOENT', + message: `connect ENOENT ${fp}` })); } { - assert.throws(() => { - net.createConnection({ path: {} }); - }, TypeError); + assert.throws( + () => net.createConnection({ path: {} }), + common.expectsError({ code: 'ERR_INVALID_ARG_TYPE' }) + ); }