Skip to content

Commit

Permalink
net: emit error on invalid address family
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
cjihrig authored and BridgeAR committed May 1, 2018
1 parent 3b4f4c4 commit a37beba
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 1 deletion.
5 changes: 5 additions & 0 deletions doc/api/errors.md
Expand Up @@ -973,6 +973,11 @@ The `inspector` module is not available for use.
While using the `inspector` module, an attempt was made to use the inspector
before it was connected.

<a id="ERR_INVALID_ADDRESS_FAMILY"></a>
### ERR_INVALID_ADDRESS_FAMILY

The provided address family is not understood by the Node.js API.

<a id="ERR_INVALID_ARG_TYPE"></a>
### ERR_INVALID_ARG_TYPE

Expand Down
1 change: 1 addition & 0 deletions lib/internal/errors.js
Expand Up @@ -623,6 +623,7 @@ E('ERR_INSPECTOR_ALREADY_CONNECTED',
E('ERR_INSPECTOR_CLOSED', 'Session was closed', Error);
E('ERR_INSPECTOR_NOT_AVAILABLE', 'Inspector is not available', Error);
E('ERR_INSPECTOR_NOT_CONNECTED', 'Session is not connected', Error);
E('ERR_INVALID_ADDRESS_FAMILY', 'Invalid address family: %s', RangeError);
E('ERR_INVALID_ARG_TYPE', invalidArgType, TypeError);
E('ERR_INVALID_ARG_VALUE', (name, value, reason = 'is invalid') => {
const util = lazyUtil();
Expand Down
9 changes: 9 additions & 0 deletions lib/net.js
Expand Up @@ -50,6 +50,9 @@ const { async_id_symbol } = process.binding('async_wrap');
const { newUid, defaultTriggerAsyncIdScope } = require('internal/async_hooks');
const { nextTick } = require('internal/process/next_tick');
const errors = require('internal/errors');
const {
ERR_INVALID_ADDRESS_FAMILY
} = errors.codes;
const dns = require('dns');

const kLastWriteQueueSize = Symbol('lastWriteQueueSize');
Expand Down Expand Up @@ -1093,6 +1096,12 @@ function lookupAndConnect(self, options) {
err.port = options.port;
err.message = err.message + ' ' + options.host + ':' + options.port;
process.nextTick(connectErrorNT, self, err);
} else if (addressType !== 4 && addressType !== 6) {
err = new ERR_INVALID_ADDRESS_FAMILY(addressType);
err.host = options.host;
err.port = options.port;
err.message = err.message + ' ' + options.host + ':' + options.port;
process.nextTick(connectErrorNT, self, err);
} else {
self._unrefTimer();
defaultTriggerAsyncIdScope(
Expand Down
11 changes: 10 additions & 1 deletion test/parallel/test-net-options-lookup.js
Expand Up @@ -29,5 +29,14 @@ function connectDoesNotThrow(input) {
lookup: input
};

net.connect(opts);
return net.connect(opts);
}

{
// Verify that an error is emitted when an invalid address family is returned.
const s = connectDoesNotThrow((host, options, cb) => {
cb(null, '127.0.0.1', 100);
});

s.on('error', common.expectsError({ code: 'ERR_INVALID_ADDRESS_FAMILY' }));
}

0 comments on commit a37beba

Please sign in to comment.