Skip to content

Commit a37beba

Browse files
cjihrigBridgeAR
authored andcommitted
net: emit error on invalid address family
This commit adds proper error handling to net.connect() when a custom lookup() function returns an invalid address family. PR-URL: nodejs#19415 Fixes: nodejs#19407 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Santiago Gimeno <santiago.gimeno@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Khaidi Chu <i@2333.moe> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 3b4f4c4 commit a37beba

4 files changed

Lines changed: 25 additions & 1 deletion

File tree

doc/api/errors.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -973,6 +973,11 @@ The `inspector` module is not available for use.
973973
While using the `inspector` module, an attempt was made to use the inspector
974974
before it was connected.
975975

976+
<a id="ERR_INVALID_ADDRESS_FAMILY"></a>
977+
### ERR_INVALID_ADDRESS_FAMILY
978+
979+
The provided address family is not understood by the Node.js API.
980+
976981
<a id="ERR_INVALID_ARG_TYPE"></a>
977982
### ERR_INVALID_ARG_TYPE
978983

lib/internal/errors.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,7 @@ E('ERR_INSPECTOR_ALREADY_CONNECTED',
623623
E('ERR_INSPECTOR_CLOSED', 'Session was closed', Error);
624624
E('ERR_INSPECTOR_NOT_AVAILABLE', 'Inspector is not available', Error);
625625
E('ERR_INSPECTOR_NOT_CONNECTED', 'Session is not connected', Error);
626+
E('ERR_INVALID_ADDRESS_FAMILY', 'Invalid address family: %s', RangeError);
626627
E('ERR_INVALID_ARG_TYPE', invalidArgType, TypeError);
627628
E('ERR_INVALID_ARG_VALUE', (name, value, reason = 'is invalid') => {
628629
const util = lazyUtil();

lib/net.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ const { async_id_symbol } = process.binding('async_wrap');
5050
const { newUid, defaultTriggerAsyncIdScope } = require('internal/async_hooks');
5151
const { nextTick } = require('internal/process/next_tick');
5252
const errors = require('internal/errors');
53+
const {
54+
ERR_INVALID_ADDRESS_FAMILY
55+
} = errors.codes;
5356
const dns = require('dns');
5457

5558
const kLastWriteQueueSize = Symbol('lastWriteQueueSize');
@@ -1093,6 +1096,12 @@ function lookupAndConnect(self, options) {
10931096
err.port = options.port;
10941097
err.message = err.message + ' ' + options.host + ':' + options.port;
10951098
process.nextTick(connectErrorNT, self, err);
1099+
} else if (addressType !== 4 && addressType !== 6) {
1100+
err = new ERR_INVALID_ADDRESS_FAMILY(addressType);
1101+
err.host = options.host;
1102+
err.port = options.port;
1103+
err.message = err.message + ' ' + options.host + ':' + options.port;
1104+
process.nextTick(connectErrorNT, self, err);
10961105
} else {
10971106
self._unrefTimer();
10981107
defaultTriggerAsyncIdScope(

test/parallel/test-net-options-lookup.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,14 @@ function connectDoesNotThrow(input) {
2929
lookup: input
3030
};
3131

32-
net.connect(opts);
32+
return net.connect(opts);
33+
}
34+
35+
{
36+
// Verify that an error is emitted when an invalid address family is returned.
37+
const s = connectDoesNotThrow((host, options, cb) => {
38+
cb(null, '127.0.0.1', 100);
39+
});
40+
41+
s.on('error', common.expectsError({ code: 'ERR_INVALID_ADDRESS_FAMILY' }));
3342
}

0 commit comments

Comments
 (0)