Handle errors thrown when converting to utf-8

This commit is contained in:
Franck Royer 2022-03-01 16:51:21 +11:00
parent 35414eaffd
commit f73afc5244
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
5 changed files with 56 additions and 25 deletions

View File

@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
### Fixed
- Handle errors thrown by `bytesToUtf8`.
## [0.18.0] - 2022-02-24
### Changed

View File

@ -79,38 +79,45 @@ export class DnsNodeDiscovery {
subdomain: string,
context: SearchContext
): Promise<ENR | null> {
const entry = await this._getTXTRecord(subdomain, context);
context.visits[subdomain] = true;
let next: string;
let branches: string[];
const entryType = getEntryType(entry);
try {
switch (entryType) {
case ENRTree.ROOT_PREFIX:
next = ENRTree.parseAndVerifyRoot(entry, context.publicKey);
return await this._search(next, context);
case ENRTree.BRANCH_PREFIX:
branches = ENRTree.parseBranch(entry);
next = selectRandomPath(branches, context);
return await this._search(next, context);
case ENRTree.RECORD_PREFIX:
return ENR.decodeTxt(entry);
default:
return null;
const entry = await this._getTXTRecord(subdomain, context);
context.visits[subdomain] = true;
let next: string;
let branches: string[];
const entryType = getEntryType(entry);
try {
switch (entryType) {
case ENRTree.ROOT_PREFIX:
next = ENRTree.parseAndVerifyRoot(entry, context.publicKey);
return await this._search(next, context);
case ENRTree.BRANCH_PREFIX:
branches = ENRTree.parseBranch(entry);
next = selectRandomPath(branches, context);
return await this._search(next, context);
case ENRTree.RECORD_PREFIX:
return ENR.decodeTxt(entry);
default:
return null;
}
} catch (error) {
dbg(
`Failed to search DNS tree ${entryType} at subdomain ${subdomain}: ${error}`
);
return null;
}
} catch (error) {
dbg(
`Failed to search DNS tree ${entryType} at subdomain ${subdomain}: ${error}`
);
dbg(`Failed to retrieve TXT record at subdomain ${subdomain}: ${error}`);
return null;
}
}
/**
* Retrieves the TXT record stored at a location from either
* this DNS tree cache or via DNS query
* this DNS tree cache or via DNS query.
*
* @throws if the TXT Record contains non-UTF-8 values.
*/
private async _getTXTRecord(
subdomain: string,

View File

@ -31,6 +31,14 @@ export class DnsOverHttps implements DnsClient {
public endpoints: Endpoints = [cloudflare, google, opendns]
) {}
/**
* Resolves a TXT record
*
* @param domain The domain name
*
* @throws if the result is provided in byte form which cannot be decoded
* to UTF-8
*/
async resolveTXT(domain: string): Promise<string[]> {
const response = await query({
questions: [{ type: "TXT", name: domain }],

View File

@ -1,4 +1,5 @@
import * as RLP from "@ethersproject/rlp";
import debug from "debug";
import { Multiaddr, protocols } from "multiaddr";
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore: No types available
@ -21,6 +22,8 @@ import { decodeMultiaddrs, encodeMultiaddrs } from "./multiaddrs_codec";
import { ENRKey, ENRValue, NodeId, SequenceNumber } from "./types";
import * as v4 from "./v4";
const dbg = debug("waku:enr");
export class ENR extends Map<ENRKey, ENRValue> {
public static readonly RECORD_PREFIX = "enr:";
public seq: SequenceNumber;
@ -78,7 +81,11 @@ export class ENR extends Map<ENRKey, ENRValue> {
}
const obj: Record<ENRKey, ENRValue> = {};
for (let i = 0; i < kvs.length; i += 2) {
obj[bytesToUtf8(kvs[i])] = kvs[i + 1];
try {
obj[bytesToUtf8(kvs[i])] = kvs[i + 1];
} catch (e) {
dbg("Failed to decode ENR key to UTF-8, skipping it", kvs[i], e);
}
}
const enr = new ENR(obj, BigInt("0x" + bytesToHex(seq)), signature);

View File

@ -256,7 +256,12 @@ export class WakuMessage {
return "";
}
return bytesToUtf8(this.proto.payload);
try {
return bytesToUtf8(this.proto.payload);
} catch (e) {
dbg("Could not decode byte as UTF-8", e);
return "";
}
}
get payload(): Uint8Array | undefined {