From 52fd9523d1e4739d9a9e082e11686f244c767cc1 Mon Sep 17 00:00:00 2001 From: amankumarsingh77 Date: Sat, 2 May 2026 15:48:28 +0530 Subject: fix(cdp): validate inbound CDP messages with a runtime type guard --- src/cdp/types.ts | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'src/cdp/types.ts') diff --git a/src/cdp/types.ts b/src/cdp/types.ts index cbcb2d5..2bcc352 100644 --- a/src/cdp/types.ts +++ b/src/cdp/types.ts @@ -49,3 +49,24 @@ const INTERNAL_PREFIXES: ReadonlyArray = [ export const isInternalUrl = (url: string): boolean => INTERNAL_PREFIXES.some((p) => url.startsWith(p)); + +const isObject = (v: unknown): v is Readonly> => + typeof v === "object" && v !== null && !Array.isArray(v); + +export const isCdpRawMessage = (v: unknown): v is CdpRawMessage => { + if (!isObject(v)) return false; + const id = v["id"]; + if (id !== undefined && typeof id !== "number") return false; + const method = v["method"]; + if (method !== undefined && typeof method !== "string") return false; + const sessionId = v["sessionId"]; + if (sessionId !== undefined && typeof sessionId !== "string") return false; + const errVal = v["error"]; + if (errVal !== undefined) { + if (!isObject(errVal)) return false; + if (typeof errVal["message"] !== "string") return false; + const code = errVal["code"]; + if (code !== undefined && typeof code !== "number") return false; + } + return true; +}; -- cgit v1.2.3