Skip to content

Commit f58990b

Browse files
committedMay 18, 2023
More robust message checking in socket providers (#4051).
1 parent 984f6fa commit f58990b

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed
 

‎src.ts/providers/provider-socket.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ export class SocketProvider extends JsonRpcApiProvider {
231231
async _processMessage(message: string): Promise<void> {
232232
const result = <JsonRpcResult | JsonRpcError | JsonRpcSubscription>(JSON.parse(message));
233233

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

245245
callback.resolve(result);
246246

247-
} else if (result.method === "eth_subscription") {
247+
} else if (result && result.method === "eth_subscription") {
248248
const filterId = result.params.subscription;
249249
const subscriber = this.#subs.get(filterId);
250250
if (subscriber) {
@@ -257,6 +257,13 @@ export class SocketProvider extends JsonRpcApiProvider {
257257
}
258258
pending.push(result.params.result);
259259
}
260+
261+
} else {
262+
this.emit("error", makeError("received unexpected message", "UNKNOWN_ERROR", {
263+
reasonCode: "UNEXPECTED_MESSAGE",
264+
result
265+
}));
266+
return;
260267
}
261268
}
262269

0 commit comments

Comments
 (0)
Please sign in to comment.