From d4cfe2d8ac90bc407062e9835939cac2a356df86 Mon Sep 17 00:00:00 2001 From: Filip Skokan Date: Mon, 25 May 2026 10:23:24 +0200 Subject: [PATCH] dns: coerce -0 to +0 in lookup and resolver inputs Signed-off-by: Filip Skokan PR-URL: https://github.com/nodejs/node/pull/63556 Reviewed-By: Jordan Harband Reviewed-By: Chemi Atlow --- lib/dns.js | 9 ++++++--- lib/internal/dns/promises.js | 6 ++++-- lib/internal/dns/utils.js | 8 ++++++-- test/parallel/test-dns-negative-zero.js | 14 ++++++++++++++ 4 files changed, 30 insertions(+), 7 deletions(-) create mode 100644 test/parallel/test-dns-negative-zero.js diff --git a/lib/dns.js b/lib/dns.js index 9dffad8bf40..715351d8d29 100644 --- a/lib/dns.js +++ b/lib/dns.js @@ -158,7 +158,8 @@ function lookup(hostname, options, callback) { validateFunction(callback, 'callback'); validateOneOf(options, 'family', validFamilies); - family = options; + // Coerce -0 to +0. + family = options + 0; } else if (options !== undefined && typeof options !== 'object') { validateFunction(arguments.length === 2 ? options : callback, 'callback'); throw new ERR_INVALID_ARG_TYPE('options', ['integer', 'object'], options); @@ -180,7 +181,8 @@ function lookup(hostname, options, callback) { break; default: validateOneOf(options.family, 'options.family', validFamilies); - family = options.family; + // Coerce -0 to +0. + family = options.family + 0; break; } } @@ -280,7 +282,8 @@ function lookupService(address, port, callback) { validateFunction(callback, 'callback'); - port = +port; + // Coerce -0 to +0. + port = +port + 0; const req = new GetNameInfoReqWrap(); req.callback = callback; diff --git a/lib/internal/dns/promises.js b/lib/internal/dns/promises.js index dcaae5346bb..899e8c6672d 100644 --- a/lib/internal/dns/promises.js +++ b/lib/internal/dns/promises.js @@ -207,7 +207,8 @@ function lookup(hostname, options) { if (typeof options === 'number') { validateOneOf(options, 'family', validFamilies); - family = options; + // Coerce -0 to +0. + family = options + 0; } else if (options !== undefined && typeof options !== 'object') { throw new ERR_INVALID_ARG_TYPE('options', ['integer', 'object'], options); } else { @@ -218,7 +219,8 @@ function lookup(hostname, options) { } if (options?.family != null) { validateOneOf(options.family, 'options.family', validFamilies); - family = options.family; + // Coerce -0 to +0. + family = options.family + 0; } if (options?.all != null) { validateBoolean(options.all, 'options.all'); diff --git a/lib/internal/dns/utils.js b/lib/internal/dns/utils.js index 32f8794d62a..9453b9e80f1 100644 --- a/lib/internal/dns/utils.js +++ b/lib/internal/dns/utils.js @@ -45,14 +45,18 @@ const { } = require('internal/v8/startup_snapshot'); function validateTimeout(options) { - const { timeout = -1 } = { ...options }; + let { timeout = -1 } = { ...options }; validateInt32(timeout, 'options.timeout', -1); + // Coerce -0 to +0. + timeout += 0; return timeout; } function validateMaxTimeout(options) { - const { maxTimeout = 0 } = { ...options }; + let { maxTimeout = 0 } = { ...options }; validateUint32(maxTimeout, 'options.maxTimeout'); + // Coerce -0 to +0. + maxTimeout += 0; return maxTimeout; } diff --git a/test/parallel/test-dns-negative-zero.js b/test/parallel/test-dns-negative-zero.js new file mode 100644 index 00000000000..2b07144bc06 --- /dev/null +++ b/test/parallel/test-dns-negative-zero.js @@ -0,0 +1,14 @@ +'use strict'; + +const common = require('../common'); + +const dns = require('dns'); + +dns.lookup('localhost', -0, common.mustCall()); +dns.lookup('localhost', { family: -0 }, common.mustCall()); +dns.lookupService('127.0.0.1', -0, common.mustCall()); + +new dns.Resolver({ timeout: -0 }); +new dns.Resolver({ maxTimeout: -0 }); + +dns.promises.lookup('localhost', { family: -0 }).then(common.mustCall());