fix(evidence): preserve literal keys and response body ownership
Retain exact empty and computed literal keys in JavaScript observations. Bind CDP response bodies to request occurrences, account for late responses, and validate the target after final progress callbacks.
This commit is contained in:
@@ -39,7 +39,9 @@ it("preserves an already captured response shape without marking it unavailable"
|
||||
},
|
||||
},
|
||||
});
|
||||
events.ingestResponseBody("request-1", {
|
||||
const candidate = events.responseBodyRequests()[0];
|
||||
expect(candidate).toBeDefined();
|
||||
events.ingestResponseBody(candidate!, {
|
||||
body: '{"response":true}',
|
||||
base64Encoded: false,
|
||||
});
|
||||
@@ -62,7 +64,7 @@ it("preserves an already captured response shape without marking it unavailable"
|
||||
capturedShape,
|
||||
);
|
||||
expect(events.network.get("request-1")?.body_shapes.status).toBe("included");
|
||||
expect(events.responseBodyRequestIds()).toEqual([]);
|
||||
expect(events.responseBodyRequests()).toEqual([]);
|
||||
expect(events.completeness.snapshot().unavailable_sections).not.toContain(
|
||||
"json_body_shapes",
|
||||
);
|
||||
|
||||
@@ -7,11 +7,18 @@ import type { CdpCaptureEventsState } from "./CdpCaptureEventState.js";
|
||||
import type { CapturedScript, NetworkState } from "./CdpCaptureEventTypes.js";
|
||||
import * as handlers from "./CdpCaptureEventHandlers.js";
|
||||
import { isJsonMediaType } from "./CdpCaptureEventHelpers.js";
|
||||
import { recordValue } from "./CdpCaptureValues.js";
|
||||
import { cdpStringValue, recordValue } from "./CdpCaptureValues.js";
|
||||
|
||||
export interface ResponseBodyCandidate {
|
||||
readonly requestId: string;
|
||||
readonly occurrence: number;
|
||||
}
|
||||
|
||||
/** Event accumulator that validates and normalizes payloads at ingestion. */
|
||||
export class CdpCaptureEvents implements CdpCaptureEventsState {
|
||||
#scriptMetadataBudgetExclusionsRecorded = false;
|
||||
readonly #networkRequestOccurrences = new Map<string, number>();
|
||||
readonly #responseBodyExaminedOccurrences = new Map<string, number>();
|
||||
readonly scripts = new Map<string, CapturedScript>();
|
||||
readonly scriptMetadataBudgetOmissionsById = new Map<string, true>();
|
||||
rejectedScriptMetadataBudgetCount = 0;
|
||||
@@ -64,6 +71,8 @@ export class CdpCaptureEvents implements CdpCaptureEventsState {
|
||||
this.executionContextFrames.clear();
|
||||
this.network.clear();
|
||||
this.networkRequestTimestamps.clear();
|
||||
this.#networkRequestOccurrences.clear();
|
||||
this.#responseBodyExaminedOccurrences.clear();
|
||||
this.malformedRedirectRequestIds.clear();
|
||||
this.webSocketConnectionsById.clear();
|
||||
this.console.length = 0;
|
||||
@@ -128,9 +137,18 @@ export class CdpCaptureEvents implements CdpCaptureEventsState {
|
||||
case "Debugger.scriptParsed":
|
||||
handlers.handleScriptParsed(this, params);
|
||||
break;
|
||||
case "Network.requestWillBeSent":
|
||||
case "Network.requestWillBeSent": {
|
||||
const requestId = cdpStringValue(params.requestId);
|
||||
const previous =
|
||||
requestId === undefined ? undefined : this.network.get(requestId);
|
||||
handlers.handleRequestWillBeSent(this, params);
|
||||
if (requestId !== undefined && this.network.get(requestId) !== previous)
|
||||
this.#networkRequestOccurrences.set(
|
||||
requestId,
|
||||
(this.#networkRequestOccurrences.get(requestId) ?? 0) + 1,
|
||||
);
|
||||
break;
|
||||
}
|
||||
case "Network.responseReceived":
|
||||
handlers.handleResponseReceived(this, params);
|
||||
break;
|
||||
@@ -165,24 +183,68 @@ export class CdpCaptureEvents implements CdpCaptureEventsState {
|
||||
: null;
|
||||
}
|
||||
|
||||
responseBodyRequestIds(): readonly string[] {
|
||||
responseBodyRequests(): readonly ResponseBodyCandidate[] {
|
||||
if (!this.input.include_json_body_shapes) return [];
|
||||
return [...this.network.values()]
|
||||
.filter(
|
||||
(request) => !this.malformedRedirectRequestIds.has(request.request_id),
|
||||
)
|
||||
.filter((request) => isJsonMediaType(request.mime_type))
|
||||
.map((request) => request.request_id);
|
||||
.flatMap((request) => {
|
||||
const occurrence = this.#networkRequestOccurrences.get(
|
||||
request.request_id,
|
||||
);
|
||||
return occurrence === undefined
|
||||
? []
|
||||
: [{ requestId: request.request_id, occurrence }];
|
||||
});
|
||||
}
|
||||
|
||||
responseBodyUnavailable(requestId: string): void {
|
||||
if (this.malformedRedirectRequestIds.has(requestId)) return;
|
||||
bodyShapes.updateResponseBodyShape(this, requestId, null);
|
||||
currentResponseBodyRequest(
|
||||
candidate: ResponseBodyCandidate,
|
||||
): NetworkState | undefined {
|
||||
if (
|
||||
candidate.occurrence !==
|
||||
this.#networkRequestOccurrences.get(candidate.requestId)
|
||||
)
|
||||
return undefined;
|
||||
return this.network.get(candidate.requestId);
|
||||
}
|
||||
|
||||
responseBodyUnavailable(candidate: ResponseBodyCandidate): void {
|
||||
if (this.currentResponseBodyRequest(candidate) === undefined) return;
|
||||
this.#responseBodyExaminedOccurrences.set(
|
||||
candidate.requestId,
|
||||
candidate.occurrence,
|
||||
);
|
||||
if (this.malformedRedirectRequestIds.has(candidate.requestId)) return;
|
||||
bodyShapes.updateResponseBodyShape(this, candidate.requestId, null);
|
||||
this.completeness.unavailable("json_body_shapes");
|
||||
}
|
||||
|
||||
ingestResponseBody(requestId: string, value: unknown): void {
|
||||
if (this.malformedRedirectRequestIds.has(requestId)) return;
|
||||
bodyShapes.ingestResponseBodyShape(this, requestId, value);
|
||||
/**
|
||||
* Mark eligible response bodies that arrived after the fetch pass was
|
||||
* enumerated.
|
||||
*/
|
||||
settleUnexaminedResponseBodies(): void {
|
||||
for (const candidate of this.responseBodyRequests()) {
|
||||
const request = this.currentResponseBodyRequest(candidate);
|
||||
if (
|
||||
request?.body_shapes.response === null &&
|
||||
this.#responseBodyExaminedOccurrences.get(candidate.requestId) !==
|
||||
candidate.occurrence
|
||||
)
|
||||
this.responseBodyUnavailable(candidate);
|
||||
}
|
||||
}
|
||||
|
||||
ingestResponseBody(candidate: ResponseBodyCandidate, value: unknown): void {
|
||||
if (this.currentResponseBodyRequest(candidate) === undefined) return;
|
||||
this.#responseBodyExaminedOccurrences.set(
|
||||
candidate.requestId,
|
||||
candidate.occurrence,
|
||||
);
|
||||
if (this.malformedRedirectRequestIds.has(candidate.requestId)) return;
|
||||
bodyShapes.ingestResponseBodyShape(this, candidate.requestId, value);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -153,6 +153,7 @@ const captureAuthorizedPage = async (
|
||||
);
|
||||
if (!input.include_storage_keys)
|
||||
state.events.completeness.exclude("storage_keys", "not_approved", null);
|
||||
await report(context.progress, 3, "Normalizing browser evidence");
|
||||
const completedFrameResult = await authorizedMainFrame({
|
||||
connection: context.connection,
|
||||
sessionId: context.sessionId,
|
||||
@@ -165,7 +166,10 @@ const captureAuthorizedPage = async (
|
||||
throw new BrowserObservationError("inspect_web_page", "target_not_allowed");
|
||||
if (state.events.navigationDuringCapture || completedUrl !== attachedUrl)
|
||||
throw new BrowserObservationError("inspect_web_page", "target_changed");
|
||||
await report(context.progress, 3, "Normalizing browser evidence");
|
||||
// Body IDs were enumerated before asynchronous CDP reads. Events remain
|
||||
// enabled through the final capture, so account for eligible responses that
|
||||
// arrived while those reads were pending.
|
||||
state.events.settleUnexaminedResponseBodies();
|
||||
state.events.recordScriptMetadataBudgetExclusions();
|
||||
return {
|
||||
inspection: normalizedInspection(state, {
|
||||
@@ -185,14 +189,14 @@ const captureAuthorizedPage = async (
|
||||
const captureJsonResponseBodies = async (
|
||||
state: CaptureState,
|
||||
): Promise<void> => {
|
||||
const requestIds = state.events.responseBodyRequestIds();
|
||||
for (let index = 0; index < requestIds.length; index += 1) {
|
||||
const requestId = requestIds[index];
|
||||
if (requestId === undefined) continue;
|
||||
const request = state.events.network.get(requestId);
|
||||
if (request === undefined) continue;
|
||||
if (request.encoded_data_length === null) {
|
||||
state.events.responseBodyUnavailable(requestId);
|
||||
const requests = state.events.responseBodyRequests();
|
||||
for (let index = 0; index < requests.length; index += 1) {
|
||||
const candidate = requests[index];
|
||||
if (candidate === undefined) continue;
|
||||
const current = state.events.currentResponseBodyRequest(candidate);
|
||||
if (current === undefined) continue;
|
||||
if (current.encoded_data_length === null) {
|
||||
state.events.responseBodyUnavailable(candidate);
|
||||
continue;
|
||||
}
|
||||
let result: unknown;
|
||||
@@ -200,7 +204,7 @@ const captureJsonResponseBodies = async (
|
||||
result = await optionalCdpCommand(
|
||||
state.context,
|
||||
"Network.getResponseBody",
|
||||
{ requestId },
|
||||
{ requestId: candidate.requestId },
|
||||
state.limitations,
|
||||
);
|
||||
} catch (cause: unknown) {
|
||||
@@ -211,17 +215,17 @@ const captureJsonResponseBodies = async (
|
||||
cause.reportedMessage === null
|
||||
)
|
||||
throw cause;
|
||||
state.events.responseBodyUnavailable(requestId);
|
||||
state.events.responseBodyUnavailable(candidate);
|
||||
state.limitations.push(
|
||||
`Response body unavailable for request ${requestId}: ${cause.userMessage}`,
|
||||
`Response body unavailable for request ${candidate.requestId}: ${cause.userMessage}`,
|
||||
);
|
||||
continue;
|
||||
}
|
||||
if (result !== undefined) {
|
||||
state.events.ingestResponseBody(requestId, result);
|
||||
state.events.ingestResponseBody(candidate, result);
|
||||
continue;
|
||||
}
|
||||
for (const remaining of requestIds.slice(index))
|
||||
for (const remaining of requests.slice(index))
|
||||
state.events.responseBodyUnavailable(remaining);
|
||||
return;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user