mirror of https://github.com/nodejs/node.git
dns: type check for dns.setServers argument.
Added type check for argument for dns.setServers and dnsPromises.setServers. PR-URL: https://github.com/nodejs/node/pull/21944 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Jon Moss <me@jonathanmoss.me> Reviewed-By: Yuta Hiroto <hello@hiroppy.me> Reviewed-By: Tobias Nießen <tniessen@tnie.de> Reviewed-By: Сковорода Никита Андреевич <chalkerx@gmail.com>pull/22109/merge
parent
fdbc668ea3
commit
4e1c4e8193
|
@ -12,6 +12,7 @@ const IPv6RE = /^\[([^[\]]*)\]/;
|
|||
const addrSplitRE = /(^.+?)(?::(\d+))?$/;
|
||||
const {
|
||||
ERR_DNS_SET_SERVERS_FAILED,
|
||||
ERR_INVALID_ARG_TYPE,
|
||||
ERR_INVALID_IP_ADDRESS,
|
||||
ERR_INVALID_OPT_VALUE
|
||||
} = errors.codes;
|
||||
|
@ -37,13 +38,20 @@ class Resolver {
|
|||
}
|
||||
|
||||
setServers(servers) {
|
||||
if (!Array.isArray(servers)) {
|
||||
throw new ERR_INVALID_ARG_TYPE('servers', 'Array', servers);
|
||||
}
|
||||
|
||||
// Cache the original servers because in the event of an error while
|
||||
// setting the servers, c-ares won't have any servers available for
|
||||
// resolution.
|
||||
const orig = this._handle.getServers();
|
||||
const newSet = [];
|
||||
|
||||
servers.forEach((serv) => {
|
||||
servers.forEach((serv, index) => {
|
||||
if (typeof serv !== 'string') {
|
||||
throw new ERR_INVALID_ARG_TYPE(`servers[${index}]`, 'string', serv);
|
||||
}
|
||||
var ipVersion = isIP(serv);
|
||||
|
||||
if (ipVersion !== 0)
|
||||
|
|
|
@ -0,0 +1,87 @@
|
|||
'use strict';
|
||||
require('../common');
|
||||
const { addresses } = require('../common/internet');
|
||||
const assert = require('assert');
|
||||
const dns = require('dns');
|
||||
const resolver = new dns.promises.Resolver();
|
||||
const dnsPromises = dns.promises;
|
||||
const promiseResolver = new dns.promises.Resolver();
|
||||
|
||||
{
|
||||
[
|
||||
null,
|
||||
undefined,
|
||||
Number(addresses.DNS4_SERVER),
|
||||
addresses.DNS4_SERVER,
|
||||
{
|
||||
address: addresses.DNS4_SERVER
|
||||
}
|
||||
].forEach((val) => {
|
||||
const errObj = {
|
||||
code: 'ERR_INVALID_ARG_TYPE',
|
||||
name: 'TypeError [ERR_INVALID_ARG_TYPE]',
|
||||
message: 'The "servers" argument must be of type Array. Received type ' +
|
||||
typeof val
|
||||
};
|
||||
assert.throws(
|
||||
() => {
|
||||
dns.setServers(val);
|
||||
}, errObj
|
||||
);
|
||||
assert.throws(
|
||||
() => {
|
||||
resolver.setServers(val);
|
||||
}, errObj
|
||||
);
|
||||
assert.throws(
|
||||
() => {
|
||||
dnsPromises.setServers(val);
|
||||
}, errObj
|
||||
);
|
||||
assert.throws(
|
||||
() => {
|
||||
promiseResolver.setServers(val);
|
||||
}, errObj
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
{
|
||||
[
|
||||
[null],
|
||||
[undefined],
|
||||
[Number(addresses.DNS4_SERVER)],
|
||||
[
|
||||
{
|
||||
address: addresses.DNS4_SERVER
|
||||
}
|
||||
]
|
||||
].forEach((val) => {
|
||||
const errObj = {
|
||||
code: 'ERR_INVALID_ARG_TYPE',
|
||||
name: 'TypeError [ERR_INVALID_ARG_TYPE]',
|
||||
message: 'The "servers[0]" argument must be of type string. ' +
|
||||
`Received type ${typeof val[0]}`
|
||||
};
|
||||
assert.throws(
|
||||
() => {
|
||||
dns.setServers(val);
|
||||
}, errObj
|
||||
);
|
||||
assert.throws(
|
||||
() => {
|
||||
resolver.setServers(val);
|
||||
}, errObj
|
||||
);
|
||||
assert.throws(
|
||||
() => {
|
||||
dnsPromises.setServers(val);
|
||||
}, errObj
|
||||
);
|
||||
assert.throws(
|
||||
() => {
|
||||
promiseResolver.setServers(val);
|
||||
}, errObj
|
||||
);
|
||||
});
|
||||
}
|
Loading…
Reference in New Issue