From 7c73cd4c70513dd4fa1f7ea13e3bb3270696eabe Mon Sep 17 00:00:00 2001 From: cjihrig Date: Sat, 17 Mar 2018 12:43:50 -0400 Subject: [PATCH] net: emit error on invalid address family MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This commit adds proper error handling to net.connect() when a custom lookup() function returns an invalid address family. PR-URL: https://github.com/nodejs/node/pull/19415 Fixes: https://github.com/nodejs/node/issues/19407 Reviewed-By: Anna Henningsen Reviewed-By: Santiago Gimeno Reviewed-By: Michaƫl Zasso Reviewed-By: Joyee Cheung Reviewed-By: Khaidi Chu Reviewed-By: James M Snell --- doc/api/errors.md | 5 +++++ lib/internal/errors.js | 1 + lib/net.js | 7 +++++++ test/parallel/test-net-options-lookup.js | 11 ++++++++++- 4 files changed, 23 insertions(+), 1 deletion(-) diff --git a/doc/api/errors.md b/doc/api/errors.md index 5c38dde4130b2b..5e94535a3ea384 100644 --- a/doc/api/errors.md +++ b/doc/api/errors.md @@ -1042,6 +1042,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. + +### ERR_INVALID_ADDRESS_FAMILY + +The provided address family is not understood by the Node.js API. + ### ERR_INVALID_ARG_TYPE diff --git a/lib/internal/errors.js b/lib/internal/errors.js index d5260bd501faab..33a3185f797ca9 100644 --- a/lib/internal/errors.js +++ b/lib/internal/errors.js @@ -737,6 +737,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(); diff --git a/lib/net.js b/lib/net.js index 19f5b135891eb2..466531744eea0f 100644 --- a/lib/net.js +++ b/lib/net.js @@ -54,6 +54,7 @@ const { } = require('internal/async_hooks'); const errors = require('internal/errors'); const { + ERR_INVALID_ADDRESS_FAMILY, ERR_INVALID_ARG_TYPE, ERR_INVALID_FD_TYPE, ERR_INVALID_IP_ADDRESS, @@ -1117,6 +1118,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( diff --git a/test/parallel/test-net-options-lookup.js b/test/parallel/test-net-options-lookup.js index 0b4e18f12f04f0..007be66f4516bb 100644 --- a/test/parallel/test-net-options-lookup.js +++ b/test/parallel/test-net-options-lookup.js @@ -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' })); }