Skip to content

Commit c9c387f

Browse files
benjamingrjasnell
authored andcommitted
dns: Use object without protoype for map
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>
1 parent 4985c34 commit c9c387f

File tree

2 files changed

+6
-1
lines changed

2 files changed

+6
-1
lines changed

lib/dns.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ function resolver(bindingName) {
243243
}
244244

245245

246-
var resolveMap = {};
246+
var resolveMap = Object.create(null);
247247
exports.resolve4 = resolveMap.A = resolver('queryA');
248248
exports.resolve6 = resolveMap.AAAA = resolver('queryAaaa');
249249
exports.resolveCname = resolveMap.CNAME = resolver('queryCname');

test/parallel/test-c-ares.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ assert.throws(function() {
2727
dns.resolve('www.google.com', 'HI');
2828
}, /Unknown type/);
2929

30+
// Try calling resolve with an unsupported type that's an object key
31+
assert.throws(function() {
32+
dns.resolve('www.google.com', 'toString');
33+
}, /Unknown type/);
34+
3035
// Windows doesn't usually have an entry for localhost 127.0.0.1 in
3136
// C:\Windows\System32\drivers\etc\hosts
3237
// so we disable this test on Windows.

0 commit comments

Comments
 (0)