fix(message-transport): only ignore messages that lack jsonrpc: "2.0" (#448)

Tighten the parse-failure handler: silently ignore postMessage frames
where event.data?.jsonrpc !== "2.0" (i.e. not JSON-RPC at all — null,
undefined, a primitive, or an object from the host environment that
doesn't carry the jsonrpc field). Messages that do carry jsonrpc: "2.0"
but are otherwise malformed still surface via onerror so genuine protocol
errors aren't swallowed.

https://claude.ai/code/session_01DaJpUtSJf4hA7tnu3nJz8s

Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
Olivier Chafik 2026-02-12 15:28:32 +00:00 • committed by GitHub
commit fd8cc978d4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -82,7 +82,17 @@ export class PostMessageTransport implements Transport {
if (parsed.success) {
console.debug("Parsed message", parsed.data);
this.onmessage?.(parsed.data);
} else if (event.data?.jsonrpc !== "2.0") {
// Not a JSON-RPC message at all (e.g. internal frames injected by
// the host environment). Ignore silently so the transport stays alive.
console.debug(
"Ignoring non-JSON-RPC message",
parsed.error.message,
event,
);
} else {
// Has jsonrpc: "2.0" but is otherwise malformed — surface as a real
// protocol error.
console.error("Failed to parse message", parsed.error.message, event);
this.onerror?.(
new Error(