diff --git a/src/domain/webModuleTrace.ts b/src/domain/webModuleTrace.ts index ae3e41fb9..059aa9a7f 100644 --- a/src/domain/webModuleTrace.ts +++ b/src/domain/webModuleTrace.ts @@ -1,6 +1,7 @@ import { z } from "zod"; import { digestSchema } from "./digests.js"; +import { localPathStringSchema } from "./localPath.js"; import { exportedWebScriptSchema, webScriptExportManifestSchema, @@ -8,10 +9,9 @@ import { /** Inspect one captured script's native ES module relationships. */ export const webModuleTraceInputSchema = z.strictObject({ - manifest_path: z - .string() - .min(1) - .describe("Absolute path to an export_web_scripts manifest"), + manifest_path: localPathStringSchema.describe( + "Absolute path to an export_web_scripts manifest", + ), script_index: z .number() .int() @@ -26,10 +26,9 @@ export const webModuleTraceInputSchema = z.strictObject({ ), import_map: z .strictObject({ - path: z - .string() - .min(1) - .describe("Absolute local import-map JSON path; no fetch"), + path: localPathStringSchema.describe( + "Absolute local import-map JSON path; no fetch", + ), base_url: z .string() .min(1) diff --git a/src/domain/webNetworkCapture.ts b/src/domain/webNetworkCapture.ts index bda9334bb..78309d1dc 100644 --- a/src/domain/webNetworkCapture.ts +++ b/src/domain/webNetworkCapture.ts @@ -1,5 +1,6 @@ import { z } from "zod"; import { jsonValueSchema } from "./jsonValue.js"; +import { localPathStringSchema } from "./localPath.js"; /** Complete historical evidence budgets; exceeding a budget returns no partial capture. */ export const WEB_NETWORK_CAPTURE_LIMITS = { @@ -11,10 +12,9 @@ export const WEB_NETWORK_CAPTURE_LIMITS = { /** Inspect an explicit retained capture without contacting its recorded destinations. */ export const inspectWebNetworkCaptureInputSchema = z.strictObject({ - capture_path: z - .string() - .min(1) - .describe("Absolute local HAR or mitmproxy capture path."), + capture_path: localPathStringSchema.describe( + "Absolute local HAR or mitmproxy capture path.", + ), format: z.enum(["har", "mitmproxy"]), record_ordinals: z .array(z.number().int().nonnegative()) diff --git a/src/domain/webScriptArtifacts.ts b/src/domain/webScriptArtifacts.ts index 24b38bc64..24be79001 100644 --- a/src/domain/webScriptArtifacts.ts +++ b/src/domain/webScriptArtifacts.ts @@ -1,13 +1,13 @@ import { z } from "zod"; import { digestSchema } from "./digests.js"; +import { localPathStringSchema } from "./localPath.js"; import { webScriptExportManifestSchema } from "./webScriptExport.js"; /** Select one source from an existing captured-script export. */ export const selectedWebScriptInputSchema = z.strictObject({ - manifest_path: z - .string() - .min(1) - .describe("Absolute export_web_scripts manifest path"), + manifest_path: localPathStringSchema.describe( + "Absolute export_web_scripts manifest path", + ), script_index: z .number() .int() diff --git a/src/domain/webScriptExport.ts b/src/domain/webScriptExport.ts index c52b08a29..a5524a110 100644 --- a/src/domain/webScriptExport.ts +++ b/src/domain/webScriptExport.ts @@ -3,21 +3,16 @@ import { z } from "zod"; import { browserCompletenessSchema } from "./browserCompleteness.js"; import { browserScenarioCompletenessSchema } from "./browserScenarioCaptureValues.js"; import { digestSchema, prefixedDigestSchema } from "./digests.js"; +import { localPathStringSchema } from "./localPath.js"; /** Export already captured scripts into one absent, caller-selected directory. */ export const exportWebScriptsInputSchema = z.strictObject({ - capture_path: z - .string() - .min(1) - .describe( - "Absolute filesystem path to saved inspect_web_page or capture_browser_scenario JSON, normalized result or complete Evidence", - ), - output_directory: z - .string() - .min(1) - .describe( - "Absolute absent filesystem directory for verified scripts and manifest; parent directory must exist", - ), + capture_path: localPathStringSchema.describe( + "Absolute filesystem path to saved inspect_web_page or capture_browser_scenario JSON, normalized result or complete Evidence", + ), + output_directory: localPathStringSchema.describe( + "Absolute absent filesystem directory for verified scripts and manifest; parent directory must exist", + ), }); export type ExportWebScriptsInput = z.output< typeof exportWebScriptsInputSchema diff --git a/src/domain/webSourceLocation.ts b/src/domain/webSourceLocation.ts index 54db26209..62b9a9dc1 100644 --- a/src/domain/webSourceLocation.ts +++ b/src/domain/webSourceLocation.ts @@ -1,5 +1,6 @@ import { z } from "zod"; import { digestSchema } from "./digests.js"; +import { localPathStringSchema } from "./localPath.js"; import { exportedWebScriptSchema } from "./webScriptExport.js"; import { capturedWebManifestIdentitySchema, @@ -17,10 +18,9 @@ export const webSourcePositionSchema = z.strictObject({ export const webSourceLocationInputSchema = selectedWebScriptInputSchema.extend( { source_map: z.strictObject({ - path: z - .string() - .min(1) - .describe("Absolute local source-map path; no fetch"), + path: localPathStringSchema.describe( + "Absolute local source-map path; no fetch", + ), url: z .string() .min(1) diff --git a/src/domain/webToolPathSchemas.test.ts b/src/domain/webToolPathSchemas.test.ts new file mode 100644 index 000000000..86265de6b --- /dev/null +++ b/src/domain/webToolPathSchemas.test.ts @@ -0,0 +1,103 @@ +import { describe, expect, it } from "vitest"; + +import { inspectWebNetworkCaptureInputSchema } from "./webNetworkCapture.js"; +import { webModuleTraceInputSchema } from "./webModuleTrace.js"; +import { exportWebScriptsInputSchema } from "./webScriptExport.js"; +import { webSourceLocationInputSchema } from "./webSourceLocation.js"; + +const nul = "/tmp/selected\0filename"; + +/** + * Every caller-selected local path these web tools accept must refuse NUL at + * the contract boundary, matching the shared local-path schema used by the + * other local-input tools. A NUL is not a valid filesystem byte on any + * supported host, and a downstream `isAbsolute` check admits it. + */ +describe("web tool local path inputs", () => { + it.each([ + [ + "export_web_scripts capture_path", + () => + exportWebScriptsInputSchema.safeParse({ + capture_path: nul, + output_directory: "/tmp/exported-scripts", + }).success, + ], + [ + "export_web_scripts output_directory", + () => + exportWebScriptsInputSchema.safeParse({ + capture_path: "/tmp/capture.json", + output_directory: nul, + }).success, + ], + [ + "inspect_web_network_capture capture_path", + () => + inspectWebNetworkCaptureInputSchema.safeParse({ + capture_path: nul, + format: "har", + }).success, + ], + [ + "trace_web_source_location manifest_path", + () => + webSourceLocationInputSchema.safeParse({ + manifest_path: nul, + script_index: 0, + source_map: { path: "/tmp/source.map", url: "http://127.0.0.1/a" }, + generated_position: { line: 1, column: 0 }, + }).success, + ], + [ + "trace_web_source_location source_map.path", + () => + webSourceLocationInputSchema.safeParse({ + manifest_path: "/tmp/manifest.json", + script_index: 0, + source_map: { path: nul, url: "http://127.0.0.1/a" }, + generated_position: { line: 1, column: 0 }, + }).success, + ], + [ + "trace_web_module_imports manifest_path", + () => + webModuleTraceInputSchema.safeParse({ + manifest_path: nul, + script_index: 0, + }).success, + ], + [ + "trace_web_module_imports import_map.path", + () => + webModuleTraceInputSchema.safeParse({ + manifest_path: "/tmp/manifest.json", + script_index: 0, + import_map: { path: nul, base_url: "http://127.0.0.1/a" }, + }).success, + ], + ])("rejects NUL in %s", (_label, parse) => { + expect(parse()).toBe(false); + }); + + it("still accepts ordinary absolute paths", () => { + expect( + exportWebScriptsInputSchema.safeParse({ + capture_path: "/tmp/capture.json", + output_directory: "/tmp/exported-scripts", + }).success, + ).toBe(true); + expect( + inspectWebNetworkCaptureInputSchema.safeParse({ + capture_path: "/tmp/session.har", + format: "har", + }).success, + ).toBe(true); + expect( + webModuleTraceInputSchema.safeParse({ + manifest_path: "/tmp/manifest.json", + script_index: 0, + }).success, + ).toBe(true); + }); +}); diff --git a/tests/acceptance/tools/localToolContracts.test.ts b/tests/acceptance/tools/localToolContracts.test.ts index c82a72349..638240296 100644 --- a/tests/acceptance/tools/localToolContracts.test.ts +++ b/tests/acceptance/tools/localToolContracts.test.ts @@ -91,6 +91,42 @@ const pathRequests: readonly [string, Record][] = [ ), ["capture_electron_scenario", { ...electron, args: ["a\0b"] }], ["demangle_swift", { symbols: ["abc\0def"] }], + ["inspect_web_network_capture", { capture_path: nul, format: "har" }], + [ + "export_web_scripts", + { capture_path: nul, output_directory: "/tmp/exported-scripts" }, + ], + [ + "export_web_scripts", + { capture_path: "/tmp/capture.json", output_directory: nul }, + ], + [ + "trace_web_source_location", + { + manifest_path: nul, + script_index: 0, + source_map: { path: "/tmp/source.map", url: "http://127.0.0.1/a" }, + generated_position: { line: 1, column: 0 }, + }, + ], + [ + "trace_web_source_location", + { + manifest_path: "/tmp/manifest.json", + script_index: 0, + source_map: { path: nul, url: "http://127.0.0.1/a" }, + generated_position: { line: 1, column: 0 }, + }, + ], + ["trace_web_module_imports", { manifest_path: nul, script_index: 0 }], + [ + "trace_web_module_imports", + { + manifest_path: "/tmp/manifest.json", + script_index: 0, + import_map: { path: nul, base_url: "http://127.0.0.1/a" }, + }, + ], ]; it("rejects OS-invalid local inputs through the real advertised MCP contracts", async () => {