More robust message checking in socket providers (#4051).

This commit is contained in:
Richard Moore 2023-05-18 16:56:50 -04:00
parent 984f6fa155
commit f58990b80c

View File

@ -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", {
@ -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) {
@ -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;
}
}