Skip to content

Commit 2816677

Browse files
BridgeARaddaleax
authored andcommitted
net: fix abort on bad address input
PR-URL: #13726 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Refael Ackermann <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent c8b134b commit 2816677

File tree

2 files changed

+27
-10
lines changed

2 files changed

+27
-10
lines changed

lib/net.js

+11-3
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ const WriteWrap = process.binding('stream_wrap').WriteWrap;
4242
const async_id_symbol = process.binding('async_wrap').async_id_symbol;
4343
const { newUid, setInitTriggerId } = require('async_hooks');
4444
const nextTick = require('internal/process/next_tick').nextTick;
45+
const errors = require('internal/errors');
4546

4647
var cluster;
4748
var dns;
@@ -964,8 +965,9 @@ Socket.prototype.connect = function() {
964965
this._sockname = null;
965966
}
966967

967-
var pipe = !!options.path;
968-
debug('pipe', pipe, options.path);
968+
const path = options.path;
969+
var pipe = !!path;
970+
debug('pipe', pipe, path);
969971

970972
if (!this._handle) {
971973
this._handle = pipe ? new Pipe() : new TCP();
@@ -982,7 +984,13 @@ Socket.prototype.connect = function() {
982984
this.writable = true;
983985

984986
if (pipe) {
985-
internalConnect(this, options.path);
987+
if (typeof path !== 'string') {
988+
throw new errors.TypeError('ERR_INVALID_ARG_TYPE',
989+
'options.path',
990+
'string',
991+
path);
992+
}
993+
internalConnect(this, path);
986994
} else {
987995
lookupAndConnect(this, options);
988996
}

test/parallel/test-net-better-error-messages-path.js

+16-7
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,21 @@
22
const common = require('../common');
33
const net = require('net');
44
const assert = require('assert');
5-
const fp = '/tmp/fadagagsdfgsdf';
6-
const c = net.connect(fp);
75

8-
c.on('connect', common.mustNotCall());
6+
{
7+
const fp = '/tmp/fadagagsdfgsdf';
8+
const c = net.connect(fp);
99

10-
c.on('error', common.mustCall(function(e) {
11-
assert.strictEqual(e.code, 'ENOENT');
12-
assert.strictEqual(e.message, `connect ENOENT ${fp}`);
13-
}));
10+
c.on('connect', common.mustNotCall());
11+
c.on('error', common.expectsError({
12+
code: 'ENOENT',
13+
message: `connect ENOENT ${fp}`
14+
}));
15+
}
16+
17+
{
18+
assert.throws(
19+
() => net.createConnection({ path: {} }),
20+
common.expectsError({ code: 'ERR_INVALID_ARG_TYPE' })
21+
);
22+
}

0 commit comments

Comments
 (0)