test(web): control authorization retries and consolidate session termination (#13196)

This commit is contained in:
morluto authored and GitHub committed 2026-11-21 19:41:41 +00:00
1 parent ae72c30e2c
commit 905090c0ca
7 files changed
+107 -200

No files matched your search

@@ -1,4 +1,4 @@
import { expect, it } from "vitest";
import { expect, it, onTestFinished, vi } from "vitest";
import { CdpBrowserProvider } from "../../../src/browser/CdpBrowserProvider.js";
import { CdpWebRuntimeProvider } from "../../../src/browser/execution/CdpWebRuntimeProvider.js";
import { inspectWebPageInputSchema } from "../../../src/domain/browserObservation.js";
@@ -7,6 +7,7 @@ import { observeWebExecutionInputSchema } from "../../../src/domain/webExecution
import { projectAnalysisError } from "../../../src/domain/analysisErrorProjection.js";
import { startRuntimeBrowser } from "../../fixtures/webRuntime.js";
import type { FakeCdpBrowser } from "../../fixtures/fakeCdpBrowser.js";
import { phaseArrival } from "../../support/lifecycleHarness.js";
import { trackBrowser } from "./cdpBrowserProvider.support.js";
const operations = ["inspect", "session", "runtime"] as const;
@@ -95,10 +96,24 @@ it.each(operations)(
trackBrowser(browser);
const result = await run(browser, operation);
if (!result.ok) throw result.error;
expect(result.value.target).toMatchObject({
target_id: "allowed-page",
[operation === "inspect" ? "url" : "initial_url"]:
`${browser.allowedOrigin}/app`,
const { allowedOrigin } = browser;
expect(result.value).toMatchObject({
target: {
target_id: "allowed-page",
[operation === "inspect" ? "url" : "initial_url"]:
`${allowedOrigin}/app`,
...(operation === "inspect" ? { origin: allowedOrigin } : {}),
},
...(operation === "inspect"
? {
frames: expect.arrayContaining([
expect.objectContaining({
frame_id: "runtime-main",
origin: allowedOrigin,
}),
]),
}
: {}),
});
expect(readsAtEnable).toBe(startupFrames.length + 1);
},
@@ -122,9 +137,17 @@ it.each([
details: { reason: "target_not_allowed" },
});
expect(frameReads(browser)).toHaveLength(1);
expect(browser.commands.map(({ method }) => method)).not.toContain(
protectedDomain[operation],
);
const methods = browser.commands.map(({ method }) => method);
expect(methods).not.toContain(protectedDomain[operation]);
if (operation === "inspect")
expect(methods).toContain("Target.detachFromTarget");
const evidenceMethods = [
"DOMSnapshot.captureSnapshot",
"Storage.getUsageAndQuota",
];
expect(
methods.filter((method) => evidenceMethods.includes(method)),
).toEqual([]);
},
);
@@ -197,10 +220,34 @@ it("withholds the final session URL when the proposed root has a parent", async
expect(frameReads(browser)).toHaveLength(2);
});
// Keep socket delivery real. Advance the clock only after the parsed reply
// has placed the operation at the retry delay.
const useAuthorizationClock = (advanceRetries: boolean) => {
vi.useFakeTimers({ toFake: ["setTimeout", "clearTimeout"] });
const retry = phaseArrival();
const schedule = globalThis.setTimeout;
const timerSpy = vi
.spyOn(globalThis, "setTimeout")
.mockImplementation((callback, delay, ...args) => {
const timer = schedule(callback, delay, ...args);
if (delay === 25) {
retry.arrive();
if (advanceRetries) queueMicrotask(() => vi.advanceTimersByTime(25));
}
return timer;
});
onTestFinished(() => {
timerSpy.mockRestore();
vi.useRealTimers();
});
return retry;
};
it("bounds unsupported malformed frame retries without authorizing execution", async () => {
const browser = await startFrameBrowser(() =>
selectedFrame("about:blank", { urlFragment: 7 }),
);
useAuthorizationClock(true);
const result = await run(browser, "runtime");
expect(result).toMatchObject({
ok: false,
@@ -216,13 +263,16 @@ it("bounds unsupported malformed frame retries without authorizing execution", a
);
});
it("preserves cancellation while an unsupported malformed frame awaits authorization", async () => {
it("settles caller cancellation while the authorization retry clock is held", async () => {
const controller = new AbortController();
const browser = await startFrameBrowser(() => {
setTimeout(() => controller.abort("caller stopped authorization"), 5);
return selectedFrame("about:blank", { urlFragment: 7 });
});
expect(await run(browser, "inspect", controller.signal)).toMatchObject({
const browser = await startFrameBrowser(() =>
selectedFrame("about:blank", { urlFragment: 7 }),
);
const retry = useAuthorizationClock(false);
const operation = run(browser, "inspect", controller.signal);
await retry.reached(operation, "main-frame authorization retry");
controller.abort("caller stopped authorization");
expect(await operation).toMatchObject({
ok: false,
error: {
_tag: "AnalysisCancelledError",
@@ -293,34 +293,6 @@ describe("CdpBrowserProvider: cancellation and session events", () => {
});
describe("CdpBrowserProvider: session termination and scope changes", () => {
it("ends a browser session when its flat target session detaches", async () => {
const browser = await startFakeCdpBrowser({
sessionTimeline: "target_detached",
});
trackBrowser(browser);
const result = await new CdpBrowserProvider().observeSession(
observeWebSessionInputSchema.parse({
cdp_endpoint: browser.endpoint,
allowed_origins: [browser.allowedOrigin],
target_id: "allowed-page",
observation_ms: 1_000,
}),
);
if (!result.ok) throw result.error;
expect(result.value.window.end_reason).toBe("session_detached");
expect(result.value.timeline.at(-1)).toMatchObject({
type: "session_detached",
detail: "session_detached",
timestamp: null,
});
expect(result.value.limitations).toContainEqual(
expect.stringMatching(
/Buffered events.*unknown timing.*context only.*do not establish.*observation window/iu,
),
);
});
it("reports target termination only for an observed target crash", async () => {
const browser = await startFakeCdpBrowser({
sessionTimeline: "target_crashed",
@@ -371,46 +343,6 @@ describe("CdpBrowserProvider: session termination and scope changes", () => {
expect(methods).not.toContain("Target.closeTarget");
});
it("ends immediately when a redirect leaves policy without exposing its URL", async () => {
const browser = await startFakeCdpBrowser({
sessionTimeline: "outside_policy",
});
trackBrowser(browser);
const result = await new CdpBrowserProvider().observeSession(
observeWebSessionInputSchema.parse({
cdp_endpoint: browser.endpoint,
allowed_origins: [browser.allowedOrigin],
target_id: "allowed-page",
observation_ms: 1_000,
}),
);
if (!result.ok) throw result.error;
expect(result.value.window.end_reason).toBe("target_left_scope");
expect(result.value.timeline.at(-1)).toMatchObject({
type: "redirect",
url: null,
destination_scope: "outside_policy",
redirect_response: {
url: `${browser.allowedOrigin}/reloaded?token=session-secret`,
status: 302,
scope: "approved",
},
});
expect(result.value.completeness).toMatchObject({
status: "policy_filtered",
policy_filtered_sections: ["timeline"],
excluded: [
{
section: "timeline",
reason: "out_of_target_scope",
count: 1,
},
],
});
expect(JSON.stringify(result.value)).not.toContain("private.example.test");
});
it("fails closed when the final session frame leaves policy without an event", async () => {
const browser = await startFakeCdpBrowser({
frameUrlAfterFirstRead: "https://private.example.test/final",
@@ -527,89 +459,3 @@ describe("CdpBrowserProvider: protocol failures and cancellation cleanup", () =>
}
});
});
describe("CdpBrowserProvider: navigation commit and target identity", () => {
it("waits for navigation commit and rechecks the attached main-frame origin", async () => {
const transitioning = await startFakeCdpBrowser({
transitionalFrameReads: 2,
});
trackBrowser(transitioning);
const input = (browser: FakeCdpBrowser) =>
inspectWebPageInputSchema.parse({
cdp_endpoint: browser.endpoint,
allowed_origins: [browser.allowedOrigin],
target_id: "allowed-page",
observation_ms: 0,
});
const committed = await new CdpBrowserProvider().inspectPage(
input(transitioning),
);
if (!committed.ok) throw committed.error;
expect(committed.value.target.origin).toBe(transitioning.allowedOrigin);
expect(committed.value.frames).toContainEqual(
expect.objectContaining({
frame_id: "frame-main",
origin: transitioning.allowedOrigin,
}),
);
const navigated = await startFakeCdpBrowser({
attachedFrameUrl: "https://unapproved.example.test/after-attach",
});
trackBrowser(navigated);
const denied = await new CdpBrowserProvider().inspectPage(input(navigated));
expect(denied).toMatchObject({
ok: false,
error: { _tag: "BrowserObservationError", reason: "target_not_allowed" },
});
const methods = navigated.commands.map(({ method }) => method);
expect(methods).toContain("Target.detachFromTarget");
expect(methods).not.toContain("DOMSnapshot.captureSnapshot");
expect(methods).not.toContain("Storage.getUsageAndQuota");
const duringObservation = await startFakeCdpBrowser({
navigateDuringObservationUrl:
"https://unapproved.example.test/during-observation",
});
trackBrowser(duringObservation);
const interrupted = await new CdpBrowserProvider().inspectPage(
input(duringObservation),
);
expect(interrupted).toMatchObject({
ok: false,
error: { _tag: "BrowserObservationError", reason: "target_not_allowed" },
});
expect(
duringObservation.commands.map(({ method }) => method),
).not.toContain("Accessibility.getFullAXTree");
const captureOptions: { navigateDuringCaptureUrl?: string } = {};
const duringCapture = await startFakeCdpBrowser(captureOptions);
captureOptions.navigateDuringCaptureUrl = `${duringCapture.allowedOrigin}/changed`;
trackBrowser(duringCapture);
const discarded = await new CdpBrowserProvider().inspectPage(
input(duringCapture),
);
expect(discarded).toMatchObject({
ok: false,
error: { _tag: "BrowserObservationError", reason: "target_changed" },
});
expect(duringCapture.commands.map(({ method }) => method)).toContain(
"Target.detachFromTarget",
);
const crossOriginCapture = await startFakeCdpBrowser({
navigateDuringCaptureUrl:
"https://unapproved.example.test/during-capture",
});
trackBrowser(crossOriginCapture);
const rejectedCapture = await new CdpBrowserProvider().inspectPage(
input(crossOriginCapture),
);
// The document changed, which is reported before the new document's scope.
expect(rejectedCapture).toMatchObject({
ok: false,
error: { _tag: "BrowserObservationError", reason: "target_changed" },
});
});
});
@@ -330,22 +330,43 @@ describe("CdpBrowserProvider navigation timeline", () => {
);
if (sessionTimeline === "outside_policy") {
expect(result.value.target.final_url).toBeNull();
expect(result.value.timeline).toContainEqual(
expect.objectContaining({
type: "redirect",
request_id: "document-request",
url: null,
destination_scope: "outside_policy",
}),
expect(result.value.timeline.at(-1)).toMatchObject({
type: "redirect",
request_id: "document-request",
url: null,
destination_scope: "outside_policy",
redirect_response: {
url: `${browser.allowedOrigin}/reloaded?token=session-secret`,
status: 302,
scope: "approved",
},
});
expect(result.value.completeness).toMatchObject({
status: "policy_filtered",
policy_filtered_sections: ["timeline"],
excluded: [
{ section: "timeline", reason: "out_of_target_scope", count: 1 },
],
});
expect(JSON.stringify(result.value)).not.toContain(
"private.example.test",
);
} else {
expect(result.value.timeline).toContainEqual(
expect.objectContaining({ type: "session_detached" }),
);
expect(result.value.timeline.at(-1)).toMatchObject({
type: "session_detached",
detail: "session_detached",
timestamp: null,
});
expect(result.value.timeline).not.toContainEqual(
expect.objectContaining({ type: "target_terminated" }),
);
expect(result.value.limitations).toContainEqual(
expect.stringMatching(
/Buffered events.*unknown timing.*context only.*do not establish.*observation window/iu,
),
);
}
await expect.poll(() => browser.activeConnectionCount()).toBe(0);
},
);
// Event count has no ceiling; only a dropped event truncates the timeline.
@@ -19,14 +19,14 @@ it("distinguishes document invalidation, origin refusal, and an independent scri
reason,
})),
),
{
...[false, true].map((sourceFails) => ({
navigation: {
command: "DOMSnapshot.captureSnapshot",
identity: { loaderId: "reloaded-document" },
},
sourceFails: true,
reason: "target_changed",
},
sourceFails,
reason: "target_changed" as const,
})),
{
navigation: {
command: "DOMSnapshot.captureSnapshot",
@@ -111,11 +111,13 @@ it("distinguishes document invalidation, origin refusal, and an independent scri
const methods = browser.commands
.slice(before)
.map(({ method }) => method);
if (
"navigation" in expected &&
expected.navigation.command === "Network.enable"
)
expect(methods, label).not.toContain("DOMSnapshot.captureSnapshot");
if ("navigation" in expected) {
expect(methods, label).toContain(expected.navigation.command);
if (expected.navigation.command === "Network.enable") {
expect(methods, label).not.toContain("DOMSnapshot.captureSnapshot");
expect(methods, label).not.toContain("Accessibility.getFullAXTree");
}
}
expect(methods, label).toContain("Target.detachFromTarget");
await expect.poll(() => browser.activeConnectionCount()).toBe(0);
}
-7
View File
@@ -274,13 +274,6 @@ const emitNetworkEvents = (
options: FakeOptions,
): void => {
if (command.method !== "Network.enable") return;
if (options.navigateDuringObservationUrl !== undefined)
event(socket, "Page.frameNavigated", command.sessionId, {
frame: {
id: "frame-main",
url: options.navigateDuringObservationUrl,
},
});
const url = `http://127.0.0.1:${String(port)}/api?token=network-secret`;
event(socket, "Network.requestWillBeSent", command.sessionId, {
requestId: "request-1",
+1 -3
View File
@@ -115,9 +115,7 @@ const frameTreeResult = (
port,
frameTreeReads > 1 && options.frameUrlAfterFirstRead !== undefined
? options.frameUrlAfterFirstRead
: frameTreeReads <= (options.transitionalFrameReads ?? 0)
? ":"
: (options.electronFileUrl ?? options.attachedFrameUrl),
: options.electronFileUrl,
options.extraCollections === true,
options.webMcpFrameCount,
);
-3
View File
@@ -75,10 +75,7 @@ export interface FakeOptions {
readonly closeOnMethod?: string;
readonly hangOnMethod?: string;
readonly unsupportedMethods?: readonly string[];
readonly transitionalFrameReads?: number;
readonly attachedFrameUrl?: string;
readonly frameUrlAfterFirstRead?: string;
readonly navigateDuringObservationUrl?: string;
readonly navigateDuringCaptureUrl?: string;
readonly navigateDuringScreenshotUrl?: string;
readonly extraCollections?: boolean;