Skip to content

Commit 0200292

Browse files
panvaaduh95
authored andcommitted
dns: coerce -0 to +0 in lookup and resolver inputs
Signed-off-by: Filip Skokan <panva.ip@gmail.com> PR-URL: #63556 Reviewed-By: Jordan Harband <ljharb@gmail.com> Reviewed-By: Chemi Atlow <chemi@atlow.co.il>
1 parent 0ba5cc4 commit 0200292

4 files changed

Lines changed: 30 additions & 7 deletions

File tree

lib/dns.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,8 @@ function lookup(hostname, options, callback) {
158158
validateFunction(callback, 'callback');
159159

160160
validateOneOf(options, 'family', validFamilies);
161-
family = options;
161+
// Coerce -0 to +0.
162+
family = options + 0;
162163
} else if (options !== undefined && typeof options !== 'object') {
163164
validateFunction(arguments.length === 2 ? options : callback, 'callback');
164165
throw new ERR_INVALID_ARG_TYPE('options', ['integer', 'object'], options);
@@ -180,7 +181,8 @@ function lookup(hostname, options, callback) {
180181
break;
181182
default:
182183
validateOneOf(options.family, 'options.family', validFamilies);
183-
family = options.family;
184+
// Coerce -0 to +0.
185+
family = options.family + 0;
184186
break;
185187
}
186188
}
@@ -280,7 +282,8 @@ function lookupService(address, port, callback) {
280282

281283
validateFunction(callback, 'callback');
282284

283-
port = +port;
285+
// Coerce -0 to +0.
286+
port = +port + 0;
284287

285288
const req = new GetNameInfoReqWrap();
286289
req.callback = callback;

lib/internal/dns/promises.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,8 @@ function lookup(hostname, options) {
207207

208208
if (typeof options === 'number') {
209209
validateOneOf(options, 'family', validFamilies);
210-
family = options;
210+
// Coerce -0 to +0.
211+
family = options + 0;
211212
} else if (options !== undefined && typeof options !== 'object') {
212213
throw new ERR_INVALID_ARG_TYPE('options', ['integer', 'object'], options);
213214
} else {
@@ -218,7 +219,8 @@ function lookup(hostname, options) {
218219
}
219220
if (options?.family != null) {
220221
validateOneOf(options.family, 'options.family', validFamilies);
221-
family = options.family;
222+
// Coerce -0 to +0.
223+
family = options.family + 0;
222224
}
223225
if (options?.all != null) {
224226
validateBoolean(options.all, 'options.all');

lib/internal/dns/utils.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,18 @@ const {
4545
} = require('internal/v8/startup_snapshot');
4646

4747
function validateTimeout(options) {
48-
const { timeout = -1 } = { ...options };
48+
let { timeout = -1 } = { ...options };
4949
validateInt32(timeout, 'options.timeout', -1);
50+
// Coerce -0 to +0.
51+
timeout += 0;
5052
return timeout;
5153
}
5254

5355
function validateMaxTimeout(options) {
54-
const { maxTimeout = 0 } = { ...options };
56+
let { maxTimeout = 0 } = { ...options };
5557
validateUint32(maxTimeout, 'options.maxTimeout');
58+
// Coerce -0 to +0.
59+
maxTimeout += 0;
5660
return maxTimeout;
5761
}
5862

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
5+
const dns = require('dns');
6+
7+
dns.lookup('localhost', -0, common.mustCall());
8+
dns.lookup('localhost', { family: -0 }, common.mustCall());
9+
dns.lookupService('127.0.0.1', -0, common.mustCall());
10+
11+
new dns.Resolver({ timeout: -0 });
12+
new dns.Resolver({ maxTimeout: -0 });
13+
14+
dns.promises.lookup('localhost', { family: -0 }).then(common.mustCall());

0 commit comments

Comments
 (0)