Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix RootServer resolution of SYNTH and GLUE records #444

Merged
merged 4 commits into from
Jul 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions lib/dns/resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,16 +183,16 @@ class Resource extends Struct {
switch (record.type) {
case hsTypes.GLUE4:
case hsTypes.GLUE6:
if (!util.isSubdomain(name, record.ns))
continue;
pinheadmz marked this conversation as resolved.
Show resolved Hide resolved
break;
case hsTypes.SYNTH4:
case hsTypes.SYNTH6:
break;
default:
continue;
}

if (!util.isSubdomain(name, record.ns))
continue;

additional.push(record.toGlue(record.ns, this.ttl));
}

Expand Down Expand Up @@ -237,7 +237,7 @@ class Resource extends Struct {
continue;

set.add(rr.data.ns);
}
}``

zone.push(rr);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/dns/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ class RootServer extends DNSServer {
// Handle reverse pointers.
if (tld === '_synth' && labels.length === 2 && name[0] === '_') {
const hash = util.label(name, labels, -2);
const ip = base32.decodeHex(hash.substring(1));
const ip = IP.map(base32.decodeHex(hash.substring(1)));
const res = new Message();
const rr = new Record();

Expand Down Expand Up @@ -397,7 +397,7 @@ class RootServer extends DNSServer {

const res = await this.response(req, rinfo);

if (!util.equal(tld, '_synth'))
if (!util.equal(tld, '_synth.'))
this.cache.set(name, type, res);

return res;
Expand Down
110 changes: 110 additions & 0 deletions test/ns-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
'use strict';

const assert = require('bsert');
const {wire} = require('bns');
const {RootServer} = require('../lib/dns/server');

describe('RootServer', function() {
const ns = new RootServer({
port: 25349 // regtest
});

before(async () => {
await ns.open();
});

after(async () => {
await ns.close();
});

it('should resolve a SYNTH4', async () => {
const name = '_fs0000g._synth.';
const req = {
question: [{name}]
};

const res = await ns.resolve(req);
const answer = res.answer;
const rec = answer[0];

assert.strictEqual(rec.name, name);
assert.strictEqual(rec.type, wire.types.A);
assert.strictEqual(rec.data.address, '127.0.0.2');
});

it('should resolve a SYNTH6', async () => {
const name = '_00000000000000000000000008._synth.';
const req = {
question: [{name}]
};

const res = await ns.resolve(req);
const answer = res.answer;
const rec = answer[0];

assert.strictEqual(rec.name, name);
assert.strictEqual(rec.type, wire.types.AAAA);
assert.strictEqual(rec.data.address, '::2');
});

it('should not cache synth record', async () => {
// Start fresh
const cache = ns.cache.cache;
cache.reset();
assert.strictEqual(cache.size, 0);

// Query a record the RootResolver knows even without a database
let name = '.';
let req = {
question: [
{
name,
type: wire.types.NS
}
]
};
let res = await ns.resolve(req);

// Added to cache
assert.strictEqual(cache.size, 1);

// Query a SYNTH6 record
name = '_00000000000000000000000008._synth.';
req = {
question: [
{name}
]
};
res = await ns.resolve(req);
let answer = res.answer;
let rec = answer[0];

assert.strictEqual(rec.name, name);
assert.strictEqual(rec.type, wire.types.AAAA);
assert.strictEqual(rec.data.address, '::2');

// Nothing was added to the cache
assert.strictEqual(cache.size, 1);

// Handshake RootServer cache is keyed exclusively by the TLD when the
pinheadmz marked this conversation as resolved.
Show resolved Hide resolved
// name has more than one label (normally indicating a referral).
// If the cache doesn't handle the pseudo-TLD `_synth.` correctly,
// This SYNTH4 request would return the result of the SYNTH6
// record from the last request.
name = '_fs0000g._synth.';
req = {
question: [{name}]
};

res = await ns.resolve(req);
answer = res.answer;
rec = answer[0];

assert.strictEqual(rec.name, name);
assert.strictEqual(rec.type, wire.types.A);
assert.strictEqual(rec.data.address, '127.0.0.2');

// Nothing was added to the cache
assert.strictEqual(cache.size, 1);
});
});
28 changes: 22 additions & 6 deletions test/resource-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,14 @@ describe('Resource', function() {
ns: 'ns2.hns.',
address: '127.0.0.1'
},
{
type: 'GLUE4',
ns: 'ns3.some-other-domain.',
address: '10.20.30.40'
},
{
type: 'GLUE6',
ns: 'ns2.hns.',
ns: 'ns4.hns.',
address: '::1'
},
{
Expand Down Expand Up @@ -58,16 +63,21 @@ describe('Resource', function() {
const msg = res.toDNS('hns.', types.MX);

assert(msg.answer.length === 0);
assert(msg.authority.length === 6);
assert(msg.additional.length === 2);
assert(msg.authority.length === 8);
// Notice that the glue for `ns3.some-other-domain.` is omitted
assert(msg.additional.length === 4);

const [ns1, ns2, synth4, synth6, ds, rrsig] = msg.authority;
const [glue4, glue6] = msg.additional;
const [ns1, ns2, ns3, ns4, synth4, synth6, ds, rrsig] = msg.authority;
const [glue4, glue6, synthA, synthAAAA] = msg.additional;

assert.strictEqual(ns1.type, types.NS);
assert.strictEqual(ns1.name, 'hns.');
assert.strictEqual(ns2.type, types.NS);
assert.strictEqual(ns2.name, 'hns.');
assert.strictEqual(ns3.type, types.NS);
assert.strictEqual(ns3.name, 'hns.');
assert.strictEqual(ns4.type, types.NS);
assert.strictEqual(ns4.name, 'hns.');
assert.strictEqual(synth4.type, types.NS);
assert.strictEqual(synth4.name, 'hns.');
assert.strictEqual(synth6.type, types.NS);
Expand All @@ -79,7 +89,11 @@ describe('Resource', function() {
assert.strictEqual(glue4.type, types.A);
assert.strictEqual(glue4.name, 'ns2.hns.');
assert.strictEqual(glue6.type, types.AAAA);
assert.strictEqual(glue6.name, 'ns2.hns.');
assert.strictEqual(glue6.name, 'ns4.hns.');
assert.strictEqual(synthA.type, types.A);
assert.strictEqual(synthA.name, '_fs0000g._synth.');
assert.strictEqual(synthAAAA.type, types.AAAA);
assert.strictEqual(synthAAAA.name, '_00000000000000000000000008._synth.');

assert.strictEqual(ns1.data.ns, 'ns1.hns.');
assert.strictEqual(ns2.data.ns, 'ns2.hns.');
Expand All @@ -88,6 +102,8 @@ describe('Resource', function() {
assert.bufferEqual(ds.data.digest, json.records[0].digest);
assert.strictEqual(glue4.data.address, '127.0.0.1');
assert.strictEqual(glue6.data.address, '::1');
assert.strictEqual(synthA.data.address, '127.0.0.2');
assert.strictEqual(synthAAAA.data.address, '::2');
});

it('should synthesize an answer', () => {
Expand Down