Skip to content

Commit 3adb7e9

Browse files
committed
Remove invalid pathname auto-completion and enforce RFC8484 dns query
parameter, and fix #95
1 parent e4fa035 commit 3adb7e9

File tree

3 files changed

+29
-4
lines changed

3 files changed

+29
-4
lines changed

client/doh.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,16 @@ const makeRequest = (url, query) => new Promise((resolve, reject) => {
3737
const index = url.indexOf('://');
3838
if (index === -1) url = `https://${url}`;
3939
const u = new URL(url);
40+
// The DNS query is included in a single variable named “dns” in the
41+
// query component of the request URI. The value of the “dns” variable
42+
// is the content of the DNS request message, encoded with base64url
43+
// [RFC4648](https://datatracker.ietf.org/doc/html/rfc8484#section-4.1).
44+
const searchParams = u.searchParams;
45+
searchParams.set('dns', query);
46+
u.search = searchParams.toString();
4047
const get = protocols[u.protocol];
4148
if (!get) throw new Error(`Unsupported protocol: ${u.protocol}, must be specified (http://, https:// or h2://)`);
42-
if (!u.pathname) url += '/dns-query?dns={query}';
43-
url = url.replace('{query}', query);
44-
const req = get(url, { headers: { accept: 'application/dns-message' } }, resolve);
49+
const req = get(u.toString(), { headers: { accept: 'application/dns-message' } }, resolve);
4550
if (req) req.on('error', reject);
4651
});
4752

example/client/doh.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@ const { DOHClient } = require('../..');
1818
// })('cdnjs.com', 'NS').then(console.log);
1919

2020
DOHClient({
21-
dns: 'h2://ada.openbld.net/dns-query?dns={query}',
21+
dns: 'https://1.0.0.1/dns-query',
2222
})('cdnjs.com', 'NS').then(console.log);

test/index.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,3 +457,23 @@ function get(url, options) {
457457
}
458458
});
459459
}
460+
461+
test('client/doh', async() => {
462+
const res = await DOHClient({
463+
dns: 'https://1.0.0.1/dns-query',
464+
})('cdnjs.com', 'NS');
465+
466+
// console.log(res);
467+
assert.equal(res.answers.length, 2);
468+
assert.equal(res.answers[0].name, 'cdnjs.com');
469+
assert.equal(res.answers[0].type, Packet.TYPE.NS);
470+
assert.equal(res.answers[0].class, Packet.CLASS.IN);
471+
assert.equal(res.answers[0].ns, 'ben.ns.cloudflare.com');
472+
assert.equal(res.answers[1].name, 'cdnjs.com');
473+
assert.equal(res.answers[1].type, Packet.TYPE.NS);
474+
assert.equal(res.answers[1].class, Packet.CLASS.IN);
475+
assert.equal(res.answers[1].ns, 'lara.ns.cloudflare.com');
476+
assert.equal(res.header.qr, 1);
477+
assert.equal(res.header.ancount, 2);
478+
assert.equal(res.header.rcode, 0);
479+
});

0 commit comments

Comments
 (0)