Skip to content

Commit

Permalink
dns: type check for dns.setServers argument.
Browse files Browse the repository at this point in the history
Added type check for argument for dns.setServers and dnsPromises.setServers.

PR-URL: #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>
  • Loading branch information
Masashi Hirano authored and trivikr committed Aug 3, 2018
1 parent fdbc668 commit 4e1c4e8
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 1 deletion.
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
);
});
}

0 comments on commit 4e1c4e8

Please sign in to comment.