aboutsummaryrefslogtreecommitdiff
path: root/src/domains/readpage
diff options
context:
space:
mode:
authoramankumarsingh77 <amankumarsingh7702@gmail.com>2026-07-28 00:53:19 +0530
committerAman Kumar <145339995+amankumarsingh77@users.noreply.github.com>2026-08-02 19:12:46 +0530
commitb9c77bb6f9b8b83574489344ef6efd207a611e98 (patch)
tree3c59bf53bcce3ac5c6bbf9a22884b6e6f787dc91 /src/domains/readpage
parent1c73f4af643aa60ca554a3770264791d299e64e2 (diff)
refactor: validate JSON at every parse boundary
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) <noreply@anthropic.com>
Diffstat (limited to 'src/domains/readpage')
-rw-r--r--src/domains/readpage/read-page.ts42
1 files changed, 30 insertions, 12 deletions
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<string, unknown>;
- 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<Result<ToolOk, ToolErr>> => {
- 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");