refactor(observation): return complete inventories
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
import { createServer } from "node:http";
|
||||
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { CdpBrowserProvider } from "../../../src/browser/CdpBrowserProvider.js";
|
||||
|
||||
describe("CDP target inventory", () => {
|
||||
it("returns every allowed target within the bounded discovery payload", async () => {
|
||||
let port = 0;
|
||||
const targets = Array.from({ length: 1_101 }, (_, index) => {
|
||||
const id = `target-${String(index).padStart(4, "0")}`;
|
||||
return {
|
||||
id,
|
||||
type: "page",
|
||||
title: `Page ${String(index)}`,
|
||||
url: `https://allowed.example/page/${String(index)}`,
|
||||
attached: false,
|
||||
webSocketDebuggerUrl: `ws://127.0.0.1:${String(port)}/devtools/page/${id}`,
|
||||
};
|
||||
});
|
||||
const server = createServer((request, response) => {
|
||||
response.setHeader("content-type", "application/json");
|
||||
if (request.url === "/json/version") {
|
||||
response.end(
|
||||
JSON.stringify({
|
||||
Browser: "Chrome/1",
|
||||
"Protocol-Version": "1.3",
|
||||
"User-Agent": "Chrome",
|
||||
"V8-Version": "1",
|
||||
"WebKit-Version": "1",
|
||||
webSocketDebuggerUrl: `ws://127.0.0.1:${String(port)}/devtools/browser/browser`,
|
||||
}),
|
||||
);
|
||||
return;
|
||||
}
|
||||
if (request.url === "/json/list") {
|
||||
response.end(JSON.stringify(targets));
|
||||
return;
|
||||
}
|
||||
response.writeHead(404).end();
|
||||
});
|
||||
|
||||
try {
|
||||
await new Promise<void>((resolve, reject) => {
|
||||
server.once("error", reject);
|
||||
server.listen(0, "127.0.0.1", resolve);
|
||||
});
|
||||
const address = server.address();
|
||||
if (address === null || typeof address === "string")
|
||||
throw new TypeError("Expected a TCP listener address");
|
||||
port = address.port;
|
||||
const endpoint = `http://127.0.0.1:${String(port)}`;
|
||||
|
||||
const result = await new CdpBrowserProvider().listTargets({
|
||||
cdp_endpoint: endpoint,
|
||||
allowed_origins: ["https://allowed.example"],
|
||||
});
|
||||
|
||||
expect(result.ok).toBe(true);
|
||||
if (!result.ok) throw result.error;
|
||||
expect(result.value.targets).toHaveLength(targets.length);
|
||||
expect(result.value.targets[0]?.target_id).toBe("target-0000");
|
||||
expect(result.value.targets.at(-1)?.target_id).toBe("target-1100");
|
||||
} finally {
|
||||
if (server.listening)
|
||||
await new Promise<void>((resolve, reject) => {
|
||||
server.close((error) =>
|
||||
error === undefined ? resolve() : reject(error),
|
||||
);
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -29,6 +29,33 @@ afterEach(async () => {
|
||||
});
|
||||
|
||||
describe("CdpElectronProvider target access", () => {
|
||||
it("returns every in-scope resource beyond the former collection ceiling", async () => {
|
||||
const root = await electronFixture();
|
||||
const index = join(root, "index.html");
|
||||
const count = 2_001;
|
||||
await Promise.all(
|
||||
Array.from({ length: count }, (_value, item) =>
|
||||
writeFile(join(root, `inventory-${String(item)}.js`), ""),
|
||||
),
|
||||
);
|
||||
const browser = await startFakeCdpBrowser({
|
||||
electronFileUrl: pathToFileURL(index).href,
|
||||
electronInventoryCount: count,
|
||||
});
|
||||
browsers.push(browser);
|
||||
const inspected = await new CdpElectronProvider().inspectPage(
|
||||
inspectElectronPageInputSchema.parse({
|
||||
cdp_endpoint: browser.endpoint,
|
||||
allowed_file_roots: [root],
|
||||
target_id: "electron-page",
|
||||
observation_ms: 0,
|
||||
}),
|
||||
);
|
||||
if (!inspected.ok) throw inspected.error;
|
||||
expect(inspected.value.resources).toHaveLength(count + 1);
|
||||
expect(inspected.value.completeness.truncated_sections).toEqual([]);
|
||||
});
|
||||
|
||||
it("returns every approved Electron page target inline", async () => {
|
||||
const root = await electronFixture();
|
||||
const index = join(root, "index.html");
|
||||
@@ -48,7 +75,9 @@ describe("CdpElectronProvider target access", () => {
|
||||
expect(listed.value.targets[0]?.target_id).toBe("electron-page");
|
||||
expect(listed.value.targets.at(-1)?.target_id).toBe("electron-page-0204");
|
||||
});
|
||||
});
|
||||
|
||||
describe("CdpElectronProvider target selection", () => {
|
||||
it("lists and inspects only canonical file targets beneath approved roots", async () => {
|
||||
const root = await electronFixture();
|
||||
const index = join(root, "index.html");
|
||||
@@ -180,7 +209,7 @@ describe("CdpElectronProvider target access", () => {
|
||||
});
|
||||
|
||||
describe("Electron request schemas", () => {
|
||||
it("rejects unknown fields and misspelled inspection limits", () => {
|
||||
it("rejects unknown fields and removed collection limits", () => {
|
||||
expect(
|
||||
listElectronTargetsInputSchema.safeParse({
|
||||
cdp_endpoint: "http://127.0.0.1:9223",
|
||||
@@ -193,7 +222,8 @@ describe("Electron request schemas", () => {
|
||||
cdp_endpoint: "http://127.0.0.1:9223",
|
||||
allowed_file_roots: ["/tmp/app"],
|
||||
target_id: "page-1",
|
||||
limits: { max_dom_nodes: 20, max_dom_node: 20 },
|
||||
observation_ms: 0,
|
||||
limits: { max_dom_nodes: 20 },
|
||||
}).success,
|
||||
).toBe(false);
|
||||
});
|
||||
|
||||
@@ -212,16 +212,15 @@ describe("undeclared V8 runtime role", () => {
|
||||
});
|
||||
|
||||
describe("passive V8 Inspector evidence", () => {
|
||||
test("marks capture truncation instead of claiming complete coverage", async () => {
|
||||
test("returns every authorized script beyond the former collection ceiling", async () => {
|
||||
const fixture = await runtimeFixture();
|
||||
const second = join(fixture.root, "second.js");
|
||||
await writeFile(second, "export {};\n");
|
||||
const scriptUrls = [
|
||||
pathToFileURL(fixture.entry).href,
|
||||
...Array.from({ length: 2_004 }, () => pathToFileURL(fixture.entry).href),
|
||||
];
|
||||
const fake = await startFakeV8Inspector({
|
||||
targetUrl: pathToFileURL(fixture.entry).href,
|
||||
scriptUrls: [
|
||||
pathToFileURL(fixture.entry).href,
|
||||
pathToFileURL(second).href,
|
||||
],
|
||||
scriptUrls,
|
||||
});
|
||||
try {
|
||||
const input = observeInput(
|
||||
@@ -230,15 +229,13 @@ describe("passive V8 Inspector evidence", () => {
|
||||
fixture.root,
|
||||
"node",
|
||||
);
|
||||
const result = await new V8InspectorProvider().observe({
|
||||
...input,
|
||||
limits: { ...input.limits, max_scripts: 1 },
|
||||
});
|
||||
const result = await new V8InspectorProvider().observe(input);
|
||||
expect(result.ok).toBe(true);
|
||||
if (!result.ok) return;
|
||||
expect(result.value.capture.truncated).toBe(true);
|
||||
expect(result.value.capture.truncation_reasons).toEqual(["max_scripts"]);
|
||||
expect(result.value.capture.events_dropped).toBe(1);
|
||||
expect(result.value.capture.truncated).toBe(false);
|
||||
expect(result.value.capture.truncation_reasons).toEqual([]);
|
||||
expect(result.value.capture.events_dropped).toBe(0);
|
||||
expect(result.value.scripts.items).toHaveLength(scriptUrls.length);
|
||||
} finally {
|
||||
await fake.close();
|
||||
}
|
||||
@@ -302,13 +299,6 @@ describe("passive V8 Inspector evidence", () => {
|
||||
target_id: "example-v8-target",
|
||||
runtime_kind: "electron-main",
|
||||
observation_ms: 100,
|
||||
limits: {
|
||||
max_events: 10_000,
|
||||
max_scripts: 2_000,
|
||||
max_execution_contexts: 1_000,
|
||||
max_location_bytes: 16_384,
|
||||
max_total_metadata_bytes: 4_194_304,
|
||||
},
|
||||
},
|
||||
observation,
|
||||
V8_INSPECTOR_PROVIDER_IDENTITY,
|
||||
@@ -354,13 +344,6 @@ const observeInput = (
|
||||
target_id: targetId,
|
||||
runtime_kind: runtimeKind,
|
||||
observation_ms: 10,
|
||||
limits: {
|
||||
max_events: 10_000,
|
||||
max_scripts: 2_000,
|
||||
max_execution_contexts: 1_000,
|
||||
max_location_bytes: 16_384,
|
||||
max_total_metadata_bytes: 4_194_304,
|
||||
},
|
||||
});
|
||||
|
||||
const authorityFor = async (endpoint: string, root: string) => {
|
||||
|
||||
+56
-22
@@ -70,15 +70,15 @@ const frameTreeResult = (
|
||||
);
|
||||
|
||||
const resourceTreeResult = (port: number, options: FakeOptions) =>
|
||||
resourceTree(
|
||||
port,
|
||||
options.extraCollections === true,
|
||||
options.electronFileUrl,
|
||||
options.duplicateElectronInventory === true,
|
||||
);
|
||||
resourceTree(port, options);
|
||||
|
||||
const domSnapshotResult = (port: number, options: FakeOptions) =>
|
||||
domSnapshot(port, options.electronFileUrl, options.extraCollections === true);
|
||||
domSnapshot(
|
||||
port,
|
||||
options.electronFileUrl,
|
||||
options.extraCollections === true,
|
||||
options.electronInventoryCount ?? 0,
|
||||
);
|
||||
|
||||
const accessibilityTreeResult = (options: FakeOptions) =>
|
||||
accessibilityTree(options.extraCollections === true);
|
||||
@@ -260,40 +260,38 @@ const frameTree = (
|
||||
|
||||
const resourceTree = (
|
||||
port: number,
|
||||
extraCollections = false,
|
||||
electronFileUrl?: string,
|
||||
duplicateElectronInventory = false,
|
||||
options: FakeOptions = {},
|
||||
): Readonly<Record<string, unknown>> => ({
|
||||
frameTree: {
|
||||
frame: {
|
||||
id: "frame-main",
|
||||
loaderId: "loader-main",
|
||||
url: electronFileUrl ?? `http://127.0.0.1:${String(port)}/app`,
|
||||
url: options.electronFileUrl ?? `http://127.0.0.1:${String(port)}/app`,
|
||||
},
|
||||
resources: [
|
||||
{
|
||||
url:
|
||||
electronFileUrl === undefined
|
||||
options.electronFileUrl === undefined
|
||||
? `http://127.0.0.1:${String(port)}/app.js?token=script-secret`
|
||||
: new URL("app.js", electronFileUrl).href,
|
||||
: new URL("app.js", options.electronFileUrl).href,
|
||||
type: "Script",
|
||||
mimeType: "text/javascript",
|
||||
contentSize: 128,
|
||||
},
|
||||
...(duplicateElectronInventory
|
||||
...(options.duplicateElectronInventory === true
|
||||
? [
|
||||
{
|
||||
url:
|
||||
electronFileUrl === undefined
|
||||
options.electronFileUrl === undefined
|
||||
? `http://127.0.0.1:${String(port)}/app.js?token=script-secret`
|
||||
: new URL("app.js", electronFileUrl).href,
|
||||
: new URL("app.js", options.electronFileUrl).href,
|
||||
type: "Script",
|
||||
mimeType: "text/javascript",
|
||||
contentSize: 128,
|
||||
},
|
||||
]
|
||||
: []),
|
||||
...(extraCollections
|
||||
...(options.extraCollections === true
|
||||
? [
|
||||
{
|
||||
url: `http://127.0.0.1:${String(port)}/app.css`,
|
||||
@@ -303,6 +301,21 @@ const resourceTree = (
|
||||
},
|
||||
]
|
||||
: []),
|
||||
...Array.from(
|
||||
{ length: options.electronInventoryCount ?? 0 },
|
||||
(_value, index) => ({
|
||||
url:
|
||||
options.electronFileUrl === undefined
|
||||
? `http://127.0.0.1:${String(port)}/inventory-${String(index)}.js`
|
||||
: new URL(
|
||||
`inventory-${String(index)}.js`,
|
||||
options.electronFileUrl,
|
||||
).href,
|
||||
type: "Script",
|
||||
mimeType: "text/javascript",
|
||||
contentSize: 128,
|
||||
}),
|
||||
),
|
||||
{
|
||||
url: "https://private.example.test/private.js?secret=forbidden",
|
||||
type: "Script",
|
||||
@@ -316,6 +329,7 @@ const domSnapshot = (
|
||||
port: number,
|
||||
electronFileUrl?: string,
|
||||
extraCollections = false,
|
||||
electronInventoryCount = 0,
|
||||
): Readonly<Record<string, unknown>> => {
|
||||
const secondDocumentUrl =
|
||||
electronFileUrl !== undefined && extraCollections
|
||||
@@ -343,11 +357,31 @@ const domSnapshot = (
|
||||
{
|
||||
documentURL: 0,
|
||||
nodes: {
|
||||
nodeType: [9, 1],
|
||||
nodeName: [1, 3],
|
||||
nodeValue: [2, 2],
|
||||
parentIndex: [-1, 0],
|
||||
attributes: [[], [4, 5, 8, 9, 10, 11]],
|
||||
nodeType: [
|
||||
9,
|
||||
1,
|
||||
...Array.from({ length: electronInventoryCount }, () => 1),
|
||||
],
|
||||
nodeName: [
|
||||
1,
|
||||
3,
|
||||
...Array.from({ length: electronInventoryCount }, () => 3),
|
||||
],
|
||||
nodeValue: [
|
||||
2,
|
||||
2,
|
||||
...Array.from({ length: electronInventoryCount }, () => 2),
|
||||
],
|
||||
parentIndex: [
|
||||
-1,
|
||||
0,
|
||||
...Array.from({ length: electronInventoryCount }, () => 0),
|
||||
],
|
||||
attributes: [
|
||||
[],
|
||||
[4, 5, 8, 9, 10, 11],
|
||||
...Array.from({ length: electronInventoryCount }, () => []),
|
||||
],
|
||||
},
|
||||
},
|
||||
{
|
||||
|
||||
+1
@@ -61,6 +61,7 @@ export interface FakeOptions {
|
||||
readonly webMcpChildLeavesScope?: boolean;
|
||||
readonly electronFileUrl?: string;
|
||||
readonly duplicateElectronInventory?: boolean;
|
||||
readonly electronInventoryCount?: number;
|
||||
readonly urlShapedAllowedTitle?:
|
||||
| boolean
|
||||
| "host-path"
|
||||
|
||||
Reference in New Issue
Block a user