Files
rea/src/browser/network/PlaywrightNetworkContent.ts
T
N0zoM1z0 f9eaf056ef feat(browser): capture correlated network content and lifecycle evidence (#797)
* feat(browser): capture correlated network content and lifecycle evidence

Keep request identities and redirect relationships across ordered scenario events.
Capture selected browser-exposed bytes and duplicate headers through modular
readers, settle bounded reads before cleanup, and preserve unavailable states.
Keep content-selection mismatches and legacy network coverage unknown during
comparison. Verify real CLI/MCP, installed package, and browser lifecycle paths.

Refs #774

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

* fix(browser): align capture defaults and redact encoded media metadata

Correct the complete documented request and legacy default expectation caught by CI. Cover and redact encoded declared values in captured body media types.

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

* test(browser): verify polling capture cutoff completeness

Accept incomplete event coverage only when the continuously polling fixture has explicit unfinished requests; retain failures for other missing or truncated sections. Include actual completeness and unfinished transactions in diagnostics.

Assisted-by: Yukari (gensokyo-skills:yukari-boundary-analysis)
Assisted-by: Kogasa (gensokyo-skills:kogasa-surprise-testing)
2026-10-07 04:35:49 +08:00

121 lines
3.6 KiB
TypeScript

import { createHash } from "node:crypto";
import {
AnalysisCancelledError,
AnalysisTimeoutError,
} from "../../domain/analysisErrorCore.js";
import type {
BrowserNetworkBody,
BrowserNetworkHeaders,
} from "../../domain/browserNetworkEvidence.js";
import type { BrowserScenarioSecrets } from "../BrowserScenarioSecrets.js";
import { withPlaywrightExecutionBoundary } from "../PlaywrightExecutionBoundary.js";
/** The producer seam for ordered, duplicate-preserving HTTP headers. */
export interface NetworkHeadersSource {
headers(): Record<string, string>;
headersArray(): Promise<{ name: string; value: string }[]>;
}
/** A bounded read cannot delay browser cleanup indefinitely. */
export interface NetworkReadOptions {
readonly timeoutMs: number;
readonly signal?: AbortSignal;
}
const credentialHeaders = new Set([
"authorization",
"proxy-authorization",
"cookie",
"set-cookie",
]);
const errorMessage = (
cause: unknown,
secrets: BrowserScenarioSecrets,
): string =>
secrets.redact(
cause instanceof Error && cause.message !== ""
? cause.message
: "Network read failed",
);
/** Read actual producer headers without flattening duplicates or exposing credentials. */
export const captureNetworkHeaders = async (
source: NetworkHeadersSource,
secrets: BrowserScenarioSecrets,
options: NetworkReadOptions,
): Promise<BrowserNetworkHeaders> => {
try {
const headers = await withPlaywrightExecutionBoundary(
() => source.headersArray(),
options.timeoutMs,
options.signal,
);
return {
state: "captured",
items: headers.map(({ name, value }) => {
const safeName = secrets.redact(name);
const credential = credentialHeaders.has(name.toLowerCase());
const safeValue = credential
? null
: secrets.redactBytes(Buffer.from(value)).toString("utf8");
return {
name: safeName,
value: safeValue,
redacted: credential || safeName !== name || safeValue !== value,
};
}),
};
} catch (cause: unknown) {
return { state: "unavailable", message: errorMessage(cause, secrets) };
}
};
/** Retain only bytes exposed by the browser; no fetch or wire-byte reconstruction occurs. */
export const captureNetworkBody = async (
read: () => Promise<Buffer | null>,
phase: "request" | "response",
source: NetworkHeadersSource,
secrets: BrowserScenarioSecrets,
options: NetworkReadOptions,
): Promise<BrowserNetworkBody> => {
try {
const observed = await withPlaywrightExecutionBoundary(
read,
options.timeoutMs,
options.signal,
);
if (observed === null) return { state: "not_exposed" };
const retained = secrets.redactBytes(observed);
const contentType = source.headers()["content-type"];
return {
state: "captured",
representation:
phase === "request"
? "browser-exposed-request-bytes"
: "browser-decoded-response-bytes",
encoding: "base64",
content: retained.toString("base64"),
bytes: retained.length,
sha256: createHash("sha256").update(retained).digest("hex"),
media_type:
contentType === undefined
? null
: secrets.redactBytes(Buffer.from(contentType)).toString("utf8"),
redacted: !retained.equals(observed),
};
} catch (cause: unknown) {
return {
state: "unavailable",
reason:
cause instanceof AnalysisCancelledError
? "cancelled"
: cause instanceof AnalysisTimeoutError
? "body-read-timeout"
: "body-read-failed",
message: errorMessage(cause, secrets),
};
}
};