mirror of
https://github.com/morluto/rea.git
synced 2026-11-23 15:05:51 +00:00
* feat(browser): count credential-named members in HAR capture inspection inspect_web_network_capture now runs the credential-name scan that environment-build already applies over the selected HAR records and reports matches as limitations by pointer, never by value, together with the bodies the scan could not read. Values the decoder already excluded are not counted, and nothing is excluded on a name. Refs #8207 Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> * fix(browser): bound the listed credential-name pointers The limitation lists the pointers that fit within 1 KiB of text and says how many counted names are not listed, so one long body member name cannot inflate the limitation that summaries and record pages repeat. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> * fix(environment): bound the credential scan's values before parsing a body A body under the scan's 4 MiB text limit could still expand into millions of JSON values or form fields when parsed in the application process, and the inspection now runs the scan on every HAR capture. The scan counts the characters that can start a value (`[`, `{`, `,` for JSON, `&` for forms) before parsing and skips a body that could hold more than 524288, reporting it like a body over the text limit. Both consumers' messages and docs name the bound. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> * fix(web): charge every body and name/value list to one credential-scan budget The credential-name scan bounded each body's values on its own, so a capture of many compact bodies still expanded millions of values in the application process, and HAR header, query and form-parameter lists were copied twice without any bound. One 524288-value budget now covers the whole scan: a body's values are bounded from its text before it is parsed and a list is charged by its length before it is read in place, so what would pass the budget is reported as not scanned instead. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> * fix(web): read credential names in place and charge nothing for empty text The credential-name scan split each member, header and query name with two regex replacements, a lowercase copy and a split into a segment array before matching. A near-4 MiB alternating-case member name (`x-aAaA…`) therefore expanded into about two million segments: more than 192 MiB of heap in the application process after the decoder's containment had ended. `credentialNamed` now reads the name once in place with the same segmentation (case changes, digits, separators, and `toLowerCase`'s KELVIN SIGN and dotted capital I), keeping at most two segments no longer than the longest credential word, so a name of any length allocates no copies. `valueBound` started every text at one value, so an empty query, a bare `?`, an empty form and whitespace-only JSON were each charged a value they do not hold; once earlier content spent the budget, they were reported as unscanned in `credential-scan-incomplete`. JSON now holds its first value only after a non-whitespace character, and a form holds one field per non-empty `&`-separated piece after an optional leading `?`, as `URLSearchParams` and the query reader split it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * docs(browser): point the environment build link at the physical guide The guide lives at docs/agent-environments.md, so the relative link from docs/guides/web must climb two levels. The Documentation job failed on the dead link; vitepress build now completes. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> * test: drop the hard-coded timeout from the heap-bound name test The test finishes in about half a second, so the shared test deadline is enough, and a literal 60_000 bypasses the CI and coverage deadline scaling. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> * fix(web-network-capture): name an unavailable response source in the scan limitation The credential-name scan counts a response body whose retained source is unavailable as not scanned, and WebNetworkCaptureService reuses that scan. Its limitation text still listed only a cut body, text over 4 MiB, or past the value budget, so CLI and MCP reports gave none of those reasons for such a body. Name the unavailable source in the text, matching the environment-build warning, and cover it with a test that fails on the old wording. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
880 lines
30 KiB
TypeScript
880 lines
30 KiB
TypeScript
import { fileIdentity } from "../../tests/fixtures/evidenceSubject.js";
|
|
import { expect, it } from "vitest";
|
|
import { WebNetworkCaptureService } from "./WebNetworkCaptureService.js";
|
|
import { historicalHar } from "../../tests/fixtures/historicalHar.js";
|
|
import { err, ok } from "../domain/result.js";
|
|
import { projectAnalysisError } from "../domain/analysisErrorProjection.js";
|
|
import { parseEvidence } from "../domain/evidence.js";
|
|
import {
|
|
AnalysisCancelledError,
|
|
AnalysisOutputError,
|
|
} from "../domain/analysisErrorCore.js";
|
|
import {
|
|
webNetworkCaptureSchema,
|
|
type WebNetworkCapture,
|
|
} from "../domain/webNetworkCapture.js";
|
|
import {
|
|
AnalysisCleanupLease,
|
|
AnalysisCleanupLeaseGroup,
|
|
} from "../resources/AnalysisCleanupLease.js";
|
|
import { ProviderCleanupError } from "../domain/providerCleanupError.js";
|
|
import { analysisErrorWithCleanupFailure } from "../domain/analysisErrorCleanup.js";
|
|
|
|
it.each(["REDACTED", "[", "]", "/capture.har"])(
|
|
"excludes sensitive path text even when a replacement marker would collide: %s",
|
|
async (literal) => {
|
|
const report = fixture();
|
|
report.artifact.path = `/capture.har/${literal}`;
|
|
const result = await new WebNetworkCaptureService({
|
|
inspect: () => Promise.resolve(ok(report)),
|
|
}).inspect({
|
|
capture_path: report.artifact.path,
|
|
format: "har",
|
|
sensitive_values: [literal],
|
|
});
|
|
if (!result.ok) throw result.error;
|
|
expect(result.value.subject?.local_path).not.toContain(literal);
|
|
expect(result.value.parameters.capture_path).not.toContain(literal);
|
|
expect(
|
|
result.value.locations.every(
|
|
(location) =>
|
|
location.kind !== "artifact-path" || !location.path.includes(literal),
|
|
),
|
|
).toBe(true);
|
|
expect(fileIdentity(result.value.subject).sha256).toBe(
|
|
report.artifact.sha256,
|
|
);
|
|
},
|
|
);
|
|
|
|
it("records an explicit capture deadline and omits the default", async () => {
|
|
const seen: Array<number | undefined> = [];
|
|
const service = new WebNetworkCaptureService({
|
|
inspect: (input) => {
|
|
seen.push(input.capture_timeout_ms);
|
|
return Promise.resolve(ok(fixture()));
|
|
},
|
|
});
|
|
const explicit = await service.inspect({
|
|
capture_path: "/capture.har",
|
|
format: "har",
|
|
capture_timeout_ms: 250,
|
|
});
|
|
if (!explicit.ok) throw explicit.error;
|
|
expect(explicit.value.parameters.capture_timeout_ms).toBe(250);
|
|
const omitted = await service.inspect({
|
|
capture_path: "/capture.har",
|
|
format: "har",
|
|
});
|
|
if (!omitted.ok) throw omitted.error;
|
|
expect(omitted.value.parameters).not.toHaveProperty("capture_timeout_ms");
|
|
expect(seen).toEqual([250, undefined]);
|
|
});
|
|
|
|
it("keeps observed artifact identity when the entire sensitive path is excluded", async () => {
|
|
const result = await new WebNetworkCaptureService({
|
|
inspect: () => Promise.resolve(ok(fixture())),
|
|
}).inspect({
|
|
capture_path: "/capture.har",
|
|
format: "har",
|
|
sensitive_values: ["/", "[", "REDACTED"],
|
|
});
|
|
if (!result.ok) throw result.error;
|
|
expect(result.value.subject).toMatchObject({
|
|
local_path: "",
|
|
identity: { sha256: "a".repeat(64) },
|
|
});
|
|
expect(result.value.locations).toEqual([]);
|
|
});
|
|
|
|
const fixture = (): WebNetworkCapture => ({
|
|
decoder: { id: "test-historical-port", name: "test port", version: "1" },
|
|
container: {
|
|
reported: null,
|
|
numeric_literals: [],
|
|
redactions: [],
|
|
records_pointer: "/log/entries",
|
|
},
|
|
total_records: 1,
|
|
records: [
|
|
{
|
|
ordinal: 0,
|
|
location: { kind: "json-pointer", pointer: "/log/entries/0" },
|
|
reported: historicalHar().log.entries[0] ?? null,
|
|
numeric_literals: [],
|
|
binary_fields: [],
|
|
redactions: [],
|
|
limitations: [],
|
|
},
|
|
],
|
|
artifact: { path: "/capture.har", sha256: "a".repeat(64), bytes: 10 },
|
|
format: "har",
|
|
runtime_attribution: "unknown",
|
|
limitations: ["historical fixture"],
|
|
});
|
|
|
|
it.each(["a".repeat(64), "aaaa"])(
|
|
"marks an explicitly sensitive artifact digest unknown without inventing another identity: %s",
|
|
async (literal) => {
|
|
const result = await new WebNetworkCaptureService({
|
|
inspect: () => Promise.resolve(ok(fixture())),
|
|
}).inspect({
|
|
capture_path: "/capture.har",
|
|
format: "har",
|
|
sensitive_values: [literal],
|
|
});
|
|
if (!result.ok) throw result.error;
|
|
expect(result.value.subject.identity.kind).toBe("unknown");
|
|
expect(result.value.normalized_result).toMatchObject({
|
|
artifact: { path: "/capture.har", sha256: null, bytes: 10 },
|
|
});
|
|
expect(result.value.parameters.capture_path).toBe("/capture.har");
|
|
expect(result.value.locations).toEqual([
|
|
{ kind: "artifact-path", path: "/capture.har" },
|
|
]);
|
|
expect(parseEvidence(result.value)).toEqual(result.value);
|
|
},
|
|
);
|
|
|
|
it("projects the unknown-subject explanation before generating its Evidence ID", async () => {
|
|
const result = await new WebNetworkCaptureService({
|
|
inspect: () => Promise.resolve(ok(fixture())),
|
|
}).inspect({
|
|
capture_path: "/capture.har",
|
|
format: "har",
|
|
sensitive_values: ["a"],
|
|
});
|
|
if (!result.ok) throw result.error;
|
|
expect(result.value.subject.identity.kind).toBe("unknown");
|
|
expect(result.value.limitations).toContainEqual(
|
|
expect.stringMatching(/digest.*unknown.*Evidence/u),
|
|
);
|
|
expect(result.value.limitations.every((text) => !text.includes("a"))).toBe(
|
|
true,
|
|
);
|
|
expect(parseEvidence(result.value)).toEqual(result.value);
|
|
});
|
|
|
|
it("rejects an adapter omitting its original observed digest before caller projection", async () => {
|
|
const report = fixture();
|
|
report.artifact.sha256 = null;
|
|
const result = await new WebNetworkCaptureService({
|
|
inspect: () => Promise.resolve(ok(report)),
|
|
}).inspect({ capture_path: report.artifact.path, format: "har" });
|
|
expect(result.ok).toBe(false);
|
|
if (!result.ok) expect(result.error._tag).toBe("AnalysisOutputError");
|
|
});
|
|
|
|
it("retains valid completed capture Evidence on late cancellation and preserves invalid output", async () => {
|
|
const controller = new AbortController();
|
|
const report = fixture();
|
|
const late = await new WebNetworkCaptureService({
|
|
inspect: () => {
|
|
controller.abort();
|
|
return Promise.resolve(ok(report));
|
|
},
|
|
}).inspect(
|
|
{ capture_path: report.artifact.path, format: "har" },
|
|
{ signal: controller.signal },
|
|
);
|
|
expect(late.ok).toBe(false);
|
|
if (!late.ok) {
|
|
expect(late.error).toBeInstanceOf(AnalysisCancelledError);
|
|
expect(parseEvidence(late.error.partialObservation)).toMatchObject({
|
|
operation: "inspect_web_network_capture",
|
|
normalized_result: expect.objectContaining({ total_records: 1 }),
|
|
});
|
|
}
|
|
|
|
const malformed = fixture();
|
|
malformed.artifact.path = "/other.har";
|
|
const outputController = new AbortController();
|
|
const invalid = await new WebNetworkCaptureService({
|
|
inspect: () => {
|
|
outputController.abort();
|
|
return Promise.resolve(ok(malformed));
|
|
},
|
|
}).inspect(
|
|
{ capture_path: "/capture.har", format: "har" },
|
|
{ signal: outputController.signal },
|
|
);
|
|
expect(invalid.ok).toBe(false);
|
|
if (!invalid.ok) expect(invalid.error).toBeInstanceOf(AnalysisOutputError);
|
|
});
|
|
|
|
it("projects completed raw capture facts on failure through the sensitivity rules", async () => {
|
|
const report = fixture();
|
|
report.artifact.path = "/private-marker/capture.har";
|
|
report.limitations = ["private-marker producer limitation"];
|
|
const reason = { request: "cancel after decoder completion" };
|
|
const error = new AnalysisCancelledError("inspect_web_network_capture", {
|
|
cause: reason,
|
|
partialObservation: report,
|
|
});
|
|
const result = await new WebNetworkCaptureService({
|
|
inspect: () => Promise.resolve({ ok: false, error }),
|
|
}).inspect(
|
|
{
|
|
capture_path: report.artifact.path,
|
|
format: "har",
|
|
record_ordinals: [0],
|
|
sensitive_values: ["private-marker"],
|
|
},
|
|
undefined,
|
|
);
|
|
if (result.ok) throw new Error("Cancellation must remain the primary result");
|
|
expect(result.error._tag).toBe("AnalysisCancelledError");
|
|
expect(result.error.cause).toBe(reason);
|
|
const partial = parseEvidence(result.error.partialObservation);
|
|
expect(partial).toMatchObject({
|
|
operation: "inspect_web_network_capture",
|
|
parameters: { record_ordinals: [0] },
|
|
subject: { local_path: "" },
|
|
normalized_result: {
|
|
artifact: { path: "", sha256: "a".repeat(64) },
|
|
records: [{ ordinal: 0 }],
|
|
},
|
|
});
|
|
// Caller spellings belong to the CLI delivery, not the service.
|
|
expect(partial.caller_input_path).toBeUndefined();
|
|
expect(JSON.stringify(partial)).not.toContain("private-marker");
|
|
});
|
|
|
|
it("does not expose a malformed capture-shaped partial observation", async () => {
|
|
const malformed = fixture();
|
|
malformed.artifact.path = "/private-marker";
|
|
malformed.artifact.sha256 = "invalid";
|
|
const error = new AnalysisCancelledError("inspect_web_network_capture", {
|
|
partialObservation: malformed,
|
|
});
|
|
const result = await new WebNetworkCaptureService({
|
|
inspect: () => Promise.resolve({ ok: false, error }),
|
|
}).inspect({
|
|
capture_path: "/capture.har",
|
|
format: "har",
|
|
sensitive_values: ["private-marker"],
|
|
});
|
|
expect(result.ok).toBe(false);
|
|
if (!result.ok) expect(result.error.partialObservation).toBeUndefined();
|
|
});
|
|
|
|
it("does not recover a rejected capture from a cleanup primary", async () => {
|
|
const malformed = fixture();
|
|
malformed.artifact.sha256 = "invalid";
|
|
malformed.limitations = ["rejected-capture-only-marker"];
|
|
const primary = new AnalysisCancelledError("inspect_web_network_capture", {
|
|
partialObservation: malformed,
|
|
});
|
|
const cleanup = new ProviderCleanupError("fixture", ["workspace"], {});
|
|
const error = analysisErrorWithCleanupFailure(primary, cleanup);
|
|
const result = await new WebNetworkCaptureService({
|
|
inspect: async () => err(error),
|
|
}).inspect({ capture_path: "/capture.har", format: "har" });
|
|
if (result.ok) throw new Error("expected rejected capture");
|
|
const projected = projectAnalysisError(result.error);
|
|
expect(projected.code).toBe("cleanup_incomplete");
|
|
expect(JSON.stringify(projected)).not.toContain(
|
|
"rejected-capture-only-marker",
|
|
);
|
|
});
|
|
|
|
it("keeps a redacted cleanup error linked to its live owner through service close", async () => {
|
|
const report = fixture();
|
|
const cleanupPath = "/tmp/private-marker-capture-root";
|
|
const lease = new AnalysisCleanupLease(async () => {}, [cleanupPath]);
|
|
const original = new ProviderCleanupError(
|
|
"test-capture",
|
|
[cleanupPath],
|
|
{ reason: "private-marker cleanup remains pending" },
|
|
{
|
|
operation: "inspect_web_network_capture",
|
|
partialObservation: report,
|
|
},
|
|
);
|
|
new AnalysisCleanupLeaseGroup(original, [lease]);
|
|
const service = new WebNetworkCaptureService({
|
|
inspect: () => Promise.resolve(err(original)),
|
|
close: () => lease.release(),
|
|
});
|
|
const result = await service.inspect({
|
|
capture_path: report.artifact.path,
|
|
format: "har",
|
|
sensitive_values: ["private-marker"],
|
|
});
|
|
if (result.ok) throw new Error("Retained cleanup must remain a failure");
|
|
const returnedError = result.error;
|
|
expect(returnedError.cleanupIncomplete).toBe(true);
|
|
expect(returnedError.cleanupResources).toEqual([
|
|
"/tmp/[REDACTED]-capture-root",
|
|
]);
|
|
expect(JSON.stringify(projectAnalysisError(returnedError))).not.toContain(
|
|
"private-marker",
|
|
);
|
|
expect(parseEvidence(returnedError.partialObservation)).toMatchObject({
|
|
operation: "inspect_web_network_capture",
|
|
});
|
|
|
|
await service.close();
|
|
expect(returnedError.cleanupIncomplete).toBe(false);
|
|
expect(returnedError.cleanupResources).toEqual([]);
|
|
});
|
|
|
|
it.each([
|
|
["producer"],
|
|
["artifact"],
|
|
["coordinates"],
|
|
["producer", "REDACTED", "…"],
|
|
["producer", "[", "]"],
|
|
])(
|
|
"excludes declared literals from all authored limitations: %j",
|
|
async (...literals) => {
|
|
const report = fixture();
|
|
report.artifact.path = "/artifact/capture.har";
|
|
report.limitations = [
|
|
"Historical producer artifact coordinates are retained.",
|
|
];
|
|
const first = report.records[0];
|
|
if (first === undefined) throw new Error("fixture missing record");
|
|
first.limitations = ["This is producer-decoded artifact evidence."];
|
|
const result = await new WebNetworkCaptureService({
|
|
inspect: () => Promise.resolve(ok(report)),
|
|
}).inspect({
|
|
capture_path: report.artifact.path,
|
|
format: "har",
|
|
sensitive_values: literals,
|
|
});
|
|
if (!result.ok) throw result.error;
|
|
const projected = webNetworkCaptureSchema.parse(
|
|
result.value.normalized_result,
|
|
);
|
|
const texts = [
|
|
result.value.limitations,
|
|
projected.limitations,
|
|
...projected.records.map((record) => record.limitations),
|
|
].flat();
|
|
for (const literal of literals)
|
|
expect(texts.every((text) => !text.includes(literal))).toBe(true);
|
|
expect(fileIdentity(result.value.subject).sha256).toBe(
|
|
report.artifact.sha256,
|
|
);
|
|
expect(projected.decoder).toEqual(report.decoder);
|
|
expect(projected.records[0]?.reported).toEqual(first.reported);
|
|
},
|
|
);
|
|
|
|
it("keeps explicitly sensitive values out of Evidence parameters and preserves historical authority", async () => {
|
|
const service = new WebNetworkCaptureService({
|
|
inspect: () => Promise.resolve(ok(fixture())),
|
|
});
|
|
const result = await service.inspect({
|
|
capture_path: "/capture.har",
|
|
format: "har",
|
|
sensitive_values: ["explicit-private-value"],
|
|
});
|
|
if (!result.ok) throw result.error;
|
|
expect(result.value.parameters).toMatchObject({
|
|
sensitive_value_count: 1,
|
|
record_ordinals: [0],
|
|
});
|
|
expect(JSON.stringify(result.value)).not.toContain("explicit-private-value");
|
|
expect(result.value.authority).toBe("historical-reference");
|
|
expect(result.value.provider).toEqual(fixture().decoder);
|
|
});
|
|
|
|
it.each(["har", "ha"])(
|
|
"omits a format parameter containing a declared sensitive literal: %s",
|
|
async (literal) => {
|
|
const result = await new WebNetworkCaptureService({
|
|
inspect: () => Promise.resolve(ok(fixture())),
|
|
}).inspect({
|
|
capture_path: "/capture.har",
|
|
format: "har",
|
|
sensitive_values: [literal],
|
|
});
|
|
if (!result.ok) throw result.error;
|
|
expect(result.value.parameters).not.toHaveProperty("format");
|
|
expect(JSON.stringify(result.value.parameters)).not.toContain(literal);
|
|
expect(result.value.normalized_result).toMatchObject({ format: "har" });
|
|
},
|
|
);
|
|
|
|
it.each([
|
|
{ record_ordinals: [0, 0] },
|
|
{ record_ordinals: [1] },
|
|
{ capture_path: "relative.har" },
|
|
])("rejects invalid caller selection %j", async (extra) => {
|
|
const result = await new WebNetworkCaptureService({
|
|
inspect: () => Promise.resolve(ok(fixture())),
|
|
}).inspect({ capture_path: "/capture.har", format: "har", ...extra });
|
|
expect(result.ok).toBe(false);
|
|
if (!result.ok) expect(result.error._tag).toBe("AnalysisInputError");
|
|
});
|
|
|
|
it("reports an ordinal past the capture's records as an out-of-range selection", async () => {
|
|
const report = fixture();
|
|
const result = await new WebNetworkCaptureService({
|
|
inspect: () => Promise.resolve(ok(report)),
|
|
}).inspect({
|
|
capture_path: "/capture.har",
|
|
format: "har",
|
|
record_ordinals: [0, 5],
|
|
});
|
|
if (result.ok) throw new Error("expected an out-of-range ordinal");
|
|
expect(projectAnalysisError(result.error)).toMatchObject({
|
|
code: "invalid_request",
|
|
details: {
|
|
issues: [
|
|
{
|
|
path: ["record_ordinals", 1],
|
|
reason: "out_of_range",
|
|
minimum: 0,
|
|
maximum: report.total_records - 1,
|
|
},
|
|
],
|
|
},
|
|
});
|
|
});
|
|
|
|
it("rejects a port changing the artifact identity or producer ordinal sequence", async () => {
|
|
const report = fixture();
|
|
report.artifact.path = "/different.har";
|
|
const result = await new WebNetworkCaptureService({
|
|
inspect: () => Promise.resolve(ok(report)),
|
|
}).inspect({ capture_path: "/capture.har", format: "har" });
|
|
expect(result.ok).toBe(false);
|
|
if (!result.ok) expect(result.error._tag).toBe("AnalysisOutputError");
|
|
});
|
|
|
|
it("applies an explicit sensitive path literal after verifying the selected artifact", async () => {
|
|
const result = await new WebNetworkCaptureService({
|
|
inspect: () => Promise.resolve(ok(fixture())),
|
|
}).inspect({
|
|
capture_path: "/capture.har",
|
|
format: "har",
|
|
sensitive_values: ["capture.har"],
|
|
});
|
|
if (!result.ok) throw result.error;
|
|
expect(JSON.stringify(result.value)).not.toContain("capture.har");
|
|
expect(result.value.parameters.capture_path).toBe("");
|
|
});
|
|
|
|
it.each(["log", "entries"])(
|
|
"excludes a sensitive producer coordinate without inventing a replacement: %s",
|
|
async (literal) => {
|
|
const result = await new WebNetworkCaptureService({
|
|
inspect: () => Promise.resolve(ok(fixture())),
|
|
}).inspect({
|
|
capture_path: "/capture.har",
|
|
format: "har",
|
|
sensitive_values: [literal],
|
|
});
|
|
if (!result.ok) throw result.error;
|
|
expect(result.value.normalized_result).toMatchObject({
|
|
container: { records_pointer: null },
|
|
records: [
|
|
{
|
|
ordinal: 0,
|
|
location: { kind: "unknown", reason: "explicit-sensitive-value" },
|
|
},
|
|
],
|
|
});
|
|
expect(JSON.stringify(result.value)).not.toContain(literal);
|
|
},
|
|
);
|
|
|
|
it("excludes an escaped sensitive sidecar coordinate while preserving its reported field", async () => {
|
|
const report = fixture();
|
|
report.container.reported = { "field/name": 42 };
|
|
report.container.numeric_literals = [
|
|
{ pointer: "/field~1name", producer_type: "json-number", literal: "42" },
|
|
];
|
|
report.container.redactions = [
|
|
{ pointer: "/field~1name", reason: "explicit-sensitive-value" },
|
|
];
|
|
const result = await new WebNetworkCaptureService({
|
|
inspect: () => Promise.resolve(ok(report)),
|
|
}).inspect({
|
|
capture_path: "/capture.har",
|
|
format: "har",
|
|
sensitive_values: ["~1"],
|
|
});
|
|
if (!result.ok) throw result.error;
|
|
expect(result.value.normalized_result).toMatchObject({
|
|
container: {
|
|
reported: { "field/name": 42 },
|
|
numeric_literals: [],
|
|
redactions: [],
|
|
},
|
|
});
|
|
expect(JSON.stringify(result.value)).not.toContain("~1");
|
|
});
|
|
|
|
it.each([
|
|
{ literal: "12345", input: { record_ordinals: [12345] } },
|
|
{ literal: "Each", input: { record_ordinals: [0, 0] } },
|
|
{ literal: "absolute", input: { capture_path: "relative.har" } },
|
|
{ literal: "secret", input: { mysecret: true } },
|
|
])(
|
|
"excludes declared text from application-generated input errors: $literal",
|
|
async ({ literal, input }) => {
|
|
const result = await new WebNetworkCaptureService({
|
|
inspect: () => Promise.resolve(ok(fixture())),
|
|
}).inspect({
|
|
capture_path: "/capture.har",
|
|
format: "har",
|
|
sensitive_values: [literal],
|
|
...input,
|
|
});
|
|
if (result.ok) throw new Error("Expected invalid selection");
|
|
expect(result.error._tag).toBe("AnalysisInputError");
|
|
expect(JSON.stringify(projectAnalysisError(result.error))).not.toContain(
|
|
literal,
|
|
);
|
|
},
|
|
);
|
|
|
|
it.each(["12345", "67890", "234", "789"])(
|
|
"excludes sensitive native byte-range coordinates: %s",
|
|
async (literal) => {
|
|
const report = fixture();
|
|
report.format = "mitmproxy";
|
|
report.artifact.path = "/capture.mitm";
|
|
const record = report.records[0];
|
|
if (record === undefined) throw new Error("record missing");
|
|
record.location = { kind: "byte-range", offset: 12345, bytes: 67890 };
|
|
const result = await new WebNetworkCaptureService({
|
|
inspect: () => Promise.resolve(ok(report)),
|
|
}).inspect({
|
|
capture_path: report.artifact.path,
|
|
format: "mitmproxy",
|
|
sensitive_values: [literal],
|
|
});
|
|
if (!result.ok) throw result.error;
|
|
expect(result.value.normalized_result).toMatchObject({
|
|
records: [
|
|
{ location: { kind: "unknown", reason: "explicit-sensitive-value" } },
|
|
],
|
|
});
|
|
expect(result.value.raw_result).toBeNull();
|
|
expect(parseEvidence(result.value)).toEqual(result.value);
|
|
},
|
|
);
|
|
|
|
it("preserves a pre-abort reason without entering the capture adapter", async () => {
|
|
const reason = new Error("stop historical capture inspection");
|
|
const controller = new AbortController();
|
|
controller.abort(reason);
|
|
let providerCalls = 0;
|
|
const service = new WebNetworkCaptureService({
|
|
inspect: async () => {
|
|
providerCalls += 1;
|
|
return ok(fixture());
|
|
},
|
|
});
|
|
|
|
const result = await service.inspect(
|
|
{
|
|
capture_path: "/capture.har",
|
|
format: "har",
|
|
sensitive_values: [],
|
|
},
|
|
{ signal: controller.signal },
|
|
);
|
|
|
|
expect(result.ok).toBe(false);
|
|
if (!result.ok) {
|
|
expect(result.error).toBeInstanceOf(AnalysisCancelledError);
|
|
expect(result.error.cause).toBe(reason);
|
|
}
|
|
expect(providerCalls).toBe(0);
|
|
});
|
|
|
|
it("reports malformed retained facts while keeping their cleanup owner retryable", async () => {
|
|
const report = fixture();
|
|
report.artifact.sha256 = "invalid";
|
|
const lease = new AnalysisCleanupLease(async () => {}, [
|
|
"/tmp/private-marker-root",
|
|
]);
|
|
const cleanup = new ProviderCleanupError(
|
|
"test-historical-port",
|
|
["/tmp/private-marker-root"],
|
|
{ reason: "close denied" },
|
|
{ operation: "inspect_web_network_capture", partialObservation: report },
|
|
);
|
|
new AnalysisCleanupLeaseGroup(cleanup, [lease]);
|
|
const service = new WebNetworkCaptureService({
|
|
inspect: async () => err(cleanup),
|
|
close: async () => {
|
|
await lease.release();
|
|
},
|
|
});
|
|
const result = await service.inspect({
|
|
capture_path: "/capture.har",
|
|
format: "har",
|
|
sensitive_values: ["private-marker"],
|
|
});
|
|
if (result.ok) throw new Error("Expected malformed retained evidence");
|
|
expect(result.error.cleanupIncomplete).toBe(true);
|
|
expect(result.error.cleanupPrimary).toBeInstanceOf(AnalysisOutputError);
|
|
expect(result.error.partialObservation).toBeUndefined();
|
|
expect(JSON.stringify(projectAnalysisError(result.error))).not.toContain(
|
|
"private-marker",
|
|
);
|
|
await service.close();
|
|
expect(result.error.cleanupIncomplete).toBe(false);
|
|
expect(projectAnalysisError(result.error).code).toBe("unreadable_output");
|
|
});
|
|
|
|
it("rejects relative descriptor paths before starting a capture decoder", async () => {
|
|
const service = new WebNetworkCaptureService({
|
|
inspect: () => {
|
|
throw new Error("Invalid input must fail before provider admission");
|
|
},
|
|
});
|
|
const result = await service.inspect({
|
|
capture_path: "/capture.har",
|
|
format: "har",
|
|
record_ordinals: [0],
|
|
protobuf: {
|
|
schema: {
|
|
descriptor_set_path: "relative.pb",
|
|
message_type: "capture.Message",
|
|
},
|
|
},
|
|
});
|
|
if (result.ok) throw new Error("Expected typed invalid input");
|
|
expect(projectAnalysisError(result.error)).toMatchObject({
|
|
code: "invalid_request",
|
|
details: {
|
|
issues: [{ path: ["protobuf", "schema", "descriptor_set_path"] }],
|
|
},
|
|
});
|
|
});
|
|
|
|
/** HAR-shaped record whose credential-named members hold values the decoder retains. */
|
|
const credentialNamedReport = (): WebNetworkCapture => {
|
|
const report = fixture();
|
|
report.records[0]!.reported = {
|
|
request: {
|
|
method: "POST",
|
|
url: "https://example.test/login?api_key=demo-key",
|
|
headers: [
|
|
{ name: "Authorization", value: null },
|
|
{ name: "X-Demo-Token", value: "demo-token" },
|
|
{ name: "Accept", value: "application/json" },
|
|
],
|
|
queryString: [{ name: "api_key", value: "demo-key" }],
|
|
postData: {
|
|
mimeType: "application/json",
|
|
text: '{"user":"demo","password":"demo-pass"}',
|
|
},
|
|
},
|
|
response: {
|
|
status: 200,
|
|
headers: [{ name: "Set-Cookie", value: null }],
|
|
content: {
|
|
mimeType: "application/json",
|
|
text: '{"session_token":"demo-session"}',
|
|
},
|
|
},
|
|
};
|
|
return report;
|
|
};
|
|
|
|
const credentialNameLimitation = (limitations: readonly string[]) =>
|
|
limitations.filter((text) => text.includes("look like credentials"));
|
|
|
|
it("counts credential-named members that hold a retained value, by pointer and never by value", async () => {
|
|
const result = await new WebNetworkCaptureService({
|
|
inspect: () => Promise.resolve(ok(credentialNamedReport())),
|
|
}).inspect({ capture_path: "/capture.har", format: "har" });
|
|
if (!result.ok) throw result.error;
|
|
const [limitation, ...others] = credentialNameLimitation(
|
|
result.value.limitations,
|
|
);
|
|
expect(others).toEqual([]);
|
|
expect(limitation).toMatch(/^4 /);
|
|
for (const pointer of [
|
|
"record 0 request header X-Demo-Token",
|
|
"record 0 request query api_key",
|
|
"record 0 request /password",
|
|
"record 0 response /session_token",
|
|
])
|
|
expect(limitation).toContain(pointer);
|
|
// Redacted standard credentials and ordinary names are not counted.
|
|
expect(limitation).not.toContain("Authorization");
|
|
expect(limitation).not.toContain("Accept");
|
|
for (const value of ["demo-token", "demo-key", "demo-pass", "demo-session"])
|
|
expect(limitation).not.toContain(value);
|
|
expect(result.value.normalized_result).toMatchObject({
|
|
limitations: expect.arrayContaining([limitation]),
|
|
});
|
|
expect(parseEvidence(result.value)).toEqual(result.value);
|
|
});
|
|
|
|
it("bounds the listed pointers so a long member name cannot inflate the limitation", async () => {
|
|
const report = credentialNamedReport();
|
|
const long = "k".repeat(2048);
|
|
report.records[0]!.reported = {
|
|
request: {
|
|
method: "POST",
|
|
url: "https://example.test/login",
|
|
postData: {
|
|
mimeType: "application/json",
|
|
text: JSON.stringify({
|
|
[long]: { token: "demo-token" },
|
|
password: "p",
|
|
}),
|
|
},
|
|
},
|
|
response: { status: 204 },
|
|
};
|
|
const result = await new WebNetworkCaptureService({
|
|
inspect: () => Promise.resolve(ok(report)),
|
|
}).inspect({ capture_path: "/capture.har", format: "har" });
|
|
if (!result.ok) throw result.error;
|
|
const [limitation] = credentialNameLimitation(result.value.limitations);
|
|
expect(limitation).toMatch(/^2 /);
|
|
expect(limitation).toContain("record 0 request /password");
|
|
expect(limitation).toContain("1 more not listed");
|
|
expect(limitation).not.toContain(long);
|
|
expect(limitation!.length).toBeLessThan(1024 + 400);
|
|
});
|
|
|
|
it("adds no credential-name limitation when no counted name holds a value", async () => {
|
|
const result = await new WebNetworkCaptureService({
|
|
inspect: () => Promise.resolve(ok(fixture())),
|
|
}).inspect({ capture_path: "/capture.har", format: "har" });
|
|
if (!result.ok) throw result.error;
|
|
expect(credentialNameLimitation(result.value.limitations)).toEqual([]);
|
|
});
|
|
|
|
it("scans only HAR records for credential names", async () => {
|
|
const report = credentialNamedReport();
|
|
report.format = "mitmproxy";
|
|
const result = await new WebNetworkCaptureService({
|
|
inspect: () => Promise.resolve(ok(report)),
|
|
}).inspect({ capture_path: "/capture.har", format: "mitmproxy" });
|
|
if (!result.ok) throw result.error;
|
|
expect(credentialNameLimitation(result.value.limitations)).toEqual([]);
|
|
});
|
|
|
|
it("reports a body whose values would expand past the credential-name scan's bound", async () => {
|
|
const report = credentialNamedReport();
|
|
report.records[0]!.reported = {
|
|
request: { method: "GET", url: "https://example.test/expansive" },
|
|
response: {
|
|
status: 200,
|
|
content: {
|
|
mimeType: "application/json",
|
|
text: `[${"[],".repeat(300_000)}{"password":"x"}]`,
|
|
},
|
|
},
|
|
};
|
|
const result = await new WebNetworkCaptureService({
|
|
inspect: () => Promise.resolve(ok(report)),
|
|
}).inspect({ capture_path: "/capture.har", format: "har" });
|
|
if (!result.ok) throw result.error;
|
|
expect(credentialNameLimitation(result.value.limitations)).toEqual([]);
|
|
const skipped = result.value.limitations.filter((text) =>
|
|
text.includes("were not scanned"),
|
|
);
|
|
expect(skipped).toHaveLength(1);
|
|
expect(skipped[0]).toMatch(/^1 /);
|
|
expect(skipped[0]).toContain("record 0 response");
|
|
});
|
|
|
|
it("shares the credential-name scan's value budget across the selected records", async () => {
|
|
const expansive = (path: string) => ({
|
|
request: { method: "GET", url: `https://example.test/${path}` },
|
|
response: {
|
|
status: 200,
|
|
content: {
|
|
mimeType: "application/json",
|
|
text: `[${"1,".repeat(300_000)}{"password":"x"}]`,
|
|
},
|
|
},
|
|
});
|
|
const base = credentialNamedReport();
|
|
const first = { ...base.records[0]!, reported: expansive("first") };
|
|
const report: WebNetworkCapture = {
|
|
...base,
|
|
total_records: 2,
|
|
records: [
|
|
first,
|
|
{
|
|
...first,
|
|
ordinal: 1,
|
|
location: { kind: "json-pointer", pointer: "/log/entries/1" },
|
|
reported: expansive("second"),
|
|
},
|
|
],
|
|
};
|
|
const result = await new WebNetworkCaptureService({
|
|
inspect: () => Promise.resolve(ok(report)),
|
|
}).inspect({ capture_path: "/capture.har", format: "har" });
|
|
if (!result.ok) throw result.error;
|
|
const [found] = credentialNameLimitation(result.value.limitations);
|
|
expect(found).toMatch(/^1 /);
|
|
expect(found).toContain("record 0 response /300000/password");
|
|
const skipped = result.value.limitations.filter((text) =>
|
|
text.includes("were not scanned"),
|
|
);
|
|
expect(skipped).toHaveLength(1);
|
|
expect(skipped[0]).toMatch(/^1 /);
|
|
expect(skipped[0]).toContain("record 1 response");
|
|
});
|
|
|
|
it("reports a response body whose retained source is unavailable", async () => {
|
|
const report = credentialNamedReport();
|
|
// A base64 body with no retained binary field has no source to decode.
|
|
report.records[0]!.reported = {
|
|
request: { method: "GET", url: "https://example.test/unretained" },
|
|
response: {
|
|
status: 200,
|
|
content: {
|
|
mimeType: "application/json",
|
|
text: "eyJwYXNzd29yZCI6IngifQ==",
|
|
encoding: "base64",
|
|
},
|
|
},
|
|
};
|
|
const result = await new WebNetworkCaptureService({
|
|
inspect: () => Promise.resolve(ok(report)),
|
|
}).inspect({ capture_path: "/capture.har", format: "har" });
|
|
if (!result.ok) throw result.error;
|
|
expect(credentialNameLimitation(result.value.limitations)).toEqual([]);
|
|
const skipped = result.value.limitations.filter((text) =>
|
|
text.includes("were not scanned"),
|
|
);
|
|
expect(skipped).toHaveLength(1);
|
|
expect(skipped[0]).toMatch(/^1 /);
|
|
expect(skipped[0]).toContain("record 0 response");
|
|
expect(skipped[0]).toContain("retained source is unavailable");
|
|
});
|
|
|
|
it("reports bodies the credential-name scan could not read", async () => {
|
|
const report = credentialNamedReport();
|
|
report.records[0]!.reported = {
|
|
request: { method: "GET", url: "https://example.test/large" },
|
|
response: {
|
|
status: 200,
|
|
content: {
|
|
mimeType: "application/json",
|
|
text: `{"password":"${"x".repeat(4 * 1024 * 1024)}"}`,
|
|
},
|
|
},
|
|
};
|
|
const result = await new WebNetworkCaptureService({
|
|
inspect: () => Promise.resolve(ok(report)),
|
|
}).inspect({ capture_path: "/capture.har", format: "har" });
|
|
if (!result.ok) throw result.error;
|
|
expect(credentialNameLimitation(result.value.limitations)).toEqual([]);
|
|
const skipped = result.value.limitations.filter((text) =>
|
|
text.includes("were not scanned"),
|
|
);
|
|
expect(skipped).toHaveLength(1);
|
|
expect(skipped[0]).toMatch(/^1 /);
|
|
expect(skipped[0]).toContain("record 0 response");
|
|
});
|