aboutsummaryrefslogtreecommitdiff
path: root/src/domains/search
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/search
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/search')
-rw-r--r--src/domains/search/web-search.ts34
1 files changed, 16 insertions, 18 deletions
diff --git a/src/domains/search/web-search.ts b/src/domains/search/web-search.ts
index 69677b4..9327f15 100644
--- a/src/domains/search/web-search.ts
+++ b/src/domains/search/web-search.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";
@@ -26,11 +28,14 @@ const WebSearchArgs = Type.Object({
const buildSerpUrl = (query: string, limit: number): string =>
`https://www.google.com/search?q=${encodeURIComponent(query)}&num=${limit}&hl=en`;
-const isSerpExtraction = (value: unknown): value is SerpExtraction => {
- if (typeof value !== "object" || value === null) return false;
- const v = value as Record<string, unknown>;
- return Array.isArray(v["anchors"]) && typeof v["pageText"] === "string";
-};
+const SerpExtractionSchema = Type.Object({
+ anchors: Type.Array(
+ Type.Object({ href: Type.String(), heading: Type.String(), snippet: Type.String() }),
+ ),
+ pageText: Type.String(),
+});
+
+const serpExtractionValidator = Compile(SerpExtractionSchema);
const formatResults = (query: string, results: ReadonlyArray<SearchResult>): string => {
const lines = results.map((r) => `${r.rank}. ${r.title}\n ${r.url}\n ${r.snippet}`);
@@ -69,13 +74,14 @@ export const webSearchTool = defineBrowserTool({
const extracted = await evalInIsolatedTab(client, tab, buildSerpExtractionExpr(), isJsonText);
if (!extracted.success) return err(extracted.error);
- const parsedJson = safeParse(extracted.data);
- if (!isSerpExtraction(parsedJson)) {
- return err({ kind: "internal", message: "SERP extraction returned an unexpected shape" });
+ const parsedJson = parseJson(extracted.data, serpExtractionValidator);
+ if (!parsedJson.success) {
+ return err({ kind: "internal", message: `SERP extraction returned an unexpected shape: ${parsedJson.error}` });
}
+ const extraction: SerpExtraction = parsedJson.data;
- const results = parseGoogleSerp(parsedJson.anchors, limit);
- const verdict = classifySerp(parsedJson.pageText, results.length);
+ const results = parseGoogleSerp(extraction.anchors, limit);
+ const verdict = classifySerp(extraction.pageText, results.length);
if (verdict !== "ok") {
return err({
kind: "invalid_state",
@@ -108,11 +114,3 @@ export const webSearchTool = defineBrowserTool({
return renderExpandableText("web_search", result, expanded, theme);
},
});
-
-const safeParse = (text: string): unknown => {
- try {
- return JSON.parse(text) as unknown;
- } catch {
- return undefined;
- }
-};