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

Add error handling to Ethereum lookups #10

Merged
merged 2 commits into from
Jul 15, 2021
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: 8 additions & 0 deletions lib/ethereum.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ class Ethereum {

async getResolverFromRegistry(name, registry) {
const resolverAddr = await registry.resolver(this.namehash(name));
if (resolverAddr === '0x0000000000000000000000000000000000000000') {
return null;
}

return new ethers.Contract(resolverAddr, RESOLVER_ABI, this.infura);
}

Expand Down Expand Up @@ -101,6 +105,10 @@ class Ethereum {
}

async resolveDnsWithResolver(name, type, node, resolver) {
if (!resolver) {
return null;
}

let record = this.cache.getRecord(name, type, resolver.address);
if (!record) {
record = await resolver.dnsRecord(
Expand Down
97 changes: 49 additions & 48 deletions lib/handover.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,18 @@ class Plugin {
case 'eth.':
if (labels.length < 2) {
return this.sendSOA();
} else {
data = await this.ethereum.resolveDnsFromEns(name, type);
if (!data || data.length === 0)
return this.sendSOA();
}

return this.sendData(data, type);
try {
data = await this.ethereum.resolveDnsFromEns(name, type);
if (data && data.length > 0)
return this.sendData(data, type);
} catch (e) {
this.logger.warning('Resolution failed for name: %s', name);
this.logger.debug(e.stack);
}

return this.sendSOA();
case '_eth.':
return this.sendSOA();
}
Expand All @@ -67,76 +72,72 @@ class Plugin {
if (!res.authority.length)
return res;

let hasEnsReferral = false;
// Check NS records for referals to TLDs `.eth` and `._eth`
let hip5Referral = false;
// Check NS records for HIP-5 referrals
for (const rr of res.authority) {
if (rr.type !== wire.types.NS)
continue;

// Look up the ENS resolver specified in the NS record
// and query it for the user's original request
if (rr.data.ns.slice(-5) === '.eth.') {
hasEnsReferral = true;

// If the recursive is being minimal, don't look up the name.
// Send the SOA back and get the full query from the recursive .
if (labels.length < 2) {
return this.sendSOA();
}

this.logger.debug(
'Intercepted referral to .eth: %s %s -> NS: %s',
name,
wire.typesByVal[type],
rr.data.ns
);
data = await this.ethereum.resolveDnsFromEns(
name,
type,
rr.data.ns
);
}
const ending = util.label(rr.data.ns, util.split(rr.data.ns), -1);

// Look up an alternate (forked) ENS contract by the Ethereum
// address specified in the NS record, and query it for
// the user's original request
if (rr.data.ns.slice(-6) === '._eth.') {
hasEnsReferral = true;
// Look for any supported HIP-5 extension in the NS record
// and query it for the user's original request.
if (ending === '_eth' || ending === 'eth') {
hip5Referral = true;

// If the recursive is being minimal, don't look up the name.
// Send the SOA back and get the full query from the recursive .
if (labels.length < 2) {
return this.sendSOA();
}
this.logger.debug(
'Intercepted referral to ._eth: %s %s -> %s NS: %s',
'Intercepted referral to .%s: %s %s -> %s NS: %s',
ending,
name,
wire.typesByVal[type],
rr.name,
rr.data.ns
);
data = await this.ethereum.resolveDnsFromAbstractEns(
name,
type,
rr.data.ns
);

try {
switch (ending) {
case 'eth':
data = await this.ethereum.resolveDnsFromEns(
name,
type,
rr.data.ns
);
break;
case '_eth':
// Look up an alternate (forked) ENS contract by the Ethereum
// address specified in the NS record
data = await this.ethereum.resolveDnsFromAbstractEns(
name,
type,
rr.data.ns
);
break;
}
} catch (e) {
this.logger.warning('Resolution failed for name: %s', name);
this.logger.debug(e.stack);
}
}
}

// If the Ethereum stuff came up empty, return the
// HNS root server response unmodified.
if (!data || data.length === 0) {
// never send referrals that end with .eth or ._eth
// since recursive may cache these referrals causing a servfail
// for future lookups
if (hasEnsReferral) {
// Never send HIP-5 type referrals to recursive resolvers
// since they aren't real delegations and it could end up
// poisoning their cache.
if (hip5Referral) {
return this.sendSOA();
}

// return the HNS root server response unmodified.
return res;
}

// If we did get an answer from Ethereum, mark the response
// If we did get an answer, mark the response
// as authoritative and send the new answer.
this.logger.debug('Returning answers from alternate naming system');
return this.sendData(data, type);
Expand Down