Files
rea/src/domain/webScriptExport.ts
T
DeryFerd 499ac18011 fix(web): reject NUL bytes in web tool local path inputs
Four web tools accepted caller-selected local paths as plain strings, so
a NUL byte passed contract validation and reached filesystem calls. The
services only checked isAbsolute, and isAbsolute admits a NUL, so the
failure surfaced later as a raw ERR_INVALID_ARG_VALUE instead of a named
input constraint. Every other local-input tool already refuses NUL through
the shared local-path schema, and the existing OS-invalid-local-input
harness covers those but not these web tools.

inspect_web_network_capture.capture_path, export_web_scripts.capture_path
and output_directory, trace_web_source_location.manifest_path and
source_map.path, and trace_web_module_imports.manifest_path and
import_map.path now use localPathStringSchema. Each field already required
a nonempty string, so the advertised pattern and minLength stay the same
and only the NUL refusal is added.

Validation: a new schema test rejects NUL in all seven fields while
accepting ordinary absolute paths; the four tools are added to the
OS-invalid-local-input harness and reject NUL through the real advertised
MCP contracts; typecheck, oxlint, oxfmt, and toolSchemaValidity with a
regenerated catalog. Two webScriptExport boundary cases fail identically
with and without this change on this Windows host (symlink EPERM and
private-runtime-root authority), so they are environmental.
2026-10-11 23:32:52 +07:00

125 lines
4.1 KiB
TypeScript

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: 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
>;
const sourceSchema = z.discriminatedUnion("kind", [
z.strictObject({
kind: z.literal("page-script"),
script_key: z.string().min(1),
frame_id: z.string().nullable(),
is_module: z.boolean().nullable(),
language: z.string().nullable(),
source_map_url: z.string().nullable(),
}),
z.strictObject({
kind: z.literal("scenario-response"),
transaction_id: z.string().min(1),
request_sequence: z.number().int().min(1),
response_sequence: z.number().int().min(1).nullable(),
status: z.number().nullable(),
}),
]);
/** Exact retained-source identity and publication outcome. */
export const exportedWebScriptSchema = z.strictObject({
source: sourceSchema,
url: z.string(),
content: z.discriminatedUnion("state", [
z.strictObject({
state: z.literal("exported"),
relative_path: z.string().min(1),
layout: z.enum(["url-path", "isolated"]),
layout_reason: z.string().nullable(),
sha256: digestSchema,
bytes: z.number().int().min(0),
media_type: z.string().nullable(),
redacted: z.boolean().nullable(),
representation: z.enum([
"debugger-source-utf8",
"browser-decoded-response-bytes",
]),
}),
z.strictObject({
state: z.literal("unavailable"),
reason: z.string().min(1),
message: z.string().min(1),
}),
]),
});
export type ExportedWebScript = z.output<typeof exportedWebScriptSchema>;
/** Persisted manifest, also returned inline without duplicating script bytes. */
export const webScriptExportManifestSchema = z.strictObject({
capture_path: z.string().min(1),
capture_sha256: digestSchema,
capture_kind: z.enum(["page-inspection", "browser-scenario"]),
source_evidence_id: prefixedDigestSchema("ev").nullable(),
capture_completeness: z.union([
browserCompletenessSchema,
browserScenarioCompletenessSchema,
]),
output_directory: z.string().min(1),
analysis_input: z
.strictObject({
input_path: z.string().min(1),
format: z.literal("directory"),
})
.nullable(),
scripts: z.array(exportedWebScriptSchema),
limitations: z.array(z.string()),
});
/** Verified publication including the digest of the persisted manifest. */
export const webScriptExportResultSchema = webScriptExportManifestSchema.extend(
{
manifest: z.strictObject({
path: z.string().min(1),
sha256: digestSchema,
bytes: z.number().int().min(0),
}),
},
);
export type WebScriptExportResult = z.output<
typeof webScriptExportResultSchema
>;
/** Completed publication retained when cleanup fails after durable commit. */
export interface WebScriptExportPartialObservation {
readonly kind: "web-script-export";
readonly result: WebScriptExportResult;
}
/** Adapter input to the provider-neutral path planner and publisher. */
export interface CapturedWebScript {
readonly source: ExportedWebScript["source"];
readonly url: string;
readonly content:
| {
readonly state: "captured";
readonly bytes: Buffer;
readonly sha256: string;
readonly media_type: string | null;
readonly redacted: boolean | null;
readonly representation:
| "debugger-source-utf8"
| "browser-decoded-response-bytes";
}
| Extract<ExportedWebScript["content"], { state: "unavailable" }>;
}