Skip to content

Commit

Permalink
Merge #589
Browse files Browse the repository at this point in the history
589: Handle errors thrown when converting to utf-8 r=D4nte a=D4nte

`bytesToUtf8` replaced a previously used library. The previous library did not throw when failing to decode whereas `bytesToUtf8` does. Need to handle those errors.

Co-authored-by: Franck Royer <franck@status.im>
  • Loading branch information
status-bors-ng[bot] and D4nte authored Mar 2, 2022
2 parents 6b0b71f + 067ebeb commit acdd95b
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 30 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 1 addition & 5 deletions examples/web-chat/src/Message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,7 @@ export class Message {
return new Message(chatMsg, wakuMsg.timestamp);
}
} catch (e) {
console.error(
"Failed to decode chat message",
wakuMsg.payloadAsUtf8,
e
);
console.error("Failed to decode chat message", e);
}
}
return;
Expand Down
53 changes: 30 additions & 23 deletions src/lib/discovery/dns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
8 changes: 8 additions & 0 deletions src/lib/discovery/dns_over_https.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }],
Expand Down
9 changes: 8 additions & 1 deletion src/lib/enr/enr.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -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);

Expand Down
7 changes: 6 additions & 1 deletion src/lib/waku_message/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit acdd95b

Please sign in to comment.