Skip to content

Commit 96204c3

Browse files
committed
net: do not manipulate potential user code
The error provided in this function could come from user code. Thus the error should not be manipulated in any way. The added properties do not seem to provide any actual value either as can not be part of the error. The `hostname` is already set on the error and adding the `host` property with the identical value does not seem right in this case. PR-URL: #26751 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Rich Trott <[email protected]>
1 parent bdea725 commit 96204c3

3 files changed

+13
-19
lines changed

lib/net.js

-6
Original file line numberDiff line numberDiff line change
@@ -971,12 +971,6 @@ function lookupAndConnect(self, options) {
971971
// net.createConnection() creates a net.Socket object and immediately
972972
// calls net.Socket.connect() on it (that's us). There are no event
973973
// listeners registered yet so defer the error event to the next tick.
974-
// TODO(BridgeAR): The error could either originate from user code or
975-
// by the C++ layer. The port is never the cause for the error as it is
976-
// not used in the lookup. We should probably just remove this.
977-
err.host = options.host;
978-
err.port = options.port;
979-
err.message = err.message + ' ' + options.host + ':' + options.port;
980974
process.nextTick(connectErrorNT, self, err);
981975
} else if (addressType !== 4 && addressType !== 6) {
982976
err = new ERR_INVALID_ADDRESS_FAMILY(addressType,

test/parallel/test-net-better-error-messages-port-hostname.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
const common = require('../common');
88
const net = require('net');
9-
const assert = require('assert');
109

1110
const { addresses } = require('../common/internet');
1211
const {
@@ -23,8 +22,9 @@ const c = net.createConnection({
2322

2423
c.on('connect', common.mustNotCall());
2524

26-
c.on('error', common.mustCall(function(e) {
27-
assert.strictEqual(e.code, mockedErrorCode);
28-
assert.strictEqual(e.port, 0);
29-
assert.strictEqual(e.hostname, addresses.INVALID_HOST);
25+
c.on('error', common.expectsError({
26+
code: mockedErrorCode,
27+
hostname: addresses.INVALID_HOST,
28+
port: undefined,
29+
host: undefined
3030
}));

test/parallel/test-net-connect-immediate-finish.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
// 'connect' and defer the handling until the 'connect' event is handled.
2727

2828
const common = require('../common');
29-
const assert = require('assert');
3029
const net = require('net');
3130

3231
const { addresses } = require('../common/internet');
@@ -42,13 +41,14 @@ const client = net.connect({
4241
lookup: common.mustCall(errorLookupMock())
4342
}, common.mustNotCall());
4443

45-
client.once('error', common.mustCall((err) => {
46-
assert(err);
47-
assert.strictEqual(err.code, err.errno);
48-
assert.strictEqual(err.code, mockedErrorCode);
49-
assert.strictEqual(err.host, err.hostname);
50-
assert.strictEqual(err.host, addresses.INVALID_HOST);
51-
assert.strictEqual(err.syscall, mockedSysCall);
44+
client.once('error', common.expectsError({
45+
code: mockedErrorCode,
46+
errno: mockedErrorCode,
47+
syscall: mockedSysCall,
48+
hostname: addresses.INVALID_HOST,
49+
message: 'getaddrinfo ENOTFOUND something.invalid',
50+
port: undefined,
51+
host: undefined
5252
}));
5353

5454
client.end();

0 commit comments

Comments
 (0)