From a163c21f43ac5dfdc10479002974465318116cb4 Mon Sep 17 00:00:00 2001 From: "S. M. Mohiuddin Khan Shiam" Date: Fri, 13 Jun 2025 03:48:20 +0600 Subject: [PATCH] dns: allow Resolver#setLocalAddress() to accept IPv6-only (or IPv4-only) addresses MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Title dns: allow `resolver.setLocalAddress()` to accept IPv6-only or IPv4-only addresses ## Description `ResolverBase.prototype.setLocalAddress()` previously **required** the first argument (`ipv4`) to be a non-empty string, throwing `ERR_INVALID_ARG_TYPE` whenever a user tried to set **only** an IPv6 local address: ```js const r = new (require('dns').Resolver)(); r.setLocalAddress(undefined, '::1'); // ❌ TypeError This broke documented API behaviour and prevented IPv6-only configurations. Changes lib/internal/dns/utils.js Imported ERR_MISSING_ARGS. Updated setLocalAddress() : If both ipv4 and ipv6 are undefined, throw ERR_MISSING_ARGS('ipv4', 'ipv6'). Validate each address only when provided. Preserve snapshot-state logic. Result setLocalAddress() now works correctly with any of: resolver.setLocalAddress('0.0.0.0'); // IPv4-only resolver.setLocalAddress(undefined, '::1'); // IPv6-only resolver.setLocalAddress('0.0.0.0', '::1'); // both Restores compliance with public documentation and unblocks IPv6-only deployments. --- lib/internal/dns/utils.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/internal/dns/utils.js b/lib/internal/dns/utils.js index 85918f0d43d5..57f371cc554a 100644 --- a/lib/internal/dns/utils.js +++ b/lib/internal/dns/utils.js @@ -16,6 +16,7 @@ const { ERR_DNS_SET_SERVERS_FAILED, ERR_INVALID_ARG_VALUE, ERR_INVALID_IP_ADDRESS, + ERR_MISSING_ARGS, }, } = require('internal/errors'); const { isIP } = require('internal/net'); @@ -162,10 +163,15 @@ class ResolverBase { } } - setLocalAddress(ipv4, ipv6) { - validateString(ipv4, 'ipv4'); + // Allow setting either IPv4 or IPv6 (or both). Throw if both are omitted. + if (ipv4 === undefined && ipv6 === undefined) { + throw new ERR_MISSING_ARGS('ipv4', 'ipv6'); + } + if (ipv4 !== undefined) { + validateString(ipv4, 'ipv4'); + } if (ipv6 !== undefined) { validateString(ipv6, 'ipv6'); }