Files
reverse-engineering-anything/scripts/verify-web-network-captures.mjs
T
morluto 123b693f08 feat(delivery)!: bound default results by an agent result budget (#14252)
* feat(delivery)!: bound default results by an agent result budget

Omitted detail now returns complete Evidence only when it fits the new
delivery.agent_result_budget_bytes runtime setting (default 49152 compact
JSON bytes, about 12k tokens at an estimated 4 bytes per token). Larger
results are retained and delivered as the producer's summary with a
limitation naming the budget and the recovery call; nothing is refused or
truncated. inspect_analysis_view gains an evidence-json view that pages the
exact JSON text of retained Evidence. The CLI applies the same budget and
publishes the complete bundle with --evidence_output.

inspect_managed_members type_names now also narrows member_refs and
exported_types, and inspect-byte-entropy gains CLI evidence delivery.

BREAKING CHANGE: An omitted detail no longer always returns complete
Evidence inline on MCP or the CLI for tools with a detail selector
(including inspect_managed_members, inspect_managed_artifact,
inspect_byte_entropy, inspect_artifact, analyze_javascript_application,
list_procedures, list_strings and the other operations listed in
src/domain/agentBudgetOperations.ts). Over the budget they return the
retained summary; pass detail complete (--detail complete) for complete
inline delivery. inspect_managed_members type_names narrows member_refs
and exported_types.

Fixes #14172
Fixes #14180
Part of #14187
Part of #14156

* test(schemas): record advertised summaries for budgeted detail

* fix(delivery): keep budgeted summaries canonical and carry delivery facts beside them

Review fixes for the agent result budget:

- Deliver the producer's own summary record, identical to detail summary,
  so its identity holds in summary consumers such as compare_artifacts.
  The budget, sizes, retention and the exact paging call travel as
  delivery facts: a second MCP text block and _meta, or a delivery field
  in CLI output.
- Capture the caller's detail for application-validated tools too, so
  inspect_binary_layout and similar tools are budgeted.
- Keep recovery facts when a budgeted summary itself exceeds the frame.
- Size an omitted evidence-json limit by escaped UTF-8 bytes and never
  split a surrogate pair, even in a one-unit page.
- Add the REA_AGENT_RESULT_BUDGET_BYTES launch seed; the shared test
  environment raises it for semantic tests, and budget tests clear it.
- Align tool descriptions and verifiers that read complete Evidence.

* fix(delivery): retain the budgeted summary like an explicit summary

* fix(delivery): read the budget seed from the captured environment and count delivery facts in the frame

- The agent result budget seed is read from the environment the server or
  CLI invocation captured: ToolResultDelivery takes it at construction,
  and a CLI invocation binds it in its publication context. The ambient
  process environment is no longer read.
- The frame check reserves the bytes of the delivery block and _meta that
  are added after encoding, so a budgeted response cannot exceed the frame.
- Conformance workflows that resend complete Evidence raise the budget
  through the shared inline-Evidence environment, as do the artifact and
  binary layout verifiers.

* fix(delivery)!: request complete Evidence explicitly and never spill CLI overflow

- Shared test and verifier environments no longer raise the agent result
  budget. Every test and verifier workflow that reads complete Evidence
  from a budgeted tool now requests it with detail complete or --detail
  complete, including the real-provider verifiers outside PR CI (Ghidra,
  Hopper, IDA, Kuna and client verifiers). Default delivery is exercised
  as shipped.
- Budgeting applies only to tools with a detail selector, so every
  budgeted call has a complete opt-out.
- CLI overflow without --evidence_output writes nothing; its delivery
  facts carry exact rerun argv for --detail complete and
  --evidence_output, omitted when the command declares sensitive values.

* test(delivery): prove captured budget seeds with valid in-range values

* test: state complete-detail requests once and fix codemod regressions

- Add tests/support/completeDetail.ts with COMPLETE_DETAIL and
  completeArguments so tests that read complete Evidence request it in one
  place, keeping functions within max-lines-per-function without raising
  the limit.
- Restore provider-client tests (GhidraClient, Hopper health): their
  callTool("name", ...) calls are bridge requests, not MCP tools, and take
  no detail.
- Update the game-scale type inventory test for member_refs narrowing.

* test: request complete Evidence in shared JavaScript analysis helpers and remaining CLI workflows

* test: request complete Evidence by default in the shared test transports

The shared MCP client helpers (createTestMcp, connectLocalToolsMcp and
the CLI fixture's shared MCP session) and the CLI runners (the cli
fixture and the shared CLI session) now apply one named default,
completeEvidenceByDefault. A budgeted tool called with neither detail nor
view gets detail complete; the CLI does the same with --detail complete,
chosen by the command's tool contract. A budgeted operation that resolves
to no contract fails loudly at load. Tests whose subject is delivery opt
out with delivery "budgeted". This is a test-harness choice; production
delivery is unchanged and the budget tests prove the default path.

Per-call complete-detail edits are reverted in the 204 test files whose
transports all apply the default. Explicit requests remain where a test
drives its own SDK client, server, CLI instance or process, and where
the detail choice is the test's subject.

* test: resolve CLI complete-detail defaults from the CLI registry

The Real Apple and browser verifiers read complete Evidence through
artifactCliEvidence, which ran the compiled CLI without a detail choice
and got the budgeted {summary, delivery} output. The helper now requests
complete Evidence for commands that take --detail, read from the compiled
CLI's own registry (scripts/lib/complete-evidence-cli.mjs).

The test harness default had a sibling bug: it treated every cli-flagged
budgeted contract as taking --detail, but only Evidence-delivery commands
do (inspect-binary-layout does not). It now reads the source CLI registry
too, and covers every command the CLI budgets.

* test(verifiers): request complete Evidence at each verifier's SDK client and CLI argv

The real-browser failure was the same missing normalized_result as the
Apple one: browser-capture-metadata-budget-e2e reads analyze-web-bundle
through artifactCliEvidence, fixed in the previous commit.

The shared verifier module, scripts/lib/complete-evidence.mjs, now owns
both transports:
- completeEvidenceClient wraps each verifier's SDK Client once, so every
  callTool to a budgeted tool with neither detail nor view requests
  complete Evidence, whatever local call helper a verifier uses (86
  clients, including the Ghidra, Hopper, IDA and Kuna verifiers'
  argument-less list_procedures and inspect_native_load_image calls).
- completeEvidenceCliArguments covers the remaining verifier CLI argv
  that chose no detail, such as function, decompile, analyze and the
  Android and Apple commands.
Commands and tools that are not budgeted are sent as written.

* test(delivery): await async completeEvidenceCliArguments helper in boundary test

* test(delivery): request complete detail in Electron CLI artifact format test

* docs(delivery): document delivery.rerun as add-only argument arrays

* test(schemas): record advertised summaries for the unified delivery budget

Tool descriptions now name the agent result budget, and the evidence-json default page is derived from the 32 KiB default, which changes the advertised input digests of the 28 affected tools. Recorded from the Build package job's output.

* test(delivery): pin the budget boundary and keep failures unbudgeted

Main's retainedSummaryDelivery tests pinned the size decision at exactly
32,768 and 32,769 UTF-8 JSON bytes. Unifying that decision into
budgetedSummaryBytes deleted them, and their port never landed, so the
byte measurement went unchecked.

Restore them against budgetedSummaryBytes at the configured budget and
one byte over, for the default and a runtime budget, with ASCII, escaped
and multibyte text, plus each condition of the decision. Off-by-one
mutants (< for <=, a cap one byte short) fail six cases; measuring UTF-16
code units instead of UTF-8 bytes fails the multibyte cases.

An MCP case pins that a target failure from a budgeted tool under the
minimum budget is identical with detail omitted and complete, with no
delivery facts.

* test(verifiers): route literal CLI argv for budgeted commands through complete Evidence

real-browser failed with "Scenario CLI did not report external disconnect
ownership": runScenarioCli ran capture-browser-scenario without --detail,
so an over-budget capture came back as its summary, which has no
browser.cleanup. The earlier sweep counted these files as covered
because their MCP clients were wrapped, but their CLI argv was not.

Route the shared runScenarioCli helper (five callers), the local cli
helpers in the module, source-map and script-export verifiers, and
every other literal argv for a budgeted CLI command without --detail or
--view (EVM interface, binary layout, packet capture, devicetree, DOM
destinations, Ghidra relocation symbols, WABT cleanup, Termux browser)
through completeEvidenceCliArguments. A sweep of scripts/ for budgeted
command literals with --json and no detail selection now finds none.

* fix(verifiers): leave clients without callTool unwrapped

The native verifier cleanup test drives native-value-e2e with a connection double whose Client has only connect and close. completeEvidenceClient bound client.callTool unconditionally, so wrapping threw a TypeError before connect ran. That TypeError replaced the primary connection failure the test asserts is preserved, and all four fault cases failed in Test shard 5 and Portable tests. A client without callTool has no tool call to complete, so the helper now returns it unchanged. Locally all five fault modes of the fixture now exit 0.

* fix(delivery): keep the catalog recorder on shipped helpers and expect view-implied summary

The verifier sweep wrapped record-mcp-catalog.mjs in completeEvidenceClient,
which lives in an unshipped helper. deferredRuntime copies only the
package's shipped files plus the recorder, so the recorder failed to load
(ERR_MODULE_NOT_FOUND) and its cleanup recorder never wrote. The recorder
only lists tools and prompts, so the wrapper had no effect; it is removed.

This branch makes a view with no --detail select summary delivery, as MCP
does for a view. Three CLI cases still expected the old view refusal; with
a view alone the missing input is now the summary bundle, so they expect
evidence_output, and a view with --detail complete still refuses the view.

* fix(delivery): request complete Evidence where callers need the whole result

Budgeted delivery made four callers receive a summary they cannot use:

- The GitHub Actions example uploads the analysis as a CI artifact, so it
  now asks for --detail complete; over budget it had saved a summary.
- javascriptIntegrityPolicyCli spawns the CLI itself, outside the test
  harness default, and reads normalized_result; it now applies the same
  completeEvidenceCliArguments default.
- nativeObservationViewsMcp builds its own client to prove complete
  observations overflow into a paging continuation; it now applies the
  harness default so each call asks for complete Evidence.
- The CLI parity goldens are re-recorded for the --detail and
  --evidence_output options the budgeted commands now advertise.

* fix(tests): give processes tests spawn the largest agent result budget

About 90 tests spawn scripts/rea.mjs or a server directly, outside the
harness default that asks for complete Evidence, and read analysis facts
from the result. Over the default budget they received a summary instead;
javascriptChildHandleOwnership failed this way.

The Vitest environment now seeds REA_AGENT_RESULT_BUDGET_BYTES with the
largest accepted value, so a spawned CLI or server delivers complete
Evidence, as the in-process default does. Delivery tests keep their own
budget: agentResultBudgetMcp builds its server without the seed, so its
default-budget cases still exercise the shipped default.

* fix(tests): give budgeted harness tests the shipped agent result budget

The test environment seeds the largest agent result budget so spawned
processes deliver complete Evidence. In-memory servers built by
createApplicationMcpHarness (and connectGhidraMcp, which uses it) inherit
process.env too, so a test that chose testDelivery "budgeted" kept the
seed: the 600-row list_procedures case and the omitted-detail web page
views returned complete Evidence instead of a summary.

testDeliveryEnvironment now owns that choice: a budgeted test's server
environment drops the seed and uses the shipped default. The application
harness applies it to every server it builds.

* fix(tests): expect view-implied summary delivery in the Playwright trace refusal

A view with no --detail now selects summary delivery, so its refusal
names the missing evidence_output bundle; --detail complete still refuses
the view. The trace test still expected a view refusal in both cases.
2026-11-25 11:25:18 -05:00

1103 lines
34 KiB
JavaScript

#!/usr/bin/env node
import assert from "node:assert/strict";
import { execFile } from "node:child_process";
import { createHash, randomUUID } from "node:crypto";
import {
access,
copyFile,
chmod,
readFile,
truncate,
writeFile,
} from "node:fs/promises";
import { dirname, isAbsolute, join, resolve } from "node:path";
import { fileURLToPath } from "node:url";
import { promisify } from "node:util";
import { Client } from "@modelcontextprotocol/client";
import { StdioClientTransport } from "@modelcontextprotocol/client/stdio";
import { runOwnedCommand } from "../dist/process/OwnedCommand.js";
import { WEB_NETWORK_CAPTURE_LIMITS } from "../dist/domain/webNetworkCapture.js";
import {
mcpTextValue,
requireMcpOutput,
requireMcpToolError,
} from "./lib/mcp-verifier-results.mjs";
import { createVerifierRun, completeVerifierRun } from "./lib/verifier-run.mjs";
import { VerifierFixture } from "./lib/verifier-fixture.mjs";
import { completeEvidenceClient } from "./lib/complete-evidence.mjs";
const executable = process.env.REA_MITMDUMP_COMMAND;
if (
process.platform !== "linux" ||
executable === undefined ||
!isAbsolute(executable)
)
throw new Error(
"verify:web:network-captures requires Linux and absolute REA_MITMDUMP_COMMAND for caller-supplied mitmdump 12.3.1",
);
await access(executable);
const entrypoint =
process.argv[2] ?? fileURLToPath(new URL("./rea.mjs", import.meta.url));
const run = createVerifierRun();
const runtime = await VerifierFixture.open({
prefix: "rea-history-verifier-",
});
const environment = Object.fromEntries(
Object.entries(process.env).filter(([, value]) => typeof value === "string"),
);
const client = completeEvidenceClient(
new Client({
name: "historical-web-capture-verifier",
version: "1",
}),
);
const transport = new StdioClientTransport({
command: process.execPath,
args: [entrypoint, "mcp"],
env: environment,
stderr: "pipe",
});
const failures = [];
let cases = 0;
try {
await runOwnedCommand(
{
command: executable,
arguments: [
"--no-server",
"-q",
"--set",
`confdir=${join(runtime.path, "config")}`,
"--set",
`rea_fixture_root=${runtime.path}`,
"--set",
`rea_adapter_path=${resolve(dirname(entrypoint), "../bridge/mitmproxy/capture.py")}`,
"-s",
fileURLToPath(
new URL("./fixtures/generate-mitmproxy-capture.py", import.meta.url),
),
],
cwd: runtime.path,
runId: `rea-history-fixture-${randomUUID()}`,
hostEnvironment: environment,
},
{ timeoutMs: 30_000, diagnosticBytes: 1024 * 1024 },
);
assert.deepEqual(
JSON.parse(
await readFile(
join(runtime.path, "native-error-classifications.json"),
"utf8",
),
),
{ passed: 7 },
);
await client.connect(transport);
for (const format of ["har", "mitmproxy"]) {
const path = join(
runtime.path,
format === "har" ? "producer.har" : "flows.mitm",
);
const raw = await readFile(path);
const input = { capture_path: path, format, record_ordinals: [1, 0] };
for (const mode of ["cli", "mcp"]) {
const value = await inspect(mode, input);
assert.equal(value.total_records, 2);
assert.deepEqual(
value.records.map((record) => record.ordinal),
[1, 0],
);
assert.equal(
value.artifact.sha256,
createHash("sha256").update(raw).digest("hex"),
);
assert.equal(value.runtime_attribution, "unknown");
const first = value.records[1];
if (format === "mitmproxy") {
assert.equal(first.reported.id, "original-producer-id");
assert.equal(value.records[0].reported.id, first.reported.id);
assert.equal(first.location.offset, 0);
assert.equal(
value.records[0].location.offset + value.records[0].location.bytes,
raw.length,
);
assert.equal(
first.reported.request.path,
"http://example.test/a?token=ordinary#fragment",
);
assertBinary(
first,
"/request/content",
Buffer.from([0, 255, ...Buffer.from("body")]),
);
assertBinary(
first,
"/response/content",
Buffer.from([0, 254, ...Buffer.from("answer")]),
);
assertBinary(
first,
"/websocket/messages/0/2",
Buffer.from([0, 253, ...Buffer.from("message")]),
);
assert.equal(value.records[0].reported.request.content, null);
assertBinary(value.records[0], "/response/content", Buffer.alloc(0));
assert.ok(
first.numeric_literals.some(
(number) =>
number.pointer === "/metadata/big_integer" &&
number.literal === "9007199254740993",
),
);
assert.ok(
first.numeric_literals.some(
(number) =>
number.pointer === "/metadata/nonfinite" &&
number.literal === "inf",
),
);
assert.equal(
first.reported.metadata.response.headers[0][1],
"ordinary-extension-credential-name",
);
} else {
assert.equal(value.container.reported.log.creator.name, "mitmproxy");
assert.equal(value.container.reported.log.creator.version, "12.3.1");
assert.equal(value.records[0].reported.request.postData.text, null);
assertBinary(
first,
"/response/content/text",
Buffer.from([0, 255, 1, 254]),
);
}
const text = JSON.stringify(value);
for (const secret of [
"native-transport-secret",
"cookie-secret",
"redirect-password",
"user:password",
])
assert.ok(
!text.includes(secret),
`Credential leaked through ${mode}/${format}`,
);
cases++;
}
}
for (const mode of ["cli", "mcp"]) {
const value = await inspect(mode, {
capture_path: join(runtime.path, "string-urls.mitm"),
format: "mitmproxy",
});
const first = value.records[0];
for (const state of [first.reported, first.reported.backup]) {
assert.equal(state.request.path, "https://example.test/string-path");
assert.equal(state.request.authority, "example.test");
assert.deepEqual(
state.request.headers.map((field) => field[1]),
["https://example.test/from", "//example.test"],
);
assert.equal(state.response.headers[0][1], "https://example.test/to");
}
assert.ok(!JSON.stringify(value).includes("password"));
assert.ok(
first.redactions.some(
(redaction) =>
redaction.pointer === "/backup/request/authority" &&
redaction.reason === "transport-credential",
),
);
cases++;
}
// Verify Python float spellings through the actual native producer and both public adapters.
for (const mode of ["cli", "mcp"]) {
for (const marked of [false, true]) {
const value = await inspect(mode, {
capture_path: join(runtime.path, "flows.mitm"),
format: "mitmproxy",
sensitive_values: marked ? ["1e-7", "000000"] : [],
});
assert.deepEqual(
value.records.map(({ ordinal }) => ordinal),
[0, 1],
);
for (const record of value.records) {
assert.deepEqual(record.reported.metadata.numeric_spellings, {
small: marked ? null : 1e-7,
large: marked ? null : 1e20,
ordinary: 42,
});
for (const [key, literal] of [
["small", "1e-07"],
["large", "1e+20"],
]) {
const pointer = "/metadata/numeric_spellings/" + key;
assert.deepEqual(
record.numeric_literals
.filter((number) => number.pointer === pointer)
.map(({ literal }) => literal),
marked ? [] : [literal],
);
assert.equal(
record.redactions.some(
(redaction) =>
redaction.pointer === pointer &&
redaction.reason === "explicit-sensitive-value",
),
marked,
);
}
}
cases++;
}
}
for (const sensitive_values of [
["REDACTED", "["],
["secret", "REDACTED"],
]) {
for (const format of ["har", "mitmproxy"]) {
for (const mode of ["cli", "mcp"]) {
const value = await inspect(mode, {
capture_path: join(
runtime.path,
format === "har" ? "producer.har" : "string-urls.mitm",
),
format,
record_ordinals: [0],
sensitive_values,
});
const first = value.records[0];
const markers =
format === "har"
? first.reported._markers
: first.reported.metadata._markers;
assert.equal(markers.sensitive, null);
assert.equal(markers.ordinary, "unmarked");
assert.equal(
sensitive_values.includes("[") ? markers.bracket : markers.overlap,
null,
);
if (format === "mitmproxy")
assert.equal(first.reported.backup.metadata._markers.sensitive, null);
cases++;
}
}
}
for (const name of [
"equal-number",
"equal-null",
"equal-object",
"escaped-key",
"core",
]) {
const path = join(runtime.path, `duplicate-${name}.har`);
const original = await readFile(path);
for (const mode of ["cli", "mcp"]) {
await inspect(
mode,
{ capture_path: path, format: "har" },
"invalid_input",
);
assert.deepEqual(await readFile(path), original);
cases++;
}
}
for (const format of ["har", "mitmproxy"]) {
const path = join(
runtime.path,
format === "har" ? "prototype.har" : "prototype.mitm",
);
const original = await readFile(path);
for (const mode of ["cli", "mcp"]) {
const value = await inspect(mode, { capture_path: path, format });
const reported =
format === "har"
? value.container.reported._extension
: value.records[0].reported.metadata;
assert.equal(Object.hasOwn(reported, "__proto__"), true);
assert.deepEqual(reported["__proto__"], { preserved: 7 });
assert.equal(Object.getPrototypeOf(reported), Object.prototype);
assert.equal(Reflect.get(Object.prototype, "preserved"), undefined);
if (format === "har") assert.equal(reported.constructor, "ordinary");
assert.deepEqual(await readFile(path), original);
cases++;
}
}
for (const format of ["har", "mitmproxy"]) {
const path = join(runtime.path, `malformed-${format}`);
await writeFile(path, format === "har" ? "{invalid-json" : "999999999999:");
for (const mode of ["cli", "mcp"]) {
await inspect(mode, { capture_path: path, format }, "invalid_input");
cases++;
}
}
for (const format of ["har", "mitmproxy"]) {
for (const mode of ["cli", "mcp"]) {
const value = await inspect(mode, {
capture_path: join(
runtime.path,
format === "har" ? "producer.har" : "string-urls.mitm",
),
format,
sensitive_values: ["private-property"],
});
const record = value.records[0];
const properties =
format === "har"
? record.reported._private_properties
: record.reported.metadata._private_properties;
assert.deepEqual(properties, { kept: 7 });
assert.ok(!JSON.stringify(value).includes("private-property"));
assert.ok(
record.redactions.some(
(item) =>
item.scope === "property-name" &&
item.pointer ===
(format === "har"
? "/_private_properties"
: "/metadata/_private_properties"),
),
);
if (format === "mitmproxy")
assert.deepEqual(record.reported.backup.metadata._private_properties, {
kept: 7,
});
cases++;
}
}
for (const [fixture, prefix] of [
["ows", " \t"],
["controls", "\r\n \v\f\x00\x1f"],
]) {
for (const format of ["har", "mitmproxy"]) {
for (const mode of ["cli", "mcp"]) {
const value = await inspect(mode, {
capture_path: join(
runtime.path,
`${fixture}.${format === "har" ? "har" : "mitm"}`,
),
format,
});
assert.ok(!JSON.stringify(value).includes("ows-secret"));
const record = value.records[0];
const states =
format === "har"
? [record.reported]
: [record.reported, record.reported.backup];
for (const state of states) {
if (format === "har") {
for (const side of ["request", "response"])
assert.ok(
state[side].headers.some(
(header) =>
header.value ===
`${prefix}HTTPS://example.test/path?token=ordinary#fragment\t `,
),
);
} else {
assert.deepEqual(state.request.headers, [
[
"Referer",
`${prefix}HTTPS://example.test/path?token=ordinary#fragment\t `,
],
[
"Origin",
`${prefix}//example.test/path?token=ordinary#fragment\t `,
],
]);
assert.deepEqual(state.response.headers, [
[
"Location",
`${prefix}HTTPS://example.test/path?token=ordinary#fragment\t `,
],
]);
}
}
if (format === "mitmproxy") {
for (const pointer of [
"/request/headers/0/1",
"/backup/request/headers/0/1",
])
assert.deepEqual(
record.binary_fields.find((field) => field.pointer === pointer),
{
pointer,
representation: "producer-bytes",
state: "redacted",
content_base64: null,
bytes: null,
sha256: null,
},
);
}
cases++;
}
}
}
for (const mode of ["cli", "mcp"]) {
const value = await inspect(mode, {
capture_path: join(runtime.path, "non-utf8-urls.mitm"),
format: "mitmproxy",
});
const record = value.records[0];
assert.ok(!JSON.stringify(value).includes("byte-secret"));
for (const prefix of ["", "/backup"]) {
for (const suffix of [
"/request/path",
"/request/authority",
"/request/headers/0/1",
"/request/headers/1/1",
"/response/headers/0/1",
]) {
const pointer = prefix + suffix;
assert.deepEqual(
record.binary_fields.find((field) => field.pointer === pointer),
{
pointer,
representation: "producer-bytes",
state: "redacted",
content_base64: null,
bytes: null,
sha256: null,
},
);
}
}
cases++;
}
const nativeCoordinateInput = {
capture_path: join(runtime.path, "flows.mitm"),
format: "mitmproxy",
};
const nativeCoordinates = await inspect("mcp", nativeCoordinateInput);
const originalLocation = nativeCoordinates.records[1].location;
assert.equal(originalLocation.kind, "byte-range");
for (const literal of [
String(originalLocation.offset),
String(originalLocation.bytes),
]) {
for (const mode of ["cli", "mcp"]) {
const excluded = await inspect(mode, {
...nativeCoordinateInput,
sensitive_values: [literal],
});
assert.deepEqual(excluded.records[1].location, {
kind: "unknown",
reason: "explicit-sensitive-value",
});
cases++;
}
}
cases++;
const deniedPath = join(runtime.path, "unreadable.har");
await copyFile(join(runtime.path, "producer.har"), deniedPath);
await chmod(deniedPath, 0o000);
for (const mode of ["cli", "mcp"]) {
for (const format of ["har", "mitmproxy"]) {
const failure = await inspect(
mode,
{ capture_path: deniedPath, format },
"unavailable",
);
assert.equal(failure.code, "access_denied");
assert.equal(failure.details.system_code, "EACCES");
assert.equal(failure.details.path, deniedPath);
cases++;
}
for (const literal of ["log", "entries"]) {
const value = await inspect(mode, {
capture_path: join(runtime.path, "producer.har"),
format: "har",
sensitive_values: [literal],
});
assert.equal(value.container.records_pointer, null);
assert.ok(
value.records.every(
(record) =>
record.location.kind === "unknown" &&
record.location.reason === "explicit-sensitive-value",
),
);
assert.ok(!JSON.stringify(value).includes(literal));
cases++;
}
for (const format of ["har", "mitmproxy"]) {
const value = await inspect(mode, {
capture_path: join(
runtime.path,
format === "har" ? "producer.har" : "string-urls.mitm",
),
format,
sensitive_values: ["~1"],
});
assert.ok(!JSON.stringify(value).includes("~1"));
cases++;
}
}
for (const mode of ["cli", "mcp"]) {
for (const format of ["har", "mitmproxy"]) {
for (const sensitive_values of [
["producer"],
["producer", "REDACTED", "…"],
]) {
await inspect(mode, {
capture_path: join(
runtime.path,
format === "har" ? "producer.har" : "flows.mitm",
),
format,
sensitive_values,
});
cases++;
}
}
}
for (const mode of ["cli", "mcp"]) {
for (const selection of [
{ record_ordinals: [12345], sensitive_values: ["12345"] },
{ record_ordinals: [0, 0], sensitive_values: ["Each"] },
]) {
await inspect(
mode,
{
capture_path: join(runtime.path, "producer.har"),
format: "har",
...selection,
},
"invalid_input",
);
cases++;
}
}
await inspect(
"mcp",
{
capture_path: "relative.har",
format: "har",
sensitive_values: ["absolute"],
},
"invalid_input",
);
cases++;
const privatePath = join(runtime.path, "REDACTED.har");
for (const name of ["mysecret", "/ordinary/secret"]) {
const unknownArgument = await inspect(
"mcp",
{
capture_path: join(runtime.path, "producer.har"),
format: "har",
sensitive_values: ["secret"],
[name]: true,
},
"invalid_input",
);
assert.deepEqual(unknownArgument.details.issues[0].path, []);
cases++;
}
await inspect("mcp", {
capture_path: join(runtime.path, "flows.mitm"),
format: "mitmproxy",
sensitive_values: ["\ud800", "ordinary-scalar"],
});
cases++;
const replacementHar = JSON.parse(
await readFile(join(runtime.path, "producer.har"), "utf8"),
);
replacementHar.log.entries[0].response.content = {
size: 3,
mimeType: "application/octet-stream",
encoding: "base64",
text: "77+9",
};
const replacementHarPath = join(runtime.path, "unicode-scalar.har");
await writeFile(replacementHarPath, JSON.stringify(replacementHar));
for (const literal of ["\ud800", "\udfff", "\ufffd"]) {
const unicode = await inspect("mcp", {
capture_path: replacementHarPath,
format: "har",
sensitive_values: [literal],
});
const body = unicode.records[0].binary_fields.find(
(field) => field.pointer === "/response/content/text",
);
assert.equal(body?.state, literal === "\ufffd" ? "redacted" : "retained");
assert.equal(body.content_base64, literal === "\ufffd" ? null : "77+9");
cases++;
}
const numericHar = (
await readFile(join(runtime.path, "producer.har"), "utf8")
).replace('"cache": {}', '"cache": {}, "_big": 9007199254740993');
const numericPath = join(runtime.path, "numeric.har");
await writeFile(numericPath, numericHar);
assert.ok(numericHar.includes('"_big": 9007199254740993'));
for (const mode of ["cli", "mcp"]) {
for (const format of ["har", "mitmproxy"]) {
for (const literal of ["9007199254740993", "4740993"]) {
const value = await inspect(mode, {
capture_path:
format === "har" ? numericPath : join(runtime.path, "flows.mitm"),
format,
sensitive_values: [literal],
});
assert.ok(!JSON.stringify(value).includes(literal));
const first = value.records[0];
const pointer = format === "har" ? "/_big" : "/metadata/big_integer";
assert.ok(
first.redactions.some(
(redaction) =>
redaction.pointer === pointer &&
redaction.reason === "explicit-sensitive-value",
),
);
assert.ok(
first.numeric_literals.every(
(number) => !number.literal.includes(literal),
),
);
cases++;
}
}
}
for (const mode of ["cli", "mcp"]) {
for (const format of ["har", "mitmproxy"]) {
await inspect(mode, {
capture_path: join(
runtime.path,
format === "har" ? "producer.har" : "flows.mitm",
),
format,
sensitive_values: [format],
});
cases++;
}
}
const encodedHar = JSON.parse(
await readFile(join(runtime.path, "producer.har"), "utf8"),
);
encodedHar.log.entries[0].response.content = {
size: 6,
mimeType: "application/octet-stream",
encoding: "base64",
text: "c2 VjcmV0",
};
const encodedHarPath = join(runtime.path, "encoded-sensitive.har");
await writeFile(encodedHarPath, JSON.stringify(encodedHar));
for (const mode of ["cli", "mcp"]) {
for (const format of ["har", "mitmproxy"]) {
const bytes =
format === "har"
? Buffer.from("secret")
: Buffer.from([0, 255, ...Buffer.from("body")]);
for (const literal of [
bytes.toString("base64"),
bytes.toString("base64").slice(0, 4),
createHash("sha256").update(bytes).digest("hex"),
]) {
const value = await inspect(mode, {
capture_path:
format === "har"
? encodedHarPath
: join(runtime.path, "flows.mitm"),
format,
sensitive_values: [literal],
});
const pointer =
format === "har" ? "/response/content/text" : "/request/content";
const field = value.records[0].binary_fields.find(
(field) => field.pointer === pointer,
);
assert.equal(field?.state, "redacted");
assert.equal(field.content_base64, null);
assert.equal(field.sha256, null);
assert.ok(!JSON.stringify(value).includes(literal));
cases++;
}
}
}
for (const mode of ["cli", "mcp"]) {
for (const format of ["har", "mitmproxy"]) {
const capturePath = join(
runtime.path,
format === "har" ? "producer.har" : "flows.mitm",
);
const digest = createHash("sha256")
.update(await readFile(capturePath))
.digest("hex");
for (const literal of [digest, digest.slice(0, 12)]) {
const value = await inspect(mode, {
capture_path: capturePath,
format,
sensitive_values: [literal],
});
assert.equal(value.artifact.sha256, null);
assert.equal(value.artifact.path, capturePath);
assert.ok(!JSON.stringify(value).includes(literal));
cases++;
}
}
}
const oversizedPath = join(runtime.path, "oversized-capture");
for (const mode of ["cli", "mcp"]) {
for (const literal of ["log", "entries"]) {
const hidden = await inspect(mode, {
capture_path: join(runtime.path, "producer.har"),
format: "har",
sensitive_values: [literal],
});
assert.equal(hidden.total_records, 2);
assert.equal(hidden.container.records_pointer, null);
for (const [ordinal, record] of hidden.records.entries()) {
assert.equal(record.ordinal, ordinal);
assert.deepEqual(record.location, {
kind: "unknown",
reason: "explicit-sensitive-value",
});
assert.equal(record.reported, null);
assert.deepEqual(record.binary_fields, []);
assert.deepEqual(record.numeric_literals, []);
}
cases++;
}
}
await writeFile(oversizedPath, "");
await truncate(oversizedPath, WEB_NETWORK_CAPTURE_LIMITS.inputBytes + 1);
await copyFile(join(runtime.path, "producer.har"), privatePath);
for (const mode of ["cli", "mcp"]) {
for (const format of ["har", "mitmproxy"]) {
const failure = await inspect(
mode,
{ capture_path: oversizedPath, format },
"invalid_input",
);
assert.equal(failure.details.issues[0].reason, "out_of_range");
assert.deepEqual(failure.details.issues[0].path, [
mode === "cli" ? "capturePath" : "capture_path",
]);
assert.equal(
failure.details.issues[0].expected.maximum_capture_bytes,
WEB_NETWORK_CAPTURE_LIMITS.inputBytes,
);
cases++;
}
for (const format of ["har", "mitmproxy"]) {
const failure = await inspect(
mode,
{
capture_path: join(
runtime.path,
format === "har" ? "deep.har" : "deep.mitm",
),
format,
},
"invalid_input",
);
const issue = failure.details.issues[0];
assert.equal(issue.reason, "out_of_range");
assert.equal(
issue.path[0],
mode === "cli" ? "capturePath" : "capture_path",
);
assert.match(issue.path[1], /^\/_extension\/child/);
assert.equal(
issue.expected.maximum_capture_nesting,
WEB_NETWORK_CAPTURE_LIMITS.depth,
);
cases++;
}
await inspect(
mode,
{
capture_path: join(runtime.path, "invalid-credential-key.mitm"),
format: "mitmproxy",
},
"invalid_input",
);
cases++;
const credentialDepth = await inspect(
mode,
{
capture_path: join(runtime.path, "deep-credential.mitm"),
format: "mitmproxy",
},
"invalid_input",
);
assert.equal(credentialDepth.details.issues[0].reason, "out_of_range");
assert.match(
credentialDepth.details.issues[0].path[1],
/^\/request\/headers\/0\/1\/child/,
);
cases++;
const value = await inspect(mode, {
capture_path: privatePath,
format: "har",
sensitive_values: ["REDACTED", "["],
});
assert.equal(value.artifact.path, "");
assert.ok(!JSON.stringify(value).includes("REDACTED"));
cases++;
await inspect(
mode,
{
capture_path: join(runtime.path, "invalid-private-parent.har"),
format: "har",
sensitive_values: ["response"],
},
"invalid_input",
);
cases++;
await inspect(
mode,
{
capture_path: join(runtime.path, "private-property-absent.har"),
format: "har",
sensitive_values: ["private-property", "REDACTED"],
},
"invalid_input",
);
cases++;
await inspect(
mode,
{
capture_path: join(runtime.path, "invalid-private-key.mitm"),
format: "mitmproxy",
sensitive_values: ["private-property"],
},
"invalid_input",
);
cases++;
}
for (const mode of ["cli", "mcp"]) {
const value = await inspect(mode, {
capture_path: join(runtime.path, "flows.mitm"),
format: "mitmproxy",
record_ordinals: [0],
sensitive_values: ["body"],
});
assert.equal(
value.records[0].binary_fields.find(
(field) => field.pointer === "/request/content",
).state,
"redacted",
);
cases++;
}
for (const format of ["har", "mitmproxy"]) {
const path = join(runtime.path, `empty-${format}`);
await writeFile(
path,
format === "har"
? JSON.stringify({
log: {
version: "1.2",
creator: { name: "source-owned-empty-fixture", version: "1" },
entries: [],
},
})
: "",
);
for (const mode of ["cli", "mcp"]) {
const value = await inspect(mode, { capture_path: path, format });
assert.equal(value.total_records, 0);
cases++;
}
}
} catch (cause) {
failures.push(cause);
} finally {
// The root is removed only after the client and transport confirm.
runtime.releaseAfter(() => client.close());
runtime.releaseAfter(() => transport.close());
try {
await runtime.release();
} catch (cause) {
failures.push(cause);
}
}
const verifier = await completeVerifierRun(run);
try {
assert.equal(verifier.process_lineage.status, "verified");
assert.deepEqual(verifier.process_lineage.descendants, []);
} catch (cause) {
failures.push(cause);
}
if (failures.length > 0) {
console.error(
JSON.stringify(
{ status: "failed", public_cases: cases, verifier },
null,
2,
),
);
throw Object.assign(
new AggregateError(
failures,
"Historical capture verification failed; analysis and cleanup failures are retained.",
),
// The caller can retry the retained root from the final failure.
runtime.released ? {} : { cleanupLeases: [runtime] },
);
}
console.log(
JSON.stringify(
{
status: "passed",
public_cases: cases,
upstream: "mitmproxy 12.3.1 FlowWriter + SaveHar",
native_failure_classifications: 7,
offline: true,
verifier,
},
null,
2,
),
);
async function inspect(mode, input, errorCategory) {
if (mode === "mcp") {
const result = await client.callTool({
name: "inspect_web_network_capture",
arguments: { ...input, detail: "complete" },
});
if (errorCategory !== undefined) {
const error = requireMcpToolError(result);
assert.equal(error.category, errorCategory);
const rawError = JSON.parse(mcpTextValue(result)).error;
for (const literal of input.sensitive_values ?? [])
assert.ok(!JSON.stringify(rawError).includes(literal));
return error;
}
const value = requireMcpOutput(result, "inspect_web_network_capture");
assertSensitiveLimitations(value, input.sensitive_values ?? []);
if (input.format === "har") {
const surface = await client.callTool({
name: "inspect_analysis_view",
arguments: {
source: { kind: "retained-evidence", evidence_id: value.evidence_id },
view: { kind: "api-surface" },
},
});
if ((input.sensitive_values ?? []).length > 0) {
// Derived view text cannot be checked against undisclosed declarations.
requireMcpToolError(surface);
const refused = JSON.parse(mcpTextValue(surface));
for (const literal of input.sensitive_values)
assert.ok(!JSON.stringify(refused).includes(literal));
} else {
const projected = requireMcpOutput(surface, "inspect_analysis_view");
assert.equal(projected.normalized_result.kind, "api-surface");
assert.equal(
projected.normalized_result.records.examined,
value.normalized_result.records.length,
);
}
const manifest = await client.callTool({
name: "inspect_analysis_view",
arguments: {
source: { kind: "retained-evidence", evidence_id: value.evidence_id },
view: { kind: "environment-manifest" },
},
});
if ((input.sensitive_values ?? []).length > 0) {
requireMcpToolError(manifest);
const refusedManifest = JSON.parse(mcpTextValue(manifest));
for (const literal of input.sensitive_values)
assert.ok(!JSON.stringify(refusedManifest).includes(literal));
} else {
const projected = requireMcpOutput(manifest, "inspect_analysis_view");
assert.equal(projected.normalized_result.kind, "environment-manifest");
assert.equal(projected.normalized_result.unknowns.length, 3);
for (const record of projected.normalized_result.records)
assert.deepEqual(Object.keys(record).sort(), [
"content_type",
"method",
"ordinal",
"path",
"status",
]);
}
}
return value.normalized_result;
}
const args = [
entrypoint,
"inspect-web-network-capture",
"--detail",
"complete",
input.capture_path,
input.format,
"--json",
...(input.record_ordinals ?? []).flatMap((ordinal) => [
"--record",
String(ordinal),
]),
...(input.sensitive_values ?? []).flatMap((value) => [
"--sensitive-value",
value,
]),
];
let result;
try {
result = await promisify(execFile)(process.execPath, args, {
env: environment,
timeout: 40_000,
maxBuffer: 96 * 1024 * 1024,
});
} catch (cause) {
if (errorCategory === undefined) throw cause;
assert.equal(typeof cause.code, "number");
const value = JSON.parse(cause.stdout);
assert.equal(value.category, errorCategory);
for (const literal of input.sensitive_values ?? [])
assert.ok(!JSON.stringify(value).includes(literal));
return value;
}
assert.equal(errorCategory, undefined, "Expected malformed capture to fail");
const evidence = JSON.parse(result.stdout);
assertSensitiveLimitations(evidence, input.sensitive_values ?? []);
return evidence.normalized_result;
}
function assertSensitiveLimitations(evidence, literals) {
for (const value of Object.values(evidence.parameters))
if (typeof value === "string")
for (const literal of literals)
assert.ok(
!value.includes(literal),
"Declared literal retained in Evidence parameter",
);
const result = evidence.normalized_result;
if (result.artifact.sha256 === null)
assert.equal(evidence.subject.identity.kind, "unknown");
else assert.equal(evidence.subject.identity.sha256, result.artifact.sha256);
assert.deepEqual(
evidence.provider,
result.decoder,
"Evidence provider tuple changed",
);
assert.equal(
result.decoder.name,
result.format === "har"
? "REA HAR capture adapter"
: "REA offline native mitmproxy adapter",
);
const texts = [
evidence.limitations,
result.limitations,
...result.records.map((record) => record.limitations),
].flat();
for (const literal of literals)
assert.ok(
texts.every((text) => !text.includes(literal)),
"Declared literal retained in authored limitations",
);
}
function assertBinary(record, pointer, bytes) {
const field = record.binary_fields.find((item) => item.pointer === pointer);
assert.ok(field, `Missing native byte field ${pointer}`);
assert.equal(field.state, "retained");
assert.equal(field.content_base64, bytes.toString("base64"));
assert.equal(field.bytes, bytes.length);
assert.equal(field.sha256, createHash("sha256").update(bytes).digest("hex"));
}