Merge origin/main into feat/artifact-integrity-policy
Keep main's skill metadata version (27) and regenerate the package, tool catalog and product metadata for the merged tree. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,106 @@
|
||||
import { execFile, spawn } from "node:child_process";
|
||||
import { once } from "node:events";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { promisify } from "node:util";
|
||||
import { expect, it, onTestFinished } from "vitest";
|
||||
import { parseEvidence } from "../../src/domain/evidence.js";
|
||||
import { webEventListenersSchema } from "../../src/domain/webEventListeners.js";
|
||||
import { startRuntimeBrowser } from "../fixtures/webRuntime.js";
|
||||
|
||||
const entrypoint = fileURLToPath(
|
||||
new URL("../../scripts/rea.mjs", import.meta.url),
|
||||
);
|
||||
const environment = () => ({
|
||||
...process.env,
|
||||
REA_LOG_LEVEL: "silent",
|
||||
HOPPER_LAUNCHER_PATH: "/rea-unconfigured-provider/hopper",
|
||||
});
|
||||
|
||||
it("runs the compiled public listener command through its default native CDP composition", async () => {
|
||||
const browser = await startRuntimeBrowser();
|
||||
onTestFinished(() => browser.close());
|
||||
const { stdout } = await promisify(execFile)(
|
||||
process.execPath,
|
||||
[
|
||||
entrypoint,
|
||||
"inspect-web-event-listeners",
|
||||
browser.endpoint,
|
||||
"allowed-page",
|
||||
"#selected",
|
||||
"--json",
|
||||
],
|
||||
{ env: environment(), timeout: 20_000 },
|
||||
);
|
||||
const result = webEventListenersSchema.parse(
|
||||
parseEvidence(JSON.parse(stdout)).normalized_result,
|
||||
);
|
||||
expect(result.listeners[0]?.location).toMatchObject({
|
||||
script_id: "script-a",
|
||||
source_association: "script_id",
|
||||
});
|
||||
expect(result.selected_node.selector).toBe("#selected");
|
||||
});
|
||||
|
||||
it("translates SIGINT after real CLI arming into awaited instrumentation cleanup and a nonzero CLI failure", async () => {
|
||||
const browser = await startRuntimeBrowser();
|
||||
onTestFinished(() => browser.close());
|
||||
const child = spawn(
|
||||
process.execPath,
|
||||
[
|
||||
entrypoint,
|
||||
"observe-web-execution",
|
||||
browser.endpoint,
|
||||
"allowed-page",
|
||||
"--observation-ms",
|
||||
"30000",
|
||||
"--json",
|
||||
],
|
||||
{ env: environment(), stdio: ["ignore", "pipe", "pipe"] },
|
||||
);
|
||||
onTestFinished(() => {
|
||||
if (child.exitCode === null && child.signalCode === null)
|
||||
child.kill("SIGKILL");
|
||||
});
|
||||
let stderr = "";
|
||||
let stdout = "";
|
||||
let armed = false;
|
||||
const timer = setTimeout(() => child.kill("SIGKILL"), 20_000);
|
||||
child.stdout.on("data", (chunk) => {
|
||||
stdout += chunk;
|
||||
});
|
||||
child.stderr.on("data", (chunk) => {
|
||||
stderr += chunk;
|
||||
if (
|
||||
!armed &&
|
||||
stderr.includes('"completed":1') &&
|
||||
stderr.includes("browser_execution")
|
||||
) {
|
||||
armed = true;
|
||||
child.kill("SIGINT");
|
||||
}
|
||||
});
|
||||
try {
|
||||
const [code, signal] = await once(child, "close");
|
||||
expect(armed, stderr).toBe(true);
|
||||
expect(code, stderr).not.toBe(0);
|
||||
expect(signal).toBeNull();
|
||||
const result: unknown = JSON.parse(stdout);
|
||||
expect(result).toMatchObject({
|
||||
code: "cancelled",
|
||||
category: "cancelled",
|
||||
details: { cleanup: "complete", operation: "observe_web_execution" },
|
||||
});
|
||||
expect(
|
||||
browser.commands.some(
|
||||
(command) => command.method === "Profiler.stopPreciseCoverage",
|
||||
),
|
||||
).toBe(true);
|
||||
expect(
|
||||
browser.commands.some(
|
||||
(command) => command.method === "Target.detachFromTarget",
|
||||
),
|
||||
).toBe(true);
|
||||
} finally {
|
||||
clearTimeout(timer);
|
||||
}
|
||||
});
|
||||
@@ -62,4 +62,26 @@ describe("CDP connection", () => {
|
||||
await connection.close();
|
||||
}
|
||||
});
|
||||
it("preserves the selected payload limit reason for pending and subsequent commands", async () => {
|
||||
const browser = await startFakeCdpBrowser({
|
||||
commandResult: () => ({ oversized: "x".repeat(2_048) }),
|
||||
});
|
||||
browsers.push(browser);
|
||||
const connection = await CdpConnection.connect(
|
||||
browser.browserWebSocketUrl,
|
||||
"observe_web_execution",
|
||||
undefined,
|
||||
{ maxPayloadBytes: 512 },
|
||||
);
|
||||
try {
|
||||
for (const method of ["Runtime.enable", "Debugger.enable"]) {
|
||||
await expect(connection.send(method)).rejects.toMatchObject({
|
||||
reason: "payload_limit",
|
||||
userMessage: expect.stringContaining("512 byte protocol budget"),
|
||||
});
|
||||
}
|
||||
} finally {
|
||||
await connection.close();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
import { expect, it, onTestFinished } from "vitest";
|
||||
import { CdpConnection } from "../../../src/browser/CdpConnection.js";
|
||||
import type { BrowserObservationError } from "../../../src/domain/browserObservationError.js";
|
||||
import { startFakeCdpBrowser } from "../../fixtures/fakeCdpBrowser.js";
|
||||
|
||||
it("reports one fatal wire failure without a pending command and retains it for late subscribers", async () => {
|
||||
const browser = await startFakeCdpBrowser();
|
||||
onTestFinished(() => browser.close());
|
||||
const connection = await CdpConnection.connect(
|
||||
browser.browserWebSocketUrl,
|
||||
"observe_web_execution",
|
||||
);
|
||||
onTestFinished(() => connection.close());
|
||||
const failures: BrowserObservationError[] = [];
|
||||
const failure = new Promise<BrowserObservationError>((resolve) => {
|
||||
connection.onProtocolFailure((error) => {
|
||||
failures.push(error);
|
||||
resolve(error);
|
||||
});
|
||||
});
|
||||
browser.emitRawMessage("{not-json");
|
||||
browser.emitRawMessage(JSON.stringify({ method: 42 }));
|
||||
expect(await failure).toMatchObject({ reason: "protocol_error" });
|
||||
let lateFailure: BrowserObservationError | undefined;
|
||||
connection.onProtocolFailure((error) => {
|
||||
lateFailure = error;
|
||||
});
|
||||
expect(lateFailure).toMatchObject({ reason: "protocol_error" });
|
||||
await expect(connection.send("Page.getFrameTree")).rejects.toMatchObject({
|
||||
reason: "protocol_error",
|
||||
});
|
||||
expect(failures).toHaveLength(1);
|
||||
expect(browser.commands).toEqual([]);
|
||||
});
|
||||
@@ -0,0 +1,433 @@
|
||||
import { createHash } from "node:crypto";
|
||||
import { describe, expect, it, onTestFinished } from "vitest";
|
||||
import { CdpWebRuntimeProvider } from "../../../src/browser/execution/CdpWebRuntimeProvider.js";
|
||||
import { observeWebExecutionInputSchema } from "../../../src/domain/webExecution.js";
|
||||
import { inspectWebEventListenersInputSchema } from "../../../src/domain/webEventListeners.js";
|
||||
import {
|
||||
runtimeSourceA,
|
||||
runtimeSourceB,
|
||||
startRuntimeBrowser,
|
||||
} from "../../fixtures/webRuntime.js";
|
||||
import type { FakeOptions } from "../../fixtures/fakeCdpBrowser.js";
|
||||
|
||||
const fixture = async (options: FakeOptions = {}) => {
|
||||
const browser = await startRuntimeBrowser(options);
|
||||
onTestFinished(() => browser.close());
|
||||
const provider = new CdpWebRuntimeProvider();
|
||||
const input = {
|
||||
cdp_endpoint: browser.endpoint,
|
||||
target_id: "allowed-page",
|
||||
observation_ms: 5,
|
||||
};
|
||||
return {
|
||||
browser,
|
||||
provider,
|
||||
execution: observeWebExecutionInputSchema.parse(input),
|
||||
listeners: inspectWebEventListenersInputSchema.parse({
|
||||
cdp_endpoint: browser.endpoint,
|
||||
target_id: "allowed-page",
|
||||
selector: "#selected",
|
||||
}),
|
||||
};
|
||||
};
|
||||
|
||||
describe("native runtime producer boundary", () => {
|
||||
it("binds complete producer stacks by script ID and ignores foreign sessions without URL guessing", async () => {
|
||||
const { browser, provider, execution } = await fixture();
|
||||
const params = {
|
||||
requestId: "selected-request",
|
||||
frameId: "runtime-main",
|
||||
timestamp: 12.5,
|
||||
request: {
|
||||
url: `${browser.endpoint}/request?caller-value=retained`,
|
||||
method: "POST",
|
||||
},
|
||||
initiator: {
|
||||
type: "script",
|
||||
producerExtension: "retain-unknown-fields",
|
||||
stack: {
|
||||
description: "observed-sync-stack",
|
||||
callFrames: [
|
||||
{
|
||||
scriptId: "script-b",
|
||||
url: `${browser.endpoint}/same.js`,
|
||||
functionName: "alias",
|
||||
lineNumber: 0,
|
||||
columnNumber: 2,
|
||||
},
|
||||
],
|
||||
parent: {
|
||||
description: "observed-async-stack",
|
||||
callFrames: [
|
||||
{
|
||||
scriptId: "unknown-script",
|
||||
url: `${browser.endpoint}/same.js`,
|
||||
functionName: "unknown",
|
||||
lineNumber: 0,
|
||||
columnNumber: 3,
|
||||
},
|
||||
],
|
||||
parentId: { id: "async-parent", debuggerId: "other-debugger" },
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
const result = await provider.observeExecution(
|
||||
{ ...execution, observation_ms: 30 },
|
||||
{
|
||||
progress: {
|
||||
report: async (update) => {
|
||||
if (update.completed !== 1) return;
|
||||
browser.emitEvent({
|
||||
method: "Network.requestWillBeSent",
|
||||
params,
|
||||
sessionId: "foreign-session",
|
||||
});
|
||||
browser.emitEvent({
|
||||
method: "Network.requestWillBeSent",
|
||||
params,
|
||||
sessionId: "session-1",
|
||||
});
|
||||
},
|
||||
},
|
||||
},
|
||||
);
|
||||
if (!result.ok) throw result.error;
|
||||
expect(result.value.requests).toHaveLength(1);
|
||||
expect(result.value.requests[0]?.reported_initiator).toEqual(
|
||||
params.initiator,
|
||||
);
|
||||
expect(result.value.requests[0]?.callsites).toMatchObject([
|
||||
{ script_id: "script-b", source_association: "script_id" },
|
||||
{ script_id: "unknown-script", source_association: "unknown" },
|
||||
]);
|
||||
expect(result.value.requests[0]?.async_parent_ids).toEqual([
|
||||
{ id: "async-parent", debugger_id: "other-debugger" },
|
||||
]);
|
||||
expect(result.value.requests[0]?.causal_attribution).toBe("unknown");
|
||||
});
|
||||
});
|
||||
|
||||
describe("runtime window and producer granularity", () => {
|
||||
it("ends at document replacement and leaves coverage explicitly unavailable", async () => {
|
||||
const { browser, provider, execution } = await fixture();
|
||||
const result = await provider.observeExecution(
|
||||
{ ...execution, observation_ms: 30_000 },
|
||||
{
|
||||
progress: {
|
||||
report: async (update) => {
|
||||
if (update.completed === 1)
|
||||
browser.emitEvent({
|
||||
method: "Page.frameNavigated",
|
||||
sessionId: "session-1",
|
||||
params: {
|
||||
frame: {
|
||||
id: "runtime-main",
|
||||
url: `${browser.endpoint}/new-document`,
|
||||
},
|
||||
},
|
||||
});
|
||||
},
|
||||
},
|
||||
},
|
||||
);
|
||||
if (!result.ok) throw result.error;
|
||||
expect(result.value.window.end_reason).toBe("document_changed");
|
||||
expect(result.value.coverage.state).toBe("unavailable");
|
||||
expect(result.value.instrumentation.takes_one_resetting_sample).toBe(false);
|
||||
});
|
||||
|
||||
it("reports disconnected cleanup as unknown instead of successful detachment", async () => {
|
||||
const { provider, execution } = await fixture({
|
||||
closeOnMethod: "Profiler.takePreciseCoverage",
|
||||
});
|
||||
const result = await provider.observeExecution(execution);
|
||||
if (result.ok)
|
||||
throw new Error("Disconnect cannot confirm instrumentation cleanup");
|
||||
expect(result.error.cleanupIncomplete).toBe(true);
|
||||
});
|
||||
|
||||
it("retains function-only producer granularity without fabricating zero branches", async () => {
|
||||
const { provider, execution } = await fixture({
|
||||
commandResult: (command, origin) =>
|
||||
command.method === "Profiler.takePreciseCoverage"
|
||||
? {
|
||||
timestamp: 13,
|
||||
result: [
|
||||
{
|
||||
scriptId: "script-a",
|
||||
url: `${origin}/same.js`,
|
||||
functions: [
|
||||
{
|
||||
functionName: "selected",
|
||||
isBlockCoverage: false,
|
||||
ranges: [
|
||||
{
|
||||
startOffset: 0,
|
||||
endOffset: runtimeSourceA.length,
|
||||
count: 1,
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
}
|
||||
: undefined,
|
||||
});
|
||||
const result = await provider.observeExecution(execution);
|
||||
if (!result.ok) throw result.error;
|
||||
expect(result.value.coverage.scripts[0]?.functions[0]).toMatchObject({
|
||||
is_block_coverage: false,
|
||||
ranges: [{ count: 1 }],
|
||||
});
|
||||
expect(result.value.script_inventory.not_reported_script_ids).toContain(
|
||||
"script-b",
|
||||
);
|
||||
expect(result.value.script_inventory.coverage_absence).toBe("unknown");
|
||||
});
|
||||
});
|
||||
|
||||
describe("runtime source and listener identity", () => {
|
||||
it("retains same-URL script identities, UTF-16 bounds, nested zero counts and actual cleanup", async () => {
|
||||
const { browser, provider, execution } = await fixture();
|
||||
const result = await provider.observeExecution(execution);
|
||||
if (!result.ok) throw result.error;
|
||||
expect(
|
||||
result.value.coverage.scripts.map((script) => script.script_id),
|
||||
).toEqual(["script-a", "script-b"]);
|
||||
expect(result.value.coverage.excluded_scripts).toBe(1);
|
||||
expect(
|
||||
result.value.sources.map((script) =>
|
||||
script.source.state === "captured" ? script.source.text : null,
|
||||
),
|
||||
).toEqual([runtimeSourceA, runtimeSourceB]);
|
||||
const source = result.value.sources[0];
|
||||
expect(source?.producer_hash).toBe("producer-reported-hash");
|
||||
expect(source?.source).toMatchObject({
|
||||
sha256: createHash("sha256").update(runtimeSourceA).digest("hex"),
|
||||
utf16_units: runtimeSourceA.length,
|
||||
utf8_bytes: Buffer.byteLength(runtimeSourceA),
|
||||
});
|
||||
expect(
|
||||
result.value.coverage.scripts[0]?.functions[0]?.ranges[1],
|
||||
).toMatchObject({ count: 0, source_bounds: "verified" });
|
||||
expect(
|
||||
browser.commands.some(
|
||||
(command) => command.method === "Profiler.stopPreciseCoverage",
|
||||
),
|
||||
).toBe(true);
|
||||
expect(
|
||||
browser.commands.some(
|
||||
(command) => command.method === "Target.detachFromTarget",
|
||||
),
|
||||
).toBe(true);
|
||||
expect(
|
||||
browser.commands.some((command) =>
|
||||
["Browser.close", "Target.closeTarget", "Runtime.evaluate"].includes(
|
||||
command.method,
|
||||
),
|
||||
),
|
||||
).toBe(false);
|
||||
});
|
||||
|
||||
it("inspects listener source and releases only its owned object group", async () => {
|
||||
const { browser, provider, listeners } = await fixture();
|
||||
const result = await provider.inspectEventListeners(listeners);
|
||||
if (!result.ok) throw result.error;
|
||||
expect(result.value.listeners[0]).toMatchObject({
|
||||
type: "click",
|
||||
location: {
|
||||
script_id: "script-a",
|
||||
source_association: "script_id",
|
||||
line_number: 1,
|
||||
},
|
||||
execution: "unknown",
|
||||
});
|
||||
const resolve = browser.commands.find(
|
||||
(command) => command.method === "DOM.resolveNode",
|
||||
);
|
||||
const release = browser.commands.find(
|
||||
(command) => command.method === "Runtime.releaseObjectGroup",
|
||||
);
|
||||
expect(release?.params.objectGroup).toBe(resolve?.params.objectGroup);
|
||||
expect(resolve?.params.objectGroup).toMatch(/^rea-listeners-/u);
|
||||
});
|
||||
|
||||
it("returns an explicit missing node without source/handler evaluation", async () => {
|
||||
const { provider, listeners } = await fixture();
|
||||
const result = await provider.inspectEventListeners({
|
||||
...listeners,
|
||||
selector: "#missing",
|
||||
});
|
||||
if (!result.ok) throw result.error;
|
||||
expect(result.value.selected_node.match).toBe("not_found");
|
||||
expect(result.value.listeners).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("runtime lifecycle and invalid producer data", () => {
|
||||
it("releases instrumentation after cancellation delivered at the actual armed point", async () => {
|
||||
const { browser, provider, execution } = await fixture();
|
||||
const controller = new AbortController();
|
||||
const result = await provider.observeExecution(
|
||||
{ ...execution, observation_ms: 30_000 },
|
||||
{
|
||||
signal: controller.signal,
|
||||
progress: {
|
||||
report: async (update) => {
|
||||
if (update.completed === 1) controller.abort();
|
||||
},
|
||||
},
|
||||
},
|
||||
);
|
||||
expect(result.ok).toBe(false);
|
||||
if (result.ok)
|
||||
throw new Error("Cancellation must not produce successful evidence");
|
||||
expect(result.error._tag).toBe("AnalysisCancelledError");
|
||||
expect(
|
||||
browser.commands.some(
|
||||
(command) => command.method === "Profiler.stopPreciseCoverage",
|
||||
),
|
||||
).toBe(true);
|
||||
});
|
||||
|
||||
it("returns cleanup failure when coverage stop cannot be confirmed", async () => {
|
||||
const { browser, provider, execution } = await fixture({
|
||||
unsupportedMethods: ["Profiler.stopPreciseCoverage"],
|
||||
});
|
||||
const result = await provider.observeExecution(execution);
|
||||
expect(result.ok).toBe(false);
|
||||
if (result.ok) throw new Error("Failed cleanup must not return success");
|
||||
expect(result.error.cleanupIncomplete).toBe(true);
|
||||
expect(
|
||||
browser.commands.some(
|
||||
(command) => command.method === "Target.detachFromTarget",
|
||||
),
|
||||
).toBe(true);
|
||||
});
|
||||
|
||||
it("preserves the unsupported command reason", async () => {
|
||||
const { provider, execution } = await fixture({
|
||||
unsupportedMethods: ["Profiler.startPreciseCoverage"],
|
||||
});
|
||||
const result = await provider.observeExecution(execution);
|
||||
if (result.ok) throw new Error("Unsupported instrumentation must fail");
|
||||
expect(result.error.userMessage).toContain("Profiler.startPreciseCoverage");
|
||||
expect(result.error.userMessage).toContain("Method not found");
|
||||
});
|
||||
|
||||
it("rejects a malformed sample and still stops coverage", async () => {
|
||||
const { browser, provider, execution } = await fixture({
|
||||
commandResult: (command) =>
|
||||
command.method === "Profiler.takePreciseCoverage"
|
||||
? { timestamp: 1, result: "wrong" }
|
||||
: undefined,
|
||||
});
|
||||
const result = await provider.observeExecution(execution);
|
||||
if (result.ok) throw new Error("Malformed producer coverage must fail");
|
||||
expect(result.error._tag).toBe("AnalysisOutputError");
|
||||
expect(
|
||||
browser.commands.some(
|
||||
(command) => command.method === "Profiler.stopPreciseCoverage",
|
||||
),
|
||||
).toBe(true);
|
||||
});
|
||||
|
||||
it("rejects a source range measured as UTF-8 bytes instead of UTF-16 units", async () => {
|
||||
const { provider, execution } = await fixture({
|
||||
commandResult: (command, origin) =>
|
||||
command.method === "Profiler.takePreciseCoverage"
|
||||
? {
|
||||
timestamp: 13,
|
||||
result: [
|
||||
{
|
||||
scriptId: "script-a",
|
||||
url: `${origin}/same.js`,
|
||||
functions: [
|
||||
{
|
||||
functionName: "bad",
|
||||
isBlockCoverage: true,
|
||||
ranges: [
|
||||
{
|
||||
startOffset: 0,
|
||||
endOffset: Buffer.byteLength(runtimeSourceA),
|
||||
count: 1,
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
}
|
||||
: undefined,
|
||||
});
|
||||
const result = await provider.observeExecution(execution);
|
||||
if (result.ok)
|
||||
throw new Error("Byte offsets must not be mislabeled UTF-16");
|
||||
expect(result.error._tag).toBe("AnalysisOutputError");
|
||||
});
|
||||
});
|
||||
|
||||
describe("runtime document identity and authorization failures", () => {
|
||||
it.each(["execution", "listeners"] as const)(
|
||||
"rejects newly reported loader identity for %s when the initial loader was unknown",
|
||||
async (action) => {
|
||||
const { provider, execution, listeners } = await fixture({
|
||||
commandResult: (command, origin, frameReads) =>
|
||||
command.method === "Page.getFrameTree"
|
||||
? {
|
||||
frameTree: {
|
||||
frame: {
|
||||
id: "runtime-main",
|
||||
url: `${origin}/allowed`,
|
||||
...(frameReads === 1 ? {} : { loaderId: "new-document" }),
|
||||
},
|
||||
},
|
||||
}
|
||||
: undefined,
|
||||
});
|
||||
const result =
|
||||
action === "execution"
|
||||
? await provider.observeExecution(execution)
|
||||
: await provider.inspectEventListeners(listeners);
|
||||
if (result.ok)
|
||||
throw new Error(
|
||||
"Unknown loader must not accept a newly reported document identity",
|
||||
);
|
||||
expect(result.error).toMatchObject({ reason: "target_changed" });
|
||||
},
|
||||
);
|
||||
|
||||
it.each(["execution", "listeners"] as const)(
|
||||
"reports the selected %s operation when the live frame origin is denied",
|
||||
async (action) => {
|
||||
const { provider, execution, listeners } = await fixture({
|
||||
commandResult: (command) =>
|
||||
command.method === "Page.getFrameTree"
|
||||
? {
|
||||
frameTree: {
|
||||
frame: {
|
||||
id: "runtime-main",
|
||||
url: "https://unselected.example/document",
|
||||
loaderId: "denied",
|
||||
},
|
||||
},
|
||||
}
|
||||
: undefined,
|
||||
});
|
||||
const result =
|
||||
action === "execution"
|
||||
? await provider.observeExecution(execution)
|
||||
: await provider.inspectEventListeners(listeners);
|
||||
if (result.ok) throw new Error("Unselected live origin must be denied");
|
||||
expect(result.error).toMatchObject({
|
||||
reason: "target_not_allowed",
|
||||
operation:
|
||||
action === "execution"
|
||||
? "observe_web_execution"
|
||||
: "inspect_web_event_listeners",
|
||||
});
|
||||
},
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,140 @@
|
||||
import { describe, expect, it, onTestFinished } from "vitest";
|
||||
import { CdpWebRuntimeProvider } from "../../../src/browser/execution/CdpWebRuntimeProvider.js";
|
||||
import type { CdpEvent } from "../../../src/browser/CdpConnection.js";
|
||||
import { observeWebExecutionInputSchema } from "../../../src/domain/webExecution.js";
|
||||
import { inspectWebEventListenersInputSchema } from "../../../src/domain/webEventListeners.js";
|
||||
import { startRuntimeBrowser } from "../../fixtures/webRuntime.js";
|
||||
|
||||
describe("runtime final collection boundaries", () => {
|
||||
it("retains a script parsed during the final resetting coverage command", async () => {
|
||||
let emitEvent: ((event: CdpEvent) => void) | undefined;
|
||||
const browser = await startRuntimeBrowser({
|
||||
commandResult: (command, origin) => {
|
||||
if (command.method === "Profiler.takePreciseCoverage") {
|
||||
emitEvent?.({
|
||||
method: "Debugger.scriptParsed",
|
||||
sessionId: "session-1",
|
||||
params: {
|
||||
scriptId: "late-script",
|
||||
url: `${origin}/late.js`,
|
||||
executionContextId: 1,
|
||||
executionContextAuxData: { frameId: "runtime-main" },
|
||||
startLine: 0,
|
||||
startColumn: 0,
|
||||
endLine: 0,
|
||||
endColumn: 1,
|
||||
},
|
||||
});
|
||||
return {
|
||||
timestamp: 12,
|
||||
result: [
|
||||
{
|
||||
scriptId: "late-script",
|
||||
url: `${origin}/late.js`,
|
||||
functions: [
|
||||
{
|
||||
functionName: "late",
|
||||
isBlockCoverage: true,
|
||||
ranges: [{ startOffset: 0, endOffset: 1, count: 1 }],
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
};
|
||||
}
|
||||
if (
|
||||
command.method === "Debugger.getScriptSource" &&
|
||||
command.params.scriptId === "late-script"
|
||||
)
|
||||
return { scriptSource: ";" };
|
||||
return undefined;
|
||||
},
|
||||
});
|
||||
emitEvent = browser.emitEvent;
|
||||
onTestFinished(() => browser.close());
|
||||
const result = await new CdpWebRuntimeProvider().observeExecution(
|
||||
observeWebExecutionInputSchema.parse({
|
||||
cdp_endpoint: browser.endpoint,
|
||||
target_id: "allowed-page",
|
||||
observation_ms: 5,
|
||||
}),
|
||||
);
|
||||
if (!result.ok) throw result.error;
|
||||
expect(result.value.coverage.scripts[0]).toMatchObject({
|
||||
script_id: "late-script",
|
||||
functions: [{ ranges: [{ count: 1, source_bounds: "verified" }] }],
|
||||
});
|
||||
expect(
|
||||
result.value.sources.find(({ script_id }) => script_id === "late-script")
|
||||
?.source,
|
||||
).toMatchObject({ state: "captured", text: ";" });
|
||||
expect(result.value.coverage.excluded_scripts).toBe(0);
|
||||
});
|
||||
|
||||
it.each(["#selected", "#missing"])(
|
||||
"rejects known malformed source metadata during final listener assertion for %s",
|
||||
async (selector) => {
|
||||
let emitEvent: ((event: CdpEvent) => void) | undefined;
|
||||
const browser = await startRuntimeBrowser({
|
||||
commandResult: (command, _origin, reads) => {
|
||||
if (command.method === "Page.getFrameTree" && reads === 3)
|
||||
emitEvent?.({
|
||||
method: "Debugger.scriptParsed",
|
||||
sessionId: "session-1",
|
||||
params: {},
|
||||
});
|
||||
return undefined;
|
||||
},
|
||||
});
|
||||
emitEvent = browser.emitEvent;
|
||||
onTestFinished(() => browser.close());
|
||||
const result = await new CdpWebRuntimeProvider().inspectEventListeners(
|
||||
inspectWebEventListenersInputSchema.parse({
|
||||
cdp_endpoint: browser.endpoint,
|
||||
target_id: "allowed-page",
|
||||
selector,
|
||||
}),
|
||||
);
|
||||
if (result.ok)
|
||||
throw new Error(
|
||||
"Known producer failure cannot return partial listener success",
|
||||
);
|
||||
expect(result.error._tag).toBe("AnalysisOutputError");
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
describe("runtime fatal protocol stream", () => {
|
||||
it.each(["{not-json", JSON.stringify({ method: 42 })])(
|
||||
"stops an armed command-free window on malformed wire data %s",
|
||||
async (message) => {
|
||||
const browser = await startRuntimeBrowser();
|
||||
onTestFinished(() => browser.close());
|
||||
const result = await new CdpWebRuntimeProvider().observeExecution(
|
||||
observeWebExecutionInputSchema.parse({
|
||||
cdp_endpoint: browser.endpoint,
|
||||
target_id: "allowed-page",
|
||||
observation_ms: 2_147_483_647,
|
||||
}),
|
||||
{
|
||||
signal: AbortSignal.timeout(2_000),
|
||||
progress: {
|
||||
report: async (update) => {
|
||||
if (update.completed === 1) browser.emitRawMessage(message);
|
||||
},
|
||||
},
|
||||
},
|
||||
);
|
||||
if (result.ok)
|
||||
throw new Error(
|
||||
"Malformed wire data must not produce successful evidence",
|
||||
);
|
||||
expect(result.error).toMatchObject({
|
||||
cleanupIncomplete: true,
|
||||
diagnostics: {
|
||||
previous_error: expect.stringMatching(/protocol|malformed/iu),
|
||||
},
|
||||
});
|
||||
},
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,133 @@
|
||||
import { describe, expect, it, onTestFinished } from "vitest";
|
||||
import { CdpWebRuntimeProvider } from "../../../src/browser/execution/CdpWebRuntimeProvider.js";
|
||||
import { observeWebExecutionInputSchema } from "../../../src/domain/webExecution.js";
|
||||
import { startRuntimeBrowser } from "../../fixtures/webRuntime.js";
|
||||
|
||||
describe("runtime initiator URL representation", () => {
|
||||
it.each(["before", "after"])(
|
||||
"omits proven transport userinfo and preserves declared names with metadata %s the request",
|
||||
async (ordering) => {
|
||||
const browser = await startRuntimeBrowser();
|
||||
onTestFinished(() => browser.close());
|
||||
const resourceUrl =
|
||||
browser.endpoint.replace("http://", "http://transport:credential@") +
|
||||
"/resource.js?caller=value#fragment";
|
||||
const declaredUrl =
|
||||
browser.endpoint.replace("http://", "http://declared:label@") +
|
||||
"/declaration.js?caller=value#fragment";
|
||||
const metadata = (
|
||||
scriptId: string,
|
||||
url: string,
|
||||
hasSourceURL: boolean,
|
||||
) => ({
|
||||
scriptId,
|
||||
url,
|
||||
hasSourceURL,
|
||||
executionContextId: 999,
|
||||
startLine: 0,
|
||||
startColumn: 0,
|
||||
endLine: 1,
|
||||
endColumn: 0,
|
||||
});
|
||||
const frames = [
|
||||
{
|
||||
scriptId: "resource-script",
|
||||
url: resourceUrl,
|
||||
functionName: "resource",
|
||||
lineNumber: 0,
|
||||
columnNumber: 0,
|
||||
},
|
||||
{
|
||||
scriptId: "declared-script",
|
||||
url: declaredUrl,
|
||||
functionName: "declared",
|
||||
lineNumber: 0,
|
||||
columnNumber: 0,
|
||||
},
|
||||
];
|
||||
const initiator = {
|
||||
type: "script",
|
||||
url: resourceUrl,
|
||||
lineNumber: 0,
|
||||
producerExtension: { url: declaredUrl, opaque: "keep caller data" },
|
||||
stack: {
|
||||
description: "producer sync",
|
||||
callFrames: [frames[0]],
|
||||
parent: {
|
||||
description: "producer async",
|
||||
callFrames: [frames[1]],
|
||||
parentId: { id: "retained-parent" },
|
||||
},
|
||||
},
|
||||
};
|
||||
const result = await new CdpWebRuntimeProvider().observeExecution(
|
||||
observeWebExecutionInputSchema.parse({
|
||||
cdp_endpoint: browser.endpoint,
|
||||
target_id: "allowed-page",
|
||||
observation_ms: 30,
|
||||
}),
|
||||
{
|
||||
progress: {
|
||||
report: async (update) => {
|
||||
if (update.completed !== 1) return;
|
||||
const scripts = () => {
|
||||
for (const params of [
|
||||
metadata("resource-script", resourceUrl, false),
|
||||
metadata("declared-script", declaredUrl, true),
|
||||
])
|
||||
browser.emitEvent({
|
||||
method: "Debugger.scriptParsed",
|
||||
params,
|
||||
sessionId: "session-1",
|
||||
});
|
||||
};
|
||||
if (ordering === "before") scripts();
|
||||
browser.emitEvent({
|
||||
method: "Network.requestWillBeSent",
|
||||
sessionId: "session-1",
|
||||
params: {
|
||||
requestId: "request",
|
||||
frameId: "runtime-main",
|
||||
timestamp: 12,
|
||||
request: {
|
||||
url: `${browser.endpoint}/selected`,
|
||||
method: "GET",
|
||||
},
|
||||
initiator,
|
||||
},
|
||||
});
|
||||
if (ordering === "after") scripts();
|
||||
},
|
||||
},
|
||||
},
|
||||
);
|
||||
if (!result.ok) throw result.error;
|
||||
const sanitized = `${browser.endpoint}/resource.js?caller=value#fragment`;
|
||||
expect(result.value.requests[0]?.reported_initiator).toEqual({
|
||||
...initiator,
|
||||
url: sanitized,
|
||||
stack: {
|
||||
...initiator.stack,
|
||||
callFrames: [{ ...frames[0], url: sanitized }],
|
||||
},
|
||||
});
|
||||
expect(result.value.requests[0]?.callsites.map(({ url }) => url)).toEqual(
|
||||
[sanitized, declaredUrl, sanitized],
|
||||
);
|
||||
expect(
|
||||
result.value.sources.find(
|
||||
({ script_id }) => script_id === "resource-script",
|
||||
)?.url,
|
||||
).toBe(sanitized);
|
||||
expect(
|
||||
result.value.sources.find(
|
||||
({ script_id }) => script_id === "declared-script",
|
||||
)?.url,
|
||||
).toBe(declaredUrl);
|
||||
expect(JSON.stringify(result.value)).not.toContain(
|
||||
"transport:credential@",
|
||||
);
|
||||
expect(JSON.stringify(result.value)).toContain("declared:label@");
|
||||
},
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,219 @@
|
||||
import { describe, expect, it, onTestFinished } from "vitest";
|
||||
import { CdpWebRuntimeProvider } from "../../../src/browser/execution/CdpWebRuntimeProvider.js";
|
||||
import { observeWebExecutionInputSchema } from "../../../src/domain/webExecution.js";
|
||||
import { inspectWebEventListenersInputSchema } from "../../../src/domain/webEventListeners.js";
|
||||
import { startRuntimeBrowser } from "../../fixtures/webRuntime.js";
|
||||
|
||||
describe("runtime document identity across same-document URL changes", () => {
|
||||
it.each(["execution", "listeners"] as const)(
|
||||
"accepts a stable loader for %s while retaining the initial location",
|
||||
async (operation) => {
|
||||
const browser = await startRuntimeBrowser({
|
||||
commandResult: (command, origin, reads) =>
|
||||
command.method === "Page.getFrameTree"
|
||||
? {
|
||||
frameTree: {
|
||||
frame: {
|
||||
id: "runtime-main",
|
||||
url: `${origin}/${reads === 1 ? "allowed" : "new-path?caller=value#fragment"}`,
|
||||
loaderId: "same-document",
|
||||
},
|
||||
},
|
||||
}
|
||||
: undefined,
|
||||
});
|
||||
onTestFinished(() => browser.close());
|
||||
const provider = new CdpWebRuntimeProvider();
|
||||
const scope = {
|
||||
cdp_endpoint: browser.endpoint,
|
||||
target_id: "allowed-page",
|
||||
};
|
||||
const result =
|
||||
operation === "execution"
|
||||
? await provider.observeExecution(
|
||||
observeWebExecutionInputSchema.parse({
|
||||
...scope,
|
||||
observation_ms: 5,
|
||||
}),
|
||||
)
|
||||
: await provider.inspectEventListeners(
|
||||
inspectWebEventListenersInputSchema.parse({
|
||||
...scope,
|
||||
selector: "#selected",
|
||||
}),
|
||||
);
|
||||
if (!result.ok) throw result.error;
|
||||
expect(result.value.target.initial_url).toBe(
|
||||
`${browser.endpoint}/allowed`,
|
||||
);
|
||||
expect(result.value.target.loader_id).toBe("same-document");
|
||||
},
|
||||
);
|
||||
|
||||
it.each([
|
||||
{ loader: undefined, denied: false, reason: "target_changed" },
|
||||
{ loader: "same-document", denied: true, reason: "target_not_allowed" },
|
||||
])(
|
||||
"preserves conservative identity and origin checks: $reason",
|
||||
async ({ loader, denied, reason }) => {
|
||||
const browser = await startRuntimeBrowser({
|
||||
commandResult: (command, origin, reads) =>
|
||||
command.method === "Page.getFrameTree"
|
||||
? {
|
||||
frameTree: {
|
||||
frame: {
|
||||
id: "runtime-main",
|
||||
url:
|
||||
reads === 1
|
||||
? `${origin}/allowed`
|
||||
: denied
|
||||
? "https://unselected.example/new"
|
||||
: `${origin}/new`,
|
||||
...(loader === undefined ? {} : { loaderId: loader }),
|
||||
},
|
||||
},
|
||||
}
|
||||
: undefined,
|
||||
});
|
||||
onTestFinished(() => browser.close());
|
||||
const result = await new CdpWebRuntimeProvider().inspectEventListeners(
|
||||
inspectWebEventListenersInputSchema.parse({
|
||||
cdp_endpoint: browser.endpoint,
|
||||
target_id: "allowed-page",
|
||||
selector: "#selected",
|
||||
}),
|
||||
);
|
||||
if (result.ok) throw new Error("Unverified document or origin must fail");
|
||||
expect(result.error).toMatchObject({ reason });
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
describe("runtime collector failure and uncertain source metadata", () => {
|
||||
it.each(["Debugger.scriptParsed", "Network.requestWillBeSent"])(
|
||||
"stops an armed long window immediately for malformed %s",
|
||||
async (method) => {
|
||||
const browser = await startRuntimeBrowser();
|
||||
onTestFinished(() => browser.close());
|
||||
const result = await new CdpWebRuntimeProvider().observeExecution(
|
||||
observeWebExecutionInputSchema.parse({
|
||||
cdp_endpoint: browser.endpoint,
|
||||
target_id: "allowed-page",
|
||||
observation_ms: 2_147_483_647,
|
||||
}),
|
||||
{
|
||||
signal: AbortSignal.timeout(2_000),
|
||||
progress: {
|
||||
report: async (update) => {
|
||||
if (update.completed === 1)
|
||||
browser.emitEvent({
|
||||
method,
|
||||
params: {},
|
||||
sessionId: "session-1",
|
||||
});
|
||||
},
|
||||
},
|
||||
},
|
||||
);
|
||||
if (result.ok) throw new Error("Malformed producer data must fail");
|
||||
expect(result.error._tag).toBe("AnalysisOutputError");
|
||||
expect(
|
||||
browser.commands.some(
|
||||
({ method: command }) => command === "Profiler.stopPreciseCoverage",
|
||||
),
|
||||
).toBe(true);
|
||||
expect(
|
||||
browser.commands.some(
|
||||
({ method: command }) => command === "Target.detachFromTarget",
|
||||
),
|
||||
).toBe(true);
|
||||
},
|
||||
);
|
||||
|
||||
it("retains reported metadata without claiming ownership or reading unowned source", async () => {
|
||||
const browser = await startRuntimeBrowser();
|
||||
onTestFinished(() => browser.close());
|
||||
const script = {
|
||||
scriptId: "uncertain-script",
|
||||
url: `${browser.endpoint}/declared.js?caller=value#fragment`,
|
||||
executionContextId: 999,
|
||||
hash: "observed-hash",
|
||||
sourceMapURL: "../declared.map?query=value#fragment",
|
||||
hasSourceURL: true,
|
||||
scriptLanguage: "JavaScript",
|
||||
startLine: 4,
|
||||
startColumn: 8,
|
||||
endLine: 8,
|
||||
endColumn: 0,
|
||||
};
|
||||
const result = await new CdpWebRuntimeProvider().observeExecution(
|
||||
observeWebExecutionInputSchema.parse({
|
||||
cdp_endpoint: browser.endpoint,
|
||||
target_id: "allowed-page",
|
||||
observation_ms: 30,
|
||||
}),
|
||||
{
|
||||
progress: {
|
||||
report: async (update) => {
|
||||
if (update.completed !== 1) return;
|
||||
browser.emitEvent({
|
||||
method: "Debugger.scriptParsed",
|
||||
params: script,
|
||||
sessionId: "session-1",
|
||||
});
|
||||
browser.emitEvent({
|
||||
method: "Network.requestWillBeSent",
|
||||
sessionId: "session-1",
|
||||
params: {
|
||||
requestId: "uncertain-request",
|
||||
frameId: "runtime-main",
|
||||
timestamp: 12,
|
||||
request: { url: `${browser.endpoint}/request`, method: "GET" },
|
||||
initiator: {
|
||||
type: "script",
|
||||
stack: {
|
||||
callFrames: [
|
||||
{
|
||||
scriptId: script.scriptId,
|
||||
url: script.url,
|
||||
functionName: "unknown",
|
||||
lineNumber: 4,
|
||||
columnNumber: 8,
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
},
|
||||
},
|
||||
},
|
||||
);
|
||||
if (!result.ok) throw result.error;
|
||||
expect(
|
||||
result.value.sources.find(
|
||||
({ script_id }) => script_id === script.scriptId,
|
||||
),
|
||||
).toMatchObject({
|
||||
url: script.url,
|
||||
execution_context_id: 999,
|
||||
frame_id: null,
|
||||
producer_hash: script.hash,
|
||||
source_map_url: script.sourceMapURL,
|
||||
has_source_url: true,
|
||||
language: "JavaScript",
|
||||
resource_start: { line_number: 4, column_number: 8 },
|
||||
source: { state: "excluded" },
|
||||
});
|
||||
expect(result.value.requests[0]?.callsites[0]?.source_association).toBe(
|
||||
"unknown",
|
||||
);
|
||||
expect(
|
||||
browser.commands.some(
|
||||
({ method, params }) =>
|
||||
method === "Debugger.getScriptSource" &&
|
||||
params.scriptId === script.scriptId,
|
||||
),
|
||||
).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,44 @@
|
||||
import { expect, it, onTestFinished } from "vitest";
|
||||
import { CdpWebRuntimeProvider } from "../../../src/browser/execution/CdpWebRuntimeProvider.js";
|
||||
import { inspectWebEventListenersInputSchema } from "../../../src/domain/webEventListeners.js";
|
||||
import { startRuntimeBrowser } from "../../fixtures/webRuntime.js";
|
||||
|
||||
it.each([
|
||||
{
|
||||
code: -32000,
|
||||
message: "DOM Error while querying",
|
||||
expected: "AnalysisInputError",
|
||||
},
|
||||
{
|
||||
code: -32000,
|
||||
message: "Could not find node with given id",
|
||||
expected: "BrowserObservationError",
|
||||
},
|
||||
{
|
||||
code: -32601,
|
||||
message: "Method not found",
|
||||
expected: "BrowserObservationError",
|
||||
},
|
||||
])(
|
||||
"classifies the actual selector rejection $message",
|
||||
async ({ code, message, expected }) => {
|
||||
const browser = await startRuntimeBrowser({
|
||||
commandError: ({ method }) =>
|
||||
method === "DOM.querySelector" ? { code, message } : undefined,
|
||||
});
|
||||
onTestFinished(() => browser.close());
|
||||
const result = await new CdpWebRuntimeProvider().inspectEventListeners(
|
||||
inspectWebEventListenersInputSchema.parse({
|
||||
cdp_endpoint: browser.endpoint,
|
||||
target_id: "allowed-page",
|
||||
selector: "[",
|
||||
}),
|
||||
);
|
||||
if (result.ok)
|
||||
throw new Error("Rejected selectors cannot produce listener evidence");
|
||||
expect(result.error._tag).toBe(expected);
|
||||
expect(result.error.cleanupIncomplete).toBe(false);
|
||||
if (result.error._tag === "AnalysisInputError")
|
||||
expect(result.error.message).toContain("inspect_web_event_listeners");
|
||||
},
|
||||
);
|
||||
@@ -0,0 +1,136 @@
|
||||
import { expect, it, onTestFinished } from "vitest";
|
||||
import type { CdpEvent } from "../../../src/browser/CdpConnection.js";
|
||||
import { CdpWebRuntimeProvider } from "../../../src/browser/execution/CdpWebRuntimeProvider.js";
|
||||
import { observeWebExecutionInputSchema } from "../../../src/domain/webExecution.js";
|
||||
import { inspectWebEventListenersInputSchema } from "../../../src/domain/webEventListeners.js";
|
||||
import { startRuntimeBrowser } from "../../fixtures/webRuntime.js";
|
||||
|
||||
it.each(
|
||||
["listeners", "execution"].flatMap((operation) =>
|
||||
["source_read", "other_source_read", "final_assertion"].map((phase) => ({
|
||||
operation,
|
||||
phase,
|
||||
})),
|
||||
),
|
||||
)(
|
||||
"revalidates $operation joins when a known script changes at $phase",
|
||||
async ({ operation, phase }) => {
|
||||
let emit: ((event: CdpEvent) => void) | undefined;
|
||||
const browser = await startRuntimeBrowser({
|
||||
commandResult: (command, origin, reads) => {
|
||||
if (
|
||||
(phase === "source_read" &&
|
||||
command.method === "Debugger.getScriptSource" &&
|
||||
command.params.scriptId === "script-a") ||
|
||||
(phase === "other_source_read" &&
|
||||
command.method === "Debugger.getScriptSource" &&
|
||||
command.params.scriptId === "script-b") ||
|
||||
(phase === "final_assertion" &&
|
||||
command.method === "Page.getFrameTree" &&
|
||||
reads === (operation === "listeners" ? 3 : 4))
|
||||
)
|
||||
emit?.({
|
||||
sessionId: "session-1",
|
||||
method: "Debugger.scriptParsed",
|
||||
params: {
|
||||
scriptId: "script-a",
|
||||
url: `${origin}/changed.js`,
|
||||
executionContextId: 1,
|
||||
executionContextAuxData: { frameId: "runtime-main" },
|
||||
startLine: 0,
|
||||
startColumn: 0,
|
||||
endLine: 1,
|
||||
endColumn: 0,
|
||||
hash: "changed-producer-hash",
|
||||
hasSourceURL: true,
|
||||
scriptLanguage: "JavaScript",
|
||||
},
|
||||
});
|
||||
if (command.method === "DOMDebugger.getEventListeners")
|
||||
return {
|
||||
listeners: ["script-a", "script-b"].map((scriptId) => ({
|
||||
type: "click",
|
||||
useCapture: false,
|
||||
passive: true,
|
||||
once: false,
|
||||
scriptId,
|
||||
lineNumber: 0,
|
||||
columnNumber: 0,
|
||||
})),
|
||||
};
|
||||
return undefined;
|
||||
},
|
||||
});
|
||||
emit = browser.emitEvent;
|
||||
onTestFinished(() => browser.close());
|
||||
const provider = new CdpWebRuntimeProvider();
|
||||
const scope = { cdp_endpoint: browser.endpoint, target_id: "allowed-page" };
|
||||
if (operation === "listeners") {
|
||||
const result = await provider.inspectEventListeners(
|
||||
inspectWebEventListenersInputSchema.parse({
|
||||
...scope,
|
||||
selector: "#selected",
|
||||
}),
|
||||
);
|
||||
if (!result.ok) throw result.error;
|
||||
expect(result.value.listeners[0]?.location.source_association).toBe(
|
||||
"unknown",
|
||||
);
|
||||
expect(result.value.sources[0]).toMatchObject({
|
||||
frame_id: null,
|
||||
source: { state: "excluded" },
|
||||
});
|
||||
} else {
|
||||
const result = await provider.observeExecution(
|
||||
observeWebExecutionInputSchema.parse({ ...scope, observation_ms: 5 }),
|
||||
{
|
||||
progress: {
|
||||
report: async (update) => {
|
||||
if (update.completed !== 1) return;
|
||||
browser.emitEvent({
|
||||
sessionId: "session-1",
|
||||
method: "Network.requestWillBeSent",
|
||||
params: {
|
||||
requestId: "source-completion-request",
|
||||
frameId: "runtime-main",
|
||||
timestamp: 12,
|
||||
request: {
|
||||
url: `${new URL(browser.endpoint).origin}/selected`,
|
||||
method: "GET",
|
||||
},
|
||||
initiator: {
|
||||
type: "script",
|
||||
stack: {
|
||||
callFrames: [
|
||||
{
|
||||
scriptId: "script-a",
|
||||
url: `${new URL(browser.endpoint).origin}/same.js`,
|
||||
lineNumber: 0,
|
||||
columnNumber: 0,
|
||||
functionName: "selected",
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
},
|
||||
},
|
||||
},
|
||||
);
|
||||
if (!result.ok) throw result.error;
|
||||
expect(
|
||||
result.value.coverage.scripts.some(
|
||||
({ script_id }) => script_id === "script-a",
|
||||
),
|
||||
).toBe(false);
|
||||
expect(result.value.requests[0]?.callsites[0]?.source_association).toBe(
|
||||
"unknown",
|
||||
);
|
||||
expect(result.value.script_inventory.main_document_scripts).toBe(1);
|
||||
expect(
|
||||
result.value.sources.find(({ script_id }) => script_id === "script-a"),
|
||||
).toMatchObject({ frame_id: null, source: { state: "excluded" } });
|
||||
}
|
||||
},
|
||||
);
|
||||
@@ -0,0 +1,176 @@
|
||||
import { describe, expect, it, onTestFinished } from "vitest";
|
||||
import { CdpWebRuntimeProvider } from "../../../src/browser/execution/CdpWebRuntimeProvider.js";
|
||||
import type { CdpEvent } from "../../../src/browser/CdpConnection.js";
|
||||
import { observeWebExecutionInputSchema } from "../../../src/domain/webExecution.js";
|
||||
import { startRuntimeBrowser } from "../../fixtures/webRuntime.js";
|
||||
|
||||
describe("runtime flat-session lifecycle", () => {
|
||||
it("rejects a producer detachment between counter startup and local arming", async () => {
|
||||
let emitEvent: ((event: CdpEvent) => void) | undefined;
|
||||
const browser = await startRuntimeBrowser({
|
||||
commandResult: (command) => {
|
||||
if (command.method !== "Profiler.startPreciseCoverage")
|
||||
return undefined;
|
||||
emitEvent?.({
|
||||
method: "Target.detachedFromTarget",
|
||||
params: { sessionId: "session-1" },
|
||||
});
|
||||
return { timestamp: 12 };
|
||||
},
|
||||
});
|
||||
emitEvent = browser.emitEvent;
|
||||
onTestFinished(() => browser.close());
|
||||
let reportedArmed = false;
|
||||
const result = await new CdpWebRuntimeProvider().observeExecution(
|
||||
observeWebExecutionInputSchema.parse({
|
||||
cdp_endpoint: browser.endpoint,
|
||||
target_id: "allowed-page",
|
||||
observation_ms: 2_147_483_647,
|
||||
}),
|
||||
{
|
||||
signal: AbortSignal.timeout(2_000),
|
||||
progress: {
|
||||
report: async (update) => {
|
||||
if (update.completed === 1) reportedArmed = true;
|
||||
},
|
||||
},
|
||||
},
|
||||
);
|
||||
if (result.ok)
|
||||
throw new Error("A terminated target cannot arm an execution window");
|
||||
expect(result.error).toMatchObject({ reason: "target_changed" });
|
||||
expect(reportedArmed).toBe(false);
|
||||
});
|
||||
|
||||
it("ends immediately on its root detachment without confusing another session", async () => {
|
||||
const browser = await startRuntimeBrowser();
|
||||
onTestFinished(() => browser.close());
|
||||
const result = await new CdpWebRuntimeProvider().observeExecution(
|
||||
observeWebExecutionInputSchema.parse({
|
||||
cdp_endpoint: browser.endpoint,
|
||||
target_id: "allowed-page",
|
||||
observation_ms: 2_147_483_647,
|
||||
}),
|
||||
{
|
||||
signal: AbortSignal.timeout(2_000),
|
||||
progress: {
|
||||
report: async (update) => {
|
||||
if (update.completed !== 1) return;
|
||||
browser.emitEvent({
|
||||
method: "Target.detachedFromTarget",
|
||||
params: { sessionId: "another-session" },
|
||||
});
|
||||
browser.emitEvent({
|
||||
method: "Target.detachedFromTarget",
|
||||
params: { sessionId: "session-1" },
|
||||
});
|
||||
},
|
||||
},
|
||||
},
|
||||
);
|
||||
if (!result.ok) throw result.error;
|
||||
expect(result.value.window.end_reason).toBe("target_terminated");
|
||||
expect(result.value.coverage.state).toBe("unavailable");
|
||||
expect(result.value.instrumentation.takes_one_resetting_sample).toBe(false);
|
||||
});
|
||||
|
||||
it("ignores root detachment for a different flat session", async () => {
|
||||
const browser = await startRuntimeBrowser();
|
||||
onTestFinished(() => browser.close());
|
||||
const result = await new CdpWebRuntimeProvider().observeExecution(
|
||||
observeWebExecutionInputSchema.parse({
|
||||
cdp_endpoint: browser.endpoint,
|
||||
target_id: "allowed-page",
|
||||
observation_ms: 30,
|
||||
}),
|
||||
{
|
||||
progress: {
|
||||
report: async (update) => {
|
||||
if (update.completed === 1)
|
||||
browser.emitEvent({
|
||||
method: "Target.detachedFromTarget",
|
||||
params: { sessionId: "another-session" },
|
||||
});
|
||||
},
|
||||
},
|
||||
},
|
||||
);
|
||||
if (!result.ok) throw result.error;
|
||||
expect(result.value.window.end_reason).toBe("window_elapsed");
|
||||
});
|
||||
});
|
||||
|
||||
describe("coverage URL representation", () => {
|
||||
it("sanitizes proven resource URLs while preserving declared source names", async () => {
|
||||
let resourceUrl = "";
|
||||
let declaredUrl = "";
|
||||
const browser = await startRuntimeBrowser({
|
||||
commandResult: (command) => {
|
||||
if (command.method === "Profiler.takePreciseCoverage")
|
||||
return {
|
||||
timestamp: 12,
|
||||
result: [
|
||||
{ scriptId: "transport-script", url: resourceUrl, functions: [] },
|
||||
{ scriptId: "declared-script", url: declaredUrl, functions: [] },
|
||||
],
|
||||
};
|
||||
if (
|
||||
command.method === "Debugger.getScriptSource" &&
|
||||
["transport-script", "declared-script"].includes(
|
||||
String(command.params.scriptId),
|
||||
)
|
||||
)
|
||||
return { scriptSource: ";" };
|
||||
return undefined;
|
||||
},
|
||||
});
|
||||
onTestFinished(() => browser.close());
|
||||
resourceUrl =
|
||||
browser.endpoint.replace("http://", "http://transport:credential@") +
|
||||
"/resource.js?caller=value#fragment";
|
||||
declaredUrl =
|
||||
browser.endpoint.replace("http://", "http://declared:label@") +
|
||||
"/declared.js?caller=value#fragment";
|
||||
const result = await new CdpWebRuntimeProvider().observeExecution(
|
||||
observeWebExecutionInputSchema.parse({
|
||||
cdp_endpoint: browser.endpoint,
|
||||
target_id: "allowed-page",
|
||||
observation_ms: 30,
|
||||
}),
|
||||
{
|
||||
progress: {
|
||||
report: async (update) => {
|
||||
if (update.completed !== 1) return;
|
||||
for (const [scriptId, url, hasSourceURL] of [
|
||||
["transport-script", resourceUrl, false],
|
||||
["declared-script", declaredUrl, true],
|
||||
] as const)
|
||||
browser.emitEvent({
|
||||
method: "Debugger.scriptParsed",
|
||||
sessionId: "session-1",
|
||||
params: {
|
||||
scriptId,
|
||||
url,
|
||||
hasSourceURL,
|
||||
executionContextId: 1,
|
||||
executionContextAuxData: { frameId: "runtime-main" },
|
||||
startLine: 0,
|
||||
startColumn: 0,
|
||||
endLine: 0,
|
||||
endColumn: 1,
|
||||
},
|
||||
});
|
||||
},
|
||||
},
|
||||
},
|
||||
);
|
||||
if (!result.ok) throw result.error;
|
||||
expect(
|
||||
result.value.coverage.scripts.map(({ reported_url }) => reported_url),
|
||||
).toEqual([
|
||||
`${browser.endpoint}/resource.js?caller=value#fragment`,
|
||||
declaredUrl,
|
||||
]);
|
||||
expect(JSON.stringify(result.value)).not.toContain("transport:credential@");
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,121 @@
|
||||
import { describe, expect, it, onTestFinished } from "vitest";
|
||||
import { CdpWebRuntimeProvider } from "../../../src/browser/execution/CdpWebRuntimeProvider.js";
|
||||
import { observeWebExecutionInputSchema } from "../../../src/domain/webExecution.js";
|
||||
import { inspectWebEventListenersInputSchema } from "../../../src/domain/webEventListeners.js";
|
||||
import { startRuntimeBrowser } from "../../fixtures/webRuntime.js";
|
||||
|
||||
const unknownWorld = { frameId: "runtime-main" };
|
||||
const defaultWorld = { ...unknownWorld, isDefault: true, type: "default" };
|
||||
const isolatedWorld = { ...unknownWorld, isDefault: false, type: "isolated" };
|
||||
|
||||
describe("runtime execution world ownership", () => {
|
||||
it.each([
|
||||
{ name: "isolated context", context: isolatedWorld, script: isolatedWorld },
|
||||
{
|
||||
name: "isolated script metadata",
|
||||
context: undefined,
|
||||
script: isolatedWorld,
|
||||
},
|
||||
{ name: "unknown world", context: unknownWorld, script: unknownWorld },
|
||||
{
|
||||
name: "contradictory worlds",
|
||||
context: isolatedWorld,
|
||||
script: defaultWorld,
|
||||
},
|
||||
{
|
||||
name: "isolated script with default context",
|
||||
context: defaultWorld,
|
||||
script: isolatedWorld,
|
||||
},
|
||||
{
|
||||
name: "worker context",
|
||||
context: { ...unknownWorld, type: "worker" },
|
||||
script: unknownWorld,
|
||||
},
|
||||
])(
|
||||
"does not attribute $name to website execution or listeners",
|
||||
async ({ context, script }) => {
|
||||
const browser = await startRuntimeBrowser({
|
||||
commandEvents: (command, origin) => {
|
||||
if (command.method === "Runtime.enable")
|
||||
return context === undefined
|
||||
? []
|
||||
: [
|
||||
{
|
||||
sessionId: "session-1",
|
||||
method: "Runtime.executionContextCreated",
|
||||
params: {
|
||||
context: {
|
||||
id: 1,
|
||||
origin,
|
||||
name: "reported-world",
|
||||
auxData: context,
|
||||
},
|
||||
},
|
||||
},
|
||||
];
|
||||
if (command.method === "Debugger.enable")
|
||||
return ["script-a", "script-b"].map((scriptId) => ({
|
||||
sessionId: "session-1",
|
||||
method: "Debugger.scriptParsed",
|
||||
params: {
|
||||
scriptId,
|
||||
url: `${origin}/same.js`,
|
||||
executionContextId: 1,
|
||||
executionContextAuxData: script,
|
||||
startLine: 0,
|
||||
startColumn: 0,
|
||||
endLine: 1,
|
||||
endColumn: 0,
|
||||
},
|
||||
}));
|
||||
return undefined;
|
||||
},
|
||||
});
|
||||
onTestFinished(() => browser.close());
|
||||
const provider = new CdpWebRuntimeProvider();
|
||||
const scope = {
|
||||
cdp_endpoint: browser.endpoint,
|
||||
target_id: "allowed-page",
|
||||
};
|
||||
const execution = await provider.observeExecution(
|
||||
observeWebExecutionInputSchema.parse({ ...scope, observation_ms: 5 }),
|
||||
);
|
||||
if (!execution.ok) throw execution.error;
|
||||
expect(execution.value.coverage.scripts).toEqual([]);
|
||||
expect(execution.value.coverage.excluded_scripts).toBe(3);
|
||||
expect(execution.value.script_inventory.main_document_scripts).toBe(0);
|
||||
const listeners = await provider.inspectEventListeners(
|
||||
inspectWebEventListenersInputSchema.parse({
|
||||
...scope,
|
||||
selector: "#selected",
|
||||
}),
|
||||
);
|
||||
if (!listeners.ok) throw listeners.error;
|
||||
expect(listeners.value.listeners[0]?.location.source_association).toBe(
|
||||
"unknown",
|
||||
);
|
||||
expect(listeners.value.sources[0]).toMatchObject({
|
||||
source: { state: "excluded" },
|
||||
});
|
||||
expect(
|
||||
listeners.value.sources[0]?.reported_script_context_aux_data,
|
||||
).toEqual(script);
|
||||
expect(listeners.value.sources[0]?.reported_execution_context).toEqual(
|
||||
context === undefined
|
||||
? null
|
||||
: {
|
||||
id: 1,
|
||||
origin: new URL(browser.endpoint).origin,
|
||||
name: "reported-world",
|
||||
auxData: context,
|
||||
},
|
||||
);
|
||||
expect(
|
||||
browser.commands.filter(
|
||||
({ method }) => method === "Debugger.getScriptSource",
|
||||
),
|
||||
).toEqual([]);
|
||||
},
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,35 @@
|
||||
import { execFile } from "node:child_process";
|
||||
import { promisify } from "node:util";
|
||||
import { expect, it } from "vitest";
|
||||
|
||||
it("attempts every fixture cleanup and retains the primary capture failure", async () => {
|
||||
const helper = new URL(
|
||||
"../../../scripts/lib/browser-runtime-fixture-lifecycle.mjs",
|
||||
import.meta.url,
|
||||
);
|
||||
const script = `
|
||||
const { closeRuntimeFixtureResources } = await import(${JSON.stringify(helper.href)});
|
||||
const seen = [];
|
||||
try {
|
||||
await closeRuntimeFixtureResources([
|
||||
async () => { seen.push('first'); throw new Error('first cleanup failed'); },
|
||||
async () => { seen.push('second'); throw new Error('second cleanup failed'); },
|
||||
async () => { seen.push('third'); },
|
||||
], new Error('capture failed'));
|
||||
throw new Error('Expected aggregate cleanup failure');
|
||||
} catch (error) {
|
||||
if (!(error instanceof AggregateError)) throw error;
|
||||
console.log(JSON.stringify({ seen, errors: error.errors.map(item => item.message) }));
|
||||
}
|
||||
`;
|
||||
const { stdout } = await promisify(execFile)(
|
||||
process.execPath,
|
||||
["--input-type=module", "-e", script],
|
||||
{ timeout: 10_000 },
|
||||
);
|
||||
const result: unknown = JSON.parse(stdout);
|
||||
expect(result).toEqual({
|
||||
seen: ["first", "second", "third"],
|
||||
errors: ["capture failed", "first cleanup failed", "second cleanup failed"],
|
||||
});
|
||||
});
|
||||
@@ -6,7 +6,7 @@ import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
|
||||
|
||||
import { writeAnalysisSnapshot } from "../../../src/application/binary/AnalysisSnapshotFiles.js";
|
||||
import { runDirectAnalysis } from "../../../src/application/DirectAnalysis.js";
|
||||
import { runDirectAnalysis } from "../../../src/composition/directAnalysis.js";
|
||||
import type { AnalysisSnapshot } from "../../../src/domain/analysisSnapshot.js";
|
||||
import {
|
||||
snapshotBinding,
|
||||
|
||||
@@ -11,7 +11,7 @@ import { describe, expect, it } from "vitest";
|
||||
|
||||
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
|
||||
|
||||
import { runProviderAnalysis } from "../../../src/application/DirectAnalysis.js";
|
||||
import { runProviderAnalysis } from "../../../src/composition/directAnalysis.js";
|
||||
import { parseEvidence } from "../../../src/domain/evidence.js";
|
||||
import {
|
||||
identifyRuntimes,
|
||||
|
||||
@@ -5,7 +5,7 @@ import { expect, it } from "vitest";
|
||||
import { z } from "zod";
|
||||
|
||||
import { parseConfig } from "../../../src/config.js";
|
||||
import { createBinarySession } from "../../../src/application/runtime.js";
|
||||
import { createBinarySession } from "../../../src/composition/binary.js";
|
||||
import { createServer } from "../../../src/server/createServer.js";
|
||||
import { artifactInspectionResultSchema } from "../../../src/domain/artifactInspection.js";
|
||||
import { artifactInventoryResultSchema } from "../../../src/domain/artifactGraph.js";
|
||||
|
||||
@@ -6,7 +6,7 @@ import { TextReader, Uint8ArrayWriter, ZipWriter } from "@zip.js/zip.js";
|
||||
import { expect, it } from "vitest";
|
||||
import { z } from "zod";
|
||||
import { parseConfig } from "../../../src/config.js";
|
||||
import { createBinarySession } from "../../../src/application/runtime.js";
|
||||
import { createBinarySession } from "../../../src/composition/binary.js";
|
||||
import { keyedArchiveResultSchema } from "../../../src/domain/apple/keyedArchive.js";
|
||||
|
||||
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
|
||||
|
||||
@@ -10,7 +10,7 @@ import {
|
||||
} from "@zip.js/zip.js";
|
||||
import { afterEach, expect, it } from "vitest";
|
||||
|
||||
import { runProviderAnalysis } from "../../../src/application/DirectAnalysis.js";
|
||||
import { runProviderAnalysis } from "../../../src/composition/directAnalysis.js";
|
||||
import { parseEvidence } from "../../../src/domain/evidence.js";
|
||||
import { createTestBinarySession } from "../../fixtures/binarySession.js";
|
||||
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
|
||||
|
||||
@@ -4,7 +4,7 @@ import { join } from "node:path";
|
||||
import { describe, expect } from "vitest";
|
||||
import { z } from "zod";
|
||||
|
||||
import { createBinarySession } from "../../../src/application/runtime.js";
|
||||
import { createBinarySession } from "../../../src/composition/binary.js";
|
||||
import { parseConfig } from "../../../src/config.js";
|
||||
import { createServer } from "../../../src/server/createServer.js";
|
||||
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
|
||||
|
||||
@@ -2,7 +2,7 @@ import type { StdioServerHandle } from "@modelcontextprotocol/server/stdio";
|
||||
import pino from "pino";
|
||||
import { expect, it } from "vitest";
|
||||
|
||||
import { createManagedBinarySession } from "../../../src/application/runtime.js";
|
||||
import { createManagedBinarySession } from "../../../src/composition/binary.js";
|
||||
import { MCP_SHUTDOWN_FAILED } from "../../../src/main/messages.js";
|
||||
import { createShutdown } from "../../../src/main/shutdown.js";
|
||||
import type { RuntimeDependencies } from "../../../src/main/types.js";
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
import { Client, InMemoryTransport } from "@modelcontextprotocol/client";
|
||||
import { Ajv2020 } from "ajv/dist/2020.js";
|
||||
import { expect, it, onTestFinished } from "vitest";
|
||||
import { WebRuntimeService } from "../../../src/application/WebRuntimeService.js";
|
||||
import { recordingWebRuntimePort } from "../../../src/application/WebRuntimeService.fixture.js";
|
||||
import { toolContract } from "../../../src/contracts/toolContracts.js";
|
||||
import { parseEvidence } from "../../../src/domain/evidence.js";
|
||||
import { createServer } from "../../../src/server/createServer.js";
|
||||
import { createTestBinarySession } from "../../fixtures/binarySession.js";
|
||||
|
||||
it("advertises actual SDK schemas, instrumentation effects and session-owned inline runtime evidence", async () => {
|
||||
const session = createTestBinarySession(() => {
|
||||
throw new Error("Binary provider must not start");
|
||||
});
|
||||
const server = createServer(session, session, {
|
||||
webRuntime: new WebRuntimeService(recordingWebRuntimePort()),
|
||||
});
|
||||
const client = new Client({ name: "web-runtime-contract", version: "1" });
|
||||
onTestFinished(async () => {
|
||||
await client.close();
|
||||
await server.close();
|
||||
await session.close();
|
||||
});
|
||||
const [clientTransport, serverTransport] =
|
||||
InMemoryTransport.createLinkedPair();
|
||||
await server.connect(serverTransport);
|
||||
await client.connect(clientTransport);
|
||||
const tools = (await client.listTools()).tools;
|
||||
const ajv = new Ajv2020({ strict: false, validateFormats: false });
|
||||
const input = {
|
||||
cdp_endpoint: "http://127.0.0.1:9222",
|
||||
target_id: "allowed-page",
|
||||
observation_ms: 5,
|
||||
};
|
||||
for (const name of [
|
||||
"observe_web_execution",
|
||||
"inspect_web_event_listeners",
|
||||
] as const) {
|
||||
const advertised = tools.find((tool) => tool.name === name);
|
||||
if (advertised?.outputSchema === undefined)
|
||||
throw new Error(`Missing advertised runtime schemas: ${name}`);
|
||||
const inputSchema: Record<string, unknown> = advertised.inputSchema;
|
||||
const outputSchema: Record<string, unknown> = advertised.outputSchema;
|
||||
expect(ajv.validateSchema(inputSchema)).toBe(true);
|
||||
expect(ajv.validateSchema(outputSchema)).toBe(true);
|
||||
const args =
|
||||
name === "observe_web_execution"
|
||||
? input
|
||||
: {
|
||||
cdp_endpoint: "http://127.0.0.1:9222",
|
||||
target_id: "allowed-page",
|
||||
selector: "#selected",
|
||||
};
|
||||
expect(ajv.validate(inputSchema, args)).toBe(true);
|
||||
const response = await client.callTool({ name, arguments: args });
|
||||
expect(response.isError).not.toBe(true);
|
||||
expect(
|
||||
ajv.validate(outputSchema, response.structuredContent),
|
||||
JSON.stringify(ajv.errors),
|
||||
).toBe(true);
|
||||
const parsed = toolContract(name).outputSchema.parse(
|
||||
response.structuredContent,
|
||||
);
|
||||
const evidence = parseEvidence(parsed.evidence);
|
||||
expect(parsed.result).toEqual(evidence.normalized_result);
|
||||
expect(session.evidenceById(evidence.evidence_id)).toEqual(evidence);
|
||||
}
|
||||
expect(toolContract("observe_web_execution").effects).toMatchObject({
|
||||
mutatesTarget: true,
|
||||
mayDiscardData: true,
|
||||
idempotent: false,
|
||||
});
|
||||
const invalid = await client.callTool({
|
||||
name: "observe_web_execution",
|
||||
arguments: { ...input, observation_ms: 2_147_483_648 },
|
||||
});
|
||||
expect(invalid.isError).toBe(true);
|
||||
});
|
||||
@@ -5,7 +5,7 @@ import { describe, expect, it } from "vitest";
|
||||
|
||||
import { createTestTempDirectory } from "../../../fixtures/temporaryDirectory.js";
|
||||
|
||||
import { runProviderAnalysis } from "../../../../src/application/DirectAnalysis.js";
|
||||
import { runProviderAnalysis } from "../../../../src/composition/directAnalysis.js";
|
||||
import { parseEvidence } from "../../../../src/domain/evidence.js";
|
||||
import { buildManagedPeFixture } from "../../../../src/dotnet/ManagedPe.fixture.js";
|
||||
|
||||
|
||||
@@ -7,7 +7,7 @@ import { describe, expect, it } from "vitest";
|
||||
|
||||
import { createTestTempDirectory } from "../../../fixtures/temporaryDirectory.js";
|
||||
|
||||
import { runProviderAnalysis } from "../../../../src/application/DirectAnalysis.js";
|
||||
import { runProviderAnalysis } from "../../../../src/composition/directAnalysis.js";
|
||||
import { ArtifactProvider } from "../../../../src/artifacts/ArtifactProvider.js";
|
||||
import { artifactInventoryResultSchema } from "../../../../src/domain/artifactGraph.js";
|
||||
import { artifactInspectionResultSchema } from "../../../../src/domain/artifactInspection.js";
|
||||
|
||||
@@ -6,7 +6,7 @@ import { describe, expect, it } from "vitest";
|
||||
|
||||
import { createTestTempDirectory } from "../../../fixtures/temporaryDirectory.js";
|
||||
|
||||
import { runProviderAnalysis } from "../../../../src/application/DirectAnalysis.js";
|
||||
import { runProviderAnalysis } from "../../../../src/composition/directAnalysis.js";
|
||||
import {
|
||||
ArtifactPathRegistry,
|
||||
normalizeArtifactPath,
|
||||
|
||||
@@ -5,7 +5,7 @@ import { createPackageWithOptions } from "@electron/asar";
|
||||
import { expect, it } from "vitest";
|
||||
|
||||
import { createTestTempDirectory } from "../../../fixtures/temporaryDirectory.js";
|
||||
import { runProviderAnalysis } from "../../../../src/application/DirectAnalysis.js";
|
||||
import { runProviderAnalysis } from "../../../../src/composition/directAnalysis.js";
|
||||
import { artifactInspectionResultSchema } from "../../../../src/domain/artifactInspection.js";
|
||||
import { parseEvidence } from "../../../../src/domain/evidence.js";
|
||||
|
||||
|
||||
@@ -17,7 +17,7 @@ import {
|
||||
} from "../../support/applicationSessionFixture.js";
|
||||
|
||||
import { projectAppleApplicationEvidence } from "../../../src/application/apple/AppleApplicationService.js";
|
||||
import { runProviderAnalysis } from "../../../src/application/DirectAnalysis.js";
|
||||
import { runProviderAnalysis } from "../../../src/composition/directAnalysis.js";
|
||||
import { appleApplicationProjectionResultSchema } from "../../../src/domain/apple/appleApplication.js";
|
||||
import { parseEvidence } from "../../../src/domain/evidence.js";
|
||||
|
||||
|
||||
@@ -16,7 +16,7 @@ import {
|
||||
} from "../../support/applicationSessionFixture.js";
|
||||
|
||||
import { projectAndroidApplicationEvidence } from "../../../src/application/android/AndroidApplicationService.js";
|
||||
import { runProviderAnalysis } from "../../../src/application/DirectAnalysis.js";
|
||||
import { runProviderAnalysis } from "../../../src/composition/directAnalysis.js";
|
||||
import { androidApplicationProjectionResultSchema } from "../../../src/domain/android/androidApplication.js";
|
||||
import { parseEvidence } from "../../../src/domain/evidence.js";
|
||||
|
||||
|
||||
@@ -0,0 +1,152 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import {
|
||||
runDirectAnalysis,
|
||||
runManagedProviderExecution,
|
||||
} from "../../src/application/DirectAnalysis.js";
|
||||
import { runCapabilityStatus } from "../../src/application/DirectAnalysisStatus.js";
|
||||
import type { DirectAnalysisDependencies } from "../../src/application/DirectAnalysisDependencies.js";
|
||||
import type { BinarySession } from "../../src/application/binary/BinarySession.js";
|
||||
import { parseEvidence } from "../../src/domain/evidence.js";
|
||||
import { observed } from "../fixtures/analysisExecution.js";
|
||||
import {
|
||||
createBinarySessionTargets,
|
||||
createTestBinarySession,
|
||||
} from "../fixtures/binarySession.js";
|
||||
|
||||
const recordingFactories = (failure?: Error) => {
|
||||
const sessions: BinarySession[] = [];
|
||||
let clientsClosed = 0;
|
||||
const createSession = () => {
|
||||
const session = createTestBinarySession(() => ({
|
||||
execute: (operation) => {
|
||||
if (failure !== undefined && operation === "read_bytes")
|
||||
return Promise.reject(failure);
|
||||
return Promise.resolve(observed({ operation }));
|
||||
},
|
||||
close: () => {
|
||||
clientsClosed += 1;
|
||||
return Promise.resolve();
|
||||
},
|
||||
}));
|
||||
sessions.push(session);
|
||||
return session;
|
||||
};
|
||||
const dependencies: DirectAnalysisDependencies = {
|
||||
createBinarySession: createSession,
|
||||
createManagedBinarySession: createSession,
|
||||
};
|
||||
return { dependencies, sessions, clientsClosed: () => clientsClosed };
|
||||
};
|
||||
|
||||
describe("one-shot analysis factory boundary", () => {
|
||||
it("keeps independent commands and releases their actual session clients", async () => {
|
||||
const [path] = await createBinarySessionTargets();
|
||||
const factories = recordingFactories();
|
||||
const first = parseEvidence(
|
||||
await runDirectAnalysis(factories.dependencies, path, "read_bytes", {}),
|
||||
);
|
||||
const second = parseEvidence(
|
||||
await runDirectAnalysis(factories.dependencies, path, "read_bytes", {}),
|
||||
);
|
||||
expect(first).toMatchObject({
|
||||
operation: "read_bytes",
|
||||
normalized_result: { operation: "read_bytes" },
|
||||
subject: { local_path: path },
|
||||
});
|
||||
expect(second.subject).toEqual(first.subject);
|
||||
expect(factories.sessions).toHaveLength(2);
|
||||
expect(factories.sessions[0]).not.toBe(factories.sessions[1]);
|
||||
expect(factories.clientsClosed()).toBe(2);
|
||||
for (const session of factories.sessions) {
|
||||
expect(session.activeTarget()).toBeUndefined();
|
||||
expect(session.evidenceById(first.evidence_id)).toBeUndefined();
|
||||
expect(session.evidenceById(second.evidence_id)).toBeUndefined();
|
||||
}
|
||||
});
|
||||
|
||||
it("preserves thrown failures while releasing clients and cancellation listeners", async () => {
|
||||
const [path] = await createBinarySessionTargets();
|
||||
const failure = new Error("fixture execution failed");
|
||||
const factories = recordingFactories(failure);
|
||||
const listeners = process.listenerCount("SIGINT");
|
||||
await expect(
|
||||
runDirectAnalysis(factories.dependencies, path, "read_bytes", {}),
|
||||
).rejects.toBe(failure);
|
||||
expect(factories.clientsClosed()).toBe(1);
|
||||
expect(process.listenerCount("SIGINT")).toBe(listeners);
|
||||
expect(factories.sessions[0]?.activeTarget()).toBeUndefined();
|
||||
});
|
||||
|
||||
it("does not acquire a client after cancellation before target opening", async () => {
|
||||
const [path] = await createBinarySessionTargets();
|
||||
const factories = recordingFactories();
|
||||
const controller = new AbortController();
|
||||
controller.abort();
|
||||
const result = await runDirectAnalysis(
|
||||
factories.dependencies,
|
||||
path,
|
||||
"read_bytes",
|
||||
{},
|
||||
{ signal: controller.signal },
|
||||
);
|
||||
expect(result).toMatchObject({
|
||||
error: "Analysis failed",
|
||||
code: "cancelled",
|
||||
});
|
||||
expect(factories.clientsClosed()).toBe(0);
|
||||
expect(factories.sessions[0]?.activeTarget()).toBeUndefined();
|
||||
});
|
||||
|
||||
it("keeps managed execution independent of native construction", async () => {
|
||||
const [path] = await createBinarySessionTargets();
|
||||
const factories = recordingFactories();
|
||||
const result = await runManagedProviderExecution(
|
||||
{
|
||||
...factories.dependencies,
|
||||
createBinarySession: () => {
|
||||
throw new Error("native factory must stay unused");
|
||||
},
|
||||
},
|
||||
path,
|
||||
"inspect_managed_artifact",
|
||||
);
|
||||
expect(result).toMatchObject({
|
||||
ok: true,
|
||||
value: { result: { operation: "inspect_managed_artifact" } },
|
||||
});
|
||||
expect(factories.clientsClosed()).toBe(1);
|
||||
expect(factories.sessions[0]?.activeTarget()).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe("status factory configuration", () => {
|
||||
it("passes the selected environment and logger without opening a target", async () => {
|
||||
const factories = recordingFactories();
|
||||
const selected: string[] = [];
|
||||
await runCapabilityStatus(
|
||||
{
|
||||
createBinarySession: (config, logger) => {
|
||||
selected.push(config.analysisProvider);
|
||||
expect(logger).toBeDefined();
|
||||
return factories.dependencies.createBinarySession(config, logger);
|
||||
},
|
||||
},
|
||||
undefined,
|
||||
{ REA_ANALYSIS_PROVIDER: "ghidra" },
|
||||
);
|
||||
expect(selected).toEqual(["ghidra"]);
|
||||
expect(factories.clientsClosed()).toBe(0);
|
||||
expect(factories.sessions[0]?.activeTarget()).toBeUndefined();
|
||||
});
|
||||
|
||||
it("retains configuration diagnostics before acquiring any session", async () => {
|
||||
const factories = recordingFactories();
|
||||
const result = await runCapabilityStatus(
|
||||
factories.dependencies,
|
||||
undefined,
|
||||
{ REA_ANALYSIS_PROVIDER: "invalid_provider" },
|
||||
);
|
||||
expect(result).toHaveProperty("error");
|
||||
expect(factories.sessions).toEqual([]);
|
||||
});
|
||||
});
|
||||
@@ -8,6 +8,40 @@ import { describe, expect, it } from "vitest";
|
||||
import { inspectModuleBoundaries } from "../../scripts/lib/module-boundaries.mjs";
|
||||
import { createTestTempDirectory } from "../fixtures/temporaryDirectory.js";
|
||||
|
||||
describe("binary production construction ownership", () => {
|
||||
it.each([
|
||||
"hopper/HopperProvider.js",
|
||||
"ghidra/GhidraProvider.js",
|
||||
"ida/IdaProvider.js",
|
||||
"artifacts/ArtifactProvider.js",
|
||||
"dotnet/ManagedStaticProvider.js",
|
||||
"native/NativeMacOSProvider.js",
|
||||
])("keeps %s in production composition", (provider) => {
|
||||
const source = `import { Provider } from "../${provider}";`;
|
||||
for (const file of ["src/application/runtime.ts", "src/server/probe.ts"])
|
||||
expect(
|
||||
inspectModuleBoundaries(file, source, process.cwd()),
|
||||
).toMatchObject([{ boundary: "provider-construction" }]);
|
||||
expect(
|
||||
inspectModuleBoundaries(
|
||||
"src/composition/binary.ts",
|
||||
source,
|
||||
process.cwd(),
|
||||
),
|
||||
).toEqual([]);
|
||||
});
|
||||
|
||||
it("rejects the former runtime composition exception", () => {
|
||||
expect(
|
||||
inspectModuleBoundaries(
|
||||
"src/application/runtime.ts",
|
||||
'export { createBinarySession } from "../composition/binary.js";',
|
||||
process.cwd(),
|
||||
),
|
||||
).toMatchObject([{ boundary: "application-composition" }]);
|
||||
});
|
||||
});
|
||||
|
||||
const execute = promisify(execFile);
|
||||
const verifier = resolve("scripts/verify-module-boundaries.mjs");
|
||||
|
||||
@@ -93,7 +127,7 @@ describe("incremental module import boundaries", () => {
|
||||
"../../android/JadxProvider.js",
|
||||
false,
|
||||
],
|
||||
["src/application/runtime.ts", "../android/JadxProvider.js", true],
|
||||
["src/application/runtime.ts", "../android/JadxProvider.js", false],
|
||||
[
|
||||
"src/application/android/runtime.ts",
|
||||
"../../android/JadxProvider.js",
|
||||
|
||||
+1
-1
@@ -6,7 +6,7 @@ import { ELECTRON_TOOL_CONTRACTS } from "../../dist/contracts/javascript/electro
|
||||
import { JAVASCRIPT_RUNTIME_OBSERVATION_TOOL_CONTRACTS } from "../../dist/contracts/javascript/javascriptRuntimeObservationToolContracts.js";
|
||||
import { TOOL_CONTRACTS } from "../../dist/contracts/toolContracts.js";
|
||||
import { parseConfig } from "../../dist/config.js";
|
||||
import { createBinarySession } from "../../dist/application/runtime.js";
|
||||
import { createBinarySession } from "../../dist/composition/binary.js";
|
||||
|
||||
// Mutate only this child's presentation arrays, before importing registration.
|
||||
if (process.argv[2] === "reversed") {
|
||||
|
||||
Vendored
+45
-13
@@ -92,6 +92,11 @@ export const startFakeCdpBrowser = async (
|
||||
socket.on("message", (raw) => {
|
||||
const command = parseCommand(raw.toString());
|
||||
commands.push(command);
|
||||
const commandError = options.commandError?.(command);
|
||||
if (commandError !== undefined) {
|
||||
socket.send(JSON.stringify({ id: command.id, error: commandError }));
|
||||
return;
|
||||
}
|
||||
if (options.closeOnMethod === command.method) {
|
||||
socket.close();
|
||||
return;
|
||||
@@ -124,19 +129,7 @@ export const startFakeCdpBrowser = async (
|
||||
return;
|
||||
}
|
||||
if (command.method === "Page.getFrameTree") frameTreeReads += 1;
|
||||
socket.send(
|
||||
JSON.stringify({
|
||||
id: command.id,
|
||||
result: resultFor(command, port, options, frameTreeReads),
|
||||
}),
|
||||
);
|
||||
if (options.malformedEventOnMethod === command.method)
|
||||
socket.send("{not-json");
|
||||
if (options.malformedEventShapeOnMethod === command.method)
|
||||
socket.send(JSON.stringify({ method: 42 }));
|
||||
emitEvents(socket, command, port, options);
|
||||
if (options.closeAfterMethod === command.method)
|
||||
setImmediate(() => socket.close());
|
||||
replyToCommand(socket, command, port, options, frameTreeReads);
|
||||
});
|
||||
});
|
||||
await new Promise<void>((resolve, reject) => {
|
||||
@@ -150,6 +143,12 @@ export const startFakeCdpBrowser = async (
|
||||
browserWebSocketUrl: `ws://127.0.0.1:${String(port)}/devtools/browser/fake`,
|
||||
allowedOrigin: endpoint,
|
||||
commands,
|
||||
emitRawMessage(message) {
|
||||
for (const socket of sockets) socket.send(message);
|
||||
},
|
||||
emitEvent(event) {
|
||||
for (const socket of sockets) socket.send(JSON.stringify(event));
|
||||
},
|
||||
httpRequests,
|
||||
async close() {
|
||||
for (const socket of sockets) socket.terminate();
|
||||
@@ -170,3 +169,36 @@ const boundPort = (
|
||||
throw new Error("Fake CDP server did not bind a TCP address");
|
||||
return address.port;
|
||||
};
|
||||
|
||||
/** Emit a configured producer reply and its associated events. */
|
||||
const replyToCommand = (
|
||||
socket: WebSocket,
|
||||
command: FakeCdpCommand,
|
||||
port: number,
|
||||
options: FakeOptions,
|
||||
frameTreeReads: number,
|
||||
): void => {
|
||||
socket.send(
|
||||
JSON.stringify({
|
||||
id: command.id,
|
||||
result:
|
||||
options.commandResult?.(
|
||||
command,
|
||||
`http://127.0.0.1:${String(port)}`,
|
||||
frameTreeReads,
|
||||
) ?? resultFor(command, port, options, frameTreeReads),
|
||||
}),
|
||||
);
|
||||
if (options.malformedEventOnMethod === command.method)
|
||||
socket.send("{not-json");
|
||||
if (options.malformedEventShapeOnMethod === command.method)
|
||||
socket.send(JSON.stringify({ method: 42 }));
|
||||
const customEvents = options.commandEvents?.(
|
||||
command,
|
||||
`http://127.0.0.1:${String(port)}`,
|
||||
);
|
||||
if (customEvents === undefined) emitEvents(socket, command, port, options);
|
||||
else for (const event of customEvents) socket.send(JSON.stringify(event));
|
||||
if (options.closeAfterMethod === command.method)
|
||||
setImmediate(() => socket.close());
|
||||
};
|
||||
|
||||
+28
@@ -10,6 +10,13 @@ export interface FakeCdpBrowser {
|
||||
readonly browserWebSocketUrl: string;
|
||||
readonly allowedOrigin: string;
|
||||
readonly commands: readonly FakeCdpCommand[];
|
||||
/** Deliver actual malformed wire data without bypassing the production transport parser. */
|
||||
emitRawMessage(message: string): void;
|
||||
emitEvent(event: {
|
||||
readonly method: string;
|
||||
readonly params: unknown;
|
||||
readonly sessionId?: string;
|
||||
}): void;
|
||||
readonly httpRequests: readonly {
|
||||
readonly url: string;
|
||||
readonly authorization: string | undefined;
|
||||
@@ -20,6 +27,27 @@ export interface FakeCdpBrowser {
|
||||
}
|
||||
|
||||
export interface FakeOptions {
|
||||
/** Return a native-shaped command rejection through the actual wire parser. */
|
||||
readonly commandError?: (
|
||||
command: FakeCdpCommand,
|
||||
) => { readonly code: number; readonly message: string } | undefined;
|
||||
/** Producer reply seam preserves the real HTTP/WebSocket transport and command parser. */
|
||||
readonly commandResult?: (
|
||||
command: FakeCdpCommand,
|
||||
origin: string,
|
||||
frameReads: number,
|
||||
) => Readonly<Record<string, unknown>> | undefined;
|
||||
/** Return undefined to use the stock producer events; an empty array suppresses them. */
|
||||
readonly commandEvents?: (
|
||||
command: FakeCdpCommand,
|
||||
origin: string,
|
||||
) =>
|
||||
| readonly {
|
||||
readonly method: string;
|
||||
readonly params: unknown;
|
||||
readonly sessionId?: string;
|
||||
}[]
|
||||
| undefined;
|
||||
readonly malformedDiscovery?: boolean;
|
||||
readonly oversizedDiscovery?: boolean;
|
||||
readonly invalidBrowserWebSocket?: boolean;
|
||||
|
||||
Vendored
+159
@@ -0,0 +1,159 @@
|
||||
import { startFakeCdpBrowser, type FakeOptions } from "./fakeCdpBrowser.js";
|
||||
import type { FakeCdpCommand } from "./fakeCdpBrowserTypes.js";
|
||||
|
||||
export const runtimeSourceA = "// 🔥\r\nfunction selected(){ return 'A'; }";
|
||||
export const runtimeSourceB = "function untouched(){ return 'B'; }";
|
||||
|
||||
/** Producer fixture uses same-URL distinct scripts and a producer hash unlike the UTF-8 digest. */
|
||||
export const startRuntimeBrowser = (overrides: FakeOptions = {}) =>
|
||||
startFakeCdpBrowser({
|
||||
...overrides,
|
||||
commandResult: (command, origin, reads) =>
|
||||
overrides.commandResult?.(command, origin, reads) ??
|
||||
runtimeResult(command, origin),
|
||||
commandEvents: (command, origin) =>
|
||||
overrides.commandEvents?.(command, origin) ??
|
||||
runtimeEvents(command, origin),
|
||||
});
|
||||
|
||||
const runtimeResult = (
|
||||
command: FakeCdpCommand,
|
||||
origin: string,
|
||||
): Readonly<Record<string, unknown>> | undefined => {
|
||||
switch (command.method) {
|
||||
case "Page.getFrameTree":
|
||||
return {
|
||||
frameTree: {
|
||||
frame: {
|
||||
id: "runtime-main",
|
||||
url: `${origin}/app`,
|
||||
loaderId: "document-1",
|
||||
},
|
||||
},
|
||||
};
|
||||
case "Profiler.startPreciseCoverage":
|
||||
return { timestamp: 12 };
|
||||
case "Profiler.takePreciseCoverage":
|
||||
return {
|
||||
timestamp: 13,
|
||||
result: [
|
||||
{
|
||||
scriptId: "script-a",
|
||||
url: `${origin}/same.js`,
|
||||
functions: [
|
||||
{
|
||||
functionName: "selected",
|
||||
isBlockCoverage: true,
|
||||
ranges: [
|
||||
{
|
||||
startOffset: 0,
|
||||
endOffset: runtimeSourceA.length,
|
||||
count: 2,
|
||||
},
|
||||
{ startOffset: 7, endOffset: 8, count: 0 },
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
scriptId: "script-b",
|
||||
url: `${origin}/same.js`,
|
||||
functions: [
|
||||
{
|
||||
functionName: "untouched",
|
||||
isBlockCoverage: true,
|
||||
ranges: [
|
||||
{
|
||||
startOffset: 0,
|
||||
endOffset: runtimeSourceB.length,
|
||||
count: 0,
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
scriptId: "script-foreign",
|
||||
url: `${origin}/same.js`,
|
||||
functions: [],
|
||||
},
|
||||
],
|
||||
};
|
||||
case "Debugger.getScriptSource":
|
||||
return {
|
||||
scriptSource:
|
||||
command.params.scriptId === "script-a"
|
||||
? runtimeSourceA
|
||||
: runtimeSourceB,
|
||||
};
|
||||
case "DOM.getDocument":
|
||||
return { root: { nodeId: 1 } };
|
||||
case "DOM.querySelector":
|
||||
return { nodeId: command.params.selector === "#missing" ? 0 : 2 };
|
||||
case "DOM.describeNode":
|
||||
return { node: { backendNodeId: 22 } };
|
||||
case "DOM.resolveNode":
|
||||
return { object: { objectId: "selected-node-object" } };
|
||||
case "DOMDebugger.getEventListeners":
|
||||
return {
|
||||
listeners: [
|
||||
{
|
||||
type: "click",
|
||||
useCapture: false,
|
||||
passive: true,
|
||||
once: false,
|
||||
scriptId: "script-a",
|
||||
lineNumber: 1,
|
||||
columnNumber: 0,
|
||||
},
|
||||
],
|
||||
};
|
||||
default:
|
||||
return undefined;
|
||||
}
|
||||
};
|
||||
|
||||
const runtimeEvents = (command: FakeCdpCommand, origin: string) => {
|
||||
const session =
|
||||
command.sessionId === undefined ? {} : { sessionId: command.sessionId };
|
||||
if (command.method === "Runtime.enable")
|
||||
return [
|
||||
{
|
||||
...session,
|
||||
method: "Runtime.executionContextCreated",
|
||||
params: {
|
||||
context: {
|
||||
id: 1,
|
||||
origin,
|
||||
name: "",
|
||||
auxData: {
|
||||
frameId: "runtime-main",
|
||||
isDefault: true,
|
||||
type: "default",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
];
|
||||
if (command.method !== "Debugger.enable") return [];
|
||||
return ["script-a", "script-b", "script-foreign"].map((scriptId) => ({
|
||||
...session,
|
||||
method: "Debugger.scriptParsed",
|
||||
params: {
|
||||
scriptId,
|
||||
url: `${origin}/same.js`,
|
||||
executionContextId: scriptId === "script-foreign" ? 99 : 1,
|
||||
executionContextAuxData: {
|
||||
frameId:
|
||||
scriptId === "script-foreign" ? "foreign-frame" : "runtime-main",
|
||||
},
|
||||
startLine: 0,
|
||||
startColumn: 0,
|
||||
endLine: 1,
|
||||
endColumn: 0,
|
||||
hash: "producer-reported-hash",
|
||||
hasSourceURL: true,
|
||||
scriptLanguage: "JavaScript",
|
||||
},
|
||||
}));
|
||||
};
|
||||
@@ -4,7 +4,7 @@ import { join } from "node:path";
|
||||
import { TextReader, Uint8ArrayWriter, ZipWriter } from "@zip.js/zip.js";
|
||||
import { expect } from "vitest";
|
||||
|
||||
import { runProviderAnalysis } from "../../src/application/DirectAnalysis.js";
|
||||
import { runProviderAnalysis } from "../../src/composition/directAnalysis.js";
|
||||
import type { AnalysisError } from "../../src/domain/analysisErrorBase.js";
|
||||
import { type Evidence, parseEvidence } from "../../src/domain/evidence.js";
|
||||
import type { Result } from "../../src/domain/result.js";
|
||||
|
||||
Reference in New Issue
Block a user