Skip to content

Commit

Permalink
More robust message checking in socket providers (ethers-io#4051).
Browse files Browse the repository at this point in the history
  • Loading branch information
ricmoo authored and Woodpile37 committed Jan 14, 2024
1 parent 2fc9a27 commit 9b05776
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src.ts/providers/provider-socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ export class SocketProvider extends JsonRpcApiProvider {
async _processMessage(message: string): Promise<void> {
const result = <JsonRpcResult | JsonRpcError | JsonRpcSubscription>(JSON.parse(message));

if ("id" in result) {
if (result && typeof(result) === "object" && "id" in result) {
const callback = this.#callbacks.get(result.id);
if (callback == null) {
this.emit("error", makeError("received result for unknown id", "UNKNOWN_ERROR", {
Expand All @@ -244,7 +244,7 @@ export class SocketProvider extends JsonRpcApiProvider {

callback.resolve(result);

} else if (result.method === "eth_subscription") {
} else if (result && result.method === "eth_subscription") {
const filterId = result.params.subscription;
const subscriber = this.#subs.get(filterId);
if (subscriber) {
Expand All @@ -257,6 +257,13 @@ export class SocketProvider extends JsonRpcApiProvider {
}
pending.push(result.params.result);
}

} else {
this.emit("error", makeError("received unexpected message", "UNKNOWN_ERROR", {
reasonCode: "UNEXPECTED_MESSAGE",
result
}));
return;
}
}

Expand Down

0 comments on commit 9b05776

Please sign in to comment.