Files
rea/src/browser/history/CaptureRedaction.ts
T
YsopwnableGitHubKaoru0822github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
2bf3663f65 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>
2026-10-08 07:53:41 +08:00

94 lines
3.4 KiB
TypeScript

import type { WebNetworkCaptureRecord } from "../../domain/webNetworkCapture.js";
type Redaction = WebNetworkCaptureRecord["redactions"][number];
const CREDENTIAL_HEADERS = new Set([
"authorization",
"proxy-authorization",
"cookie",
"set-cookie",
]);
/** Literal redaction and structural authentication exclusions preserve unrelated local evidence. */
export class CaptureRedaction {
readonly redactions: Redaction[] = [];
readonly #sensitiveByteValues: readonly Buffer[];
constructor(readonly sensitiveValues: readonly string[]) {
// An unpaired surrogate has no UTF-8 representation. Buffer.from would
// invent replacement-character bytes and incorrectly discard real evidence.
this.#sensitiveByteValues = sensitiveValues.flatMap((value) => {
const encoded = Buffer.from(value, "utf8");
return encoded.toString("utf8") === value ? [encoded] : [];
});
}
/** Preserve exact text unless explicitly sensitive or known transport URL userinfo. */
text(value: string, pointer: string, transportUrl = false): string | null {
let result = value;
if (transportUrl) {
const match =
/^([\x00-\x20]*(?:[a-z][a-z0-9+.-]*:)?\/\/)([^/?#]*@)/i.exec(result);
if (match !== null) {
result = `${match[1]}${result.slice(match[0].length)}`;
this.redactions.push({ pointer, reason: "transport-credential" });
}
}
if (
this.sensitiveValues.some(
(literal) => value.includes(literal) || result.includes(literal),
)
) {
this.redactions.push({ pointer, reason: "explicit-sensitive-value" });
return null;
}
return result;
}
/** Remove a credential field's value without retaining its digest or encoded form. */
credential(pointer: string): null {
this.redactions.push({ pointer, reason: "transport-credential" });
return null;
}
/** Known HAR credential surfaces; unknown extensions are ordinary caller-selected evidence. */
harCredential(
pointer: string,
parent: Readonly<Record<string, unknown>>,
): boolean {
return (
(/^\/(request|response)\/headers\/\d+\/value$/.test(pointer) &&
typeof parent.name === "string" &&
CREDENTIAL_HEADERS.has(parent.name.toLowerCase())) ||
/^\/(request|response)\/cookies\/\d+\/value$/.test(pointer)
);
}
/** Only actual request/redirect fields and known transport header URLs declare URL semantics. */
harTransportUrl(
pointer: string,
parent?: Readonly<Record<string, unknown>>,
): boolean {
return (
pointer === "/request/url" ||
pointer === "/response/redirectURL" ||
(/^\/(request|response)\/headers\/\d+\/value$/.test(pointer) &&
typeof parent?.name === "string" &&
["location", "referer", "origin"].includes(parent.name.toLowerCase()))
);
}
/** Preserve bytes unless an explicitly declared UTF-8 literal occurs in them. */
sensitiveBytes(bytes: Buffer): boolean {
return this.#sensitiveByteValues.some((value) => bytes.includes(value));
}
}
/** Escape a producer property for an RFC 6901 location. */
export const capturePointer = (parent: string, key: string): string =>
`${parent}/${key.replaceAll("~", "~0").replaceAll("/", "~1")}`;
/** Structural record guard does not duck-type number/byte sidecar markers. */
export const captureObject = (
value: unknown,
): value is Record<string, unknown> =>
typeof value === "object" && value !== null && !Array.isArray(value);