dns: handle array holes in setServers()
This commit adds better handling of exceptional array formats passed to dns.setServers(). Prior to this commit, the input array was validated using map(), which preserves holes, allowing them to be passed to c-ares, crashing Node. This commit replaces map() with forEach(), which skips holes. Fixes: https://github.com/nodejs/node/issues/8538 PR-URL: https://github.com/nodejs/node/pull/8567 Reviewed-By: Ilkka Myller <ilkka.myller@nodefield.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
parent
d4bf5cac43
commit
b176d30a69
@ -286,25 +286,26 @@ exports.setServers = function(servers) {
|
|||||||
// cache the original servers because in the event of an error setting the
|
// cache the original servers because in the event of an error setting the
|
||||||
// servers cares won't have any servers available for resolution
|
// servers cares won't have any servers available for resolution
|
||||||
const orig = cares.getServers();
|
const orig = cares.getServers();
|
||||||
|
const newSet = [];
|
||||||
|
|
||||||
const newSet = servers.map((serv) => {
|
servers.forEach((serv) => {
|
||||||
var ipVersion = isIP(serv);
|
var ipVersion = isIP(serv);
|
||||||
if (ipVersion !== 0)
|
if (ipVersion !== 0)
|
||||||
return [ipVersion, serv];
|
return newSet.push([ipVersion, serv]);
|
||||||
|
|
||||||
const match = serv.match(/\[(.*)\](:\d+)?/);
|
const match = serv.match(/\[(.*)\](:\d+)?/);
|
||||||
// we have an IPv6 in brackets
|
// we have an IPv6 in brackets
|
||||||
if (match) {
|
if (match) {
|
||||||
ipVersion = isIP(match[1]);
|
ipVersion = isIP(match[1]);
|
||||||
if (ipVersion !== 0)
|
if (ipVersion !== 0)
|
||||||
return [ipVersion, match[1]];
|
return newSet.push([ipVersion, match[1]]);
|
||||||
}
|
}
|
||||||
|
|
||||||
const s = serv.split(/:\d+$/)[0];
|
const s = serv.split(/:\d+$/)[0];
|
||||||
ipVersion = isIP(s);
|
ipVersion = isIP(s);
|
||||||
|
|
||||||
if (ipVersion !== 0)
|
if (ipVersion !== 0)
|
||||||
return [ipVersion, s];
|
return newSet.push([ipVersion, s]);
|
||||||
|
|
||||||
throw new Error(`IP address is not properly formatted: ${serv}`);
|
throw new Error(`IP address is not properly formatted: ${serv}`);
|
||||||
});
|
});
|
||||||
|
@ -7,6 +7,33 @@ const dns = require('dns');
|
|||||||
var existing = dns.getServers();
|
var existing = dns.getServers();
|
||||||
assert(existing.length);
|
assert(existing.length);
|
||||||
|
|
||||||
|
// Verify that setServers() handles arrays with holes and other oddities
|
||||||
|
assert.doesNotThrow(() => {
|
||||||
|
const servers = [];
|
||||||
|
|
||||||
|
servers[0] = '127.0.0.1';
|
||||||
|
servers[2] = '0.0.0.0';
|
||||||
|
dns.setServers(servers);
|
||||||
|
});
|
||||||
|
|
||||||
|
assert.doesNotThrow(() => {
|
||||||
|
const servers = ['127.0.0.1', '192.168.1.1'];
|
||||||
|
|
||||||
|
servers[3] = '127.1.0.1';
|
||||||
|
servers[4] = '127.1.0.1';
|
||||||
|
servers[5] = '127.1.1.1';
|
||||||
|
|
||||||
|
Object.defineProperty(servers, 2, {
|
||||||
|
enumerable: true,
|
||||||
|
get: () => {
|
||||||
|
servers.length = 3;
|
||||||
|
return '0.0.0.0';
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
dns.setServers(servers);
|
||||||
|
});
|
||||||
|
|
||||||
function noop() {}
|
function noop() {}
|
||||||
|
|
||||||
var goog = [
|
var goog = [
|
||||||
|
Loading…
x
Reference in New Issue
Block a user