feat(browser): inspect historical web network captures (#992)

* feat(browser): inspect retained HAR and mitmproxy capture evidence

Add independent offline format adapters, inline CLI/MCP evidence, bounded
owned decoder processes, and real producer verification.

Refs #882

Assisted-by: Yukari (gensokyo-skills:yukari-boundary-analysis)
Assisted-by: Kogasa (gensokyo-skills:kogasa-surprise-testing)

* fix(browser): preserve capture boundaries during cancellation and decoding

Honor cancellation during owned-command cleanup, retain opaque HAR
extension content, and clean verifier resources on every failure path.

Refs #882

Assisted-by: Yukari (gensokyo-skills:yukari-boundary-analysis)
Assisted-by: Kogasa (gensokyo-skills:kogasa-surprise-testing)

* fix(browser): exclude sensitive capture fields across representations

Normalize known native transport URLs for both bytes and strings, including backups. Exclude explicitly sensitive text fields with null so redaction markers cannot reintroduce declared literals. Cover both provider identities in catalog conformance.

Refs #882

Assisted-by: Yukari (gensokyo-skills:yukari-boundary-analysis)
Assisted-by: Kogasa (gensokyo-skills:kogasa-surprise-testing)

* fix(browser): exclude sensitive capture identities and bound diagnostics

* fix(browser): validate excluded HAR subtrees before projection

* fix(browser): distinguish capture input limits from decoder failures

* fix(browser): preserve decoder failure categories

* fix(browser): validate excluded native credential values

* test(browser): report capture verifier assertions directly

* fix(browser): classify native representation failures precisely

* fix(browser): exclude URL userinfo after header whitespace

* fix(browser): preserve capture evidence and failure boundaries

* fix(browser): exclude sensitive literals from capture limitations

* test(browser): check limitation values without JSON syntax

* fix(browser): preserve acquisition integrity and exclude selection diagnostics

* fix(browser): preserve capture acquisition and declaration boundaries

* fix(mcp): project sensitive capture argument validation

* fix: preserve capture bytes and prelaunch cancellation

* fix(browser): exclude sensitive HAR record ancestry

* fix(browser): preserve transport URL and parameter exclusions

* fix(browser): preserve numeric redaction and canonical capture provenance

* fix(browser): cover derived capture values and ownership preparation deadlines

* fix(browser): exclude explicitly sensitive artifact digests

* chore: regenerate generated metadata

* fix(browser): reject lossy HAR member normalization

* test(browser): assert the advertised unsupported-profile outcome

* fix(browser): preserve JSON visitor nesting failure locations

* fix(browser): exclude explicitly sensitive native source ranges

---------

Co-authored-by: Kaoru0822 <208281322+kaoru0822-kitauji@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
This commit is contained in:
Ysopwnable
2026-10-08 07:53:41 +08:00
committed by GitHub
co-authored by Kaoru0822 github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
parent 6e4535c101
commit 2bf3663f65
77 changed files with 6315 additions and 47 deletions
+244
View File
@@ -0,0 +1,244 @@
import { createHash } from "node:crypto";
import { isSafeNumber, LosslessNumber } from "lossless-json";
import { parseHarJson } from "./HarJson.js";
import type { JsonValue } from "../../domain/jsonValue.js";
import {
WEB_NETWORK_CAPTURE_LIMITS,
type WebNetworkCapture,
type WebNetworkCaptureRecord,
} from "../../domain/webNetworkCapture.js";
import { CaptureFormatError } from "./CaptureFormatError.js";
import {
CaptureRedaction,
captureObject,
capturePointer,
} from "./CaptureRedaction.js";
import { createHarValidator } from "./HarSchema.js";
import { HAR_CAPTURE_PROVIDER_IDENTITY } from "./CaptureRelease.js";
type DecodedCapture = Omit<
WebNetworkCapture,
"artifact" | "format" | "runtime_attribution" | "limitations"
>;
type Projection = Pick<
WebNetworkCaptureRecord,
"reported" | "numeric_literals" | "binary_fields" | "redactions"
>;
/** Decode a retained HAR with exact numeric lexemes and unchanged upstream schema validation. */
export const decodeHarCapture = (
text: string,
sensitiveValues: readonly string[],
): DecodedCapture => {
const raw = parseHarJson(text);
const validation = projectHar(raw, [], false);
createHarValidator()(validation.reported);
if (
!captureObject(raw) ||
!captureObject(raw.log) ||
!Array.isArray(raw.log.entries)
)
throw new CaptureFormatError("format", "HAR must contain log.entries.");
if (raw.log.version !== "1.2")
throw new CaptureFormatError(
"unsupported",
"Only the pinned HAR 1.2 schema profile is supported.",
"/log/version",
);
const entries = raw.log.entries;
const recordsExcluded = ["log", "entries"].some((key) =>
sensitiveValues.some((literal) => key.includes(literal)),
);
const container = projectHar(
{ ...raw, log: { ...raw.log, entries: null } },
sensitiveValues,
true,
);
const records = entries.map((entry, ordinal): WebNetworkCaptureRecord => {
// Validate each original record even when a structural ancestor excludes it.
const projected = projectHar(entry, sensitiveValues, true);
const projection: Projection = recordsExcluded
? {
reported: null,
numeric_literals: [],
binary_fields: [],
redactions: [{ pointer: "", reason: "explicit-sensitive-value" }],
}
: projected;
return {
ordinal,
location: recordsExcluded
? { kind: "unknown", reason: "explicit-sensitive-value" }
: { kind: "json-pointer", pointer: `/log/entries/${ordinal}` },
...projection,
limitations: [
"HAR text is producer-decoded Unicode. Original body bytes remain unknown unless content explicitly uses valid base64; declared sizes are preserved independently of retained bytes. mitmproxy 12.2.3 SaveHar may report null optional postData.text; that null is retained and omitted only from the upstream validation copy.",
...(recordsExcluded
? [
"This record's payload and sidecars are excluded because its structural ancestor property was explicitly marked sensitive.",
]
: []),
],
};
});
return {
decoder: { ...HAR_CAPTURE_PROVIDER_IDENTITY },
container: {
reported: container.reported,
numeric_literals: container.numeric_literals,
redactions: container.redactions,
records_pointer: recordsExcluded ? null : "/log/entries",
},
total_records: records.length,
records,
};
};
const projectHar = (
value: unknown,
sensitiveValues: readonly string[],
redact: boolean,
): Projection => {
const redactor = new CaptureRedaction(sensitiveValues);
const numeric_literals: Projection["numeric_literals"] = [];
const binary_fields: Projection["binary_fields"] = [];
const visit = (
item: unknown,
pointer: string,
depth: number,
parent?: Readonly<Record<string, unknown>>,
): JsonValue => {
if (depth > WEB_NETWORK_CAPTURE_LIMITS.depth)
throw new CaptureFormatError(
"input-limit",
"HAR exceeds the 64-level complete-evidence nesting budget.",
pointer,
);
if (
redact &&
parent !== undefined &&
redactor.harCredential(pointer, parent)
)
return redactor.credential(pointer);
if (item instanceof LosslessNumber) {
if (redact && redactor.text(item.value, pointer) === null) return null;
numeric_literals.push({
pointer,
producer_type: "json-number",
literal: item.value,
});
return isSafeNumber(item.value) ? Number(item.value) : null;
}
if (typeof item === "string") {
if (
redact &&
pointer === "/response/content/text" &&
parent?.encoding === "base64"
) {
const bytes = decodeHarBase64(item, pointer);
const contentBase64 = bytes.toString("base64");
const sha256 = createHash("sha256").update(bytes).digest("hex");
const hidden =
redactor.sensitiveBytes(bytes) ||
sensitiveValues.some(
(literal) =>
item.includes(literal) ||
contentBase64.includes(literal) ||
sha256.includes(literal),
);
binary_fields.push(
hidden
? {
pointer,
representation: "har-base64-content",
state: "redacted",
content_base64: null,
bytes: null,
sha256: null,
}
: {
pointer,
representation: "har-base64-content",
state: "retained",
content_base64: contentBase64,
bytes: bytes.length,
sha256,
},
);
if (hidden) {
redactor.redactions.push({
pointer,
reason: "explicit-sensitive-value",
});
return null;
}
}
return redact
? redactor.text(
item,
pointer,
redactor.harTransportUrl(pointer, parent),
)
: item;
}
if (item === null || typeof item === "boolean") return item;
if (Array.isArray(item))
return item.map((child, index) =>
visit(child, capturePointer(pointer, String(index)), depth + 1),
);
if (captureObject(item))
return Object.fromEntries(
Object.entries(item).flatMap(([key, child]) => {
if (
redact &&
sensitiveValues.some((literal) => key.includes(literal))
) {
const beforeNumbers = numeric_literals.length;
const beforeBinaries = binary_fields.length;
const beforeRedactions = redactor.redactions.length;
visit(child, capturePointer(pointer, key), depth + 1, item);
numeric_literals.splice(beforeNumbers);
binary_fields.splice(beforeBinaries);
redactor.redactions.splice(beforeRedactions);
redactor.redactions.push({
pointer,
reason: "explicit-sensitive-value",
scope: "property-name",
});
return [];
}
return [
[key, visit(child, capturePointer(pointer, key), depth + 1, item)],
];
}),
);
throw new CaptureFormatError(
"format",
"HAR parser returned a non-JSON value.",
pointer,
);
};
return {
reported: visit(value, "", 0),
numeric_literals,
binary_fields,
redactions: redactor.redactions,
};
};
const decodeHarBase64 = (value: string, pointer: string): Buffer => {
const compact = value.replace(/[\t\n\r ]/g, "");
const bytes = Buffer.from(compact, "base64");
if (
!/^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/.test(
compact,
) ||
bytes.toString("base64") !== compact
)
throw new CaptureFormatError(
"format",
"HAR content declares base64 but its text is not valid canonical base64 (ASCII whitespace is allowed).",
pointer,
);
return bytes;
};