refactor(browser): share authorized main-frame polling
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
import type { BrowserObservationOperation } from "../domain/browserObservationErrors.js";
|
||||
import { BrowserObservationError } from "../domain/errors.js";
|
||||
import type { CdpConnection } from "./CdpConnection.js";
|
||||
import { mainFrameUrl } from "./CdpCaptureDocuments.js";
|
||||
import {
|
||||
allowedSanitizedUrl,
|
||||
delayWithCancellation,
|
||||
isHttpUrl,
|
||||
} from "./CdpCaptureValues.js";
|
||||
|
||||
interface AuthorizedMainFrameOptions {
|
||||
readonly connection: CdpConnection;
|
||||
readonly sessionId: string | undefined;
|
||||
readonly signal: AbortSignal | undefined;
|
||||
readonly allowedOrigins: ReadonlySet<string>;
|
||||
readonly delayOperation: BrowserObservationOperation;
|
||||
}
|
||||
|
||||
/** Wait for the attached target's main frame to enter its approved origin. */
|
||||
export const authorizedMainFrame = async ({
|
||||
connection,
|
||||
sessionId,
|
||||
signal,
|
||||
allowedOrigins,
|
||||
delayOperation,
|
||||
}: AuthorizedMainFrameOptions): Promise<unknown> => {
|
||||
for (let attempt = 0; attempt < 80; attempt += 1) {
|
||||
const result = await connection.send(
|
||||
"Page.getFrameTree",
|
||||
{},
|
||||
sessionId,
|
||||
signal,
|
||||
);
|
||||
const url = mainFrameUrl(result);
|
||||
if (allowedSanitizedUrl(url, allowedOrigins) !== undefined) return result;
|
||||
if (isHttpUrl(url))
|
||||
throw new BrowserObservationError(
|
||||
"inspect_web_page",
|
||||
"target_not_allowed",
|
||||
);
|
||||
await delayWithCancellation(25, delayOperation, signal);
|
||||
}
|
||||
throw new BrowserObservationError("inspect_web_page", "target_not_allowed");
|
||||
};
|
||||
@@ -10,9 +10,9 @@ import {
|
||||
import type { CdpEndpointDiscovery, CdpEndpointTarget } from "./CdpEndpoint.js";
|
||||
import { CdpConnection, type CdpEvent } from "./CdpConnection.js";
|
||||
import { CdpCaptureCompleteness } from "./CdpCaptureCompleteness.js";
|
||||
import { authorizedMainFrame } from "./CdpAuthorizedMainFrame.js";
|
||||
import {
|
||||
allowedSanitizedUrl,
|
||||
delayWithCancellation,
|
||||
isHttpUrl,
|
||||
numberValue,
|
||||
recordValue,
|
||||
@@ -47,7 +47,13 @@ export const observeCdpSession = async (
|
||||
context: ObservationContext,
|
||||
): Promise<WebObservationSession> => {
|
||||
const allowedOrigins = new Set(context.input.allowed_origins);
|
||||
const initial = await authorizedFrame(context, allowedOrigins);
|
||||
const initial = await authorizedMainFrame({
|
||||
connection: context.connection,
|
||||
sessionId: context.sessionId,
|
||||
signal: context.signal,
|
||||
allowedOrigins,
|
||||
delayOperation: "observe_web_session",
|
||||
});
|
||||
const initialUrl = mainFrameUrl(initial) ?? "";
|
||||
const mainFrameId = frameId(initial);
|
||||
if (mainFrameId === undefined)
|
||||
@@ -370,29 +376,6 @@ const waitForWindow = async (
|
||||
if (signal?.aborted === true) onAbort();
|
||||
});
|
||||
|
||||
const authorizedFrame = async (
|
||||
context: ObservationContext,
|
||||
allowedOrigins: ReadonlySet<string>,
|
||||
): Promise<unknown> => {
|
||||
for (let attempt = 0; attempt < 80; attempt += 1) {
|
||||
const result = await context.connection.send(
|
||||
"Page.getFrameTree",
|
||||
{},
|
||||
context.sessionId,
|
||||
context.signal,
|
||||
);
|
||||
const url = mainFrameUrl(result);
|
||||
if (allowedSanitizedUrl(url, allowedOrigins) !== undefined) return result;
|
||||
if (isHttpUrl(url))
|
||||
throw new BrowserObservationError(
|
||||
"inspect_web_page",
|
||||
"target_not_allowed",
|
||||
);
|
||||
await delayWithCancellation(25, "observe_web_session", context.signal);
|
||||
}
|
||||
throw new BrowserObservationError("inspect_web_page", "target_not_allowed");
|
||||
};
|
||||
|
||||
const captureFinalUrl = async (
|
||||
context: ObservationContext,
|
||||
capture: TimelineCapture,
|
||||
|
||||
@@ -11,6 +11,7 @@ import { stableWebResources } from "../domain/webInventory.js";
|
||||
import type { CdpEndpointDiscovery, CdpEndpointTarget } from "./CdpEndpoint.js";
|
||||
import { CdpConnection } from "./CdpConnection.js";
|
||||
import { CdpCaptureEvents } from "./CdpCaptureEvents.js";
|
||||
import { authorizedMainFrame } from "./CdpAuthorizedMainFrame.js";
|
||||
import { captureStorage } from "./CdpCaptureStorage.js";
|
||||
import { optionalCdpCommand } from "./CdpOptionalCommand.js";
|
||||
import {
|
||||
@@ -24,7 +25,6 @@ import {
|
||||
import {
|
||||
allowedSanitizedUrl,
|
||||
delayWithCancellation,
|
||||
isHttpUrl,
|
||||
} from "./CdpCaptureValues.js";
|
||||
import { captureScripts } from "./CdpPageCaptureScripts.js";
|
||||
import { captureWorkers } from "./CdpPageCaptureWorkers.js";
|
||||
@@ -104,7 +104,13 @@ const captureAuthorizedPage = async (
|
||||
const { connection, sessionId, input, signal } = context;
|
||||
await authorizeObservationWindow(state);
|
||||
await captureJsonResponseBodies(state);
|
||||
const frameResult = await authorizedFrameTree(context, allowedOrigins);
|
||||
const frameResult = await authorizedMainFrame({
|
||||
connection: context.connection,
|
||||
sessionId: context.sessionId,
|
||||
signal: context.signal,
|
||||
allowedOrigins,
|
||||
delayOperation: context.operation,
|
||||
});
|
||||
const attachedUrl = mainFrameUrl(frameResult) ?? "";
|
||||
const frameCapture = captureFrames(
|
||||
frameResult,
|
||||
@@ -145,10 +151,13 @@ const captureAuthorizedPage = async (
|
||||
);
|
||||
if (!input.include_storage_keys)
|
||||
state.events.completeness.exclude("storage_keys", "not_approved", null);
|
||||
const completedFrameResult = await authorizedFrameTree(
|
||||
context,
|
||||
const completedFrameResult = await authorizedMainFrame({
|
||||
connection: context.connection,
|
||||
sessionId: context.sessionId,
|
||||
signal: context.signal,
|
||||
allowedOrigins,
|
||||
);
|
||||
delayOperation: context.operation,
|
||||
});
|
||||
const completedUrl = mainFrameUrl(completedFrameResult) ?? "";
|
||||
if (state.events.originViolation)
|
||||
throw new BrowserObservationError("inspect_web_page", "target_not_allowed");
|
||||
@@ -200,7 +209,13 @@ const authorizeObservationWindow = async (
|
||||
const { connection, sessionId, signal } = context;
|
||||
await report(context.progress, 1, "Enabling passive CDP domains");
|
||||
await connection.send("Page.enable", {}, sessionId, signal);
|
||||
const initialFrameResult = await authorizedFrameTree(context, allowedOrigins);
|
||||
const initialFrameResult = await authorizedMainFrame({
|
||||
connection: context.connection,
|
||||
sessionId: context.sessionId,
|
||||
signal: context.signal,
|
||||
allowedOrigins,
|
||||
delayOperation: context.operation,
|
||||
});
|
||||
const mainFrame = captureFrames(initialFrameResult, allowedOrigins).items[0];
|
||||
if (mainFrame === undefined)
|
||||
throw new BrowserObservationError("inspect_web_page", "target_not_allowed");
|
||||
@@ -289,30 +304,6 @@ const normalizedInspection = (
|
||||
limitations: state.limitations,
|
||||
});
|
||||
|
||||
const authorizedFrameTree = async (
|
||||
context: CaptureContext,
|
||||
allowedOrigins: ReadonlySet<string>,
|
||||
): Promise<unknown> => {
|
||||
for (let attempt = 0; attempt < 80; attempt += 1) {
|
||||
const result = await context.connection.send(
|
||||
"Page.getFrameTree",
|
||||
{},
|
||||
context.sessionId,
|
||||
context.signal,
|
||||
);
|
||||
const currentUrl = mainFrameUrl(result);
|
||||
if (allowedSanitizedUrl(currentUrl, allowedOrigins) !== undefined)
|
||||
return result;
|
||||
if (isHttpUrl(currentUrl))
|
||||
throw new BrowserObservationError(
|
||||
"inspect_web_page",
|
||||
"target_not_allowed",
|
||||
);
|
||||
await delayWithCancellation(25, context.operation, context.signal);
|
||||
}
|
||||
throw new BrowserObservationError("inspect_web_page", "target_not_allowed");
|
||||
};
|
||||
|
||||
const enableObservationDomains = async (
|
||||
connection: CdpConnection,
|
||||
sessionId: string | undefined,
|
||||
|
||||
Reference in New Issue
Block a user