diff options
| author | amankumarsingh77 <amankumarsingh7702@gmal.com> | 2026-05-02 15:48:28 +0530 |
|---|---|---|
| committer | amankumarsingh77 <amankumarsingh7702@gmal.com> | 2026-05-02 15:48:28 +0530 |
| commit | 52fd9523d1e4739d9a9e082e11686f244c767cc1 (patch) | |
| tree | 8277f0c8628bf9890cf3548ff51962e6b5940337 /src/cdp/types.ts | |
| parent | 22019a655c8708d5253e1d25b8ae654069032666 (diff) | |
fix(cdp): validate inbound CDP messages with a runtime type guard
Diffstat (limited to 'src/cdp/types.ts')
| -rw-r--r-- | src/cdp/types.ts | 21 |
1 files changed, 21 insertions, 0 deletions
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<string> = [ export const isInternalUrl = (url: string): boolean => INTERNAL_PREFIXES.some((p) => url.startsWith(p)); + +const isObject = (v: unknown): v is Readonly<Record<string, unknown>> => + 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; +}; |
