Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dns: type check for dns.setServers argument. #21944

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion lib/internal/dns/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
Expand Down
87 changes: 87 additions & 0 deletions test/parallel/test-dns-setservers-type-check.js
Original file line number Diff line number Diff line change
@@ -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
);
});
}