Skip to content

Commit

Permalink
dns: Use object without protoype for map
Browse files Browse the repository at this point in the history
Currently we use `{}` for the `lookup` function to find the relevant
resolver to the dns.resolve function. It is preferable to use an
object without a Object.prototype, currently for example you can do
something like:

```js
dns.resolve("google.com", "toString", console.log);
```

And get `[Object undefined]` logged and the callback would never be
called. This is unexpected and strange behavior in my opinion.
In addition, if someone adds a property to `Object.prototype` might
also create unexpected results.

This pull request fixes it, with it an appropriate error is thrown.

PR-URL: #5843
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Michaël Zasso <mic.besace@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
  • Loading branch information
benjamingr authored and evanlucas committed Mar 31, 2016
1 parent ec18131 commit 6a6112a
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/dns.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ function resolver(bindingName) {
}


var resolveMap = {};
var resolveMap = Object.create(null);
exports.resolve4 = resolveMap.A = resolver('queryA');
exports.resolve6 = resolveMap.AAAA = resolver('queryAaaa');
exports.resolveCname = resolveMap.CNAME = resolver('queryCname');
Expand Down
5 changes: 5 additions & 0 deletions test/parallel/test-c-ares.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ assert.throws(function() {
dns.resolve('www.google.com', 'HI');
}, /Unknown type/);

// Try calling resolve with an unsupported type that's an object key
assert.throws(function() {
dns.resolve('www.google.com', 'toString');
}, /Unknown type/);

// Windows doesn't usually have an entry for localhost 127.0.0.1 in
// C:\Windows\System32\drivers\etc\hosts
// so we disable this test on Windows.
Expand Down

0 comments on commit 6a6112a

Please sign in to comment.