Files
reverse-engineering-anything/src/domain/browserNetworkEvidence.ts
T
morluto 5f718ce30e refactor(kernel): give error-message, byte-hex and SHA-256 hex helpers one owner in a new src/kernel layer (#11542)
* refactor(kernel): give error-to-message text one owner in a new src/kernel layer

* fix(kernel): keep the regex worker's own error text inside its isolated source

* refactor(kernel): exclude copies that run in another realm from the error-message guard by name

The regex worker's source string and the page.evaluate fetch in PlaywrightScenarioRequest cannot import errorMessage. The guard now names them, so the counted baseline does not invite a later slice to rewrite them.

* refactor(kernel): migrate the long tail of inline error-message copies to errorMessage (#11552)

Every provider and adapter folder with six or fewer copies now calls errorMessage from src/kernel. Local helpers whose whole body was the copy (failureMessage, gdbErrorMessage, message, describe, reasonOf, recoveryFailureMessage and four local errorMessage definitions) are removed and their call sites call the owner.

Refs #11548

* refactor(kernel): give byte-to-hex formatting one owner in src/kernel (#11560)

byteToHex returns the two lowercase hexadecimal digits of one byte. All eight inline copies (native, contracts, domain firmware and desktop, pcap) call it, keeping their own 0x, % or \x notation, and a counted guard with an empty baseline keeps new copies out.

Refs #11548

* refactor(kernel): give one-shot SHA-256 hex digests one owner in src/kernel (#11562)

sha256Hex digests one in-memory value. A counted guard records the one-shot createHash("sha256").update(x).digest("hex") chains (115 in 101 files) and the first slice migrates domain, contracts and process (35 copies), removing local wrappers (sha256, sha256Text, hash) and the exported sha256Text. Incremental and streamed hashing is not counted.

Refs #11548

* refactor(kernel): migrate the application layer's inline error-message copies to errorMessage (#11563)

All 69 copies in src/application call errorMessage from src/kernel. Local helpers whose whole body was the copy (errorDetail, causeReason and five local errorMessage definitions) are removed and their call sites call the owner.

Refs #11548

* refactor(kernel): migrate the native provider's inline error-message copies to errorMessage (#11567)

All 49 copies in src/native call errorMessage from src/kernel. The local errorReason helpers in LldbCallTracer and LldbRetainedObservations, whose whole body was the copy, are removed and their call sites call the owner.

Refs #11548

* refactor(kernel): migrate the remaining inline error-message copies to errorMessage (#11569)
2026-11-16 07:05:30 +08:00

126 lines
4.3 KiB
TypeScript

import { z } from "zod";
import { sha256Hex } from "../kernel/sha256Hex.js";
export const PLAYWRIGHT_RESPONSE_BODY_PROVENANCE_LIMITATION =
"Captured response-body bytes are the result of Playwright's Response.body() API. Playwright may satisfy that API with a credentialed follow-up load of the request URL when CDP has no body, so transaction and event links do not verify that retained bytes came from the original network transaction.";
/** Resolved network content selection recorded by a capture. */
export const browserNetworkContentSelectionValuesSchema = z.strictObject({
request_body: z.boolean(),
response_body: z.boolean(),
header_values: z.boolean(),
});
/** Caller omission selects metadata alone; individual content facets remain optional. */
export const browserNetworkContentSelectionSchema =
browserNetworkContentSelectionValuesSchema
.extend({
request_body: z.boolean().default(false),
response_body: z.boolean().default(false),
header_values: z.boolean().default(false),
})
.default({
request_body: false,
response_body: false,
header_values: false,
});
/** Self-verifying retained bytes, after any declared-secret redaction. */
const unavailableBrowserNetworkBodySchema = z.discriminatedUnion("reason", [
z.strictObject({
state: z.literal("unavailable"),
reason: z.enum([
"request-failed",
"response-unfinished",
"body-read-failed",
"body-read-timeout",
"cancelled",
"capture-ended",
]),
message: z.string().min(1),
}),
z.strictObject({
state: z.literal("unavailable"),
reason: z.literal("body-size-limit"),
message: z.string().min(1),
limit_bytes: z.int().positive().safe(),
observed_bytes: z.int().positive().safe(),
}),
z.strictObject({
state: z.literal("unavailable"),
reason: z.literal("body-allocation-limit"),
message: z.string().min(1),
limit_allocation_bytes: z.int().positive().safe(),
observed_allocation_bytes: z.int().positive().safe(),
}),
]);
export const browserNetworkBodySchema = z.union([
z.strictObject({ state: z.literal("not_requested") }),
z.strictObject({ state: z.literal("not_exposed") }),
unavailableBrowserNetworkBodySchema,
z
.strictObject({
state: z.literal("captured"),
representation: z.enum([
"browser-exposed-request-bytes",
"playwright-response-body-bytes",
// Retained for captures produced before response-body provenance was
// narrowed to the bytes Playwright exposes through Response.body().
"browser-decoded-response-bytes",
]),
encoding: z.literal("base64"),
content: z.string(),
bytes: z.int().min(0),
sha256: z.string().regex(/^[a-f0-9]{64}$/u),
media_type: z.string().nullable(),
redacted: z.boolean(),
})
.superRefine((artifact, context) => {
const bytes = Buffer.from(artifact.content, "base64");
if (bytes.toString("base64") !== artifact.content)
context.addIssue({
code: "custom",
path: ["content"],
message: "Network body must use canonical base64",
});
if (bytes.length !== artifact.bytes)
context.addIssue({
code: "custom",
path: ["bytes"],
message: "Network body byte count disagrees with retained bytes",
});
if (sha256Hex(bytes) !== artifact.sha256)
context.addIssue({
code: "custom",
path: ["sha256"],
message: "Network body digest disagrees with retained bytes",
});
}),
]);
/** Headers retain producer order and duplicates, with credential values removed. */
export const browserNetworkHeadersSchema = z.union([
z.strictObject({ state: z.literal("not_requested") }),
z.strictObject({
state: z.literal("unavailable"),
message: z.string().min(1),
}),
z.strictObject({
state: z.literal("captured"),
items: z.array(
z.strictObject({
name: z.string().min(1),
value: z.string().nullable(),
redacted: z.boolean(),
}),
),
}),
]);
export type BrowserNetworkContentSelection = z.infer<
typeof browserNetworkContentSelectionSchema
>;
export type BrowserNetworkBody = z.infer<typeof browserNetworkBodySchema>;
export type BrowserNetworkHeaders = z.infer<typeof browserNetworkHeadersSchema>;