From b9c77bb6f9b8b83574489344ef6efd207a611e98 Mon Sep 17 00:00:00 2001 From: amankumarsingh77 Date: Tue, 28 Jul 2026 00:53:19 +0530 Subject: refactor: validate JSON at every parse boundary MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Ten sites cast or implicitly typed JSON.parse output; JSON.parse("null") would have satisfied all of them. parseJson returns a Result and validates against a typebox schema. js.ts keeps unknown — user script output has no fixed shape. daemon/bridge.ts assigned JSON.parse straight to CdpRawMessage with no guard at all; it now uses isCdpRawMessage like the transport does. Co-Authored-By: Claude Opus 5 (1M context) --- src/domains/readpage/read-page.ts | 42 ++++++++++++++++++++++++++++----------- 1 file changed, 30 insertions(+), 12 deletions(-) (limited to 'src/domains/readpage') diff --git a/src/domains/readpage/read-page.ts b/src/domains/readpage/read-page.ts index 453c347..0a6fadc 100644 --- a/src/domains/readpage/read-page.ts +++ b/src/domains/readpage/read-page.ts @@ -1,4 +1,6 @@ import { Type } from "typebox"; +import { Compile } from "typebox/compile"; +import { parseJson } from "../../schemas/parse"; import { type Result, err, ok } from "../../util/result"; import { applyTruncation } from "../../util/truncate"; import { defineBrowserTool, type ToolErr, type ToolOk } from "../../util/tool"; @@ -24,23 +26,39 @@ const ReadPageArgs = Type.Object({ ), }); -const isPageCapture = (value: unknown): value is PageCapture => { - if (typeof value !== "object" || value === null) return false; - const v = value as Record; - return typeof v["url"] === "string" && typeof v["title"] === "string" && Array.isArray(v["blocks"]) && typeof v["bodyText"] === "string"; -}; +const ContentBlockSchema = Type.Object({ + kind: Type.Union([ + Type.Literal("paragraph"), + Type.Literal("heading"), + Type.Literal("listitem"), + Type.Literal("blockquote"), + Type.Literal("other"), + ]), + text: Type.String(), + linkTextLength: Type.Number(), + inBoilerplate: Type.Boolean(), +}); -const safeParse = (text: string): unknown => { - try { - return JSON.parse(text) as unknown; - } catch { - return undefined; +const PageCaptureSchema = Type.Object({ + url: Type.String(), + title: Type.String(), + blocks: Type.Array(ContentBlockSchema), + bodyText: Type.String(), +}); + +const pageCaptureValidator = Compile(PageCaptureSchema); + +const toPageCapture = (raw: unknown): PageCapture | undefined => { + if (typeof raw === "string") { + const parsed = parseJson(raw, pageCaptureValidator); + return parsed.success ? parsed.data : undefined; } + return pageCaptureValidator.Check(raw) ? raw : undefined; }; const captureToResult = async (raw: unknown): Promise> => { - const parsed = typeof raw === "string" ? safeParse(raw) : raw; - if (!isPageCapture(parsed)) return err({ kind: "internal", message: "page capture returned an unexpected shape" }); + const parsed = toPageCapture(raw); + if (parsed === undefined) return err({ kind: "internal", message: "page capture returned an unexpected shape" }); const page: ReadablePage = extractReadable(parsed); const header = `# ${page.title}\n${page.url}\n(${page.wordCount} words)\n\n`; const truncated = await applyTruncation(header + page.text, "readpage"); -- cgit v1.2.3