Skip to content

Commit 3d6041c

Browse files
lazergaduh95
authored andcommitted
dns: validate address type in lookupService
Signed-off-by: Lazizbek Ergashev <lazerg2@gmail.com> PR-URL: #64878 Fixes: #64877 Reviewed-By: theanarkh <theratliter@gmail.com> Reviewed-By: Tim Perry <pimterry@gmail.com>
1 parent 93ead3a commit 3d6041c

3 files changed

Lines changed: 23 additions & 0 deletions

File tree

lib/dns.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ const {
8484
validateNumber,
8585
validateOneOf,
8686
validatePort,
87+
validateString,
8788
validateStringWithoutNullBytes,
8889
} = require('internal/validators');
8990

@@ -275,6 +276,8 @@ function lookupService(address, port, callback) {
275276
if (arguments.length !== 3)
276277
throw new ERR_MISSING_ARGS('address', 'port', 'callback');
277278

279+
validateString(address, 'address');
280+
278281
if (isIP(address) === 0)
279282
throw new ERR_INVALID_ARG_VALUE('address', address);
280283

lib/internal/dns/promises.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,8 @@ function lookupService(address, port) {
283283
if (arguments.length !== 2)
284284
throw new ERR_MISSING_ARGS('address', 'port');
285285

286+
validateString(address, 'address');
287+
286288
if (isIP(address) === 0)
287289
throw new ERR_INVALID_ARG_VALUE('address', address);
288290

test/parallel/test-dns.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,24 @@ dns.lookup('', {
344344
}, err);
345345
}
346346

347+
{
348+
const invalidAddress = Buffer.from('127.0.0.1');
349+
const err = {
350+
code: 'ERR_INVALID_ARG_TYPE',
351+
name: 'TypeError',
352+
message: 'The "address" argument must be of type string. ' +
353+
'Received an instance of Buffer'
354+
};
355+
356+
assert.throws(() => {
357+
dnsPromises.lookupService(invalidAddress, 0);
358+
}, err);
359+
360+
assert.throws(() => {
361+
dns.lookupService(invalidAddress, 0, common.mustNotCall());
362+
}, err);
363+
}
364+
347365
[null, undefined, 65538, 'test', NaN, Infinity, Symbol(), 0n, true, false, '', () => {}, {}].forEach((port) => {
348366
const err = {
349367
code: 'ERR_SOCKET_BAD_PORT',

0 commit comments

Comments
 (0)