fix(browser): bound script metadata and preserve omissions

This commit is contained in:
morluto
2026-10-09 06:32:10 +08:00
parent 0a0174b9ac
commit faf209e50d
13 changed files with 541 additions and 15 deletions
+1
View File
@@ -65,6 +65,7 @@ export class CdpCaptureCompleteness {
reason === "invalid_protocol_value"
)
this.#unavailable.add(section);
if (reason === "resource_budget_exhausted") this.#truncated.add(section);
const key = `${section}\0${reason}`;
const existing = this.#excluded.get(key);
const combined =
+102 -10
View File
@@ -26,7 +26,11 @@ import {
isMainFrameNavigation,
} from "./CdpCaptureEventHelpers.js";
import type { CdpCaptureEventsState } from "./CdpCaptureEventState.js";
import type { CapturedScript, NetworkState } from "./CdpCaptureEventTypes.js";
import {
CDP_CAPTURE_SCRIPT_METADATA_LIMITS,
type CapturedScript,
type NetworkState,
} from "./CdpCaptureEventTypes.js";
export const handleExecutionContextCreated = (
state: CdpCaptureEventsState,
@@ -64,12 +68,18 @@ export const handleScriptParsed = (
state.completeness.exclude("scripts", "invalid_protocol_value");
return;
}
const replacedBytes = state.scriptMetadataBytesById.get(scriptId) ?? 0;
state.scriptMetadataBudgetOmissionsById.delete(scriptId);
if (replacedBytes > 0) {
state.scripts.delete(scriptId);
state.scriptMetadataBytesById.delete(scriptId);
state.retainedScriptMetadataBytes -= replacedBytes;
}
if (sanitized === undefined) {
state.completeness.exclude("scripts", exclusionReasonForUrl(rawUrl));
return;
}
const sourceMap = sourceMapForScript(params.sourceMapURL, rawUrl, state);
const script: CapturedScript = {
const baseScript = {
scriptId,
rawUrl,
url: sanitized.url,
@@ -84,21 +94,79 @@ export const handleScriptParsed = (
),
isModule: params.isModule === true,
language: cdpStringValue(params.scriptLanguage) ?? null,
sourceMapUrl: sourceMap.sanitized,
sourceMapRawUrl: sourceMap.raw,
executionContextKey: executionContextKey(params.executionContextId),
};
const baseBytes = scriptMetadataStringBytes(baseScript);
const sourceMapDeclared =
typeof params.sourceMapURL === "string" && params.sourceMapURL.length > 0;
if (
state.scripts.size >= CDP_CAPTURE_SCRIPT_METADATA_LIMITS.scripts ||
baseBytes >
CDP_CAPTURE_SCRIPT_METADATA_LIMITS.retainedBytes -
state.retainedScriptMetadataBytes
) {
state.rejectedScriptMetadataBudgetCount += 1;
if (sourceMapDeclared) state.rejectedSourceMapMetadataBudgetCount += 1;
return;
}
const sourceMap = sourceMapForScript(
params.sourceMapURL,
rawUrl,
state,
CDP_CAPTURE_SCRIPT_METADATA_LIMITS.retainedBytes -
state.retainedScriptMetadataBytes -
baseBytes,
);
const { budgetExhausted, ...sourceMapFields } = sourceMap;
const script: CapturedScript = { ...baseScript, ...sourceMapFields };
if (budgetExhausted)
state.scriptMetadataBudgetOmissionsById.set(scriptId, true);
const retainedBytes =
baseBytes +
stringByteLength(sourceMap.sourceMapUrl) +
stringByteLength(sourceMap.sourceMapRawUrl);
state.retainedScriptMetadataBytes += retainedBytes;
state.scriptMetadataBytesById.set(scriptId, retainedBytes);
state.scripts.set(scriptId, script);
};
const scriptMetadataStringBytes = (script: {
readonly scriptId: string;
readonly rawUrl: string;
readonly url: string;
readonly origin: string | null;
readonly hash: string;
readonly language: string | null;
readonly executionContextKey: string | null;
}): number =>
stringByteLength(script.scriptId) +
stringByteLength(script.rawUrl) +
stringByteLength(script.url) +
stringByteLength(script.origin) +
stringByteLength(script.hash) +
stringByteLength(script.language) +
stringByteLength(script.executionContextKey);
const stringByteLength = (value: string | null): number =>
value === null ? 0 : Buffer.byteLength(value, "utf8");
const sourceMapForScript = (
value: unknown,
scriptUrl: string,
state: CdpCaptureEventsState,
): { readonly sanitized: string | null; readonly raw: string | null } => {
remainingBytes: number,
): {
readonly sourceMapUrl: string | null;
readonly sourceMapRawUrl: string | null;
readonly budgetExhausted: boolean;
} => {
const declaredUrl = cdpStringValue(value);
if (declaredUrl === undefined || declaredUrl === "")
return { sanitized: null, raw: null };
return {
sourceMapUrl: null,
sourceMapRawUrl: null,
budgetExhausted: false,
};
let rawUrl: string;
try {
rawUrl = new URL(declaredUrl, scriptUrl).href;
@@ -106,12 +174,36 @@ const sourceMapForScript = (
// Unparseable URLs are recorded as unsupported, not as failures.
void cause;
state.completeness.exclude("source_maps", "unsupported_url");
return { sanitized: null, raw: null };
return {
sourceMapUrl: null,
sourceMapRawUrl: null,
budgetExhausted: false,
};
}
const sanitized = allowedSanitizedUrl(rawUrl, state.allowedOrigins);
if (sanitized !== undefined) return { sanitized: sanitized.url, raw: rawUrl };
if (sanitized !== undefined) {
if (
stringByteLength(rawUrl) + stringByteLength(sanitized.url) >
remainingBytes
) {
return {
sourceMapUrl: null,
sourceMapRawUrl: null,
budgetExhausted: true,
};
}
return {
sourceMapUrl: sanitized.url,
sourceMapRawUrl: rawUrl,
budgetExhausted: false,
};
}
state.completeness.exclude("source_maps", exclusionReasonForUrl(rawUrl));
return { sanitized: null, raw: null };
return {
sourceMapUrl: null,
sourceMapRawUrl: null,
budgetExhausted: false,
};
};
export const handleRequestWillBeSent = (
+5
View File
@@ -9,6 +9,11 @@ export interface CdpCaptureEventsState {
readonly input: InspectWebPageInput;
readonly allowedOrigins: ReadonlySet<string>;
readonly scripts: Map<string, CapturedScript>;
readonly scriptMetadataBudgetOmissionsById: Map<string, true>;
rejectedScriptMetadataBudgetCount: number;
rejectedSourceMapMetadataBudgetCount: number;
readonly scriptMetadataBytesById: Map<string, number>;
retainedScriptMetadataBytes: number;
readonly executionContextFrames: Map<string, string>;
readonly network: Map<string, NetworkState>;
readonly networkRequestTimestamps: Map<string, number>;
+6
View File
@@ -15,3 +15,9 @@ export interface CapturedScript {
}
export type NetworkState = WebPageInspection["network"]["requests"][number];
/** Bounds retained CDP script identities independently from source text capture. */
export const CDP_CAPTURE_SCRIPT_METADATA_LIMITS = {
retainedBytes: 8 * 1024 * 1024,
scripts: 50_000,
} as const;
+34
View File
@@ -15,7 +15,13 @@ export { type CapturedScript } from "./CdpCaptureEventTypes.js";
/** Event accumulator that validates and normalizes payloads at ingestion. */
export class CdpCaptureEvents implements CdpCaptureEventsState {
#scriptMetadataBudgetExclusionsRecorded = false;
readonly scripts = new Map<string, CapturedScript>();
readonly scriptMetadataBudgetOmissionsById = new Map<string, true>();
rejectedScriptMetadataBudgetCount = 0;
rejectedSourceMapMetadataBudgetCount = 0;
readonly scriptMetadataBytesById = new Map<string, number>();
retainedScriptMetadataBytes = 0;
readonly executionContextFrames = new Map<string, string>();
readonly network = new Map<string, NetworkState>();
readonly networkRequestTimestamps = new Map<string, number>();
@@ -51,6 +57,12 @@ export class CdpCaptureEvents implements CdpCaptureEventsState {
this.navigationDuringCapture = false;
this.committedDocument = undefined;
this.scripts.clear();
this.scriptMetadataBudgetOmissionsById.clear();
this.rejectedScriptMetadataBudgetCount = 0;
this.rejectedSourceMapMetadataBudgetCount = 0;
this.scriptMetadataBytesById.clear();
this.retainedScriptMetadataBytes = 0;
this.#scriptMetadataBudgetExclusionsRecorded = false;
this.executionContextFrames.clear();
this.network.clear();
this.networkRequestTimestamps.clear();
@@ -81,6 +93,28 @@ export class CdpCaptureEvents implements CdpCaptureEventsState {
this.committedDocument = undefined;
}
/** Record final script metadata omissions after all scriptParsed replacements. */
recordScriptMetadataBudgetExclusions(): void {
if (this.#scriptMetadataBudgetExclusionsRecorded) return;
this.#scriptMetadataBudgetExclusionsRecorded = true;
const omittedScripts = this.rejectedScriptMetadataBudgetCount;
const omittedSourceMaps =
this.rejectedSourceMapMetadataBudgetCount +
this.scriptMetadataBudgetOmissionsById.size;
if (omittedScripts > 0)
this.completeness.exclude(
"scripts",
"resource_budget_exhausted",
omittedScripts,
);
if (omittedSourceMaps > 0)
this.completeness.exclude(
"source_maps",
"resource_budget_exhausted",
omittedSourceMaps,
);
}
ingest(event: CdpEvent): void {
const params = recordValue(event.params);
if (params === undefined) return;
+1
View File
@@ -162,6 +162,7 @@ const captureAuthorizedPage = async (
if (state.events.navigationDuringCapture || completedUrl !== attachedUrl)
throw new BrowserObservationError("inspect_web_page", "target_changed");
await report(context.progress, 3, "Normalizing browser evidence");
state.events.recordScriptMetadataBudgetExclusions();
return {
inspection: normalizedInspection(state, {
attachedUrl,