Skip to content

dns: setServers throws inconsistent TypeError #20441

@davisjam

Description

@davisjam
  • Version
./out/Release/node  --version
v11.0.0-pre
  • Platform
(20:24:36) jamie@woody ~/Desktop/floss/node $ uname -a
Linux woody 4.13.0-38-generic #43~16.04.1-Ubuntu SMP Wed Mar 14 17:48:43 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
  • Subsystem

DNS

Problematic behavior

The dns.setServers API responds inconsistently on different invalid input.

Good behavior:

dns.setServers([' '])
TypeError [ERR_INVALID_IP_ADDRESS]: Invalid IP address:  
    at servers.forEach (dns.js:319:11)
    at Array.forEach (<anonymous>)
    at Resolver.setServers (dns.js:295:11)
    at Object.defaultResolverSetServers [as setServers] (dns.js:360:12)

Bad behavior:

> dns.setServers(['\n'])
TypeError: Cannot read property 'Symbol(Symbol.iterator)' of null
    at servers.forEach (dns.js:312:27)
    at Array.forEach (<anonymous>)
    at Resolver.setServers (dns.js:295:11)
    at Object.defaultResolverSetServers [as setServers] (dns.js:360:12)

A null pointer dereference is rather less nice than an error like ERR_INVALID_IP_ADDRESS.

I believe the problem is in dns.js:

...
  servers.forEach((serv) => {
    ...
    const match = serv.match(IPv6RE);
    // we have an IPv6 in brackets
    if (match) {
       ....
    }
    ...
    const [, s, p] = serv.match(addrSplitRE);

While the IPv6 code only accesses match if there was a match, the addrSplitRE code assumes there is a match, which can lead to this unsavory TypeError.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions