feat: add web and Electron reverse-engineering workflows

This commit is contained in:
N0zoM1z0
2026-07-14 20:24:12 +09:00
committed by N0zoM1z0
parent c2e64a42c8
commit be76f8090b
106 changed files with 11893 additions and 404 deletions
+141 -21
View File
@@ -11,29 +11,73 @@ import type {
ListBrowserTargetsInput,
WebPageInspection,
} from "../domain/browserObservation.js";
import type {
AnalyzeWebBundleInput,
WebBundleAnalysis,
} from "../domain/webBundleAnalysis.js";
import type {
ObserveWebSessionInput,
WebObservationSession,
} from "../domain/browserSession.js";
import type {
DiscoverWebMcpToolsInput,
WebMcpDiscovery,
} from "../domain/webMcpDiscovery.js";
import type {
CompareWebCapturesInput,
WebCaptureDiff,
} from "../domain/webCaptureDiff.js";
import type {
CaptureWebScreenshotInput,
CompareWebScreenshotsInput,
WebScreenshot,
WebScreenshotDiff,
} from "../domain/webScreenshot.js";
type BrowserEvidenceInput = ListBrowserTargetsInput | InspectWebPageInput;
type BrowserEvidenceResult = BrowserTargetList | WebPageInspection;
type BrowserEvidenceInput =
| ListBrowserTargetsInput
| InspectWebPageInput
| AnalyzeWebBundleInput
| ObserveWebSessionInput
| DiscoverWebMcpToolsInput
| CompareWebCapturesInput
| CaptureWebScreenshotInput
| CompareWebScreenshotsInput;
type BrowserEvidenceResult =
| BrowserTargetList
| WebPageInspection
| WebBundleAnalysis
| WebObservationSession
| WebMcpDiscovery
| WebCaptureDiff
| WebScreenshot
| WebScreenshotDiff;
type BrowserEvidenceOperation =
| "list_browser_targets"
| "inspect_web_page"
| "analyze_web_bundle"
| "observe_web_session"
| "discover_webmcp_tools"
| "compare_web_captures"
| "capture_web_screenshot"
| "compare_web_screenshots";
/** Create Evidence v2 for a policy-scoped external browser observation. */
export const createBrowserEvidence = (
operation: "list_browser_targets" | "inspect_web_page",
operation: BrowserEvidenceOperation,
input: BrowserEvidenceInput,
result: BrowserEvidenceResult,
provider: ProviderIdentity,
): Evidence =>
createEvidence(undefined, provider, {
predicateType:
operation === "list_browser_targets"
? "rea.browser-target-list/v1"
: "rea.web-page-inspection/v1",
predicateType: browserPredicate(operation),
operation,
parameters: browserParameters(input),
result: jsonValueSchema.parse(result),
confidence: "observed",
authority: "external-service",
environment:
"target" in result
"browser" in result
? {
id: `${result.browser.product}@${result.browser.revision}`,
platform: process.platform,
@@ -46,16 +90,92 @@ export const createBrowserEvidence = (
const browserParameters = (
input: BrowserEvidenceInput,
): EvidenceObservation["parameters"] => ({
cdp_endpoint: input.cdp_endpoint,
allowed_origins: input.allowed_origins,
...("target_id" in input
? {
target_id: input.target_id,
observation_ms: input.observation_ms,
include_script_sources: input.include_script_sources,
include_storage_keys: input.include_storage_keys,
limits: input.limits,
}
: { offset: input.offset, limit: input.limit }),
});
): EvidenceObservation["parameters"] => {
if (!("cdp_endpoint" in input)) {
if ("max_changes" in input)
return {
before_target_id: input.before.inspection.target.target_id,
before_capture_ended_at:
input.before.inspection.capture_window.ended_at,
after_target_id: input.after.inspection.target.target_id,
after_capture_ended_at: input.after.inspection.capture_window.ended_at,
max_changes: input.max_changes,
};
return {
before_artifact_uri: input.before.uri,
after_artifact_uri: input.after.uri,
channel_threshold: input.channel_threshold,
maximum_pixels: input.maximum_pixels,
};
}
const scope = {
cdp_endpoint: input.cdp_endpoint,
allowed_origins: input.allowed_origins,
};
if (!("target_id" in input))
return { ...scope, offset: input.offset, limit: input.limit };
if ("screenshot_approved" in input)
return {
...scope,
target_id: input.target_id,
maximum_image_bytes: input.maximum_image_bytes,
};
if ("max_tools" in input)
return {
...scope,
target_id: input.target_id,
observation_ms: input.observation_ms,
max_tools: input.max_tools,
max_schema_bytes: input.max_schema_bytes,
max_schema_nodes: input.max_schema_nodes,
max_schema_depth: input.max_schema_depth,
};
if (!("include_accessibility_text" in input))
return {
...scope,
target_id: input.target_id,
observation_ms: input.observation_ms,
max_timeline_events: input.max_timeline_events,
};
return {
...scope,
target_id: input.target_id,
observation_ms: input.observation_ms,
include_accessibility_text: input.include_accessibility_text,
include_console_text: input.include_console_text,
include_json_body_shapes: input.include_json_body_shapes,
include_websocket_shapes: input.include_websocket_shapes,
include_script_sources: input.include_script_sources,
include_storage_keys: input.include_storage_keys,
limits: input.limits,
...(input.include_script_sources && "source_capture_approved" in input
? {
source_capture_approved: input.source_capture_approved,
fetch_source_maps: input.fetch_source_maps,
source_map_fetch_approved: input.source_map_fetch_approved,
analysis_limits: input.analysis_limits,
}
: {}),
};
};
const browserPredicate = (operation: BrowserEvidenceOperation): string => {
switch (operation) {
case "list_browser_targets":
return "rea.browser-target-list/v1";
case "inspect_web_page":
return "rea.web-page-inspection/v2";
case "analyze_web_bundle":
return "rea.web-bundle-analysis/v1";
case "observe_web_session":
return "rea.web-observation-session/v1";
case "discover_webmcp_tools":
return "rea.webmcp-discovery/v1";
case "compare_web_captures":
return "rea.web-capture-diff/v1";
case "capture_web_screenshot":
return "rea.web-screenshot/v1";
case "compare_web_screenshots":
return "rea.web-screenshot-diff/v1";
}
};
+44
View File
@@ -6,6 +6,28 @@ import type {
WebPageInspection,
} from "../domain/browserObservation.js";
import type { Result } from "../domain/result.js";
import type {
AnalyzeWebBundleInput,
WebBundleAnalysis,
} from "../domain/webBundleAnalysis.js";
import type {
ObserveWebSessionInput,
WebObservationSession,
} from "../domain/browserSession.js";
import type {
DiscoverWebMcpToolsInput,
WebMcpDiscovery,
} from "../domain/webMcpDiscovery.js";
import type {
CompareWebCapturesInput,
WebCaptureDiff,
} from "../domain/webCaptureDiff.js";
import type {
CaptureWebScreenshotInput,
CompareWebScreenshotsInput,
WebScreenshot,
WebScreenshotDiff,
} from "../domain/webScreenshot.js";
import type { ProviderIdentity } from "./AnalysisProvider.js";
import type { ExecutionOptions } from "./AnalysisProvider.js";
@@ -20,4 +42,26 @@ export interface BrowserObservationPort {
input: InspectWebPageInput,
options?: ExecutionOptions,
): Promise<Result<WebPageInspection, AnalysisError>>;
analyzeBundle(
input: AnalyzeWebBundleInput,
options?: ExecutionOptions,
): Promise<Result<WebBundleAnalysis, AnalysisError>>;
observeSession(
input: ObserveWebSessionInput,
options?: ExecutionOptions,
): Promise<Result<WebObservationSession, AnalysisError>>;
discoverWebMcpTools(
input: DiscoverWebMcpToolsInput,
options?: ExecutionOptions,
): Promise<Result<WebMcpDiscovery, AnalysisError>>;
compareCaptures(
input: CompareWebCapturesInput,
): Promise<Result<WebCaptureDiff, AnalysisError>>;
captureScreenshot(
input: CaptureWebScreenshotInput,
options?: ExecutionOptions,
): Promise<Result<WebScreenshot, AnalysisError>>;
compareScreenshots(
input: CompareWebScreenshotsInput,
): Promise<Result<WebScreenshotDiff, AnalysisError>>;
}
+166 -1
View File
@@ -3,6 +3,14 @@ import type {
InspectWebPageInput,
ListBrowserTargetsInput,
} from "../domain/browserObservation.js";
import type { AnalyzeWebBundleInput } from "../domain/webBundleAnalysis.js";
import type { ObserveWebSessionInput } from "../domain/browserSession.js";
import type { DiscoverWebMcpToolsInput } from "../domain/webMcpDiscovery.js";
import type { CompareWebCapturesInput } from "../domain/webCaptureDiff.js";
import type {
CaptureWebScreenshotInput,
CompareWebScreenshotsInput,
} from "../domain/webScreenshot.js";
import {
AnalysisCapabilityUnavailableError,
AnalysisProtocolError,
@@ -69,11 +77,154 @@ export const inspectWebPage = async (
: result;
};
/** Capture approved sources and derive bounded static web-bundle evidence. */
export const analyzeWebBundle = async (
browser: BrowserObservationPort | undefined,
authority: PermissionAuthority | undefined,
input: AnalyzeWebBundleInput,
options: ExecutionOptions = {},
): Promise<Result<Evidence, AnalysisError>> => {
const ready = await prepare(browser, authority, input, "analyze_web_bundle");
if (!ready.ok) return ready;
const analyzed = await ready.value.analyzeBundle(input, options);
if (!analyzed.ok) return analyzed;
return ok(
createBrowserEvidence(
"analyze_web_bundle",
input,
analyzed.value,
ready.value.identity(),
),
);
};
/** Observe navigation caused by external user actions during one armed window. */
export const observeWebSession = async (
browser: BrowserObservationPort | undefined,
authority: PermissionAuthority | undefined,
input: ObserveWebSessionInput,
options: ExecutionOptions = {},
): Promise<Result<Evidence, AnalysisError>> => {
const ready = await prepare(browser, authority, input, "observe_web_session");
if (!ready.ok) return ready;
const observed = await ready.value.observeSession(input, options);
return observed.ok
? ok(
createBrowserEvidence(
"observe_web_session",
input,
observed.value,
ready.value.identity(),
),
)
: observed;
};
/** Discover WebMCP declarations without exposing an invocation surface. */
export const discoverWebMcpTools = async (
browser: BrowserObservationPort | undefined,
authority: PermissionAuthority | undefined,
input: DiscoverWebMcpToolsInput,
options: ExecutionOptions = {},
): Promise<Result<Evidence, AnalysisError>> => {
const ready = await prepare(
browser,
authority,
input,
"discover_webmcp_tools",
);
if (!ready.ok) return ready;
const discovered = await ready.value.discoverWebMcpTools(input, options);
return discovered.ok
? ok(
createBrowserEvidence(
"discover_webmcp_tools",
input,
discovered.value,
ready.value.identity(),
),
)
: discovered;
};
/** Compare two already-normalized web captures without external access. */
export const compareWebCaptureEvidence = async (
browser: BrowserObservationPort | undefined,
input: CompareWebCapturesInput,
): Promise<Result<Evidence, AnalysisError>> => {
const ready = requireBrowser(browser, "compare_web_captures");
if (!ready.ok) return ready;
const compared = await ready.value.compareCaptures(input);
return compared.ok
? ok(
createBrowserEvidence(
"compare_web_captures",
input,
compared.value,
ready.value.identity(),
),
)
: compared;
};
/** Capture one explicitly approved visible-viewport screenshot. */
export const captureWebScreenshot = async (
browser: BrowserObservationPort | undefined,
authority: PermissionAuthority | undefined,
input: CaptureWebScreenshotInput,
options: ExecutionOptions = {},
): Promise<Result<Evidence, AnalysisError>> => {
const ready = await prepare(
browser,
authority,
input,
"capture_web_screenshot",
);
if (!ready.ok) return ready;
const captured = await ready.value.captureScreenshot(input, options);
return captured.ok
? ok(
createBrowserEvidence(
"capture_web_screenshot",
input,
captured.value,
ready.value.identity(),
),
)
: captured;
};
/** Compare two self-verifying PNG artifacts without external access. */
export const compareWebScreenshotEvidence = async (
browser: BrowserObservationPort | undefined,
input: CompareWebScreenshotsInput,
): Promise<Result<Evidence, AnalysisError>> => {
const ready = requireBrowser(browser, "compare_web_screenshots");
if (!ready.ok) return ready;
const compared = await ready.value.compareScreenshots(input);
return compared.ok
? ok(
createBrowserEvidence(
"compare_web_screenshots",
input,
compared.value,
ready.value.identity(),
),
)
: compared;
};
const prepare = async (
browser: BrowserObservationPort | undefined,
authority: PermissionAuthority | undefined,
input: ScopedBrowserInput,
operation: "list_browser_targets" | "inspect_web_page",
operation:
| "list_browser_targets"
| "inspect_web_page"
| "analyze_web_bundle"
| "observe_web_session"
| "discover_webmcp_tools"
| "capture_web_screenshot",
): Promise<Result<BrowserObservationPort, AnalysisError>> => {
if (authority === undefined)
return err(
@@ -115,6 +266,20 @@ const prepare = async (
: ok(browser);
};
const requireBrowser = (
browser: BrowserObservationPort | undefined,
operation: string,
): Result<BrowserObservationPort, AnalysisError> =>
browser === undefined
? err(
new AnalysisCapabilityUnavailableError(
"rea-cdp-browser",
operation,
"browser comparison provider is not configured",
),
)
: ok(browser);
const browserNetworkScope = (
allowedOrigins: readonly string[],
): "loopback" | "external" =>
+10
View File
@@ -55,6 +55,7 @@ export const buildCapabilityInventory = (
readonly processCaptureEnabled: boolean;
readonly evidenceFileRoots: number;
readonly browserObservationEnabled?: boolean;
readonly electronObservationEnabled?: boolean;
},
) => {
const status = statusSchema.parse(sessionStatus);
@@ -99,6 +100,7 @@ const availabilityFor = (
readonly processCaptureEnabled: boolean;
readonly evidenceFileRoots: number;
readonly browserObservationEnabled?: boolean;
readonly electronObservationEnabled?: boolean;
},
): {
readonly reason: ToolAvailabilityReason;
@@ -123,6 +125,14 @@ const availabilityFor = (
remediation:
"Enable browser observation and configure exact CDP endpoint and page origins.",
};
if (kind === "electron-provider")
return policy.electronObservationEnabled === true
? { reason: "available", remediation: null }
: {
reason: "policy_disabled",
remediation:
"Enable Electron observation and configure a loopback CDP endpoint and canonical file roots.",
};
if (kind === "session") return { reason: "available", remediation: null };
if (!targetOpen)
return {
+60
View File
@@ -0,0 +1,60 @@
import type { ProviderIdentity } from "./AnalysisProvider.js";
import {
createEvidence,
type Evidence,
type EvidenceObservation,
} from "../domain/evidence.js";
import { jsonValueSchema } from "../domain/jsonValue.js";
import type {
ElectronPageInspection,
ElectronTargetList,
InspectElectronPageInput,
ListElectronTargetsInput,
} from "../domain/electronObservation.js";
type ElectronOperation = "list_electron_targets" | "inspect_electron_page";
/** Create Evidence v2 for one root-confined Electron observation. */
export const createElectronEvidence = (
operation: ElectronOperation,
input: ListElectronTargetsInput | InspectElectronPageInput,
result: ElectronTargetList | ElectronPageInspection,
provider: ProviderIdentity,
): Evidence =>
createEvidence(undefined, provider, {
predicateType:
operation === "list_electron_targets"
? "rea.electron-target-list/v1"
: "rea.electron-page-inspection/v1",
operation,
parameters: parameters(input),
result: jsonValueSchema.parse(result),
confidence: "observed",
authority: "external-service",
environment:
"target" in result
? {
id: `${result.browser.product}@${result.browser.revision}`,
platform: process.platform,
architecture: process.arch,
isolation: "none",
}
: null,
limitations: result.limitations,
});
const parameters = (
input: ListElectronTargetsInput | InspectElectronPageInput,
): EvidenceObservation["parameters"] => ({
cdp_endpoint: input.cdp_endpoint,
allowed_file_roots: input.allowed_file_roots,
...("target_id" in input
? {
target_id: input.target_id,
observation_ms: input.observation_ms,
include_script_sources: input.include_script_sources,
source_capture_approved: input.source_capture_approved,
limits: input.limits,
}
: { offset: input.offset, limit: input.limit }),
});
@@ -0,0 +1,22 @@
import type { ExecutionOptions, ProviderIdentity } from "./AnalysisProvider.js";
import type { AnalysisError } from "../domain/errors.js";
import type {
ElectronPageInspection,
ElectronTargetList,
InspectElectronPageInput,
ListElectronTargetsInput,
} from "../domain/electronObservation.js";
import type { Result } from "../domain/result.js";
/** Provider-neutral boundary for root-confined Electron file-page observation. */
export interface ElectronObservationPort {
identity(): ProviderIdentity;
listTargets(
input: ListElectronTargetsInput,
options?: ExecutionOptions,
): Promise<Result<ElectronTargetList, AnalysisError>>;
inspectPage(
input: InspectElectronPageInput,
options?: ExecutionOptions,
): Promise<Result<ElectronPageInspection, AnalysisError>>;
}
@@ -0,0 +1,116 @@
import type { ExecutionOptions } from "./AnalysisProvider.js";
import type { ElectronObservationPort } from "./ElectronObservationPort.js";
import type { PermissionAuthority } from "./PermissionAuthority.js";
import type { Evidence } from "../domain/evidence.js";
import type {
InspectElectronPageInput,
ListElectronTargetsInput,
} from "../domain/electronObservation.js";
import {
AnalysisCapabilityUnavailableError,
AnalysisProtocolError,
PermissionRequiredError,
type AnalysisError,
} from "../domain/errors.js";
import { err, ok, type Result } from "../domain/result.js";
import { createElectronEvidence } from "./ElectronEvidence.js";
/** Authorize and list root-confined Electron page targets. */
export const listElectronTargets = async (
provider: ElectronObservationPort | undefined,
authority: PermissionAuthority | undefined,
input: ListElectronTargetsInput,
options: ExecutionOptions = {},
): Promise<Result<Evidence, AnalysisError>> => {
const ready = await prepare(
provider,
authority,
input,
"list_electron_targets",
);
if (!ready.ok) return ready;
const result = await ready.value.listTargets(input, options);
return result.ok
? ok(
createElectronEvidence(
"list_electron_targets",
input,
result.value,
ready.value.identity(),
),
)
: result;
};
/** Authorize and inspect one root-confined Electron page. */
export const inspectElectronPage = async (
provider: ElectronObservationPort | undefined,
authority: PermissionAuthority | undefined,
input: InspectElectronPageInput,
options: ExecutionOptions = {},
): Promise<Result<Evidence, AnalysisError>> => {
const ready = await prepare(
provider,
authority,
input,
"inspect_electron_page",
);
if (!ready.ok) return ready;
const result = await ready.value.inspectPage(input, options);
return result.ok
? ok(
createElectronEvidence(
"inspect_electron_page",
input,
result.value,
ready.value.identity(),
),
)
: result;
};
const prepare = async (
provider: ElectronObservationPort | undefined,
authority: PermissionAuthority | undefined,
input: ListElectronTargetsInput | InspectElectronPageInput,
operation: "list_electron_targets" | "inspect_electron_page",
): Promise<Result<ElectronObservationPort, AnalysisError>> => {
if (authority === undefined)
return err(
new AnalysisCapabilityUnavailableError(
"rea-cdp-electron",
operation,
"Electron observation permission policy is not configured",
),
);
const authorized = await authority.authorize(
{
capability: "electron_observe",
roots: input.allowed_file_roots,
executables: [],
environment_names: [],
origins: [input.cdp_endpoint],
network: "loopback",
mount: false,
operation_identity: `${operation}:${"target_id" in input ? input.target_id : input.cdp_endpoint}`,
},
"read",
);
if (!authorized.ok)
return err(
authorized.error instanceof PermissionRequiredError
? authorized.error
: new AnalysisProtocolError(authorized.error.message, {
cause: authorized.error,
}),
);
return provider === undefined
? err(
new AnalysisCapabilityUnavailableError(
"rea-cdp-electron",
operation,
"Electron observation provider is not configured",
),
)
: ok(provider);
};
@@ -23,6 +23,7 @@ const grantSchema = z.object({
capability: z.enum([
"process_capture",
"browser_observe",
"electron_observe",
"evidence_read",
"evidence_write",
"investigation_input",
+251 -11
View File
@@ -10,10 +10,41 @@ import {
type ListBrowserTargetsInput,
type WebPageInspection,
} from "../domain/browserObservation.js";
import { analyzeCapturedWebBundle } from "../domain/webBundleAnalyzer.js";
import {
webBundleAnalysisSchema,
type AnalyzeWebBundleInput,
type WebBundleAnalysis,
} from "../domain/webBundleAnalysis.js";
import {
webObservationSessionSchema,
type ObserveWebSessionInput,
type WebObservationSession,
} from "../domain/browserSession.js";
import {
webMcpDiscoverySchema,
type DiscoverWebMcpToolsInput,
type WebMcpDiscovery,
} from "../domain/webMcpDiscovery.js";
import {
compareWebCaptures,
webCaptureDiffSchema,
type CompareWebCapturesInput,
type WebCaptureDiff,
} from "../domain/webCaptureDiff.js";
import {
webScreenshotDiffSchema,
webScreenshotSchema,
type CaptureWebScreenshotInput,
type CompareWebScreenshotsInput,
type WebScreenshot,
type WebScreenshotDiff,
} from "../domain/webScreenshot.js";
import {
AnalysisError,
BrowserObservationError,
ProviderAdapterError,
type BrowserObservationOperation,
} from "../domain/errors.js";
import { err, ok, type Result } from "../domain/result.js";
import { CdpConnection } from "./CdpConnection.js";
@@ -22,12 +53,17 @@ import {
type CdpEndpointDiscovery,
type CdpEndpointTarget,
} from "./CdpEndpoint.js";
import { capturePage } from "./CdpPageCapture.js";
import { capturePage, type CapturedPage } from "./CdpPageCapture.js";
import { fetchWebSourceMaps } from "./WebSourceMapFetcher.js";
import { observeCdpSession } from "./CdpObservationSession.js";
import { discoverWebMcp } from "./CdpWebMcpDiscovery.js";
import { captureCdpScreenshot } from "./CdpScreenshot.js";
import { comparePngScreenshots } from "./PngVisualDiff.js";
const IDENTITY: ProviderIdentity = {
id: "rea-cdp-browser",
name: "REA Chrome DevTools Protocol observation provider",
version: "1",
version: "2",
};
/** Passive, origin-scoped browser observation through a user-owned CDP endpoint. */
@@ -81,6 +117,41 @@ export class CdpBrowserProvider implements BrowserObservationPort {
input: InspectWebPageInput,
options: ExecutionOptions = {},
): Promise<Result<WebPageInspection, AnalysisError>> {
try {
const captured = await this.#capture(input, options);
return ok(webPageInspectionSchema.parse(captured.inspection));
} catch (cause: unknown) {
return err(providerError(cause, "inspect_web_page"));
}
}
async analyzeBundle(
input: AnalyzeWebBundleInput,
options: ExecutionOptions = {},
): Promise<Result<WebBundleAnalysis, AnalysisError>> {
try {
const captured = await this.#capture(input, options);
const sourceMaps = input.fetch_source_maps
? await fetchWebSourceMaps(
captured.sourceMapRequests,
input,
options.signal,
)
: undefined;
return ok(
webBundleAnalysisSchema.parse(
analyzeCapturedWebBundle(captured.inspection, input, sourceMaps),
),
);
} catch (cause: unknown) {
return err(providerError(cause, "analyze_web_bundle"));
}
}
async observeSession(
input: ObserveWebSessionInput,
options: ExecutionOptions = {},
): Promise<Result<WebObservationSession, AnalysisError>> {
let connection: CdpConnection | undefined;
let sessionId: string | undefined;
try {
@@ -101,7 +172,177 @@ export class CdpBrowserProvider implements BrowserObservationPort {
options.signal,
);
sessionId = attachedSessionId(attached);
const result = await capturePage({
return ok(
webObservationSessionSchema.parse(
await observeCdpSession({
connection,
sessionId,
discovery,
target,
input,
...(options.signal === undefined ? {} : { signal: options.signal }),
...(options.progress === undefined
? {}
: { progress: options.progress }),
}),
),
);
} catch (cause: unknown) {
return err(providerError(cause, "observe_web_session"));
} finally {
if (connection !== undefined) {
if (sessionId !== undefined)
await bestEffortCleanup(connection, sessionId);
await connection.close();
}
}
}
async discoverWebMcpTools(
input: DiscoverWebMcpToolsInput,
options: ExecutionOptions = {},
): Promise<Result<WebMcpDiscovery, AnalysisError>> {
let connection: CdpConnection | undefined;
let sessionId: string | undefined;
try {
const discovery = await discoverCdpEndpoint(
input.cdp_endpoint,
"inspect_web_page",
options.signal,
);
const target = authorizeTarget(discovery, input);
connection = await CdpConnection.connect(
discovery.browserWebSocketUrl,
options.signal,
);
const attached = await connection.send(
"Target.attachToTarget",
{ targetId: target.id, flatten: true },
undefined,
options.signal,
);
sessionId = attachedSessionId(attached);
return ok(
webMcpDiscoverySchema.parse(
await discoverWebMcp({
connection,
sessionId,
discovery,
target,
input,
...(options.signal === undefined ? {} : { signal: options.signal }),
...(options.progress === undefined
? {}
: { progress: options.progress }),
}),
),
);
} catch (cause: unknown) {
return err(providerError(cause, "discover_webmcp_tools"));
} finally {
if (connection !== undefined) {
if (sessionId !== undefined)
await bestEffortCleanup(connection, sessionId);
await connection.close();
}
}
}
async compareCaptures(
input: CompareWebCapturesInput,
): Promise<Result<WebCaptureDiff, AnalysisError>> {
try {
return ok(webCaptureDiffSchema.parse(compareWebCaptures(input)));
} catch (cause: unknown) {
return err(providerError(cause, "compare_web_captures"));
}
}
async captureScreenshot(
input: CaptureWebScreenshotInput,
options: ExecutionOptions = {},
): Promise<Result<WebScreenshot, AnalysisError>> {
let connection: CdpConnection | undefined;
let sessionId: string | undefined;
try {
const discovery = await discoverCdpEndpoint(
input.cdp_endpoint,
"inspect_web_page",
options.signal,
);
const target = authorizeTarget(discovery, input);
connection = await CdpConnection.connect(
discovery.browserWebSocketUrl,
options.signal,
);
const attached = await connection.send(
"Target.attachToTarget",
{ targetId: target.id, flatten: true },
undefined,
options.signal,
);
sessionId = attachedSessionId(attached);
return ok(
webScreenshotSchema.parse(
await captureCdpScreenshot({
connection,
sessionId,
discovery,
target,
input,
...(options.signal === undefined ? {} : { signal: options.signal }),
...(options.progress === undefined
? {}
: { progress: options.progress }),
}),
),
);
} catch (cause: unknown) {
return err(providerError(cause, "capture_web_screenshot"));
} finally {
if (connection !== undefined) {
if (sessionId !== undefined)
await bestEffortCleanup(connection, sessionId);
await connection.close();
}
}
}
async compareScreenshots(
input: CompareWebScreenshotsInput,
): Promise<Result<WebScreenshotDiff, AnalysisError>> {
try {
return ok(webScreenshotDiffSchema.parse(comparePngScreenshots(input)));
} catch (cause: unknown) {
return err(providerError(cause, "compare_web_screenshots"));
}
}
async #capture(
input: InspectWebPageInput,
options: ExecutionOptions,
): Promise<CapturedPage> {
let connection: CdpConnection | undefined;
let sessionId: string | undefined;
try {
const discovery = await discoverCdpEndpoint(
input.cdp_endpoint,
"inspect_web_page",
options.signal,
);
const target = authorizeTarget(discovery, input);
connection = await CdpConnection.connect(
discovery.browserWebSocketUrl,
options.signal,
);
const attached = await connection.send(
"Target.attachToTarget",
{ targetId: target.id, flatten: true },
undefined,
options.signal,
);
sessionId = attachedSessionId(attached);
return await capturePage({
connection,
sessionId,
discovery,
@@ -112,9 +353,6 @@ export class CdpBrowserProvider implements BrowserObservationPort {
? {}
: { progress: options.progress }),
});
return ok(webPageInspectionSchema.parse(result));
} catch (cause: unknown) {
return err(providerError(cause, "inspect_web_page"));
} finally {
if (connection !== undefined) {
if (sessionId !== undefined)
@@ -172,7 +410,7 @@ const selectTargets = (
const authorizeTarget = (
discovery: CdpEndpointDiscovery,
input: InspectWebPageInput,
input: Pick<InspectWebPageInput, "target_id" | "allowed_origins">,
): CdpEndpointTarget => {
const target = discovery.targets.find(
(candidate) => candidate.id === input.target_id,
@@ -222,8 +460,10 @@ const bestEffortCleanup = async (
const providerError = (
cause: unknown,
operation: "list_browser_targets" | "inspect_web_page",
operation: BrowserObservationOperation,
): AnalysisError =>
cause instanceof AnalysisError
? cause
: new ProviderAdapterError(IDENTITY.id, operation, { cause });
cause instanceof BrowserObservationError && cause.operation !== operation
? new BrowserObservationError(operation, cause.reason, { cause })
: cause instanceof AnalysisError
? cause
: new ProviderAdapterError(IDENTITY.id, operation, { cause });
+119
View File
@@ -0,0 +1,119 @@
import {
classifyBrowserCompleteness,
type BrowserCompleteness,
type BrowserDroppedEventKind,
type BrowserDroppedEvents,
type BrowserExclusion,
type BrowserExclusionReason,
type BrowserSection,
} from "../domain/browserCompleteness.js";
const EVENT_SECTIONS: Readonly<
Record<BrowserDroppedEventKind, BrowserSection>
> = {
scripts: "scripts",
network_requests: "network_requests",
console_events: "console_events",
websocket_connections: "websocket_connections",
websocket_frames: "websocket_frames",
webmcp_tools: "webmcp_tools",
timeline_events: "timeline",
};
const POLICY_REASONS = new Set<BrowserExclusionReason>([
"disallowed_origin",
"unsupported_url",
"unattributed_origin",
"not_approved",
"out_of_target_scope",
]);
/** Mutable adapter ledger projected into immutable, canonical completeness v2. */
export class CdpCaptureCompleteness {
readonly #initialAttachLimited: readonly BrowserSection[];
readonly #policyFiltered = new Set<BrowserSection>();
readonly #attachLimited = new Set<BrowserSection>();
readonly #truncated = new Set<BrowserSection>();
readonly #unavailable = new Set<BrowserSection>();
readonly #excluded = new Map<string, BrowserExclusion>();
readonly #dropped: Omit<BrowserDroppedEvents, "total"> = emptyDropped();
constructor(initialAttachLimited: readonly BrowserSection[] = []) {
this.#initialAttachLimited = [...initialAttachLimited];
this.reset();
}
reset(): void {
this.#policyFiltered.clear();
this.#attachLimited.clear();
this.#truncated.clear();
this.#unavailable.clear();
this.#excluded.clear();
Object.assign(this.#dropped, emptyDropped());
for (const section of this.#initialAttachLimited)
this.#attachLimited.add(section);
}
exclude(
section: BrowserSection,
reason: BrowserExclusionReason,
count: number | null = 1,
): void {
if (POLICY_REASONS.has(reason)) this.#policyFiltered.add(section);
if (
reason === "provider_unavailable" ||
reason === "invalid_protocol_value"
)
this.#unavailable.add(section);
const key = `${section}\0${reason}`;
const existing = this.#excluded.get(key);
const combined =
count === null || existing?.count === null
? null
: (existing?.count ?? 0) + count;
this.#excluded.set(key, { section, reason, count: combined });
}
attachLimited(section: BrowserSection): void {
this.#attachLimited.add(section);
}
unavailable(section: BrowserSection): void {
this.#unavailable.add(section);
this.exclude(section, "provider_unavailable", null);
}
truncate(section: BrowserSection): void {
this.#truncated.add(section);
}
drop(kind: BrowserDroppedEventKind, count = 1): void {
this.#dropped[kind] += count;
this.#truncated.add(EVENT_SECTIONS[kind]);
}
get droppedTotal(): number {
return Object.values(this.#dropped).reduce((sum, count) => sum + count, 0);
}
snapshot(): BrowserCompleteness {
return classifyBrowserCompleteness({
policyFilteredSections: this.#policyFiltered,
attachLimitedSections: this.#attachLimited,
truncatedSections: this.#truncated,
unavailableSections: this.#unavailable,
excluded: [...this.#excluded.values()],
droppedEvents: this.#dropped,
});
}
}
const emptyDropped = (): Omit<BrowserDroppedEvents, "total"> => ({
scripts: 0,
network_requests: 0,
console_events: 0,
websocket_connections: 0,
websocket_frames: 0,
webmcp_tools: 0,
timeline_events: 0,
});
+244 -20
View File
@@ -5,6 +5,7 @@ import type {
import {
allowedSanitizedUrl,
boundedText,
isHttpUrl,
numberValue,
recordValue,
recordsValue,
@@ -12,12 +13,19 @@ import {
stringValue,
type UnknownRecord,
} from "./CdpCaptureValues.js";
import type { CdpCaptureCompleteness } from "./CdpCaptureCompleteness.js";
export type CapturedResource = Omit<
WebPageInspection["resources"][number],
"resource_key"
> & { readonly rawUrl: string };
/** Normalize allowed frames while retaining a bounded prefix and total count. */
export const captureFrames = (
result: unknown,
allowedOrigins: ReadonlySet<string>,
maximum: number,
completeness?: CdpCaptureCompleteness,
): {
readonly total: number;
readonly items: WebPageInspection["frames"];
@@ -30,14 +38,17 @@ export const captureFrames = (
const frame = recordValue(tree.frame);
const frameId = stringValue(frame?.id);
const sanitized = allowedSanitizedUrl(frame?.url, allowedOrigins);
if (
frame === undefined ||
frameId === undefined ||
frameId.length > 256 ||
sanitized === undefined ||
sanitized.origin === null
)
if (frame === undefined || frameId === undefined || frameId.length > 256) {
completeness?.exclude("frames", "invalid_protocol_value");
continue;
}
if (sanitized === undefined || sanitized.origin === null) {
completeness?.exclude(
"frames",
exclusionReasonForUrl(stringValue(frame.url)),
);
continue;
}
total += 1;
if (items.length >= maximum) continue;
items.push({
@@ -62,22 +73,30 @@ export const captureResources = (
result: unknown,
allowedOrigins: ReadonlySet<string>,
maximum: number,
completeness?: CdpCaptureCompleteness,
): {
readonly total: number;
readonly items: WebPageInspection["resources"];
readonly items: readonly CapturedResource[];
} => {
const root = recordValue(requiredRecord(result).frameTree);
if (root === undefined) return { total: 0, items: [] };
const items: WebPageInspection["resources"] = [];
const items: CapturedResource[] = [];
let total = 0;
for (const tree of walkFrameTrees(root)) {
for (const resource of recordsValue(tree.resources)) {
const url = allowedSanitizedUrl(resource.url, allowedOrigins);
if (url === undefined || url.origin === null) continue;
if (url === undefined || url.origin === null) {
completeness?.exclude(
"resources",
exclusionReasonForUrl(stringValue(resource.url)),
);
continue;
}
total += 1;
if (items.length >= maximum) continue;
const contentSize = numberValue(resource.contentSize);
items.push({
rawUrl: stringValue(resource.url) ?? "",
url: url.url,
origin: url.origin,
type: (stringValue(resource.type) ?? "Other").slice(0, 100),
@@ -110,20 +129,29 @@ export const captureDom = (
result: unknown,
allowedOrigins: ReadonlySet<string>,
input: InspectWebPageInput,
completeness?: CdpCaptureCompleteness,
): {
readonly total: number;
readonly nodes: WebPageInspection["dom"]["nodes"];
readonly urls: WebPageInspection["metadata"]["dom_urls"];
readonly agentHints: WebPageInspection["metadata"]["agent_hints"];
readonly excludedUrls: number;
} => {
const root = requiredRecord(result);
const strings = Array.isArray(root.strings)
? root.strings.map((value) => stringValue(value) ?? "")
: [];
const nodes: WebPageInspection["dom"]["nodes"] = [];
const urls: WebPageInspection["metadata"]["dom_urls"] = [];
const agentHints: WebPageInspection["metadata"]["agent_hints"] = [];
let excludedUrls = 0;
let total = 0;
for (const document of recordsValue(root.documents)) {
const documentUrl = indexedString(strings, document.documentURL);
if (allowedSanitizedUrl(documentUrl, allowedOrigins) === undefined)
if (allowedSanitizedUrl(documentUrl, allowedOrigins) === undefined) {
completeness?.exclude("dom", exclusionReasonForUrl(documentUrl));
continue;
}
const documentNodes = recordValue(document.nodes);
if (documentNodes === undefined) continue;
const nodeTypes = numberArray(documentNodes.nodeType);
@@ -142,42 +170,129 @@ export const captureDom = (
) {
const attributeIndexes = numberArray(attributes[index]);
const parent = Math.trunc(parents[index] ?? -1);
const nodeIndex = nodes.length;
const nodeName = indexedString(strings, nodeNames[index]).slice(0, 256);
nodes.push({
index: nodes.length,
index: nodeIndex,
parent_index: parent < 0 ? -1 : baseIndex + parent,
node_type: Math.max(0, Math.trunc(nodeTypes[index] ?? 0)),
node_name: indexedString(strings, nodeNames[index]).slice(0, 256),
node_name: nodeName,
node_value_length: indexedString(strings, nodeValues[index]).length,
attribute_names: attributeIndexes
.filter((_value, attributeIndex) => attributeIndex % 2 === 0)
.map((value) => indexedString(strings, value).slice(0, 256))
.slice(0, 200),
});
const metadata = domMetadata(
strings,
attributeIndexes,
documentUrl,
nodeIndex,
nodeName,
allowedOrigins,
);
for (const url of metadata.urls) {
if (urls.length >= input.limits.max_resources) {
completeness?.truncate("metadata");
continue;
}
urls.push(url);
if (url.destination_scope !== "approved") {
excludedUrls += 1;
completeness?.exclude(
"metadata",
url.destination_scope === "outside_policy"
? "disallowed_origin"
: "unsupported_url",
);
}
}
agentHints.push(...metadata.agentHints);
}
}
return { total, nodes };
return {
total,
nodes,
urls,
agentHints: agentHints.slice(0, input.limits.max_resources),
excludedUrls,
};
};
export const captureAccessibility = (
results: readonly unknown[],
maximum: number,
options: {
readonly includeText: boolean;
readonly maximumFieldBytes: number;
readonly maximumTotalBytes: number;
readonly unavailable?: boolean;
},
): {
readonly total: number;
readonly nodes: WebPageInspection["accessibility"]["nodes"];
readonly textCapture: WebPageInspection["accessibility"]["text_capture"];
} => {
const all = results.flatMap((result) =>
recordsValue(requiredRecord(result).nodes),
);
return {
total: all.length,
nodes: all.slice(0, maximum).map((node) => ({
let retainedBytes = 0;
let excludedFields = 0;
let truncatedFields = 0;
const nodes = all.slice(0, maximum).map((node) => {
const captureText = (value: unknown): string | null => {
const raw = stringValue(recordValue(value)?.value);
if (raw === undefined) return null;
if (!options.includeText) {
excludedFields += 1;
return null;
}
const available = Math.max(0, options.maximumTotalBytes - retainedBytes);
const captured = boundedUtf8(
raw,
Math.min(options.maximumFieldBytes, available),
);
retainedBytes += captured.bytes;
if (captured.truncated) {
truncatedFields += 1;
if (captured.text === "" && raw !== "") {
excludedFields += 1;
return null;
}
}
return captured.text;
};
return {
node_id: (stringValue(node.nodeId) ?? "").slice(0, 256),
parent_id: (stringValue(node.parentId) ?? "").slice(0, 256) || null,
role: axText(node.role),
name: axText(node.name),
description: axText(node.description),
name: captureText(node.name),
description: captureText(node.description),
ignored: node.ignored === true,
})),
};
});
for (const node of all.slice(maximum)) {
for (const value of [node.name, node.description]) {
if (stringValue(recordValue(value)?.value) === undefined) continue;
excludedFields += 1;
if (options.includeText) truncatedFields += 1;
}
}
return {
total: all.length,
nodes,
textCapture: {
status: options.unavailable
? "unavailable"
: !options.includeText
? "not_approved"
: truncatedFields > 0
? "truncated"
: "included",
retained_bytes: retainedBytes,
excluded_fields: excludedFields,
truncated_fields: truncatedFields,
},
};
};
@@ -196,3 +311,112 @@ const numberArray = (value: unknown): readonly number[] =>
return number === undefined ? [] : [number];
})
: [];
const domMetadata = (
strings: readonly string[],
attributes: readonly number[],
documentUrl: string,
nodeIndex: number,
nodeName: string,
allowedOrigins: ReadonlySet<string>,
): {
readonly urls: WebPageInspection["metadata"]["dom_urls"];
readonly agentHints: WebPageInspection["metadata"]["agent_hints"];
} => {
const pairs = new Map<string, string>();
for (let index = 0; index + 1 < attributes.length; index += 2) {
const name = indexedString(strings, attributes[index]).toLowerCase();
const value = indexedString(strings, attributes[index + 1]);
pairs.set(name, value);
}
const urls: WebPageInspection["metadata"]["dom_urls"] = [];
for (const attribute of domUrlAttributes) {
const value = pairs.get(attribute);
if (value === undefined) continue;
const destination = domDestination(value, documentUrl, allowedOrigins);
urls.push({
node_index: nodeIndex,
attribute,
url: destination.url,
destination_scope: destination.scope,
});
}
const rel = (pairs.get("rel") ?? "")
.toLowerCase()
.split(/\s+/u)
.filter((value) => agentRelValues.includes(value));
const href = urls.find(({ attribute }) => attribute === "href")?.url ?? null;
const agentHints =
nodeName.toLowerCase() === "link" && rel.length > 0
? rel.map((declaration) => ({
mechanism: "dom_link_rel" as const,
declaration,
url: href,
trust: "page-declared-untrusted" as const,
}))
: [];
return { urls, agentHints };
};
const domDestination = (
value: string,
documentUrl: string,
allowedOrigins: ReadonlySet<string>,
): {
readonly url: string | null;
readonly scope: "approved" | "outside_policy" | "unsupported";
} => {
try {
const parsed = new URL(value, documentUrl);
if (parsed.protocol !== "http:" && parsed.protocol !== "https:")
return { url: null, scope: "unsupported" };
if (!allowedOrigins.has(parsed.origin))
return { url: null, scope: "outside_policy" };
return {
url: allowedSanitizedUrl(parsed.href, allowedOrigins)?.url ?? null,
scope: "approved",
};
} catch {
return { url: null, scope: "unsupported" };
}
};
const boundedUtf8 = (
value: string,
maximumBytes: number,
): {
readonly text: string;
readonly bytes: number;
readonly truncated: boolean;
} => {
const total = Buffer.byteLength(value);
if (total <= maximumBytes)
return { text: value, bytes: total, truncated: false };
let text = "";
let bytes = 0;
for (const character of value) {
const characterBytes = Buffer.byteLength(character);
if (bytes + characterBytes > maximumBytes) break;
text += character;
bytes += characterBytes;
}
return { text, bytes, truncated: true };
};
const exclusionReasonForUrl = (
value: string | undefined,
): "disallowed_origin" | "unsupported_url" | "unattributed_origin" =>
value === undefined || value === ""
? "unattributed_origin"
: isHttpUrl(value)
? "disallowed_origin"
: "unsupported_url";
const domUrlAttributes = [
"href",
"src",
"action",
"formaction",
"poster",
] as const;
const agentRelValues = ["mcp", "model-context", "ai-plugin", "service-desc"];
+410 -47
View File
@@ -2,7 +2,11 @@ import type {
InspectWebPageInput,
WebPageInspection,
} from "../domain/browserObservation.js";
import { inferJsonShape, type JsonShape } from "../domain/jsonShape.js";
import type { CdpEvent } from "./CdpConnection.js";
import { CdpCaptureCompleteness } from "./CdpCaptureCompleteness.js";
import { safeResponseMetadata } from "./CdpSafeMetadata.js";
import { boundedSensitiveText } from "./SensitiveTextCapture.js";
import {
allowedSanitizedUrl,
boundedText,
@@ -16,6 +20,7 @@ import {
export interface CapturedScript {
readonly scriptId: string;
readonly rawUrl: string;
readonly url: string;
readonly origin: string | null;
readonly hash: string;
@@ -23,6 +28,7 @@ export interface CapturedScript {
readonly isModule: boolean;
readonly language: string | null;
readonly sourceMapUrl: string | null;
readonly sourceMapRawUrl: string | null;
}
type NetworkState = WebPageInspection["network"]["requests"][number];
@@ -34,10 +40,21 @@ export class CdpCaptureEvents {
readonly allowedWebSockets = new Set<string>();
readonly console: WebPageInspection["console"]["events"] = [];
readonly websockets: WebPageInspection["network"]["websocket_events"] = [];
dropped = 0;
readonly responseMetadata: WebPageInspection["metadata"]["responses"] = [];
readonly agentHints: WebPageInspection["metadata"]["agent_hints"] = [];
readonly completeness = new CdpCaptureCompleteness([
"network_requests",
"console_events",
"websocket_connections",
"websocket_frames",
"metadata",
]);
originViolation = false;
navigationDuringCapture = false;
#mainFrameId: string | undefined;
#consoleTextBytes = 0;
#jsonBodyBytes = 0;
#websocketShapeBytes = 0;
constructor(
private readonly input: InspectWebPageInput,
@@ -48,12 +65,23 @@ export class CdpCaptureEvents {
this.#mainFrameId = frameId;
this.originViolation = false;
this.navigationDuringCapture = false;
this.dropped = 0;
this.scripts.clear();
this.network.clear();
this.allowedWebSockets.clear();
this.console.length = 0;
this.websockets.length = 0;
this.responseMetadata.length = 0;
this.agentHints.length = 0;
this.#consoleTextBytes = 0;
this.#jsonBodyBytes = 0;
this.#websocketShapeBytes = 0;
this.completeness.reset();
if (!this.input.include_console_text)
this.completeness.exclude("console_text", "not_approved", null);
if (!this.input.include_json_body_shapes)
this.completeness.exclude("json_body_shapes", "not_approved", null);
if (!this.input.include_websocket_shapes)
this.completeness.exclude("websocket_shapes", "not_approved", null);
}
beginFinalCapture(frameId: string): void {
@@ -99,21 +127,25 @@ export class CdpCaptureEvents {
const scriptId = stringValue(params.scriptId);
const rawUrl = stringValue(params.url) ?? "";
const sanitized = allowedSanitizedUrl(rawUrl, this.allowedOrigins);
if (
scriptId === undefined ||
scriptId.length > 256 ||
sanitized === undefined
)
if (scriptId === undefined || scriptId.length > 256) {
this.completeness.exclude("scripts", "invalid_protocol_value");
return;
}
if (sanitized === undefined) {
this.completeness.exclude("scripts", exclusionReasonForUrl(rawUrl));
return;
}
if (
this.scripts.size >= this.input.limits.max_scripts &&
!this.scripts.has(scriptId)
) {
this.dropped += 1;
this.completeness.drop("scripts");
return;
}
const sourceMap = this.#sourceMap(params.sourceMapURL);
this.scripts.set(scriptId, {
scriptId,
rawUrl,
url: sanitized.url,
origin: sanitized.origin,
hash: (stringValue(params.hash) ?? "").slice(0, 512),
@@ -126,21 +158,43 @@ export class CdpCaptureEvents {
),
isModule: params.isModule === true,
language: boundedText(params.scriptLanguage, 100),
sourceMapUrl: this.#sourceMap(params.sourceMapURL),
sourceMapUrl: sourceMap.sanitized,
sourceMapRawUrl: sourceMap.raw,
});
}
#sourceMap(value: unknown): string | null {
const sanitized = allowedSanitizedUrl(value, this.allowedOrigins);
return sanitized?.url ?? null;
#sourceMap(value: unknown): {
readonly sanitized: string | null;
readonly raw: string | null;
} {
const rawUrl = stringValue(value);
if (rawUrl === undefined || rawUrl === "")
return { sanitized: null, raw: null };
const sanitized = allowedSanitizedUrl(rawUrl, this.allowedOrigins);
if (sanitized !== undefined)
return { sanitized: sanitized.url, raw: rawUrl };
this.completeness.exclude("source_maps", exclusionReasonForUrl(rawUrl));
return { sanitized: null, raw: null };
}
#request(params: UnknownRecord): void {
const requestId = stringValue(params.requestId);
const request = recordValue(params.request);
const sanitized = allowedSanitizedUrl(request?.url, this.allowedOrigins);
if (requestId === undefined || requestId.length > 256) return;
if (request === undefined || sanitized === undefined) {
if (requestId === undefined || requestId.length > 256) {
this.completeness.exclude("network_requests", "invalid_protocol_value");
return;
}
if (request === undefined) {
this.completeness.exclude("network_requests", "invalid_protocol_value");
this.network.delete(requestId);
return;
}
const sanitized = allowedSanitizedUrl(request.url, this.allowedOrigins);
if (sanitized === undefined) {
this.completeness.exclude(
"network_requests",
exclusionReasonForUrl(stringValue(request.url)),
);
this.network.delete(requestId);
return;
}
@@ -148,14 +202,21 @@ export class CdpCaptureEvents {
this.network.size >= this.input.limits.max_network_events &&
!this.network.has(requestId)
) {
this.dropped += 1;
this.completeness.drop("network_requests");
return;
}
const initiator = recordValue(params.initiator);
const initiatorFrame = initiatorLocation(initiator);
const rawInitiatorUrl = stringValue(initiatorFrame?.url);
const initiatorUrl = allowedSanitizedUrl(
firstCallFrame(initiator)?.url,
rawInitiatorUrl,
this.allowedOrigins,
);
if (rawInitiatorUrl !== undefined && initiatorUrl === undefined)
this.completeness.exclude(
"network_initiators",
exclusionReasonForUrl(rawInitiatorUrl),
);
this.network.set(requestId, {
request_id: requestId,
url: sanitized.url,
@@ -168,20 +229,30 @@ export class CdpCaptureEvents {
initiator: {
type: (stringValue(initiator?.type) ?? "other").slice(0, 100),
url: initiatorUrl?.url ?? null,
line: integerOrNull(firstCallFrame(initiator)?.lineNumber),
column: integerOrNull(firstCallFrame(initiator)?.columnNumber),
line: integerOrNull(initiatorFrame?.lineNumber),
column: integerOrNull(initiatorFrame?.columnNumber),
},
body_shapes: this.#requestBodyShape(request),
});
}
#response(params: UnknownRecord): void {
const requestId = stringValue(params.requestId);
if (requestId === undefined || requestId.length > 256) return;
if (requestId === undefined || requestId.length > 256) {
this.completeness.exclude("network_requests", "invalid_protocol_value");
return;
}
const current = this.network.get(requestId);
const response = recordValue(params.response);
const sanitized = allowedSanitizedUrl(response?.url, this.allowedOrigins);
if (current === undefined) return;
const sanitized = allowedSanitizedUrl(response?.url, this.allowedOrigins);
if (response === undefined || sanitized === undefined) {
this.completeness.exclude(
"network_requests",
response === undefined
? "invalid_protocol_value"
: exclusionReasonForUrl(stringValue(response.url)),
);
this.network.delete(requestId);
return;
}
@@ -190,6 +261,18 @@ export class CdpCaptureEvents {
status: numberValue(response.status) ?? null,
mime_type: boundedText(response.mimeType, 256),
});
if (this.responseMetadata.length >= this.input.limits.max_network_events) {
this.completeness.truncate("metadata");
return;
}
const metadata = safeResponseMetadata(
requestId,
sanitized.url,
response,
this.allowedOrigins,
);
this.responseMetadata.push(metadata.response);
this.agentHints.push(...metadata.agentHints);
}
#finished(params: UnknownRecord): void {
@@ -208,23 +291,35 @@ export class CdpCaptureEvents {
#console(params: UnknownRecord): void {
const frame = firstCallFrame(recordValue(params.stackTrace));
const source = allowedSanitizedUrl(frame?.url, this.allowedOrigins);
if (frame === undefined || source === undefined) return;
if (this.console.length >= this.input.limits.max_console_events) {
this.dropped += 1;
if (frame === undefined) {
this.completeness.exclude("console_events", "unattributed_origin");
return;
}
const source = allowedSanitizedUrl(frame.url, this.allowedOrigins);
if (source === undefined) {
this.completeness.exclude(
"console_events",
exclusionReasonForUrl(stringValue(frame.url)),
);
return;
}
if (this.console.length >= this.input.limits.max_console_events) {
this.completeness.drop("console_events");
return;
}
const arguments_ = recordsValue(params.args);
this.console.push({
type: (stringValue(params.type) ?? "unknown").slice(0, 100),
timestamp: numberValue(params.timestamp) ?? 0,
argument_types: recordsValue(params.args)
argument_types: arguments_
.map((argument) =>
(stringValue(argument.type) ?? "unknown").slice(0, 100),
)
.slice(0, 100),
url: source?.url ?? null,
line: integerOrNull(frame?.lineNumber),
column: integerOrNull(frame?.columnNumber),
url: source.url,
line: integerOrNull(frame.lineNumber),
column: integerOrNull(frame.columnNumber),
text_capture: this.#consoleText(arguments_),
});
}
@@ -237,48 +332,243 @@ export class CdpCaptureEvents {
)
return;
if (this.websockets.length >= this.input.limits.max_websocket_events) {
this.dropped += 1;
this.completeness.drop("websocket_frames");
return;
}
const response = recordValue(params.response);
const payload = stringValue(response?.payloadData) ?? "";
const opcode = Math.max(0, Math.trunc(numberValue(response?.opcode) ?? 0));
const decoded = opcode === 1 ? undefined : decodeBase64(payload);
if (opcode !== 1 && decoded === undefined)
this.completeness.exclude("websocket_frames", "invalid_protocol_value");
this.websockets.push({
request_id: requestId,
direction,
opcode,
payload_bytes:
opcode === 1
? Buffer.byteLength(payload)
: Buffer.from(payload, "base64").byteLength,
opcode === 1 ? Buffer.byteLength(payload) : (decoded?.byteLength ?? 0),
payload_shape: this.#websocketShape(payload, opcode),
});
}
responseBodyRequestIds(): readonly string[] {
if (!this.input.include_json_body_shapes) return [];
return [...this.network.values()]
.filter((request) => isJsonMediaType(request.mime_type))
.map((request) => request.request_id);
}
responseBodyUnavailable(requestId: string): void {
this.#updateResponseShape(requestId, null, false);
this.completeness.unavailable("json_body_shapes");
}
ingestResponseBody(requestId: string, value: unknown): void {
const result = recordValue(value);
const body = stringValue(result?.body);
if (body === undefined) {
this.#invalidResponseBody(requestId);
return;
}
const decoded =
result?.base64Encoded === true
? decodeBase64(body)?.toString("utf8")
: body;
if (decoded === undefined) {
this.#invalidResponseBody(requestId);
return;
}
const inferred = this.#inferBodyShape(decoded);
this.#updateResponseShape(requestId, inferred.shape, inferred.truncated);
}
#invalidResponseBody(requestId: string): void {
this.#updateResponseShape(requestId, null, false);
this.completeness.exclude("json_body_shapes", "invalid_protocol_value");
}
#requestBodyShape(request: UnknownRecord): NetworkState["body_shapes"] {
if (!this.input.include_json_body_shapes)
return { status: "not_approved", request: null, response: null };
const body = stringValue(request.postData);
if (!isJsonContentType(recordValue(request.headers)) || body === undefined)
return { status: "unavailable", request: null, response: null };
const inferred = this.#inferBodyShape(body);
return {
status: inferred.truncated
? "truncated"
: inferred.shape === null
? "unavailable"
: "included",
request: inferred.shape,
response: null,
};
}
#inferBodyShape(text: string): {
readonly shape: JsonShape | null;
readonly truncated: boolean;
} {
const bytes = Buffer.byteLength(text);
const remaining =
this.input.limits.max_total_json_body_bytes - this.#jsonBodyBytes;
if (bytes > this.input.limits.max_json_body_bytes || bytes > remaining) {
this.completeness.truncate("json_body_shapes");
return { shape: null, truncated: true };
}
this.#jsonBodyBytes += bytes;
const shape = inferJsonShape(text, {
maximumBytes: this.input.limits.max_json_body_bytes,
maximumNodes: this.input.limits.max_json_shape_nodes,
maximumDepth: this.input.limits.max_json_shape_depth,
});
if (shape?.truncated === true)
this.completeness.truncate("json_body_shapes");
return { shape, truncated: shape?.truncated === true };
}
#updateResponseShape(
requestId: string,
response: JsonShape | null,
truncated: boolean,
): void {
const current = this.network.get(requestId);
if (current === undefined) return;
const request = current.body_shapes.request;
const status =
truncated || current.body_shapes.status === "truncated"
? "truncated"
: response !== null
? "included"
: request !== null
? "partial"
: "unavailable";
this.network.set(requestId, {
...current,
body_shapes: { status, request, response },
});
}
#consoleText(
arguments_: readonly UnknownRecord[],
): WebPageInspection["console"]["events"][number]["text_capture"] {
if (!this.input.include_console_text)
return {
status: "not_approved",
values: [],
retained_bytes: 0,
truncated_values: 0,
};
const values: WebPageInspection["console"]["events"][number]["text_capture"]["values"] =
[];
let retainedBytes = 0;
let truncatedValues = 0;
for (const [argumentIndex, argument] of arguments_.entries()) {
const primitive = consolePrimitive(argument);
if (primitive === undefined) continue;
const remaining =
this.input.limits.max_total_console_text_bytes - this.#consoleTextBytes;
if (remaining <= 0) {
truncatedValues += 1;
continue;
}
const bounded = boundedSensitiveText(
primitive.text,
Math.min(this.input.limits.max_console_text_field_bytes, remaining),
);
values.push({
argument_index: argumentIndex,
type: primitive.type,
text: bounded.text,
});
retainedBytes += bounded.bytes;
this.#consoleTextBytes += bounded.bytes;
if (bounded.truncated) truncatedValues += 1;
}
if (truncatedValues > 0) this.completeness.truncate("console_text");
return {
status: truncatedValues > 0 ? "truncated" : "included",
values,
retained_bytes: retainedBytes,
truncated_values: truncatedValues,
};
}
#websocketShape(
payload: string,
opcode: number,
): WebPageInspection["network"]["websocket_events"][number]["payload_shape"] {
if (!this.input.include_websocket_shapes) return null;
if (opcode !== 1)
return { format: "binary", json_shape: null, truncated: false };
const bytes = Buffer.byteLength(payload);
const remaining =
this.input.limits.max_total_websocket_shape_bytes -
this.#websocketShapeBytes;
if (
bytes > this.input.limits.max_websocket_shape_bytes ||
bytes > remaining
) {
this.completeness.truncate("websocket_shapes");
return { format: "text", json_shape: null, truncated: true };
}
this.#websocketShapeBytes += bytes;
const shape = inferJsonShape(payload, {
maximumBytes: this.input.limits.max_websocket_shape_bytes,
maximumNodes: this.input.limits.max_json_shape_nodes,
maximumDepth: this.input.limits.max_json_shape_depth,
});
if (shape?.truncated === true)
this.completeness.truncate("websocket_shapes");
return {
format: shape === null ? "text" : "json",
json_shape: shape,
truncated: shape?.truncated === true,
};
}
#websocketCreated(params: UnknownRecord): void {
const requestId = stringValue(params.requestId);
const rawUrl = stringValue(params.url);
if (
requestId === undefined ||
requestId.length > 256 ||
rawUrl === undefined
)
if (requestId === undefined || requestId.length > 256) {
this.completeness.exclude(
"websocket_connections",
"invalid_protocol_value",
);
return;
}
if (rawUrl === undefined) {
this.completeness.exclude(
"websocket_connections",
"invalid_protocol_value",
);
return;
}
let parsed: URL;
try {
parsed = new URL(rawUrl);
} catch {
this.completeness.exclude("websocket_connections", "unsupported_url");
return;
}
if (parsed.protocol === "ws:") parsed.protocol = "http:";
else if (parsed.protocol === "wss:") parsed.protocol = "https:";
else return;
else {
this.completeness.exclude("websocket_connections", "unsupported_url");
return;
}
if (!this.allowedOrigins.has(parsed.origin)) {
this.completeness.exclude("websocket_connections", "disallowed_origin");
return;
}
if (
this.allowedOrigins.has(parsed.origin) &&
(this.allowedWebSockets.has(requestId) ||
this.allowedWebSockets.size < this.input.limits.max_websocket_events)
)
this.allowedWebSockets.add(requestId);
else if (this.allowedOrigins.has(parsed.origin)) this.dropped += 1;
!this.allowedWebSockets.has(requestId) &&
this.allowedWebSockets.size >= this.input.limits.max_websocket_events
) {
this.completeness.drop("websocket_connections");
return;
}
this.allowedWebSockets.add(requestId);
}
#frameNavigated(params: UnknownRecord): void {
@@ -296,12 +586,85 @@ export class CdpCaptureEvents {
const firstCallFrame = (
value: UnknownRecord | undefined,
): UnknownRecord | undefined => recordsValue(value?.callFrames)[0];
const initiatorLocation = (
initiator: UnknownRecord | undefined,
): UnknownRecord | undefined => {
const callFrames = recordsValue(value?.callFrames);
return callFrames[0];
let stack = recordValue(initiator?.stack);
for (let depth = 0; depth < 32 && stack !== undefined; depth += 1) {
const frame = firstCallFrame(stack);
if (frame !== undefined) return frame;
stack = recordValue(stack.parent);
}
return stringValue(initiator?.url) === undefined ? undefined : initiator;
};
const exclusionReasonForUrl = (
value: string | undefined,
): "disallowed_origin" | "unsupported_url" | "unattributed_origin" =>
value === undefined || value === ""
? "unattributed_origin"
: isHttpUrl(value)
? "disallowed_origin"
: "unsupported_url";
const integerOrNull = (value: unknown): number | null => {
const number = numberValue(value);
return number === undefined ? null : Math.max(0, Math.trunc(number));
};
const isJsonContentType = (headers: UnknownRecord | undefined): boolean => {
if (headers === undefined) return false;
for (const [name, value] of Object.entries(headers))
if (
name.toLowerCase() === "content-type" &&
isJsonMediaType(stringValue(value))
)
return true;
return false;
};
const isJsonMediaType = (value: string | null | undefined): boolean => {
const mediaType = value?.split(";", 1)[0]?.trim().toLowerCase();
return (
mediaType === "application/json" || mediaType?.endsWith("+json") === true
);
};
const consolePrimitive = (
value: UnknownRecord,
): { readonly type: string; readonly text: string } | undefined => {
const type = stringValue(value.type);
switch (type) {
case "string":
return typeof value.value === "string"
? { type, text: value.value }
: undefined;
case "boolean":
return typeof value.value === "boolean"
? { type, text: String(value.value) }
: undefined;
case "number":
return typeof value.value === "number"
? { type, text: String(value.value) }
: typeof value.unserializableValue === "string"
? { type, text: value.unserializableValue }
: undefined;
case "bigint":
return typeof value.unserializableValue === "string"
? { type, text: value.unserializableValue }
: undefined;
case "undefined":
return { type, text: "undefined" };
default:
return undefined;
}
};
const BASE64 = /^(?:[A-Za-z\d+/]{4})*(?:[A-Za-z\d+/]{2}==|[A-Za-z\d+/]{3}=)?$/u;
const decodeBase64 = (value: string): Buffer | undefined =>
value.length % 4 === 0 && BASE64.test(value)
? Buffer.from(value, "base64")
: undefined;
+3 -6
View File
@@ -1,10 +1,9 @@
import { createHash } from "node:crypto";
import {
AnalysisCancelledError,
BrowserObservationError,
} from "../domain/errors.js";
import { sanitizeBrowserUrl } from "../domain/browserObservation.js";
import { createWebTextArtifact } from "../domain/webContentArtifact.js";
export type UnknownRecord = Readonly<Record<string, unknown>>;
@@ -64,11 +63,9 @@ export const allowedSanitizedUrl = (
export const sourceResult = (
source: string,
): { included: true; sha256: string; bytes: number; content: string } => ({
): { included: true; artifact: ReturnType<typeof createWebTextArtifact> } => ({
included: true,
sha256: createHash("sha256").update(source).digest("hex"),
bytes: Buffer.byteLength(source),
content: source,
artifact: createWebTextArtifact(source, "text/javascript"),
});
export const sourceExcluded = (
+481
View File
@@ -0,0 +1,481 @@
import { createHash } from "node:crypto";
import canonicalize from "canonicalize";
import type { ProgressReporter } from "../application/ProgressReporter.js";
import type {
ElectronPageInspection,
InspectElectronPageInput,
} from "../domain/electronObservation.js";
import { BrowserObservationError } from "../domain/errors.js";
import { createWebTextArtifact } from "../domain/webContentArtifact.js";
import type { CdpConnection, CdpEvent } from "./CdpConnection.js";
import type { CdpEndpointDiscovery, CdpEndpointTarget } from "./CdpEndpoint.js";
import { CdpCaptureCompleteness } from "./CdpCaptureCompleteness.js";
import {
boundedText,
delayWithCancellation,
numberValue,
recordValue,
recordsValue,
requiredRecord,
stringValue,
type UnknownRecord,
} from "./CdpCaptureValues.js";
import {
authorizedElectronFile,
canonicalElectronRoots,
} from "./ElectronFileScope.js";
interface ElectronContext {
readonly connection: CdpConnection;
readonly sessionId: string;
readonly discovery: CdpEndpointDiscovery;
readonly target: CdpEndpointTarget;
readonly input: InspectElectronPageInput;
readonly signal?: AbortSignal;
readonly progress?: ProgressReporter;
}
interface ScriptDraft {
readonly scriptId: string;
readonly rawUrl: string;
readonly hash: string;
readonly length: number;
readonly isModule: boolean;
readonly language: string | null;
}
/** Passively inspect one root-confined Electron file page. */
export const inspectCdpElectronPage = async (
context: ElectronContext,
): Promise<ElectronPageInspection> => {
const roots = await canonicalElectronRoots(context.input.allowed_file_roots);
const completeness = new CdpCaptureCompleteness();
const scripts: ScriptDraft[] = [];
let mainFrameId: string | undefined;
let navigationDuringCapture = false;
const removeListener = context.connection.onEvent((event) => {
if (event.sessionId !== context.sessionId) return;
ingestScript(event, scripts, completeness);
if (mainFrameId !== undefined && navigatedFrameId(event) === mainFrameId)
navigationDuringCapture = true;
});
const startedAt = new Date().toISOString();
try {
await report(context.progress, 1, "Authorizing Electron file frames");
await context.connection.send(
"Page.enable",
{},
context.sessionId,
context.signal,
);
const before = await context.connection.send(
"Page.getFrameTree",
{},
context.sessionId,
context.signal,
);
const frames = await captureFrames(
before,
roots,
context.input.limits.max_frames,
completeness,
);
const main = frames[0];
if (main === undefined)
throw new BrowserObservationError(
"inspect_web_page",
"target_not_allowed",
);
mainFrameId = main.frame_id;
await context.connection.send(
"Debugger.enable",
{ maxScriptsCacheSize: 4 * 1_024 * 1_024 },
context.sessionId,
context.signal,
);
await delayWithCancellation(context.input.observation_ms, context.signal);
await report(context.progress, 2, "Capturing Electron structure");
const resources = await captureResources(context, roots, completeness);
const dom = await captureDom(context, roots, completeness);
const normalizedScripts = await captureScripts(
context,
roots,
scripts,
completeness,
);
const after = await context.connection.send(
"Page.getFrameTree",
{},
context.sessionId,
context.signal,
);
const afterMain = await mainFilePath(after, roots);
if (afterMain === undefined)
throw new BrowserObservationError(
"inspect_web_page",
"target_not_allowed",
);
if (navigationDuringCapture || afterMain !== main.file_path)
throw new BrowserObservationError("inspect_web_page", "target_changed");
await report(context.progress, 3, "Electron inspection complete");
return {
schema_version: 1,
browser: context.discovery.version,
target: {
target_id: context.target.id,
type: context.target.type,
title: context.target.title.slice(0, 16_384),
file_path: main.file_path,
attached: context.target.attached,
},
capture_window: {
started_at: startedAt,
ended_at: new Date().toISOString(),
observation_ms: context.input.observation_ms,
},
completeness: completeness.snapshot(),
frames,
dom,
scripts: normalizedScripts,
resources,
limitations: [
"Only canonical file paths contained by an approved root are retained.",
"REA does not evaluate renderer JavaScript, invoke Electron APIs, navigate, click, or close the page.",
"Script contents require separate source-capture approval and remain byte bounded.",
],
};
} finally {
removeListener();
}
};
const navigatedFrameId = (event: CdpEvent): string | undefined => {
const params = recordValue(event.params);
if (params === undefined) return undefined;
if (event.method === "Page.frameNavigated")
return stringValue(recordValue(params.frame)?.id);
if (event.method === "Page.navigatedWithinDocument")
return stringValue(params.frameId);
return undefined;
};
const ingestScript = (
event: CdpEvent,
scripts: ScriptDraft[],
completeness: CdpCaptureCompleteness,
): void => {
if (event.method !== "Debugger.scriptParsed") return;
const value = recordValue(event.params);
const scriptId = stringValue(value?.scriptId);
const rawUrl = stringValue(value?.url);
if (scriptId === undefined || scriptId.length > 256) {
completeness.exclude("scripts", "invalid_protocol_value");
return;
}
if (rawUrl === undefined || rawUrl === "") {
completeness.exclude("scripts", "unattributed_origin");
return;
}
if (scripts.length >= 2_000) {
completeness.drop("scripts");
return;
}
scripts.push({
scriptId,
rawUrl,
hash: (stringValue(value?.hash) ?? "").slice(0, 512),
length: nonnegativeInteger(value?.length),
isModule: value?.isModule === true,
language: boundedText(value?.scriptLanguage, 100),
});
};
const captureFrames = async (
result: unknown,
roots: readonly string[],
maximum: number,
completeness: CdpCaptureCompleteness,
): Promise<ElectronPageInspection["frames"]> => {
const frameTree = recordValue(requiredRecord(result).frameTree);
if (frameTree === undefined)
throw new BrowserObservationError("inspect_web_page", "protocol_error");
const pending: Array<{
readonly tree: UnknownRecord;
readonly parent: string | null;
}> = [{ tree: frameTree, parent: null }];
const frames: ElectronPageInspection["frames"] = [];
while (pending.length > 0) {
const current = pending.shift();
if (current === undefined) break;
const frame = recordValue(current.tree.frame);
const frameId = stringValue(frame?.id);
const path = await authorizedElectronFile(
stringValue(frame?.url) ?? "",
roots,
);
if (frameId === undefined || path === undefined) {
completeness.exclude("frames", "out_of_target_scope");
continue;
}
if (frames.length >= maximum) {
completeness.truncate("frames");
continue;
}
frames.push({
frame_id: frameId.slice(0, 256),
parent_frame_id: current.parent,
file_path: path,
});
for (const child of recordsValue(current.tree.childFrames))
pending.push({ tree: child, parent: frameId.slice(0, 256) });
}
return frames;
};
const captureResources = async (
context: ElectronContext,
roots: readonly string[],
completeness: CdpCaptureCompleteness,
): Promise<ElectronPageInspection["resources"]> => {
const result = await context.connection.send(
"Page.getResourceTree",
{},
context.sessionId,
context.signal,
);
const root = recordValue(requiredRecord(result).frameTree);
if (root === undefined)
throw new BrowserObservationError("inspect_web_page", "protocol_error");
const pending = [root];
const resources: ElectronPageInspection["resources"] = [];
const seen = new Set<string>();
while (pending.length > 0) {
const tree = pending.shift();
if (tree === undefined) break;
for (const resource of recordsValue(tree.resources)) {
const path = await authorizedElectronFile(
stringValue(resource.url) ?? "",
roots,
);
if (path === undefined) {
completeness.exclude("resources", "out_of_target_scope");
continue;
}
const item = {
file_path: path,
type: (stringValue(resource.type) ?? "Other").slice(0, 100),
mime_type: (stringValue(resource.mimeType) ?? "").slice(0, 256),
content_size:
numberValue(resource.contentSize) === undefined
? null
: Math.max(0, numberValue(resource.contentSize) ?? 0),
};
const resourceKey = `electron_resource_${digest(item)}`;
if (seen.has(resourceKey)) continue;
seen.add(resourceKey);
if (resources.length >= context.input.limits.max_resources) {
completeness.truncate("resources");
continue;
}
resources.push({
resource_key: resourceKey,
...item,
});
}
pending.push(...recordsValue(tree.childFrames));
}
return resources.sort((left, right) =>
left.resource_key.localeCompare(right.resource_key),
);
};
const captureDom = async (
context: ElectronContext,
roots: readonly string[],
completeness: CdpCaptureCompleteness,
): Promise<ElectronPageInspection["dom"]> => {
const result = requiredRecord(
await context.connection.send(
"DOMSnapshot.captureSnapshot",
{ computedStyles: [], includePaintOrder: false, includeDOMRects: false },
context.sessionId,
context.signal,
),
);
const strings = Array.isArray(result.strings)
? result.strings.map((value) => (typeof value === "string" ? value : ""))
: [];
const nodes: ElectronPageInspection["dom"]["nodes"] = [];
let total = 0;
for (const document of recordsValue(result.documents)) {
const documentUrl = stringAt(strings, document.documentURL);
if ((await authorizedElectronFile(documentUrl, roots)) === undefined) {
completeness.exclude("dom", "out_of_target_scope");
continue;
}
const nodeData = recordValue(document.nodes);
const nodeTypes = arrayValue(nodeData?.nodeType);
const nodeNames = arrayValue(nodeData?.nodeName);
const nodeValues = arrayValue(nodeData?.nodeValue);
const parents = arrayValue(nodeData?.parentIndex);
const attributes = arrayValue(nodeData?.attributes);
const baseIndex = nodes.length;
total += nodeTypes.length;
for (let index = 0; index < nodeTypes.length; index += 1) {
if (nodes.length >= context.input.limits.max_dom_nodes) continue;
const attributeIndexes = arrayValue(attributes[index]);
const parent = integer(parents[index], -1);
nodes.push({
index: nodes.length,
parent_index: parent < 0 ? -1 : baseIndex + parent,
node_type: integer(nodeTypes[index], 0),
node_name: stringAt(strings, nodeNames[index]).slice(0, 1_024),
node_value_length: Buffer.byteLength(
stringAt(strings, nodeValues[index]),
),
attribute_names: attributeIndexes
.filter((_value, attributeIndex) => attributeIndex % 2 === 0)
.map((value) => stringAt(strings, value).slice(0, 1_024))
.slice(0, 256),
});
}
}
if (total > nodes.length) completeness.truncate("dom");
return { total_nodes: total, nodes };
};
const captureScripts = async (
context: ElectronContext,
roots: readonly string[],
scripts: readonly ScriptDraft[],
completeness: CdpCaptureCompleteness,
): Promise<ElectronPageInspection["scripts"]> => {
let total = 0;
let sourceBytes = 0;
const items: ElectronPageInspection["scripts"]["items"] = [];
const seen = new Set<string>();
for (const script of scripts) {
const path = await authorizedElectronFile(script.rawUrl, roots);
if (path === undefined) {
completeness.exclude("scripts", "out_of_target_scope");
continue;
}
const identity = {
file_path: path,
cdp_hash: script.hash,
length: script.length,
is_module: script.isModule,
language: script.language,
};
const scriptKey = `electron_script_${digest(identity)}`;
if (seen.has(scriptKey)) continue;
seen.add(scriptKey);
total += 1;
if (items.length >= context.input.limits.max_scripts) {
completeness.drop("scripts");
continue;
}
let source: ElectronPageInspection["scripts"]["items"][number]["source"] = {
included: false,
reason: "source capture was not approved",
};
if (context.input.include_script_sources) {
if (
script.length > context.input.limits.max_script_source_bytes ||
sourceBytes + script.length >
context.input.limits.max_total_script_source_bytes
) {
source = {
included: false,
reason: "script source byte limit reached",
};
completeness.truncate("script_sources");
} else {
const result = requiredRecord(
await context.connection.send(
"Debugger.getScriptSource",
{ scriptId: script.scriptId },
context.sessionId,
context.signal,
),
);
const text = stringValue(result.scriptSource) ?? "";
const bytes = Buffer.byteLength(text);
if (
bytes > context.input.limits.max_script_source_bytes ||
sourceBytes + bytes >
context.input.limits.max_total_script_source_bytes
) {
source = {
included: false,
reason: "script source byte limit reached",
};
completeness.truncate("script_sources");
} else {
source = {
included: true,
artifact: createWebTextArtifact(text, "text/javascript"),
};
sourceBytes += bytes;
}
}
}
items.push({
script_key: scriptKey,
...identity,
source,
});
}
if (!context.input.include_script_sources)
completeness.exclude("script_sources", "not_approved", total);
return {
total,
items: items.sort((left, right) =>
left.script_key.localeCompare(right.script_key),
),
};
};
const mainFilePath = async (
result: unknown,
roots: readonly string[],
): Promise<string | undefined> => {
const frameTree = recordValue(requiredRecord(result).frameTree);
const frame = recordValue(frameTree?.frame);
return await authorizedElectronFile(stringValue(frame?.url) ?? "", roots);
};
const arrayValue = (value: unknown): readonly unknown[] =>
Array.isArray(value) ? value : [];
const stringAt = (strings: readonly string[], value: unknown): string =>
strings[integer(value, -1)] ?? "";
const integer = (value: unknown, fallback: number): number => {
const number = numberValue(value);
return number === undefined ? fallback : Math.trunc(number);
};
const nonnegativeInteger = (value: unknown): number =>
Math.max(0, integer(value, 0));
const digest = (value: unknown): string => {
const encoded = canonicalize(value);
if (encoded === undefined) throw new TypeError("Expected canonical JSON");
return createHash("sha256").update(encoded).digest("hex");
};
const report = async (
progress: ProgressReporter | undefined,
completed: number,
message: string,
): Promise<void> =>
await progress?.report({
phase: "browser_observation",
completed,
total: 3,
message,
...(completed === 3 ? { terminal: true } : {}),
});
+223
View File
@@ -0,0 +1,223 @@
import type {
ExecutionOptions,
ProviderIdentity,
} from "../application/AnalysisProvider.js";
import type { ElectronObservationPort } from "../application/ElectronObservationPort.js";
import {
electronPageInspectionSchema,
electronTargetListSchema,
type ElectronPageInspection,
type ElectronTargetList,
type InspectElectronPageInput,
type ListElectronTargetsInput,
} from "../domain/electronObservation.js";
import {
AnalysisError,
BrowserObservationError,
ProviderAdapterError,
type BrowserObservationOperation,
} from "../domain/errors.js";
import { err, ok, type Result } from "../domain/result.js";
import { CdpConnection } from "./CdpConnection.js";
import { discoverCdpEndpoint, type CdpEndpointTarget } from "./CdpEndpoint.js";
import { inspectCdpElectronPage } from "./CdpElectronInspection.js";
import {
authorizedElectronFile,
canonicalElectronRoots,
} from "./ElectronFileScope.js";
const IDENTITY: ProviderIdentity = {
id: "rea-cdp-electron",
name: "REA Electron file-page CDP observation provider",
version: "1",
};
/** Passive Electron provider with canonical file-root confinement. */
export class CdpElectronProvider implements ElectronObservationPort {
identity(): ProviderIdentity {
return IDENTITY;
}
async listTargets(
input: ListElectronTargetsInput,
options: ExecutionOptions = {},
): Promise<Result<ElectronTargetList, AnalysisError>> {
try {
const roots = await canonicalElectronRoots(input.allowed_file_roots);
const discovery = await discoverCdpEndpoint(
input.cdp_endpoint,
"list_browser_targets",
options.signal,
);
const allowed = [];
let outsideRoot = 0;
let unsupportedUrl = 0;
let nonPage = 0;
for (const target of discovery.targets) {
if (target.type !== "page") {
nonPage += 1;
continue;
}
const path = await authorizedElectronFile(target.url, roots);
if (path === undefined) {
if (isFileUrl(target.url)) outsideRoot += 1;
else unsupportedUrl += 1;
continue;
}
allowed.push({
target_id: target.id,
type: target.type,
title: target.title.slice(0, 16_384),
file_path: path,
attached: target.attached,
});
}
allowed.sort((left, right) =>
left.target_id.localeCompare(right.target_id),
);
const items = allowed.slice(input.offset, input.offset + input.limit);
const next = input.offset + items.length;
return ok(
electronTargetListSchema.parse({
schema_version: 1,
browser: discovery.version,
targets: {
items,
offset: input.offset,
limit: input.limit,
total: allowed.length,
next_offset: next < allowed.length ? next : null,
has_more: next < allowed.length,
},
excluded: {
outside_root: outsideRoot,
unsupported_url: unsupportedUrl,
non_page: nonPage,
},
limitations: [
"Only page targets whose canonical file path is contained by an approved root are listed.",
],
}),
);
} catch (cause: unknown) {
return err(providerError(cause, "list_electron_targets"));
}
}
async inspectPage(
input: InspectElectronPageInput,
options: ExecutionOptions = {},
): Promise<Result<ElectronPageInspection, AnalysisError>> {
let connection: CdpConnection | undefined;
let sessionId: string | undefined;
try {
const roots = await canonicalElectronRoots(input.allowed_file_roots);
const discovery = await discoverCdpEndpoint(
input.cdp_endpoint,
"inspect_web_page",
options.signal,
);
const target = await authorizeTarget(
discovery.targets,
input.target_id,
roots,
);
connection = await CdpConnection.connect(
discovery.browserWebSocketUrl,
options.signal,
);
const attached = await connection.send(
"Target.attachToTarget",
{ targetId: target.id, flatten: true },
undefined,
options.signal,
);
sessionId = attachedSessionId(attached);
return ok(
electronPageInspectionSchema.parse(
await inspectCdpElectronPage({
connection,
sessionId,
discovery,
target,
input,
...(options.signal === undefined ? {} : { signal: options.signal }),
...(options.progress === undefined
? {}
: { progress: options.progress }),
}),
),
);
} catch (cause: unknown) {
return err(providerError(cause, "inspect_electron_page"));
} finally {
if (connection !== undefined) {
if (sessionId !== undefined) await cleanup(connection, sessionId);
await connection.close();
}
}
}
}
const authorizeTarget = async (
targets: readonly CdpEndpointTarget[],
targetId: string,
roots: readonly string[],
): Promise<CdpEndpointTarget> => {
const target = targets.find(({ id }) => id === targetId);
if (target === undefined)
throw new BrowserObservationError("inspect_web_page", "target_not_found");
if (
target.type !== "page" ||
(await authorizedElectronFile(target.url, roots)) === undefined
)
throw new BrowserObservationError("inspect_web_page", "target_not_allowed");
return target;
};
const attachedSessionId = (value: unknown): string => {
if (
typeof value !== "object" ||
value === null ||
!("sessionId" in value) ||
typeof value.sessionId !== "string" ||
value.sessionId.length > 256
)
throw new BrowserObservationError("inspect_web_page", "protocol_error");
return value.sessionId;
};
const cleanup = async (
connection: CdpConnection,
sessionId: string,
): Promise<void> => {
for (const method of ["Debugger.disable", "Page.disable"])
try {
await connection.send(method, {}, sessionId);
} catch {
// Detach remains the final non-destructive cleanup boundary.
}
try {
await connection.send("Target.detachFromTarget", { sessionId });
} catch {
// Closing REA's socket never closes the Electron target.
}
};
const isFileUrl = (value: string): boolean => {
try {
return new URL(value).protocol === "file:";
} catch {
return false;
}
};
const providerError = (
cause: unknown,
operation: BrowserObservationOperation,
): AnalysisError =>
cause instanceof BrowserObservationError && cause.operation !== operation
? new BrowserObservationError(operation, cause.reason, { cause })
: cause instanceof AnalysisError
? cause
: new ProviderAdapterError(IDENTITY.id, operation, { cause });
+6 -7
View File
@@ -7,6 +7,7 @@ import {
AnalysisError,
AnalysisTimeoutError,
BrowserObservationError,
type BrowserObservationOperation,
} from "../domain/errors.js";
const HTTP_TIMEOUT_MS = 5_000;
@@ -45,12 +46,10 @@ export interface CdpEndpointDiscovery {
readonly targets: readonly CdpEndpointTarget[];
}
type BrowserOperation = "list_browser_targets" | "inspect_web_page";
/** Read bounded CDP discovery endpoints without following redirects. */
export const discoverCdpEndpoint = async (
endpoint: string,
operation: BrowserOperation,
operation: BrowserObservationOperation,
signal?: AbortSignal,
): Promise<CdpEndpointDiscovery> => {
const versionInput = await readJson(
@@ -95,7 +94,7 @@ export const discoverCdpEndpoint = async (
const parseEndpointValue = <Output>(
schema: z.ZodType<Output>,
input: unknown,
operation: BrowserOperation,
operation: BrowserObservationOperation,
): Output => {
const parsed = schema.safeParse(input);
if (!parsed.success)
@@ -108,7 +107,7 @@ const parseEndpointValue = <Output>(
const safeBrowserWebSocketUrl = (
endpoint: string,
reported: string,
operation: BrowserOperation,
operation: BrowserObservationOperation,
): string => {
let parsed: URL;
try {
@@ -136,7 +135,7 @@ const safeBrowserWebSocketUrl = (
const readJson = async (
url: URL,
maximumBytes: number,
operation: BrowserOperation,
operation: BrowserObservationOperation,
signal?: AbortSignal,
): Promise<unknown> =>
await new Promise((resolve, reject) => {
@@ -196,7 +195,7 @@ const readJson = async (
const endpointFailure = (
cause: unknown,
operation: BrowserOperation,
operation: BrowserObservationOperation,
signal?: AbortSignal,
): AnalysisError => {
if (cause instanceof AnalysisError) return cause;
+416
View File
@@ -0,0 +1,416 @@
import type { ProgressReporter } from "../application/ProgressReporter.js";
import type {
ObserveWebSessionInput,
WebObservationSession,
} from "../domain/browserSession.js";
import { BrowserObservationError } from "../domain/errors.js";
import type { CdpEndpointDiscovery, CdpEndpointTarget } from "./CdpEndpoint.js";
import { CdpConnection, type CdpEvent } from "./CdpConnection.js";
import { CdpCaptureCompleteness } from "./CdpCaptureCompleteness.js";
import {
allowedSanitizedUrl,
delayWithCancellation,
isHttpUrl,
numberValue,
recordValue,
stringValue,
type UnknownRecord,
} from "./CdpCaptureValues.js";
import { mainFrameUrl } from "./CdpCaptureDocuments.js";
interface ObservationContext {
readonly connection: CdpConnection;
readonly sessionId: string;
readonly discovery: CdpEndpointDiscovery;
readonly target: CdpEndpointTarget;
readonly input: ObserveWebSessionInput;
readonly signal?: AbortSignal;
readonly progress?: ProgressReporter;
}
type EndReason = WebObservationSession["window"]["end_reason"];
type TimelineEvent = WebObservationSession["timeline"][number];
/** Observe external user actions without rejecting approved same-origin navigation. */
export const observeCdpSession = async (
context: ObservationContext,
): Promise<WebObservationSession> => {
const allowedOrigins = new Set(context.input.allowed_origins);
const initial = await authorizedFrame(context, allowedOrigins);
const initialUrl = mainFrameUrl(initial) ?? "";
const mainFrameId = frameId(initial);
if (mainFrameId === undefined)
throw new BrowserObservationError("inspect_web_page", "protocol_error");
const capture = new TimelineCapture(
context.input.max_timeline_events,
allowedOrigins,
mainFrameId,
initialUrl,
);
const removeListener = context.connection.onEvent((event) => {
if (event.sessionId === context.sessionId) capture.ingest(event);
});
try {
await context.connection.send(
"Page.enable",
{},
context.sessionId,
context.signal,
);
await context.connection.send(
"Network.enable",
{ maxTotalBufferSize: 1_024 * 1_024 },
context.sessionId,
context.signal,
);
await context.connection.send(
"Page.setLifecycleEventsEnabled",
{ enabled: true },
context.sessionId,
context.signal,
);
const armedAt = new Date().toISOString();
await context.progress?.report({
phase: "browser_observation",
completed: 1,
total: 2,
message:
"Browser observation armed; perform the approved user action now",
});
let endReason = await waitForWindow(
context.input.observation_ms,
capture,
context.signal,
);
if (endReason === "window_elapsed")
endReason = await captureFinalUrl(context, capture, allowedOrigins);
await context.progress?.report({
phase: "browser_observation",
completed: 2,
total: 2,
message: "Browser observation window ended",
terminal: true,
});
return {
schema_version: 1,
browser: context.discovery.version,
target: {
target_id: context.target.id,
initial_url: allowedSanitizedUrl(initialUrl, allowedOrigins)?.url ?? "",
final_url: capture.finalUrl,
},
window: {
armed_at: armedAt,
ended_at: new Date().toISOString(),
requested_ms: context.input.observation_ms,
end_reason: endReason,
},
timeline: capture.timeline,
completeness: capture.completeness.snapshot(),
limitations: [
"The timeline begins after REA attaches; earlier navigation and network activity is unavailable.",
"Only navigation metadata is retained; headers, bodies, cookies, console values, and payloads are not captured by this tool.",
"An out-of-policy destination ends collection immediately and is recorded without its URL.",
],
};
} finally {
removeListener();
}
};
class TimelineCapture {
readonly timeline: TimelineEvent[] = [];
readonly completeness = new CdpCaptureCompleteness(["timeline"]);
finalUrl: string | null;
#sequence = 0;
#pendingReload = false;
#endReason: EndReason | undefined;
readonly #listeners = new Set<(reason: EndReason) => void>();
constructor(
private readonly maximum: number,
private readonly allowedOrigins: ReadonlySet<string>,
private readonly mainFrameId: string,
initialUrl: string,
) {
this.finalUrl =
allowedSanitizedUrl(initialUrl, allowedOrigins)?.url ?? null;
}
ingest(event: CdpEvent): void {
if (this.#endReason !== undefined) return;
const params = recordValue(event.params);
if (params === undefined) return;
switch (event.method) {
case "Page.frameRequestedNavigation":
this.#navigationRequested(params);
break;
case "Page.frameNavigated":
this.#navigationCommitted(params);
break;
case "Page.navigatedWithinDocument":
this.#sameDocument(params);
break;
case "Network.requestWillBeSent":
this.#redirect(params);
break;
case "Network.loadingFailed":
this.#loadingFailed(params);
break;
case "Page.lifecycleEvent":
this.#lifecycle(params);
break;
case "Inspector.targetCrashed":
case "Target.detachedFromTarget":
this.#add("target_terminated", params, null, null, "target_terminated");
this.end("target_terminated");
break;
}
}
onEnd(listener: (reason: EndReason) => void): () => void {
if (this.#endReason !== undefined) listener(this.#endReason);
else this.#listeners.add(listener);
return () => this.#listeners.delete(listener);
}
end(reason: EndReason): void {
if (this.#endReason !== undefined) return;
this.#endReason = reason;
for (const listener of this.#listeners) listener(reason);
this.#listeners.clear();
}
setFinalUrl(rawUrl: string): void {
const destination = scopedUrl(rawUrl, this.allowedOrigins);
this.finalUrl = destination.scope === "approved" ? destination.url : null;
}
#navigationRequested(params: UnknownRecord): void {
if (stringValue(params.frameId) !== this.mainFrameId) return;
const reason = safeDetail(params.reason);
this.#pendingReload = reason === "reload";
this.#add(
"navigation_requested",
params,
stringValue(params.url),
null,
reason,
);
}
#navigationCommitted(params: UnknownRecord): void {
const frame = recordValue(params.frame);
if (stringValue(frame?.id) !== this.mainFrameId) return;
const rawUrl = stringValue(frame?.url);
const destination = scopedUrl(rawUrl, this.allowedOrigins);
this.#add(
this.#pendingReload ? "same_origin_reload" : "navigation_committed",
{ ...params, frameId: frame?.id, loaderId: frame?.loaderId },
rawUrl,
null,
safeDetail(frame?.transitionType),
);
this.#pendingReload = false;
if (destination.scope === "approved") this.finalUrl = destination.url;
else {
this.finalUrl = null;
this.completeness.exclude("timeline", "out_of_target_scope");
this.end("target_left_scope");
}
}
#sameDocument(params: UnknownRecord): void {
if (stringValue(params.frameId) !== this.mainFrameId) return;
const rawUrl = stringValue(params.url);
const destination = scopedUrl(rawUrl, this.allowedOrigins);
this.#add("same_document_navigation", params, rawUrl, null, null);
if (destination.scope === "approved") this.finalUrl = destination.url;
else {
this.finalUrl = null;
this.completeness.exclude("timeline", "out_of_target_scope");
this.end("target_left_scope");
}
}
#redirect(params: UnknownRecord): void {
if (
stringValue(params.frameId) !== this.mainFrameId ||
recordValue(params.redirectResponse) === undefined
)
return;
const request = recordValue(params.request);
const rawUrl = stringValue(request?.url);
const destination = scopedUrl(rawUrl, this.allowedOrigins);
this.#add(
"redirect",
params,
rawUrl,
stringValue(params.requestId) ?? null,
null,
);
if (destination.scope !== "approved") {
this.completeness.exclude("timeline", "out_of_target_scope");
this.end("target_left_scope");
}
}
#loadingFailed(params: UnknownRecord): void {
const frame = stringValue(params.frameId);
if (frame !== undefined && frame !== this.mainFrameId) return;
this.#add(
"load_failed",
params,
null,
stringValue(params.requestId) ?? null,
safeNetworkError(params.errorText),
);
}
#lifecycle(params: UnknownRecord): void {
if (stringValue(params.frameId) !== this.mainFrameId) return;
this.#add("lifecycle", params, null, null, safeDetail(params.name));
}
#add(
type: TimelineEvent["type"],
params: UnknownRecord,
rawUrl: string | null | undefined,
requestId: string | null,
detail: string | null,
): void {
const destination =
rawUrl == null ? null : scopedUrl(rawUrl, this.allowedOrigins);
if (this.timeline.length >= this.maximum) {
this.completeness.drop("timeline_events");
return;
}
this.#sequence += 1;
this.timeline.push({
sequence: this.#sequence,
type,
timestamp: Math.max(0, numberValue(params.timestamp) ?? 0),
frame_id: boundedId(params.frameId),
loader_id: boundedId(params.loaderId),
request_id: requestId?.slice(0, 256) ?? null,
url: destination?.scope === "approved" ? destination.url : null,
destination_scope: destination?.scope ?? null,
detail,
});
}
}
const waitForWindow = async (
durationMs: number,
capture: TimelineCapture,
signal?: AbortSignal,
): Promise<EndReason> =>
await new Promise<EndReason>((resolve, reject) => {
let settled = false;
let removeEnd = (): void => {};
const finish = (reason: EndReason): void => {
if (settled) return;
settled = true;
clearTimeout(timer);
signal?.removeEventListener("abort", onAbort);
removeEnd();
resolve(reason);
};
const onAbort = (): void => {
if (settled) return;
settled = true;
clearTimeout(timer);
removeEnd();
reject(signal?.reason ?? new Error("browser observation cancelled"));
};
const timer = setTimeout(() => finish("window_elapsed"), durationMs);
removeEnd = capture.onEnd(finish);
signal?.addEventListener("abort", onAbort, { once: true });
if (signal?.aborted === true) onAbort();
});
const authorizedFrame = async (
context: ObservationContext,
allowedOrigins: ReadonlySet<string>,
): Promise<unknown> => {
for (let attempt = 0; attempt < 80; attempt += 1) {
const result = await context.connection.send(
"Page.getFrameTree",
{},
context.sessionId,
context.signal,
);
const url = mainFrameUrl(result);
if (allowedSanitizedUrl(url, allowedOrigins) !== undefined) return result;
if (isHttpUrl(url))
throw new BrowserObservationError(
"inspect_web_page",
"target_not_allowed",
);
await delayWithCancellation(25, context.signal);
}
throw new BrowserObservationError("inspect_web_page", "target_not_allowed");
};
const captureFinalUrl = async (
context: ObservationContext,
capture: TimelineCapture,
allowedOrigins: ReadonlySet<string>,
): Promise<EndReason> => {
const result = await context.connection.send(
"Page.getFrameTree",
{},
context.sessionId,
context.signal,
);
const url = mainFrameUrl(result);
if (
url !== undefined &&
allowedSanitizedUrl(url, allowedOrigins) !== undefined
) {
capture.setFinalUrl(url);
return "window_elapsed";
}
capture.finalUrl = null;
capture.completeness.exclude(
"timeline",
url === undefined ? "invalid_protocol_value" : "out_of_target_scope",
);
return "target_left_scope";
};
const frameId = (result: unknown): string | undefined =>
stringValue(
recordValue(recordValue(recordValue(result)?.frameTree)?.frame)?.id,
);
const scopedUrl = (
value: string | undefined,
allowedOrigins: ReadonlySet<string>,
): {
readonly url: string | null;
readonly scope: "approved" | "outside_policy" | "unsupported";
} => {
const allowed = allowedSanitizedUrl(value, allowedOrigins);
if (allowed !== undefined) return { url: allowed.url, scope: "approved" };
return isHttpUrl(value)
? { url: null, scope: "outside_policy" }
: { url: null, scope: "unsupported" };
};
const boundedId = (value: unknown): string | null =>
stringValue(value)?.slice(0, 256) ?? null;
const safeDetail = (value: unknown): string | null => {
const detail = stringValue(value)?.toLowerCase();
return detail !== undefined && /^[a-z0-9_.:-]{1,100}$/u.test(detail)
? detail
: null;
};
const safeNetworkError = (value: unknown): string | null => {
const error = stringValue(value);
return error !== undefined && /^net::ERR_[A-Z0-9_]{1,100}$/u.test(error)
? error
: null;
};
+226 -66
View File
@@ -4,6 +4,11 @@ import type {
} from "../domain/browserObservation.js";
import type { ProgressReporter } from "../application/ProgressReporter.js";
import { BrowserObservationError } from "../domain/errors.js";
import {
reconcileCapturedWebScript,
stableWebResources,
stableWebScriptKey,
} from "../domain/webInventory.js";
import type { CdpEndpointDiscovery, CdpEndpointTarget } from "./CdpEndpoint.js";
import { CdpConnection } from "./CdpConnection.js";
import { CdpCaptureEvents } from "./CdpCaptureEvents.js";
@@ -14,6 +19,7 @@ import {
captureDom,
captureFrames,
captureResources,
type CapturedResource,
mainFrameUrl,
} from "./CdpCaptureDocuments.js";
import {
@@ -27,6 +33,7 @@ import {
sourceResult,
stringValue,
} from "./CdpCaptureValues.js";
import type { WebSourceMapRequest } from "./WebSourceMapFetcher.js";
interface CaptureContext {
readonly connection: CdpConnection;
@@ -43,7 +50,6 @@ interface CaptureState {
readonly events: CdpCaptureEvents;
readonly allowedOrigins: ReadonlySet<string>;
readonly limitations: string[];
readonly truncatedSections: Set<string>;
readonly startedAt: string;
}
@@ -58,10 +64,20 @@ interface CapturedSections {
readonly storage: WebPageInspection["storage"];
}
export interface CapturedPage {
readonly inspection: WebPageInspection;
readonly sourceMapRequests: readonly WebSourceMapRequest[];
}
interface CapturedScripts {
readonly inventory: WebPageInspection["scripts"];
readonly sourceMapRequests: readonly WebSourceMapRequest[];
}
/** Capture and normalize one attached page without evaluating page JavaScript. */
export const capturePage = async (
context: CaptureContext,
): Promise<WebPageInspection> => {
): Promise<CapturedPage> => {
const allowedOrigins = new Set(context.input.allowed_origins);
const events = new CdpCaptureEvents(context.input, allowedOrigins);
const removeListener = context.connection.onEvent((event) => {
@@ -74,11 +90,10 @@ export const capturePage = async (
allowedOrigins,
limitations: [
"Observation starts when REA attaches; prior network and console activity is unavailable.",
"Network headers, request bodies, response bodies, cookies, storage values, console values, and WebSocket payloads are not retained.",
"Raw network headers, bodies, cookies, storage values, console objects, and WebSocket payloads are never retained; separately approved captures retain only bounded redacted text or value-free shapes.",
"Source maps are reported only as declarative URLs and are not fetched.",
"URL-less scripts and console events without an allowed source URL are excluded because their origin cannot be proven.",
],
truncatedSections: new Set<string>(),
startedAt: new Date().toISOString(),
};
try {
@@ -90,19 +105,22 @@ export const capturePage = async (
const captureAuthorizedPage = async (
state: CaptureState,
): Promise<WebPageInspection> => {
const { context, allowedOrigins, limitations, truncatedSections } = state;
): Promise<CapturedPage> => {
const { context, allowedOrigins, limitations } = state;
const { connection, sessionId, input, signal } = context;
await authorizeObservationWindow(state);
await captureJsonResponseBodies(state);
const frameResult = await authorizedFrameTree(context, allowedOrigins);
const attachedUrl = mainFrameUrl(frameResult) ?? "";
const frameCapture = captureFrames(
frameResult,
allowedOrigins,
input.limits.max_frames,
state.events.completeness,
);
const frames = frameCapture.items;
if (frameCapture.total > frames.length) truncatedSections.add("frames");
if (frameCapture.total > frames.length)
state.events.completeness.truncate("frames");
const captureFrame = frames[0];
if (captureFrame === undefined)
throw new BrowserObservationError("inspect_web_page", "target_not_allowed");
@@ -111,38 +129,39 @@ const captureAuthorizedPage = async (
await connection.send("Page.getResourceTree", {}, sessionId, signal),
allowedOrigins,
input.limits.max_resources,
state.events.completeness,
);
if (resourceCapture.total > resourceCapture.items.length)
truncatedSections.add("resources");
const dom = await capturePageDom(state);
if (dom.total > dom.nodes.length) truncatedSections.add("dom");
const accessibility = await accessibilityForFrames(
context,
frames,
limitations,
state.events.completeness.truncate("resources");
const resources = stableWebResources(
resourceCapture.items.map(publicResource),
);
const dom = await capturePageDom(state);
if (dom.total > dom.nodes.length) state.events.completeness.truncate("dom");
const accessibility = await accessibilityForFrames(state, frames);
if (accessibility.total > accessibility.nodes.length)
truncatedSections.add("accessibility");
state.events.completeness.truncate("accessibility");
const scripts = await captureScripts(
context,
state.events,
truncatedSections,
resourceCapture.items,
resources,
);
const workerCapture = await captureWorkers(
context,
allowedOrigins,
state,
new Set(frames.map((frame) => frame.frame_id)),
limitations,
);
if (workerCapture.total > workerCapture.items.length)
truncatedSections.add("workers");
state.events.completeness.truncate("workers");
const storageCapture = await captureStorage(
context,
frames[0]?.origin ?? new URL(attachedUrl).origin,
limitations,
);
if (storageCapture.truncated) truncatedSections.add("storage_keys");
if (state.events.dropped > 0) truncatedSections.add("events");
if (storageCapture.truncated)
state.events.completeness.truncate("storage_keys");
if (!input.include_storage_keys)
state.events.completeness.exclude("storage_keys", "not_approved", null);
const completedFrameResult = await authorizedFrameTree(
context,
allowedOrigins,
@@ -153,16 +172,42 @@ const captureAuthorizedPage = async (
if (state.events.navigationDuringCapture || completedUrl !== attachedUrl)
throw new BrowserObservationError("inspect_web_page", "target_changed");
await report(context.progress, 3, "Normalizing browser evidence");
return normalizedInspection(state, {
attachedUrl,
frames,
dom,
accessibility,
scripts,
resources: resourceCapture.items,
workers: workerCapture.items,
storage: storageCapture.value,
});
return {
inspection: normalizedInspection(state, {
attachedUrl,
frames,
dom,
accessibility,
scripts: scripts.inventory,
resources,
workers: workerCapture.items,
storage: storageCapture.value,
}),
sourceMapRequests: scripts.sourceMapRequests,
};
};
const captureJsonResponseBodies = async (
state: CaptureState,
): Promise<void> => {
const requestIds = state.events.responseBodyRequestIds();
for (let index = 0; index < requestIds.length; index += 1) {
const requestId = requestIds[index];
if (requestId === undefined) continue;
const result = await optionalCdpCommand(
state.context,
"Network.getResponseBody",
{ requestId },
state.limitations,
);
if (result !== undefined) {
state.events.ingestResponseBody(requestId, result);
continue;
}
for (const remaining of requestIds.slice(index))
state.events.responseBodyUnavailable(remaining);
return;
}
};
const authorizeObservationWindow = async (
@@ -199,14 +244,19 @@ const capturePageDom = async (
context.sessionId,
context.signal,
);
return captureDom(result, allowedOrigins, context.input);
return captureDom(
result,
allowedOrigins,
context.input,
state.events.completeness,
);
};
const normalizedInspection = (
state: CaptureState,
captured: CapturedSections,
): WebPageInspection => ({
schema_version: 1,
schema_version: 2,
browser: state.context.discovery.version,
target: normalizedTarget(
state.context.target,
@@ -218,15 +268,12 @@ const normalizedInspection = (
ended_at: new Date().toISOString(),
observation_ms: state.context.input.observation_ms,
},
completeness: {
status: state.truncatedSections.size === 0 ? "complete" : "truncated",
truncated_sections: [...state.truncatedSections].sort(),
dropped_events: state.events.dropped,
},
completeness: state.events.completeness.snapshot(),
frames: [...captured.frames],
dom: { total_nodes: captured.dom.total, nodes: [...captured.dom.nodes] },
accessibility: {
total_nodes: captured.accessibility.total,
text_capture: captured.accessibility.textCapture,
nodes: [...captured.accessibility.nodes],
},
scripts: captured.scripts,
@@ -243,6 +290,16 @@ const normalizedInspection = (
prior_activity_available: false,
},
workers: [...captured.workers],
metadata: {
responses: [...state.events.responseMetadata],
dom_urls: [...captured.dom.urls],
agent_hints: deduplicatedAgentHints([
...state.events.agentHints,
...captured.dom.agentHints,
]),
excluded_dom_urls: captured.dom.excludedUrls,
headers_allowlisted: true,
},
storage: captured.storage,
limitations: state.limitations,
});
@@ -295,12 +352,12 @@ const enableObservationDomains = async (
};
const accessibilityForFrames = async (
context: CaptureContext,
state: CaptureState,
frames: readonly { readonly frame_id: string }[],
limitations: string[],
): Promise<ReturnType<typeof captureAccessibility>> => {
const nodes: WebPageInspection["accessibility"]["nodes"] = [];
let total = 0;
const { context, limitations, events } = state;
const results: unknown[] = [];
let unavailable = false;
for (const frame of frames) {
const result = await optionalCdpCommand(
context,
@@ -308,24 +365,45 @@ const accessibilityForFrames = async (
{ depth: 32, frameId: frame.frame_id },
limitations,
);
if (result === undefined) continue;
const capture = captureAccessibility(
[result],
Math.max(0, context.input.limits.max_ax_nodes - nodes.length),
);
total += capture.total;
nodes.push(...capture.nodes);
if (result === undefined) unavailable = true;
else results.push(result);
}
return { total, nodes };
if (unavailable) events.completeness.unavailable("accessibility");
const capture = captureAccessibility(
results,
context.input.limits.max_ax_nodes,
{
includeText: context.input.include_accessibility_text,
maximumFieldBytes: context.input.limits.max_ax_text_field_bytes,
maximumTotalBytes: context.input.limits.max_total_ax_text_bytes,
...(results.length === 0 && unavailable ? { unavailable: true } : {}),
},
);
if (!context.input.include_accessibility_text)
events.completeness.exclude(
"accessibility",
"not_approved",
capture.textCapture.excluded_fields,
);
if (capture.textCapture.status === "truncated")
events.completeness.truncate("accessibility");
return capture;
};
const captureScripts = async (
context: CaptureContext,
events: CdpCaptureEvents,
truncatedSections: Set<string>,
): Promise<WebPageInspection["scripts"]> => {
rawResources: readonly CapturedResource[],
resources: WebPageInspection["resources"],
): Promise<CapturedScripts> => {
let totalSourceBytes = 0;
const items: WebPageInspection["scripts"]["items"] = [];
const drafts: {
readonly item: Omit<
WebPageInspection["scripts"]["items"][number],
"script_key"
>;
readonly sourceMapRawUrl: string | null;
}[] = [];
for (const script of events.scripts.values()) {
let source: WebPageInspection["scripts"]["items"][number]["source"] =
sourceExcluded("source capture was not approved");
@@ -334,13 +412,13 @@ const captureScripts = async (
source = sourceExcluded(
"declared script length exceeds per-script limit",
);
truncatedSections.add("script_sources");
events.completeness.truncate("script_sources");
} else if (
totalSourceBytes + script.length >
context.input.limits.max_total_script_source_bytes
) {
source = sourceExcluded("total script source limit reached");
truncatedSections.add("script_sources");
events.completeness.truncate("script_sources");
} else {
const result = requiredRecord(
await context.connection.send(
@@ -360,37 +438,103 @@ const captureScripts = async (
source = sourceExcluded(
"actual source exceeds configured byte limits",
);
truncatedSections.add("script_sources");
events.completeness.truncate("script_sources");
} else {
source = sourceResult(content);
totalSourceBytes += bytes;
}
}
}
items.push({
script_id: script.scriptId,
const inventoryScript = {
url: script.url,
origin: script.origin,
cdp_hash: script.hash,
length: script.length,
is_module: script.isModule,
language: script.language,
source_map_url: script.sourceMapUrl,
source,
};
drafts.push({
item: {
...inventoryScript,
origin: script.origin,
resource_reconciliation: reconcileCapturedWebScript(
{ ...inventoryScript, rawUrl: script.rawUrl },
rawResources,
resources,
),
source,
},
sourceMapRawUrl: script.sourceMapRawUrl,
});
}
return { total: events.scripts.size, items };
if (!context.input.include_script_sources)
events.completeness.exclude(
"script_sources",
"not_approved",
events.scripts.size,
);
const keyed = drafts.map((draft) => ({
...draft,
base: stableWebScriptKey(draft.item),
}));
keyed.sort(
(left, right) =>
left.base.localeCompare(right.base) ||
sourceDigest(left.item.source).localeCompare(
sourceDigest(right.item.source),
),
);
const totals = new Map<string, number>();
for (const { base } of keyed) totals.set(base, (totals.get(base) ?? 0) + 1);
const seen = new Map<string, number>();
const sourceMapRequests: WebSourceMapRequest[] = [];
const items = keyed.map(({ base, item, sourceMapRawUrl }) => {
const occurrence = (seen.get(base) ?? 0) + 1;
seen.set(base, occurrence);
const scriptKey =
totals.get(base) === 1 ? base : `${base}_${String(occurrence)}`;
if (item.source_map_url !== null && sourceMapRawUrl !== null)
sourceMapRequests.push({
scriptKey,
declaredUrl: item.source_map_url,
fetchUrl: sourceMapRawUrl,
});
return { script_key: scriptKey, ...item };
});
return {
inventory: { total: events.scripts.size, items },
sourceMapRequests,
};
};
const sourceDigest = (
source: WebPageInspection["scripts"]["items"][number]["source"],
): string => (source.included ? source.artifact.sha256 : source.reason);
const publicResource = ({ rawUrl: _rawUrl, ...resource }: CapturedResource) =>
resource;
const deduplicatedAgentHints = (
hints: WebPageInspection["metadata"]["agent_hints"],
): WebPageInspection["metadata"]["agent_hints"] => {
const seen = new Set<string>();
return hints.filter((hint) => {
const key = `${hint.mechanism}\0${hint.declaration}\0${hint.url ?? ""}`;
if (seen.has(key)) return false;
seen.add(key);
return true;
});
};
const captureWorkers = async (
context: CaptureContext,
allowedOrigins: ReadonlySet<string>,
state: CaptureState,
frameIds: ReadonlySet<string>,
limitations: string[],
): Promise<{
readonly total: number;
readonly items: WebPageInspection["workers"];
}> => {
const { context, allowedOrigins, limitations, events } = state;
const completeness = events.completeness;
const result = await optionalCdpCommand(
{ ...context, sessionId: undefined },
"Target.getTargets",
@@ -399,6 +543,7 @@ const captureWorkers = async (
);
const items: WebPageInspection["workers"] = [];
let total = 0;
if (result === undefined) completeness.unavailable("workers");
for (const target of recordsValue(recordValue(result)?.targetInfos)) {
const type = stringValue(target.type) ?? "";
const url = allowedSanitizedUrl(target.url, allowedOrigins);
@@ -406,8 +551,23 @@ const captureWorkers = async (
stringValue(target.openerId) === context.target.id ||
(stringValue(target.parentFrameId) !== undefined &&
frameIds.has(stringValue(target.parentFrameId) ?? ""));
if (!type.includes("worker") || url === undefined || !relatedToPage)
if (!type.includes("worker")) continue;
if (!relatedToPage) {
completeness.exclude("workers", "out_of_target_scope");
continue;
}
if (url === undefined) {
const rawUrl = stringValue(target.url);
completeness.exclude(
"workers",
rawUrl === undefined || rawUrl === ""
? "unattributed_origin"
: isHttpUrl(rawUrl)
? "disallowed_origin"
: "unsupported_url",
);
continue;
}
total += 1;
if (items.length >= context.input.limits.max_workers) continue;
items.push({
+277
View File
@@ -0,0 +1,277 @@
import type { WebPageInspection } from "../domain/browserObservation.js";
import { sanitizeBrowserUrl } from "../domain/browserObservation.js";
import {
numberValue,
stringValue,
type UnknownRecord,
} from "./CdpCaptureValues.js";
type ResponseMetadata = WebPageInspection["metadata"]["responses"][number];
type LinkMetadata = ResponseMetadata["links"][number];
type AgentHint = WebPageInspection["metadata"]["agent_hints"][number];
/** Normalize an allowlisted subset of response metadata and discard raw headers. */
export const safeResponseMetadata = (
requestId: string,
url: string,
response: UnknownRecord,
allowedOrigins: ReadonlySet<string>,
): {
readonly response: ResponseMetadata;
readonly agentHints: AgentHint[];
} => {
const headers = normalizedHeaders(response.headers);
const links = parseLinks(headers.get("link"), url, allowedOrigins);
const csp = parseCsp(
headers.get("content-security-policy"),
url,
allowedOrigins,
);
const agentHints = [
...links.flatMap((link) =>
link.rel.some(isAgentRel)
? [agentHint("link_rel", link.rel.join(" "), link.href)]
: [],
),
...agentHeaderNames.flatMap((name) =>
headers.has(name) ? [agentHint("response_header", name, null)] : [],
),
...wellKnownHint(url),
];
return {
response: {
request_id: requestId,
url,
mime_type: boundedHeader(stringValue(response.mimeType)),
content_length: nonnegativeInteger(headers.get("content-length")),
content_encoding: boundedHeader(headers.get("content-encoding")),
csp,
links,
policies: {
coop: policyToken(headers.get("cross-origin-opener-policy")),
coep: policyToken(headers.get("cross-origin-embedder-policy")),
corp: policyToken(headers.get("cross-origin-resource-policy")),
referrer_policy: policyToken(headers.get("referrer-policy")),
x_content_type_options: policyToken(
headers.get("x-content-type-options"),
),
permissions_policy_features: permissionFeatures(
headers.get("permissions-policy"),
),
},
},
agentHints,
};
};
const normalizedHeaders = (value: unknown): ReadonlyMap<string, string> => {
if (typeof value !== "object" || value === null || Array.isArray(value))
return new Map();
const headers = new Map<string, string>();
for (const [name, raw] of Object.entries(value).slice(0, 500)) {
const normalized = name.toLowerCase().slice(0, 256);
const header =
typeof raw === "string"
? raw
: typeof raw === "number" && Number.isFinite(raw)
? String(raw)
: undefined;
if (header !== undefined) headers.set(normalized, header.slice(0, 65_536));
}
return headers;
};
const parseCsp = (
value: string | undefined,
baseUrl: string,
allowedOrigins: ReadonlySet<string>,
): ResponseMetadata["csp"] => {
const directives: ResponseMetadata["csp"]["directives"] = [];
let nonceCount = 0;
let hashCount = 0;
for (const rawDirective of (value ?? "").split(";").slice(0, 100)) {
const [rawName, ...tokens] = rawDirective.trim().split(/\s+/u);
const name = (rawName ?? "").toLowerCase();
if (!/^[a-z][a-z0-9-]{0,99}$/u.test(name)) continue;
const sources: ResponseMetadata["csp"]["directives"][number]["sources"] =
[];
for (const token of tokens.slice(0, 100)) {
const lower = token.toLowerCase();
if (lower.startsWith("'nonce-")) {
nonceCount += 1;
continue;
}
if (
lower.startsWith("'sha256-") ||
lower.startsWith("'sha384-") ||
lower.startsWith("'sha512-")
) {
hashCount += 1;
continue;
}
sources.push(cspSource(token, baseUrl, allowedOrigins));
}
directives.push({ name, sources });
}
return { directives, nonce_count: nonceCount, hash_count: hashCount };
};
const cspSource = (
token: string,
baseUrl: string,
allowedOrigins: ReadonlySet<string>,
): ResponseMetadata["csp"]["directives"][number]["sources"][number] => {
const lower = token.toLowerCase();
if (/^'[a-z0-9-]+'$/u.test(lower))
return { kind: "keyword", value: lower.slice(0, 100) };
if (/^[a-z][a-z0-9+.-]*:$/u.test(lower))
return { kind: "scheme", value: lower.slice(0, 100) };
try {
const parsed = new URL(token, baseUrl);
return allowedOrigins.has(parsed.origin)
? { kind: "approved_origin", value: parsed.origin }
: { kind: "external_origin", value: null };
} catch {
return { kind: "other", value: null };
}
};
const parseLinks = (
value: string | undefined,
baseUrl: string,
allowedOrigins: ReadonlySet<string>,
): LinkMetadata[] => {
const links: LinkMetadata[] = [];
for (const entry of splitLinkHeader(value ?? "").slice(0, 100)) {
const match = /^\s*<([^>]{1,4096})>(.*)$/u.exec(entry);
if (match === null) continue;
const parameters = linkParameters(match[2] ?? "");
const destination = safeDestination(
match[1] ?? "",
baseUrl,
allowedOrigins,
);
links.push({
href: destination.url,
destination_scope: destination.scope,
rel: (parameters.get("rel") ?? "")
.toLowerCase()
.split(/\s+/u)
.filter(Boolean)
.slice(0, 32),
as: boundedHeader(parameters.get("as")),
type: boundedHeader(parameters.get("type")),
crossorigin: boundedHeader(parameters.get("crossorigin")),
});
}
return links;
};
const splitLinkHeader = (value: string): string[] => {
const entries: string[] = [];
let start = 0;
let quoted = false;
for (let index = 0; index < value.length; index += 1) {
const character = value[index];
if (character === '"' && value[index - 1] !== "\\") quoted = !quoted;
if (character !== "," || quoted) continue;
entries.push(value.slice(start, index));
start = index + 1;
}
entries.push(value.slice(start));
return entries;
};
const linkParameters = (value: string): ReadonlyMap<string, string> => {
const parameters = new Map<string, string>();
for (const raw of value.split(";").slice(1, 50)) {
const separator = raw.indexOf("=");
const name = (separator < 0 ? raw : raw.slice(0, separator))
.trim()
.toLowerCase();
if (!/^[a-z][a-z0-9-]{0,99}$/u.test(name)) continue;
const parameter = separator < 0 ? "" : raw.slice(separator + 1).trim();
parameters.set(name, unquote(parameter).slice(0, 1_024));
}
return parameters;
};
const safeDestination = (
value: string,
baseUrl: string,
allowedOrigins: ReadonlySet<string>,
): {
readonly url: string | null;
readonly scope: LinkMetadata["destination_scope"];
} => {
try {
const parsed = new URL(value, baseUrl);
if (parsed.protocol !== "http:" && parsed.protocol !== "https:")
return { url: null, scope: "unsupported" };
if (!allowedOrigins.has(parsed.origin))
return { url: null, scope: "outside_policy" };
return { url: sanitizeBrowserUrl(parsed.href).url, scope: "approved" };
} catch {
return { url: null, scope: "unsupported" };
}
};
const permissionFeatures = (value: string | undefined): string[] =>
[
...new Set(
(value ?? "")
.split(",")
.map((entry) => entry.split("=", 1)[0]?.trim().toLowerCase() ?? "")
.filter((feature) => /^[a-z][a-z0-9-]{0,99}$/u.test(feature)),
),
]
.sort()
.slice(0, 200);
const wellKnownHint = (url: string): AgentHint[] => {
try {
const path = new URL(url).pathname.toLowerCase();
return wellKnownAgentPaths.has(path)
? [agentHint("well_known_resource", path, sanitizeBrowserUrl(url).url)]
: [];
} catch {
return [];
}
};
const agentHint = (
mechanism: AgentHint["mechanism"],
declaration: string,
url: string | null,
): AgentHint => ({
mechanism,
declaration: declaration.slice(0, 256),
url,
trust: "page-declared-untrusted",
});
const isAgentRel = (value: string): boolean =>
["mcp", "model-context", "ai-plugin", "service-desc"].includes(value);
const policyToken = (value: string | undefined): string | null => {
const token = (value ?? "").trim().toLowerCase();
return /^[a-z][a-z0-9_.-]{0,99}$/u.test(token) ? token : null;
};
const nonnegativeInteger = (value: string | undefined): number | null => {
const number = numberValue(value === undefined ? undefined : Number(value));
return number === undefined ? null : Math.max(0, Math.trunc(number));
};
const boundedHeader = (value: string | undefined): string | null =>
value === undefined ? null : value.trim().toLowerCase().slice(0, 256);
const unquote = (value: string): string =>
value.startsWith('"') && value.endsWith('"') ? value.slice(1, -1) : value;
const agentHeaderNames = ["x-model-context", "x-webmcp", "mcp-server"] as const;
const wellKnownAgentPaths: ReadonlySet<string> = new Set([
"/.well-known/ai-plugin.json",
"/.well-known/mcp",
"/.well-known/model-context",
]);
+164
View File
@@ -0,0 +1,164 @@
import type { ProgressReporter } from "../application/ProgressReporter.js";
import type {
CaptureWebScreenshotInput,
WebScreenshot,
} from "../domain/webScreenshot.js";
import { createWebScreenshotArtifact } from "../domain/webScreenshot.js";
import { BrowserObservationError } from "../domain/errors.js";
import type { CdpEndpointDiscovery, CdpEndpointTarget } from "./CdpEndpoint.js";
import type { CdpConnection, CdpEvent } from "./CdpConnection.js";
import {
allowedSanitizedUrl,
recordValue,
stringValue,
} from "./CdpCaptureValues.js";
import { mainFrameUrl } from "./CdpCaptureDocuments.js";
import { captureFrames } from "./CdpCaptureDocuments.js";
import { CdpCaptureCompleteness } from "./CdpCaptureCompleteness.js";
import { decodeCanonicalBase64 } from "../domain/webScreenshot.js";
interface ScreenshotContext {
readonly connection: CdpConnection;
readonly sessionId: string;
readonly discovery: CdpEndpointDiscovery;
readonly target: CdpEndpointTarget;
readonly input: CaptureWebScreenshotInput;
readonly signal?: AbortSignal;
readonly progress?: ProgressReporter;
}
/** Capture the current viewport without navigation, evaluation, or page mutation. */
export const captureCdpScreenshot = async (
context: ScreenshotContext,
): Promise<WebScreenshot> => {
const origins = new Set(context.input.allowed_origins);
await context.progress?.report({
phase: "browser_observation",
completed: 1,
total: 2,
message: "Authorizing current screenshot frame",
});
await context.connection.send(
"Page.enable",
{},
context.sessionId,
context.signal,
);
const before = await context.connection.send(
"Page.getFrameTree",
{},
context.sessionId,
context.signal,
);
const beforeUrl = mainFrameUrl(before);
const authorized = allowedSanitizedUrl(beforeUrl, origins);
const mainFrame = captureFrames(before, origins, 1).items[0];
if (authorized === undefined || mainFrame === undefined)
throw new BrowserObservationError("inspect_web_page", "target_not_allowed");
const navigation = { changed: false, leftScope: false };
const removeListener = context.connection.onEvent((event) => {
if (event.sessionId !== context.sessionId) return;
observeScreenshotNavigation(event, mainFrame.frame_id, origins, navigation);
});
let result: ReturnType<typeof recordValue>;
let after: unknown;
try {
result = recordValue(
await context.connection.send(
"Page.captureScreenshot",
{ format: "png", fromSurface: true, captureBeyondViewport: false },
context.sessionId,
context.signal,
),
);
after = await context.connection.send(
"Page.getFrameTree",
{},
context.sessionId,
context.signal,
);
} finally {
removeListener();
}
const encoded = stringValue(result?.data);
const bytes =
encoded === undefined ? undefined : decodeCanonicalBase64(encoded);
if (
bytes === undefined ||
bytes.byteLength > context.input.maximum_image_bytes
)
throw new BrowserObservationError("inspect_web_page", "payload_limit");
const dimensions = pngDimensions(bytes);
if (navigation.leftScope)
throw new BrowserObservationError("inspect_web_page", "target_not_allowed");
const afterUrl = mainFrameUrl(after);
if (
navigation.changed ||
afterUrl !== beforeUrl ||
allowedSanitizedUrl(afterUrl, origins) === undefined
)
throw new BrowserObservationError("inspect_web_page", "target_changed");
await context.progress?.report({
phase: "browser_observation",
completed: 2,
total: 2,
message: "Screenshot capture complete",
terminal: true,
});
return {
schema_version: 1,
browser: context.discovery.version,
target: {
target_id: context.target.id,
url: authorized.url,
origin: authorized.origin ?? "",
},
captured_at: new Date().toISOString(),
viewport: dimensions,
artifact: createWebScreenshotArtifact(bytes),
completeness: new CdpCaptureCompleteness().snapshot(),
limitations: [
"The screenshot contains the visible viewport and may include sensitive on-screen content; capture requires separate approval.",
"REA does not scroll, evaluate JavaScript, or capture beyond the current viewport.",
],
};
};
const observeScreenshotNavigation = (
event: CdpEvent,
mainFrameId: string,
origins: ReadonlySet<string>,
state: { changed: boolean; leftScope: boolean },
): void => {
const params = recordValue(event.params);
if (params === undefined) return;
const frame =
event.method === "Page.frameNavigated" ? recordValue(params.frame) : params;
if (
(event.method !== "Page.frameNavigated" &&
event.method !== "Page.navigatedWithinDocument") ||
(stringValue(frame?.id) ?? stringValue(frame?.frameId)) !== mainFrameId
)
return;
state.changed = true;
if (allowedSanitizedUrl(frame?.url, origins) === undefined)
state.leftScope = true;
};
const pngDimensions = (
bytes: Buffer,
): { readonly width: number; readonly height: number } => {
if (
bytes.byteLength < 24 ||
!bytes.subarray(0, 8).equals(PNG_SIGNATURE) ||
bytes.subarray(12, 16).toString("ascii") !== "IHDR"
)
throw new BrowserObservationError("inspect_web_page", "protocol_error");
const width = bytes.readUInt32BE(16);
const height = bytes.readUInt32BE(20);
if (width === 0 || height === 0 || width * height > 32_000_000)
throw new BrowserObservationError("inspect_web_page", "payload_limit");
return { width, height };
};
const PNG_SIGNATURE = Buffer.from([137, 80, 78, 71, 13, 10, 26, 10]);
+373
View File
@@ -0,0 +1,373 @@
import { createHash } from "node:crypto";
import type { ProgressReporter } from "../application/ProgressReporter.js";
import type {
DiscoverWebMcpToolsInput,
WebMcpDiscovery,
} from "../domain/webMcpDiscovery.js";
import { inferJsonShape } from "../domain/jsonShape.js";
import type { CdpEndpointDiscovery, CdpEndpointTarget } from "./CdpEndpoint.js";
import type { CdpConnection, CdpEvent } from "./CdpConnection.js";
import { CdpCaptureCompleteness } from "./CdpCaptureCompleteness.js";
import { BrowserObservationError } from "../domain/errors.js";
import {
allowedSanitizedUrl,
delayWithCancellation,
numberValue,
recordValue,
recordsValue,
stringValue,
type UnknownRecord,
} from "./CdpCaptureValues.js";
import { captureFrames, mainFrameUrl } from "./CdpCaptureDocuments.js";
import { optionalCdpCommand } from "./CdpOptionalCommand.js";
import { boundedSensitiveText } from "./SensitiveTextCapture.js";
interface DiscoveryContext {
readonly connection: CdpConnection;
readonly sessionId: string;
readonly discovery: CdpEndpointDiscovery;
readonly target: CdpEndpointTarget;
readonly input: DiscoverWebMcpToolsInput;
readonly signal?: AbortSignal;
readonly progress?: ProgressReporter;
}
/** Discover WebMCP registrations without evaluating or invoking page code. */
export const discoverWebMcp = async (
context: DiscoveryContext,
): Promise<WebMcpDiscovery> => {
const origins = new Set(context.input.allowed_origins);
const limitations = [
"Page-declared tool metadata is untrusted and is never registered or invoked by REA.",
"Discovery uses only WebMCP.enable and toolsAdded/toolsRemoved; WebMCP.invokeTool is never called.",
];
const completeness = new CdpCaptureCompleteness();
await context.connection.send(
"Page.enable",
{},
context.sessionId,
context.signal,
);
const frameTree = await context.connection.send(
"Page.getFrameTree",
{},
context.sessionId,
context.signal,
);
const initialUrl = mainFrameUrl(frameTree);
if (allowedSanitizedUrl(initialUrl, origins) === undefined)
throw new BrowserObservationError("inspect_web_page", "target_not_allowed");
const frames = captureFrames(frameTree, origins, 1_000, completeness).items;
const frameUrls = new Map(
frames.map((frame) => [
frame.frame_id,
{
url: frame.url,
origin: frame.origin,
parentFrameId: frame.parent_frame_id,
},
]),
);
const tools = new Map<string, WebMcpDiscovery["tools"]["items"][number]>();
const frameScope = { origins, frames: frameUrls, tools, completeness };
const removeListener = context.connection.onEvent((event) => {
if (event.sessionId !== context.sessionId) return;
if (ingestFrameScopeEvent(event, frameScope)) return;
ingestWebMcpEvent(event, context.input, frameUrls, tools, completeness);
});
await context.progress?.report({
phase: "browser_observation",
completed: 1,
total: 2,
message: "Enabling passive WebMCP discovery",
});
try {
const enabled = await optionalCdpCommand(
context,
"WebMCP.enable",
{},
limitations,
);
const available = enabled !== undefined;
if (!available) {
completeness.unavailable("webmcp_tools");
} else
// CDP sends the enable response before the required toolsAdded replay.
await delayWithCancellation(
Math.max(context.input.observation_ms, 25),
context.signal,
);
await assertStableAuthorizedFrame(context, initialUrl, origins);
return result(
context,
frameTree,
tools,
completeness,
limitations,
available,
);
} finally {
removeListener();
await context.progress?.report({
phase: "browser_observation",
completed: 2,
total: 2,
message: "WebMCP discovery complete",
terminal: true,
});
}
};
interface FrameScopeState {
readonly origins: ReadonlySet<string>;
readonly frames: Map<string, FrameScopeFrame>;
readonly tools: Map<string, WebMcpDiscovery["tools"]["items"][number]>;
readonly completeness: CdpCaptureCompleteness;
}
interface FrameScopeFrame {
readonly url: string;
readonly origin: string | null;
readonly parentFrameId: string | null;
}
const ingestFrameScopeEvent = (
event: CdpEvent,
state: FrameScopeState,
): boolean => {
const params = recordValue(event.params);
if (params === undefined) return false;
if (event.method === "Page.frameDetached") {
const frameId = stringValue(params.frameId);
if (frameId === undefined || frameId.length > 256)
state.completeness.exclude("webmcp_tools", "invalid_protocol_value");
else removeFrame(frameId, state.frames, state.tools);
return true;
}
const frame =
event.method === "Page.frameNavigated" ? recordValue(params.frame) : params;
if (
event.method !== "Page.frameNavigated" &&
event.method !== "Page.navigatedWithinDocument"
)
return false;
const frameId = stringValue(frame?.id) ?? stringValue(frame?.frameId);
if (frameId === undefined || frameId.length > 256) {
state.completeness.exclude("webmcp_tools", "invalid_protocol_value");
return true;
}
const url = allowedSanitizedUrl(frame?.url, state.origins);
if (url === undefined || url.origin === null) {
removeFrame(frameId, state.frames, state.tools);
state.completeness.exclude("webmcp_tools", "out_of_target_scope");
return true;
}
state.frames.set(frameId, {
url: url.url,
origin: url.origin,
parentFrameId:
(stringValue(frame?.parentId) ??
state.frames.get(frameId)?.parentFrameId ??
"") ||
null,
});
return true;
};
const removeFrame = (
frameId: string,
frames: Map<string, FrameScopeFrame>,
tools: Map<string, WebMcpDiscovery["tools"]["items"][number]>,
): void => {
const pending = [frameId];
const removed = new Set<string>();
while (pending.length > 0) {
const current = pending.pop();
if (current === undefined || removed.has(current)) continue;
removed.add(current);
for (const [candidate, frame] of frames)
if (frame.parentFrameId === current) pending.push(candidate);
frames.delete(current);
}
for (const [key, tool] of tools)
if (removed.has(tool.frame_id)) tools.delete(key);
};
const assertStableAuthorizedFrame = async (
context: DiscoveryContext,
initialUrl: string | undefined,
origins: ReadonlySet<string>,
): Promise<void> => {
const finalTree = await context.connection.send(
"Page.getFrameTree",
{},
context.sessionId,
context.signal,
);
const finalUrl = mainFrameUrl(finalTree);
if (allowedSanitizedUrl(finalUrl, origins) === undefined)
throw new BrowserObservationError("inspect_web_page", "target_not_allowed");
if (finalUrl !== initialUrl)
throw new BrowserObservationError("inspect_web_page", "target_changed");
};
const ingestWebMcpEvent = (
event: CdpEvent,
input: DiscoverWebMcpToolsInput,
frames: ReadonlyMap<
string,
{ readonly url: string; readonly origin: string | null }
>,
tools: Map<string, WebMcpDiscovery["tools"]["items"][number]>,
completeness: CdpCaptureCompleteness,
): void => {
const params = recordValue(event.params);
if (params === undefined) return;
if (event.method === "WebMCP.toolsRemoved") {
for (const removed of recordsValue(params.tools)) {
const name = stringValue(removed.name);
const frameId = stringValue(removed.frameId);
if (name === undefined || frameId === undefined) continue;
for (const [key, tool] of tools)
if (tool.name === name && tool.frame_id === frameId) tools.delete(key);
}
return;
}
if (event.method !== "WebMCP.toolsAdded") return;
for (const declared of recordsValue(params.tools)) {
const normalized = normalizeTool(declared, input, frames, completeness);
if (normalized === undefined) continue;
if (!tools.has(normalized.tool_key) && tools.size >= input.max_tools) {
completeness.drop("webmcp_tools");
continue;
}
tools.set(normalized.tool_key, normalized);
}
};
const normalizeTool = (
value: UnknownRecord,
input: DiscoverWebMcpToolsInput,
frames: ReadonlyMap<
string,
{ readonly url: string; readonly origin: string | null }
>,
completeness: CdpCaptureCompleteness,
): WebMcpDiscovery["tools"]["items"][number] | undefined => {
const frameId = stringValue(value.frameId);
const name = stringValue(value.name);
const frame = frameId === undefined ? undefined : frames.get(frameId);
if (
frameId === undefined ||
name === undefined ||
frame?.origin === null ||
frame === undefined
) {
completeness.exclude("webmcp_tools", "out_of_target_scope");
return undefined;
}
const description = boundedSensitiveText(
stringValue(value.description) ?? "",
4_096,
).text;
const annotations = recordValue(value.annotations);
return {
tool_key: toolKey(frame.url, name),
name: boundedSensitiveText(name, 512).text,
description,
frame_id: frameId.slice(0, 256),
frame_url: frame.url,
owner_origin: frame.origin,
declaration_kind:
numberValue(value.backendNodeId) === undefined
? "imperative"
: "declarative",
input_schema_shape: schemaShape(value.inputSchema, input, completeness),
annotations: {
read_only: booleanOrNull(annotations?.readOnly),
untrusted_content: booleanOrNull(annotations?.untrustedContent),
autosubmit: booleanOrNull(annotations?.autosubmit),
},
registration_source: registrationSource(
recordValue(value.stackTrace),
new Set(input.allowed_origins),
),
trust: "page-declared-untrusted",
};
};
const schemaShape = (
value: unknown,
input: DiscoverWebMcpToolsInput,
completeness: CdpCaptureCompleteness,
) => {
if (recordValue(value) === undefined) return null;
const encoded = JSON.stringify(value);
const shape = inferJsonShape(encoded, {
maximumBytes: input.max_schema_bytes,
maximumNodes: input.max_schema_nodes,
maximumDepth: input.max_schema_depth,
});
if (shape === null || shape.truncated) completeness.truncate("webmcp_tools");
return shape;
};
const registrationSource = (
stack: UnknownRecord | undefined,
allowedOrigins: ReadonlySet<string>,
): WebMcpDiscovery["tools"]["items"][number]["registration_source"] => {
const frame = recordsValue(stack?.callFrames)[0];
const url = allowedSanitizedUrl(frame?.url, allowedOrigins);
return frame === undefined || url === undefined
? null
: {
url: url.url,
line: integerOrNull(frame.lineNumber),
column: integerOrNull(frame.columnNumber),
};
};
const result = (
context: DiscoveryContext,
frameTree: unknown,
tools: ReadonlyMap<string, WebMcpDiscovery["tools"]["items"][number]>,
completeness: CdpCaptureCompleteness,
limitations: string[],
available: boolean,
): WebMcpDiscovery => {
const targetUrl = mainFrameUrl(frameTree) ?? context.target.url;
const sanitized = allowedSanitizedUrl(
targetUrl,
new Set(context.input.allowed_origins),
);
return {
schema_version: 1,
browser: context.discovery.version,
target: {
target_id: context.target.id,
url: sanitized?.url ?? "[unsupported-url]",
origin: sanitized?.origin ?? "",
},
status: available ? "available" : "unavailable",
tools: {
total: tools.size + completeness.droppedTotal,
items: [...tools.values()].sort((left, right) =>
left.tool_key.localeCompare(right.tool_key),
),
},
completeness: completeness.snapshot(),
limitations,
};
};
const toolKey = (frameUrl: string, name: string): string =>
`webmcp_${createHash("sha256").update(`${frameUrl}\0${name}`).digest("hex")}`;
const booleanOrNull = (value: unknown): boolean | null =>
typeof value === "boolean" ? value : null;
const integerOrNull = (value: unknown): number | null => {
const number = numberValue(value);
return number === undefined ? null : Math.max(0, Math.trunc(number));
};
+56
View File
@@ -0,0 +1,56 @@
import { realpath } from "node:fs/promises";
import { isAbsolute, relative } from "node:path";
import { fileURLToPath } from "node:url";
/** Canonicalize operator roots before evaluating any Electron file target. */
export const canonicalElectronRoots = async (
roots: readonly string[],
): Promise<readonly string[]> => {
const canonical: string[] = [];
for (const root of roots) canonical.push(await realpath(root));
return [...new Set(canonical)].sort();
};
/** Resolve one file URL and reject host, encoding, and symlink root escapes. */
export const authorizedElectronFile = async (
value: string,
roots: readonly string[],
): Promise<string | undefined> => {
if (value.length > 65_536 || /%(?:2f|5c)/iu.test(value)) return undefined;
let url: URL;
try {
url = new URL(value);
} catch {
return undefined;
}
if (
url.protocol !== "file:" ||
url.hostname !== "" ||
url.username !== "" ||
url.password !== ""
)
return undefined;
let path: string;
try {
path = fileURLToPath(url);
} catch {
return undefined;
}
if (!isAbsolute(path) || path.includes("\0")) return undefined;
let canonical: string;
try {
canonical = await realpath(path);
} catch {
return undefined;
}
return roots.some((root) => withinRoot(root, canonical))
? canonical
: undefined;
};
const withinRoot = (root: string, path: string): boolean => {
const remainder = relative(root, path);
return (
remainder === "" || (!remainder.startsWith("..") && !isAbsolute(remainder))
);
};
+222
View File
@@ -0,0 +1,222 @@
import { inflateSync } from "node:zlib";
import type {
CompareWebScreenshotsInput,
WebScreenshotArtifact,
WebScreenshotDiff,
} from "../domain/webScreenshot.js";
import { decodeCanonicalBase64 } from "../domain/webScreenshot.js";
interface DecodedPng {
readonly width: number;
readonly height: number;
readonly rgba: Buffer;
}
/** Compare two validated screenshots using bounded, deterministic RGBA metrics. */
export const comparePngScreenshots = (
input: CompareWebScreenshotsInput,
): WebScreenshotDiff => {
const before = decodePng(input.before, input.maximum_pixels);
const after = decodePng(input.after, input.maximum_pixels);
if (before.width !== after.width || before.height !== after.height)
return {
schema_version: 1,
status: "dimension_mismatch",
before: dimensions(before),
after: dimensions(after),
channel_threshold: input.channel_threshold,
compared_pixels: 0,
changed_pixels: null,
changed_ratio: null,
maximum_channel_delta: null,
mean_absolute_channel_delta: null,
limitations: limitations(),
};
let changedPixels = 0;
let maximumDelta = 0;
let totalDelta = 0;
for (let offset = 0; offset < before.rgba.length; offset += 4) {
let changed = false;
for (let channel = 0; channel < 4; channel += 1) {
const delta = Math.abs(
(before.rgba[offset + channel] ?? 0) -
(after.rgba[offset + channel] ?? 0),
);
totalDelta += delta;
maximumDelta = Math.max(maximumDelta, delta);
if (delta > input.channel_threshold) changed = true;
}
if (changed) changedPixels += 1;
}
const pixels = before.width * before.height;
return {
schema_version: 1,
status: changedPixels === 0 ? "identical" : "different",
before: dimensions(before),
after: dimensions(after),
channel_threshold: input.channel_threshold,
compared_pixels: pixels,
changed_pixels: changedPixels,
changed_ratio: changedPixels / pixels,
maximum_channel_delta: maximumDelta,
mean_absolute_channel_delta: totalDelta / (pixels * 4),
limitations: limitations(),
};
};
const decodePng = (
artifact: WebScreenshotArtifact,
maximumPixels: number,
): DecodedPng => {
const bytes = decodeCanonicalBase64(artifact.data_base64);
if (bytes === undefined || !bytes.subarray(0, 8).equals(PNG_SIGNATURE))
throw new TypeError("Invalid PNG signature");
let offset = 8;
let header: ReturnType<typeof parseHeader> | undefined;
const compressed: Buffer[] = [];
let sawEnd = false;
while (offset < bytes.length) {
if (offset + 12 > bytes.length) throw new TypeError("Truncated PNG chunk");
const length = bytes.readUInt32BE(offset);
const type = bytes.subarray(offset + 4, offset + 8).toString("ascii");
const dataStart = offset + 8;
const dataEnd = dataStart + length;
if (dataEnd + 4 > bytes.length) throw new TypeError("Truncated PNG data");
const data = bytes.subarray(dataStart, dataEnd);
if (type === "IHDR") header = parseHeader(data, maximumPixels);
else if (type === "IDAT") compressed.push(data);
else if (type === "IEND") {
sawEnd = true;
break;
}
offset = dataEnd + 4;
}
if (header === undefined || !sawEnd || compressed.length === 0)
throw new TypeError("Incomplete PNG");
const rowBytes = header.width * header.channels;
const expected = (rowBytes + 1) * header.height;
const raw = inflateSync(Buffer.concat(compressed), {
maxOutputLength: expected + 1,
});
if (raw.byteLength !== expected) throw new TypeError("Unexpected PNG size");
return {
width: header.width,
height: header.height,
rgba: unfilter(raw, header.width, header.height, header.channels),
};
};
const parseHeader = (data: Buffer, maximumPixels: number) => {
if (data.byteLength !== 13) throw new TypeError("Invalid PNG header");
const width = data.readUInt32BE(0);
const height = data.readUInt32BE(4);
const bitDepth = data[8];
const colorType = data[9];
if (
width === 0 ||
height === 0 ||
width * height > maximumPixels ||
bitDepth !== 8 ||
(colorType !== 2 && colorType !== 6) ||
data[10] !== 0 ||
data[11] !== 0 ||
data[12] !== 0
)
throw new TypeError("Unsupported or oversized PNG");
return { width, height, channels: colorType === 6 ? 4 : 3 };
};
const unfilter = (
raw: Buffer,
width: number,
height: number,
channels: number,
): Buffer => {
const rowBytes = width * channels;
const decoded = Buffer.alloc(rowBytes * height);
for (let row = 0; row < height; row += 1) {
const rawOffset = row * (rowBytes + 1);
const outputOffset = row * rowBytes;
const filter = raw[rawOffset];
if (filter === undefined || filter > 4)
throw new TypeError("Unsupported PNG filter");
for (let column = 0; column < rowBytes; column += 1) {
const source = raw[rawOffset + 1 + column] ?? 0;
const left =
column >= channels
? (decoded[outputOffset + column - channels] ?? 0)
: 0;
const above =
row > 0 ? (decoded[outputOffset + column - rowBytes] ?? 0) : 0;
const upperLeft =
row > 0 && column >= channels
? (decoded[outputOffset + column - rowBytes - channels] ?? 0)
: 0;
decoded[outputOffset + column] = applyFilter(
filter,
source,
left,
above,
upperLeft,
);
}
}
if (channels === 4) return decoded;
const rgba = Buffer.alloc(width * height * 4);
for (
let source = 0, target = 0;
source < decoded.length;
source += 3, target += 4
) {
rgba[target] = decoded[source] ?? 0;
rgba[target + 1] = decoded[source + 1] ?? 0;
rgba[target + 2] = decoded[source + 2] ?? 0;
rgba[target + 3] = 255;
}
return rgba;
};
const applyFilter = (
filter: number,
source: number,
left: number,
above: number,
upperLeft: number,
): number => {
switch (filter) {
case 0:
return source;
case 1:
return (source + left) & 0xff;
case 2:
return (source + above) & 0xff;
case 3:
return (source + Math.floor((left + above) / 2)) & 0xff;
case 4:
return (source + paeth(left, above, upperLeft)) & 0xff;
default:
throw new TypeError("Unsupported PNG filter");
}
};
const paeth = (left: number, above: number, upperLeft: number): number => {
const prediction = left + above - upperLeft;
const leftDistance = Math.abs(prediction - left);
const aboveDistance = Math.abs(prediction - above);
const upperLeftDistance = Math.abs(prediction - upperLeft);
return leftDistance <= aboveDistance && leftDistance <= upperLeftDistance
? left
: aboveDistance <= upperLeftDistance
? above
: upperLeft;
};
const dimensions = ({ width, height }: DecodedPng) => ({ width, height });
const limitations = (): string[] => [
"Pixel comparison does not perform OCR, semantic layout analysis, or perceptual color correction.",
"Only non-interlaced 8-bit RGB and RGBA PNG screenshots are accepted.",
];
const PNG_SIGNATURE = Buffer.from([137, 80, 78, 71, 13, 10, 26, 10]);
+35
View File
@@ -0,0 +1,35 @@
const SECRET_ASSIGNMENT =
/\b(authorization|api[_-]?key|access[_-]?token|refresh[_-]?token|password|passwd|secret|cookie|session)\b\s*[:=]\s*(?:Bearer\s+[A-Za-z0-9._~+/=-]+|"[^"]*"|'[^']*'|[^\s,;]+)/giu;
const BEARER = /\bBearer\s+[A-Za-z0-9._~+/=-]+/giu;
const JWT = /\beyJ[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}\b/gu;
/** Redact credential-shaped substrings before approved console text is retained. */
export const redactSensitiveText = (value: string): string =>
value
.replace(SECRET_ASSIGNMENT, (_match, name: string) => `${name}=[REDACTED]`)
.replace(BEARER, "Bearer [REDACTED]")
.replace(JWT, "[REDACTED_JWT]");
/** Return a UTF-8-bounded prefix without splitting a code point. */
export const boundedSensitiveText = (
value: string,
maximumBytes: number,
): {
readonly text: string;
readonly bytes: number;
readonly truncated: boolean;
} => {
const redacted = redactSensitiveText(value);
const total = Buffer.byteLength(redacted);
if (total <= maximumBytes)
return { text: redacted, bytes: total, truncated: false };
let text = "";
let bytes = 0;
for (const character of redacted) {
const size = Buffer.byteLength(character);
if (bytes + size > maximumBytes) break;
text += character;
bytes += size;
}
return { text, bytes, truncated: true };
};
+445
View File
@@ -0,0 +1,445 @@
import { AnyMap, eachMapping } from "@jridgewell/trace-mapping";
import { sanitizeBrowserUrl } from "../domain/browserObservation.js";
import type {
AnalyzeWebBundleInput,
WebBundleAnalysis,
} from "../domain/webBundleAnalysis.js";
import { createWebTextArtifact } from "../domain/webContentArtifact.js";
export interface WebSourceMapRequest {
readonly scriptKey: string;
readonly declaredUrl: string;
readonly fetchUrl: string;
}
interface SourceMapFetchHost {
readonly fetch: typeof fetch;
}
type SourceMaps = WebBundleAnalysis["observations"]["source_maps"];
type SourceMapItem = SourceMaps["items"][number];
/** Fetch and validate approved source maps without browser credentials. */
export const fetchWebSourceMaps = async (
requests: readonly WebSourceMapRequest[],
input: AnalyzeWebBundleInput,
signal?: AbortSignal,
host: SourceMapFetchHost = { fetch: globalThis.fetch },
): Promise<SourceMaps> => {
const items: SourceMapItem[] = [];
let remainingBytes = input.analysis_limits.max_total_source_map_bytes;
for (const request of requests.slice(
0,
input.analysis_limits.max_source_maps,
)) {
if (signal?.aborted === true) throw signal.reason;
if (remainingBytes === 0) break;
const fetched = await fetchOne(request, {
input,
maximumBytes: Math.min(
input.analysis_limits.max_source_map_bytes,
remainingBytes,
),
signal,
host,
});
items.push(fetched.item);
remainingBytes = Math.max(0, remainingBytes - fetched.bytesRead);
}
const included = items.filter(({ status }) => status === "included").length;
const dropped = requests.length - items.length;
const truncated =
dropped > 0 || items.some(({ status }) => status === "truncated");
return {
status: truncated
? "truncated"
: items.length === 0
? "unavailable"
: included === items.length
? "included"
: included > 0
? "partial"
: "unavailable",
requested: requests.length,
processed: items.length,
dropped,
dropped_script_keys: requests
.slice(items.length)
.map(({ scriptKey }) => scriptKey),
items,
};
};
interface SourceMapFetchResult {
readonly item: SourceMapItem;
readonly bytesRead: number;
}
interface SourceMapFetchContext {
readonly input: AnalyzeWebBundleInput;
readonly maximumBytes: number;
readonly signal: AbortSignal | undefined;
readonly host: SourceMapFetchHost;
}
const fetchOne = async (
request: WebSourceMapRequest,
context: SourceMapFetchContext,
): Promise<SourceMapFetchResult> => {
const { input, maximumBytes, signal, host } = context;
const base = emptyItem(request);
if (!approvedUrl(request.fetchUrl, input.allowed_origins))
return {
item: {
...base,
status: "policy_filtered",
limitation:
"Declared source-map URL is outside the approved exact origins.",
},
bytesRead: 0,
};
try {
const response = await fetchFollowingApprovedRedirects(
request.fetchUrl,
input.allowed_origins,
signal,
host,
);
if (response === undefined)
return {
item: {
...base,
status: "policy_filtered",
limitation: "A source-map redirect left the approved exact origins.",
},
bytesRead: 0,
};
if (!response.ok)
return {
item: {
...base,
status: "fetch_failed",
limitation: `Source-map server returned HTTP ${String(response.status)}.`,
},
bytesRead: 0,
};
const body = await boundedResponseText(response, maximumBytes);
return {
item: normalizeSourceMap(request, body.text, input),
bytesRead: body.bytes,
};
} catch (cause: unknown) {
if (signal?.aborted === true) throw cause;
return {
item: {
...base,
status: isLimitError(cause) ? "truncated" : "fetch_failed",
limitation: isLimitError(cause)
? "Source-map response exceeded the remaining approved byte budget."
: "Source-map fetch or validation failed.",
},
bytesRead: cause instanceof SourceMapByteLimitError ? cause.bytesRead : 0,
};
}
};
const fetchFollowingApprovedRedirects = async (
initialUrl: string,
allowedOrigins: readonly string[],
signal: AbortSignal | undefined,
host: SourceMapFetchHost,
): Promise<Response | undefined> => {
let current = initialUrl;
for (let redirect = 0; redirect <= 5; redirect += 1) {
if (!approvedUrl(current, allowedOrigins)) return undefined;
const response = await timedFetch(current, signal, host);
if (response.status < 300 || response.status >= 400) return response;
const location = response.headers.get("location");
if (location === null) return response;
current = new URL(location, current).href;
}
throw new Error("source_map_redirect_limit");
};
const timedFetch = async (
url: string,
signal: AbortSignal | undefined,
host: SourceMapFetchHost,
): Promise<Response> => {
const timeout = AbortSignal.timeout(5_000);
return await host.fetch(url, {
method: "GET",
headers: { Accept: "application/json, application/source-map+json;q=0.9" },
redirect: "manual",
credentials: "omit",
referrerPolicy: "no-referrer",
signal: signal === undefined ? timeout : AbortSignal.any([signal, timeout]),
});
};
const boundedResponseText = async (
response: Response,
maximumBytes: number,
): Promise<{ readonly text: string; readonly bytes: number }> => {
const declared = Number(response.headers.get("content-length"));
if (Number.isFinite(declared) && declared > maximumBytes)
throw new SourceMapByteLimitError(0);
if (response.body === null) return { text: "", bytes: 0 };
const reader = response.body.getReader();
const decoder = new TextDecoder("utf-8", { fatal: true });
let bytes = 0;
let text = "";
for (;;) {
const chunk = await reader.read();
if (chunk.done) break;
bytes += chunk.value.byteLength;
if (bytes > maximumBytes) {
await reader.cancel();
throw new SourceMapByteLimitError(bytes);
}
text += decoder.decode(chunk.value, { stream: true });
}
text += decoder.decode();
return { text, bytes };
};
class SourceMapByteLimitError extends RangeError {
constructor(readonly bytesRead: number) {
super("source_map_bytes");
}
}
const normalizeSourceMap = (
request: WebSourceMapRequest,
text: string,
input: AnalyzeWebBundleInput,
): SourceMapItem => {
const base = emptyItem(request);
if (!validSourceMapEnvelope(text))
return {
...base,
status: "invalid",
limitation: "Source-map JSON is not a bounded version 3 map.",
};
try {
const map = new AnyMap(text, request.fetchUrl);
if (map.sources.length > input.analysis_limits.max_original_sources)
return {
...base,
status: "truncated",
limitation: "Source map exceeds the original-source inventory limit.",
};
const resolvedBySource = new Map<string, string>();
const originalSources = map.sources.map((source, index) => {
const content = map.sourcesContent?.[index];
const resolved =
map.resolvedSources[index] ?? source ?? "[unknown-source]";
if (source !== null) resolvedBySource.set(source, resolved);
return {
source: sanitizeSource(resolved),
artifact:
typeof content === "string"
? createWebTextArtifact(content, sourceMediaType(source))
: null,
};
});
const mappings: SourceMapItem["mappings"] = [];
let totalMappings = 0;
eachMapping(map, (mapping) => {
if (
mapping.source === null ||
mapping.originalLine === null ||
mapping.originalColumn === null
)
return;
totalMappings += 1;
if (mappings.length >= input.analysis_limits.max_source_map_mappings)
return;
mappings.push({
generated_line: mapping.generatedLine,
generated_column: mapping.generatedColumn,
source: sanitizeSource(
resolvedBySource.get(mapping.source) ?? mapping.source,
),
original_line: mapping.originalLine,
original_column: mapping.originalColumn,
name: mapping.name?.slice(0, 1_024) ?? null,
});
});
const modules = originalModuleEdges(
originalSources,
input.analysis_limits.max_findings,
);
const mappingTruncated = totalMappings > mappings.length;
const limitations = [
...(mappingTruncated
? [
`Mappings were truncated from ${String(totalMappings)} to ${String(mappings.length)}.`,
]
: []),
...(modules.truncated
? [
`Original module edges were truncated to ${String(modules.edges.length)}.`,
]
: []),
];
return {
...base,
status: mappingTruncated || modules.truncated ? "truncated" : "included",
artifact: createWebTextArtifact(text, "application/source-map+json"),
original_sources: originalSources,
original_module_edges: modules.edges,
mappings,
limitation: limitations.length === 0 ? null : limitations.join(" "),
};
} catch {
return {
...base,
status: "invalid",
limitation: "Source-map mappings could not be decoded safely.",
};
}
};
const originalModuleEdges = (
sources: SourceMapItem["original_sources"],
maximum: number,
): {
readonly edges: SourceMapItem["original_module_edges"];
readonly truncated: boolean;
} => {
const edges: SourceMapItem["original_module_edges"] = [];
const seen = new Set<string>();
for (const source of sources) {
if (source.artifact === null) continue;
for (const detector of originalImportDetectors) {
for (const match of source.artifact.text.matchAll(detector.pattern)) {
const specifier = match[1]?.slice(0, 4_096);
if (specifier === undefined) continue;
const key = `${source.source}\0${detector.kind}\0${specifier}`;
if (seen.has(key)) continue;
seen.add(key);
if (edges.length >= maximum) return { edges, truncated: true };
edges.push({
from_source: source.source,
kind: detector.kind,
specifier,
resolved_source: resolveOriginalSource(specifier, source.source),
});
}
}
}
return { edges, truncated: false };
};
const validSourceMapEnvelope = (text: string): boolean => {
let parsed: unknown;
try {
parsed = JSON.parse(text);
} catch {
return false;
}
if (!isRecord(parsed) || parsed.version !== 3) return false;
if (typeof parsed.mappings === "string")
return Array.isArray(parsed.sources) && Array.isArray(parsed.names);
if (!Array.isArray(parsed.sections) || parsed.sections.length > 10_000)
return false;
const pending: unknown[] = [...parsed.sections];
let visited = 0;
while (pending.length > 0) {
const section = pending.pop();
if (!isRecord(section) || !isRecord(section.offset) || !("map" in section))
return false;
visited += 1;
if (visited > 10_000) return false;
const map = section.map;
if (!isRecord(map) || map.version !== 3) return false;
if (Array.isArray(map.sections)) pending.push(...map.sections);
else if (
typeof map.mappings !== "string" ||
!Array.isArray(map.sources) ||
!Array.isArray(map.names)
)
return false;
}
return true;
};
const approvedUrl = (
value: string,
allowedOrigins: readonly string[],
): boolean => {
try {
const parsed = new URL(value);
return (
(parsed.protocol === "http:" || parsed.protocol === "https:") &&
parsed.username === "" &&
parsed.password === "" &&
allowedOrigins.includes(parsed.origin)
);
} catch {
return false;
}
};
const emptyItem = (request: WebSourceMapRequest): SourceMapItem => ({
script_key: request.scriptKey,
declared_url: request.declaredUrl,
status: "fetch_failed",
artifact: null,
original_sources: [],
original_module_edges: [],
mappings: [],
limitation: null,
});
const sanitizeSource = (value: string): string => {
const bounded = value.slice(0, 4_096);
try {
const parsed = new URL(bounded);
return parsed.protocol === "http:" || parsed.protocol === "https:"
? sanitizeBrowserUrl(parsed.href).url
: `${parsed.protocol}//${parsed.host}${parsed.pathname}`;
} catch {
return bounded.split("#", 1)[0]?.split("?", 1)[0] ?? "";
}
};
const resolveOriginalSource = (
specifier: string,
base: string,
): string | null => {
try {
return sanitizeSource(new URL(specifier, base).href);
} catch {
return null;
}
};
const sourceMediaType = (source: string | null): string =>
source?.endsWith(".ts") || source?.endsWith(".tsx")
? "text/typescript"
: "text/javascript";
const isRecord = (value: unknown): value is Readonly<Record<string, unknown>> =>
typeof value === "object" && value !== null && !Array.isArray(value);
const isLimitError = (value: unknown): value is RangeError =>
value instanceof RangeError;
const originalImportDetectors = [
{
kind: "static_import" as const,
pattern:
/\b(?:import|export)\s+(?:[^'"\n]*?\s+from\s+)?["']([^"'\n]+)["']/gu,
},
{
kind: "dynamic_import" as const,
pattern: /\bimport\s*\(\s*["']([^"'\n]+)["']\s*\)/gu,
},
{
kind: "require" as const,
pattern: /\brequire\s*\(\s*["']([^"'\n]+)["']\s*\)/gu,
},
] as const;
+8
View File
@@ -38,6 +38,14 @@ export const CLI_COMMAND_NAMES = [
"policy",
"list-browser-targets",
"inspect-web-page",
"analyze-web-bundle",
"observe-web-session",
"discover-webmcp-tools",
"compare-web-captures",
"capture-web-screenshot",
"compare-web-screenshots",
"list-electron-targets",
"inspect-electron-page",
] as const;
const toolCatalog = TOOL_CONTRACTS.map((contract) => ({
+4
View File
@@ -28,6 +28,8 @@ import { projectReferenceSourceImportError } from "./application/ReferenceSource
import { loadConfiguredPermissionAuthority } from "./application/PermissionConfiguration.js";
import { registerPolicyCommands } from "./cliPolicyCommands.js";
import { registerBrowserCommands } from "./cliBrowserCommands.js";
import { registerAdvancedBrowserCommands } from "./cliBrowserAdvancedCommands.js";
import { registerElectronCommands } from "./cliElectronCommands.js";
/**
* Build the one-shot Incur CLI without starting Hopper at import time.
@@ -75,6 +77,8 @@ export const createCli = (): ReturnType<typeof Cli.create> => {
registerProcessCommands(cli, logger);
registerPolicyCommands(cli, logger);
registerBrowserCommands(cli, logger);
registerAdvancedBrowserCommands(cli, logger);
registerElectronCommands(cli, logger);
return cli;
};
+220
View File
@@ -0,0 +1,220 @@
import { Cli, z } from "incur";
import {
captureWebScreenshot,
compareWebCaptureEvidence,
compareWebScreenshotEvidence,
discoverWebMcpTools,
} from "./application/BrowserObservationService.js";
import { loadConfiguredPermissionAuthority } from "./application/PermissionConfiguration.js";
import { CdpBrowserProvider } from "./browser/CdpBrowserProvider.js";
import { logCliCommand } from "./cliLogging.js";
import { parseConfig } from "./config.js";
import { AnalysisInputError, projectAnalysisError } from "./domain/errors.js";
import { compareWebCapturesInputSchema } from "./domain/webCaptureDiff.js";
import { discoverWebMcpToolsInputSchema } from "./domain/webMcpDiscovery.js";
import {
captureWebScreenshotInputSchema,
compareWebScreenshotsInputSchema,
} from "./domain/webScreenshot.js";
import type { JsonValue } from "./domain/jsonValue.js";
import type { Logger } from "./logger.js";
const scopeOptions = {
allowedOrigins: z.array(z.string().min(1)).optional(),
approved: z.boolean().default(false),
};
/** Register WebMCP, capture-diff, and screenshot CLI equivalents. */
export const registerAdvancedBrowserCommands = (
cli: ReturnType<typeof Cli.create>,
logger: Logger,
): void => {
registerWebMcp(cli, logger);
registerCaptureDiff(cli, logger);
registerScreenshot(cli, logger);
registerScreenshotDiff(cli, logger);
};
const registerWebMcp = (
cli: ReturnType<typeof Cli.create>,
logger: Logger,
): void => {
cli.command("discover-webmcp-tools", {
description: "Passively discover page-declared WebMCP tools",
args: z.object({ endpoint: z.string(), targetId: z.string() }),
options: z.object({
...scopeOptions,
observationMs: z.number().int().min(0).max(10_000).default(100),
maxTools: z.number().int().min(1).max(5_000).default(500),
maxSchemaBytes: z
.number()
.int()
.min(1)
.max(1_024 * 1_024)
.default(256 * 1_024),
maxSchemaNodes: z.number().int().min(1).max(100_000).default(5_000),
maxSchemaDepth: z.number().int().min(1).max(100).default(20),
}),
run: ({ args, options }) =>
logCliCommand(logger, "discover-webmcp-tools", async () => {
const context = await browserContext();
if (!context.ok) return context.error;
const parsed = discoverWebMcpToolsInputSchema.safeParse({
cdp_endpoint: args.endpoint,
allowed_origins:
options.allowedOrigins ?? context.allowedBrowserOrigins,
target_id: args.targetId,
approved: options.approved,
observation_ms: options.observationMs,
max_tools: options.maxTools,
max_schema_bytes: options.maxSchemaBytes,
max_schema_nodes: options.maxSchemaNodes,
max_schema_depth: options.maxSchemaDepth,
});
if (!parsed.success) return inputError("discover_webmcp_tools");
const result = await discoverWebMcpTools(
context.provider,
context.authority,
parsed.data,
);
return result.ok ? result.value : cliError(result.error);
}),
});
};
const registerCaptureDiff = (
cli: ReturnType<typeof Cli.create>,
logger: Logger,
): void => {
cli.command("compare-web-captures", {
description: "Compare two normalized web capture JSON values",
args: z.object({ beforeJson: z.string(), afterJson: z.string() }),
options: z.object({
maxChanges: z.number().int().min(1).max(20_000).default(2_000),
}),
run: ({ args, options }) =>
logCliCommand(logger, "compare-web-captures", async () => {
const before = parseJson(args.beforeJson);
const after = parseJson(args.afterJson);
const parsed = compareWebCapturesInputSchema.safeParse({
before,
after,
max_changes: options.maxChanges,
});
if (!parsed.success) return inputError("compare_web_captures");
const result = await compareWebCaptureEvidence(
new CdpBrowserProvider(),
parsed.data,
);
return result.ok ? result.value : cliError(result.error);
}),
});
};
const registerScreenshot = (
cli: ReturnType<typeof Cli.create>,
logger: Logger,
): void => {
cli.command("capture-web-screenshot", {
description: "Capture an explicitly approved visible page viewport",
args: z.object({ endpoint: z.string(), targetId: z.string() }),
options: z.object({
...scopeOptions,
screenshotApproved: z.boolean().default(false),
maximumImageBytes: z
.number()
.int()
.min(1)
.max(8 * 1_024 * 1_024)
.default(4 * 1_024 * 1_024),
}),
run: ({ args, options }) =>
logCliCommand(logger, "capture-web-screenshot", async () => {
const context = await browserContext();
if (!context.ok) return context.error;
const parsed = captureWebScreenshotInputSchema.safeParse({
cdp_endpoint: args.endpoint,
allowed_origins:
options.allowedOrigins ?? context.allowedBrowserOrigins,
target_id: args.targetId,
approved: options.approved,
screenshot_approved: options.screenshotApproved,
maximum_image_bytes: options.maximumImageBytes,
});
if (!parsed.success) return inputError("capture_web_screenshot");
const result = await captureWebScreenshot(
context.provider,
context.authority,
parsed.data,
);
return result.ok ? result.value : cliError(result.error);
}),
});
};
const registerScreenshotDiff = (
cli: ReturnType<typeof Cli.create>,
logger: Logger,
): void => {
cli.command("compare-web-screenshots", {
description: "Compare two self-verifying PNG artifact JSON values",
args: z.object({ beforeJson: z.string(), afterJson: z.string() }),
options: z.object({
channelThreshold: z.number().int().min(0).max(255).default(0),
maximumPixels: z
.number()
.int()
.min(1)
.max(32_000_000)
.default(16_000_000),
}),
run: ({ args, options }) =>
logCliCommand(logger, "compare-web-screenshots", async () => {
const parsed = compareWebScreenshotsInputSchema.safeParse({
before: parseJson(args.beforeJson),
after: parseJson(args.afterJson),
channel_threshold: options.channelThreshold,
maximum_pixels: options.maximumPixels,
});
if (!parsed.success) return inputError("compare_web_screenshots");
const result = await compareWebScreenshotEvidence(
new CdpBrowserProvider(),
parsed.data,
);
return result.ok ? result.value : cliError(result.error);
}),
});
};
const browserContext = async () => {
const config = parseConfig(process.env);
if (!config.ok) return { ok: false as const, error: cliError(config.error) };
const authority = await loadConfiguredPermissionAuthority(config.value);
if (!authority.ok)
return { ok: false as const, error: cliError(authority.error) };
return {
ok: true as const,
authority: authority.value,
provider: new CdpBrowserProvider(),
allowedBrowserOrigins: config.value.browserAllowedOrigins,
};
};
const parseJson = (value: string): unknown => {
try {
return JSON.parse(value);
} catch {
return undefined;
}
};
const inputError = (operation: string): JsonValue =>
cliError(new AnalysisInputError(operation));
const cliError = (
error: Parameters<typeof projectAnalysisError>[0],
): JsonValue => ({
error: "Browser observation failed",
...projectAnalysisError(error),
});
+197
View File
@@ -1,8 +1,10 @@
import { Cli, z } from "incur";
import {
analyzeWebBundle,
inspectWebPage,
listBrowserTargets,
observeWebSession,
} from "./application/BrowserObservationService.js";
import { loadConfiguredPermissionAuthority } from "./application/PermissionConfiguration.js";
import { CdpBrowserProvider } from "./browser/CdpBrowserProvider.js";
@@ -12,6 +14,8 @@ import {
inspectWebPageInputSchema,
listBrowserTargetsInputSchema,
} from "./domain/browserObservation.js";
import { analyzeWebBundleInputSchema } from "./domain/webBundleAnalysis.js";
import { observeWebSessionInputSchema } from "./domain/browserSession.js";
import { AnalysisInputError, projectAnalysisError } from "./domain/errors.js";
import type { JsonValue } from "./domain/jsonValue.js";
import type { Logger } from "./logger.js";
@@ -33,6 +37,8 @@ export const registerBrowserCommands = (
): void => {
registerTargetList(cli, logger);
registerPageInspection(cli, logger);
registerBundleAnalysis(cli, logger);
registerObservationSession(cli, logger);
};
const registerTargetList = (
@@ -86,11 +92,30 @@ const registerPageInspection = (
options: z.object({
...browserScopeOptions,
observationMs: z.number().int().min(0).max(10_000).default(500),
includeAccessibilityText: z.boolean().default(false),
includeConsoleText: z.boolean().default(false),
consoleTextApproved: z.boolean().default(false),
includeJsonBodyShapes: z.boolean().default(false),
jsonBodySchemaApproved: z.boolean().default(false),
includeWebsocketShapes: z.boolean().default(false),
websocketShapeApproved: z.boolean().default(false),
includeScriptSources: z.boolean().default(false),
includeStorageKeys: z.boolean().default(false),
maxFrames: z.number().int().min(1).max(1_000).default(200),
maxDomNodes: z.number().int().min(1).max(10_000).default(2_000),
maxAxNodes: z.number().int().min(1).max(10_000).default(2_000),
maxAxTextFieldBytes: z
.number()
.int()
.min(1)
.max(16 * 1_024)
.default(1_024),
maxTotalAxTextBytes: z
.number()
.int()
.min(1)
.max(1_024 * 1_024)
.default(64 * 1_024),
maxScripts: z.number().int().min(1).max(1_000).default(200),
maxResources: z.number().int().min(1).max(10_000).default(2_000),
maxWorkers: z.number().int().min(1).max(5_000).default(500),
@@ -109,7 +134,45 @@ const registerPageInspection = (
.default(4 * 1_024 * 1_024),
maxNetworkEvents: z.number().int().min(1).max(10_000).default(1_000),
maxConsoleEvents: z.number().int().min(1).max(2_000).default(200),
maxConsoleTextFieldBytes: z
.number()
.int()
.min(1)
.max(16 * 1_024)
.default(1_024),
maxTotalConsoleTextBytes: z
.number()
.int()
.min(1)
.max(1_024 * 1_024)
.default(64 * 1_024),
maxJsonBodyBytes: z
.number()
.int()
.min(1)
.max(4 * 1_024 * 1_024)
.default(1_024 * 1_024),
maxTotalJsonBodyBytes: z
.number()
.int()
.min(1)
.max(16 * 1_024 * 1_024)
.default(4 * 1_024 * 1_024),
maxJsonShapeNodes: z.number().int().min(1).max(100_000).default(5_000),
maxJsonShapeDepth: z.number().int().min(1).max(100).default(20),
maxWebsocketEvents: z.number().int().min(1).max(5_000).default(500),
maxWebsocketShapeBytes: z
.number()
.int()
.min(1)
.max(1_024 * 1_024)
.default(64 * 1_024),
maxTotalWebsocketShapeBytes: z
.number()
.int()
.min(1)
.max(16 * 1_024 * 1_024)
.default(1_024 * 1_024),
}),
run: ({ args, options }) =>
logCliCommand(logger, "inspect-web-page", async () => {
@@ -122,12 +185,21 @@ const registerPageInspection = (
approved: options.approved,
target_id: args.targetId,
observation_ms: options.observationMs,
include_accessibility_text: options.includeAccessibilityText,
include_console_text: options.includeConsoleText,
console_text_approved: options.consoleTextApproved,
include_json_body_shapes: options.includeJsonBodyShapes,
json_body_schema_approved: options.jsonBodySchemaApproved,
include_websocket_shapes: options.includeWebsocketShapes,
websocket_shape_approved: options.websocketShapeApproved,
include_script_sources: options.includeScriptSources,
include_storage_keys: options.includeStorageKeys,
limits: {
max_frames: options.maxFrames,
max_dom_nodes: options.maxDomNodes,
max_ax_nodes: options.maxAxNodes,
max_ax_text_field_bytes: options.maxAxTextFieldBytes,
max_total_ax_text_bytes: options.maxTotalAxTextBytes,
max_scripts: options.maxScripts,
max_resources: options.maxResources,
max_workers: options.maxWorkers,
@@ -136,7 +208,16 @@ const registerPageInspection = (
max_total_script_source_bytes: options.maxTotalScriptSourceBytes,
max_network_events: options.maxNetworkEvents,
max_console_events: options.maxConsoleEvents,
max_console_text_field_bytes: options.maxConsoleTextFieldBytes,
max_total_console_text_bytes: options.maxTotalConsoleTextBytes,
max_json_body_bytes: options.maxJsonBodyBytes,
max_total_json_body_bytes: options.maxTotalJsonBodyBytes,
max_json_shape_nodes: options.maxJsonShapeNodes,
max_json_shape_depth: options.maxJsonShapeDepth,
max_websocket_events: options.maxWebsocketEvents,
max_websocket_shape_bytes: options.maxWebsocketShapeBytes,
max_total_websocket_shape_bytes:
options.maxTotalWebsocketShapeBytes,
},
});
if (!parsed.success)
@@ -151,6 +232,122 @@ const registerPageInspection = (
});
};
const registerBundleAnalysis = (
cli: ReturnType<typeof Cli.create>,
logger: Logger,
): void => {
cli.command("analyze-web-bundle", {
description: "Capture and statically analyze an approved page bundle",
args: z.object({
endpoint: z.string().describe("Configured loopback CDP HTTP endpoint"),
targetId: z.string().describe("Target ID from list-browser-targets"),
}),
options: z.object({
...browserScopeOptions,
sourceCaptureApproved: z.boolean().default(false),
observationMs: z.number().int().min(0).max(10_000).default(500),
fetchSourceMaps: z.boolean().default(false),
sourceMapFetchApproved: z.boolean().default(false),
maxFindings: z.number().int().min(1).max(10_000).default(1_000),
maxAstNodes: z.number().int().min(1).max(2_000_000).default(250_000),
maxSourceMaps: z.number().int().min(1).max(1_000).default(100),
maxSourceMapBytes: z
.number()
.int()
.min(1)
.max(16 * 1_024 * 1_024)
.default(4 * 1_024 * 1_024),
maxTotalSourceMapBytes: z
.number()
.int()
.min(1)
.max(64 * 1_024 * 1_024)
.default(16 * 1_024 * 1_024),
maxSourceMapMappings: z
.number()
.int()
.min(1)
.max(100_000)
.default(10_000),
maxOriginalSources: z.number().int().min(1).max(20_000).default(2_000),
}),
run: ({ args, options }) =>
logCliCommand(logger, "analyze-web-bundle", async () => {
const context = await browserContext();
if (!context.ok) return context.error;
const parsed = analyzeWebBundleInputSchema.safeParse({
cdp_endpoint: args.endpoint,
allowed_origins:
options.allowedOrigins ?? context.allowedBrowserOrigins,
approved: options.approved,
target_id: args.targetId,
observation_ms: options.observationMs,
include_script_sources: true,
source_capture_approved: options.sourceCaptureApproved,
fetch_source_maps: options.fetchSourceMaps,
source_map_fetch_approved: options.sourceMapFetchApproved,
analysis_limits: {
max_findings: options.maxFindings,
max_ast_nodes: options.maxAstNodes,
max_source_maps: options.maxSourceMaps,
max_source_map_bytes: options.maxSourceMapBytes,
max_total_source_map_bytes: options.maxTotalSourceMapBytes,
max_source_map_mappings: options.maxSourceMapMappings,
max_original_sources: options.maxOriginalSources,
},
});
if (!parsed.success)
return cliError(new AnalysisInputError("analyze_web_bundle"));
const result = await analyzeWebBundle(
context.provider,
context.authority,
parsed.data,
);
return result.ok ? result.value : cliError(result.error);
}),
});
};
const registerObservationSession = (
cli: ReturnType<typeof Cli.create>,
logger: Logger,
): void => {
cli.command("observe-web-session", {
description: "Observe user-driven page navigation during a bounded window",
args: z.object({
endpoint: z.string().describe("Configured loopback CDP HTTP endpoint"),
targetId: z.string().describe("Target ID from list-browser-targets"),
}),
options: z.object({
...browserScopeOptions,
observationMs: z.number().int().min(1).max(60_000).default(10_000),
maxTimelineEvents: z.number().int().min(1).max(20_000).default(2_000),
}),
run: ({ args, options }) =>
logCliCommand(logger, "observe-web-session", async () => {
const context = await browserContext();
if (!context.ok) return context.error;
const parsed = observeWebSessionInputSchema.safeParse({
cdp_endpoint: args.endpoint,
allowed_origins:
options.allowedOrigins ?? context.allowedBrowserOrigins,
target_id: args.targetId,
approved: options.approved,
observation_ms: options.observationMs,
max_timeline_events: options.maxTimelineEvents,
});
if (!parsed.success)
return cliError(new AnalysisInputError("observe_web_session"));
const result = await observeWebSession(
context.provider,
context.authority,
parsed.data,
);
return result.ok ? result.value : cliError(result.error);
}),
});
};
const browserContext = async () => {
const config = parseConfig(process.env);
if (!config.ok) return { ok: false as const, error: cliError(config.error) };
+141
View File
@@ -0,0 +1,141 @@
import { Cli, z } from "incur";
import {
inspectElectronPage,
listElectronTargets,
} from "./application/ElectronObservationService.js";
import { loadConfiguredPermissionAuthority } from "./application/PermissionConfiguration.js";
import { CdpElectronProvider } from "./browser/CdpElectronProvider.js";
import { logCliCommand } from "./cliLogging.js";
import { parseConfig } from "./config.js";
import {
inspectElectronPageInputSchema,
listElectronTargetsInputSchema,
} from "./domain/electronObservation.js";
import { AnalysisInputError, projectAnalysisError } from "./domain/errors.js";
import type { JsonValue } from "./domain/jsonValue.js";
import type { Logger } from "./logger.js";
const scopeOptions = {
allowedFileRoots: z
.array(z.string().min(1))
.optional()
.describe("Filesystem roots; defaults to REA_ELECTRON_FILE_ROOTS_JSON"),
approved: z.boolean().default(false),
};
/** Register CLI equivalents of the Electron MCP tools. */
export const registerElectronCommands = (
cli: ReturnType<typeof Cli.create>,
logger: Logger,
): void => {
cli.command("list-electron-targets", {
description: "List root-confined file pages from Electron CDP",
args: z.object({ endpoint: z.string() }),
options: z.object({
...scopeOptions,
offset: z.number().int().min(0).default(0),
limit: z.number().int().min(1).max(200).default(100),
}),
run: ({ args, options }) =>
logCliCommand(logger, "list-electron-targets", async () => {
const context = await electronContext();
if (!context.ok) return context.error;
const parsed = listElectronTargetsInputSchema.safeParse({
cdp_endpoint: args.endpoint,
allowed_file_roots:
options.allowedFileRoots ?? context.allowedFileRoots,
approved: options.approved,
offset: options.offset,
limit: options.limit,
});
if (!parsed.success) return inputError("list_electron_targets");
const result = await listElectronTargets(
context.provider,
context.authority,
parsed.data,
);
return result.ok ? result.value : cliError(result.error);
}),
});
cli.command("inspect-electron-page", {
description: "Passively inspect one root-confined Electron file page",
args: z.object({ endpoint: z.string(), targetId: z.string() }),
options: z.object({
...scopeOptions,
observationMs: z.number().int().min(0).max(10_000).default(100),
includeScriptSources: z.boolean().default(false),
sourceCaptureApproved: z.boolean().default(false),
maxFrames: z.number().int().min(1).max(1_000).default(200),
maxDomNodes: z.number().int().min(1).max(10_000).default(2_000),
maxScripts: z.number().int().min(1).max(2_000).default(500),
maxResources: z.number().int().min(1).max(10_000).default(2_000),
maxScriptSourceBytes: z
.number()
.int()
.min(1)
.max(4 * 1_024 * 1_024)
.default(1_024 * 1_024),
maxTotalScriptSourceBytes: z
.number()
.int()
.min(1)
.max(16 * 1_024 * 1_024)
.default(4 * 1_024 * 1_024),
}),
run: ({ args, options }) =>
logCliCommand(logger, "inspect-electron-page", async () => {
const context = await electronContext();
if (!context.ok) return context.error;
const parsed = inspectElectronPageInputSchema.safeParse({
cdp_endpoint: args.endpoint,
allowed_file_roots:
options.allowedFileRoots ?? context.allowedFileRoots,
target_id: args.targetId,
approved: options.approved,
observation_ms: options.observationMs,
include_script_sources: options.includeScriptSources,
source_capture_approved: options.sourceCaptureApproved,
limits: {
max_frames: options.maxFrames,
max_dom_nodes: options.maxDomNodes,
max_scripts: options.maxScripts,
max_resources: options.maxResources,
max_script_source_bytes: options.maxScriptSourceBytes,
max_total_script_source_bytes: options.maxTotalScriptSourceBytes,
},
});
if (!parsed.success) return inputError("inspect_electron_page");
const result = await inspectElectronPage(
context.provider,
context.authority,
parsed.data,
);
return result.ok ? result.value : cliError(result.error);
}),
});
};
const electronContext = async () => {
const config = parseConfig(process.env);
if (!config.ok) return { ok: false as const, error: cliError(config.error) };
const authority = await loadConfiguredPermissionAuthority(config.value);
if (!authority.ok)
return { ok: false as const, error: cliError(authority.error) };
return {
ok: true as const,
authority: authority.value,
provider: new CdpElectronProvider(),
allowedFileRoots: config.value.electronFileRoots,
};
};
const inputError = (operation: string): JsonValue =>
cliError(new AnalysisInputError(operation));
const cliError = (
error: Parameters<typeof projectAnalysisError>[0],
): JsonValue => ({
error: "Electron observation failed",
...projectAnalysisError(error),
});
+1
View File
@@ -17,6 +17,7 @@ import type { Logger } from "./logger.js";
const capabilitySchema = z.enum([
"process_capture",
"browser_observe",
"electron_observe",
"evidence_read",
"evidence_write",
"investigation_input",
+55
View File
@@ -14,6 +14,7 @@ import {
browserEndpointSchema,
browserOriginSchema,
} from "./domain/browserObservation.js";
import { electronFileRootsSchema } from "./domain/electronObservation.js";
const DEFAULT_HOPPER_LAUNCHER_PATH =
"/Applications/Hopper Disassembler.app/Contents/MacOS/hopper";
@@ -38,6 +39,9 @@ export interface AppConfig {
readonly browserObservationEnabled: boolean;
readonly browserCdpEndpoints: readonly string[];
readonly browserAllowedOrigins: readonly string[];
readonly electronObservationEnabled: boolean;
readonly electronCdpEndpoints: readonly string[];
readonly electronFileRoots: readonly string[];
readonly permissionCeilings: readonly PermissionCeiling[];
readonly administratorPermissionGrants: readonly PermissionGrant[];
readonly permissionProjectRoot: string | undefined;
@@ -76,6 +80,9 @@ const environmentSchema = z
REA_BROWSER_OBSERVE_ENABLED: z.enum(["true", "false"]).default("false"),
REA_BROWSER_CDP_ENDPOINTS_JSON: z.string().default("[]"),
REA_BROWSER_ALLOWED_ORIGINS_JSON: z.string().default("[]"),
REA_ELECTRON_OBSERVE_ENABLED: z.enum(["true", "false"]).default("false"),
REA_ELECTRON_CDP_ENDPOINTS_JSON: z.string().default("[]"),
REA_ELECTRON_FILE_ROOTS_JSON: z.string().default("[]"),
REA_PERMISSION_PROJECT_ROOT: z.string().min(1).optional(),
REA_PERMISSION_PROJECT_STORE: z.string().min(1).optional(),
})
@@ -133,6 +140,31 @@ const parseBrowserArray = (
}
};
const parseElectronFileRoots = (
encoded: string,
): Result<readonly string[], ConfigurationError> => {
try {
const decoded: unknown = JSON.parse(encoded);
if (Array.isArray(decoded) && decoded.length === 0) return ok([]);
const parsed = electronFileRootsSchema.safeParse(decoded);
return parsed.success
? ok(parsed.data)
: err(
new ConfigurationError(
"REA_ELECTRON_FILE_ROOTS_JSON must encode absolute roots",
{ cause: parsed.error },
),
);
} catch (cause: unknown) {
return err(
new ConfigurationError(
"REA_ELECTRON_FILE_ROOTS_JSON must be valid JSON",
{ cause },
),
);
}
};
const filePolicy = (roots: readonly string[]): EvidenceFilePolicy => ({
roots,
maxBytes: 64 * 1024 * 1024,
@@ -266,6 +298,17 @@ export const parseConfig = (
32,
);
if (!browserOrigins.ok) return browserOrigins;
const electronEndpoints = parseBrowserArray(
parsedEnvironment.data.REA_ELECTRON_CDP_ENDPOINTS_JSON,
"REA_ELECTRON_CDP_ENDPOINTS_JSON",
browserEndpointSchema,
16,
);
if (!electronEndpoints.ok) return electronEndpoints;
const electronFileRoots = parseElectronFileRoots(
parsedEnvironment.data.REA_ELECTRON_FILE_ROOTS_JSON,
);
if (!electronFileRoots.ok) return electronFileRoots;
const permissionCeilings = [
...(parsedEnvironment.data.REA_PROCESS_CAPTURE_ENABLED === "true"
? [
@@ -297,6 +340,14 @@ export const parseConfig = (
}),
]
: []),
...(parsedEnvironment.data.REA_ELECTRON_OBSERVE_ENABLED === "true"
? [
permissionScope("electron_observe", electronFileRoots.value, {
origins: electronEndpoints.value,
network: "loopback",
}),
]
: []),
...(parsedEnvironment.data.REA_ARTIFACT_NATIVE_MOUNT_ENABLED === "true"
? [permissionScope("native_mount", [], { mount: true })]
: []),
@@ -336,6 +387,10 @@ export const parseConfig = (
parsedEnvironment.data.REA_BROWSER_OBSERVE_ENABLED === "true",
browserCdpEndpoints: browserEndpoints.value,
browserAllowedOrigins: browserOrigins.value,
electronObservationEnabled:
parsedEnvironment.data.REA_ELECTRON_OBSERVE_ENABLED === "true",
electronCdpEndpoints: electronEndpoints.value,
electronFileRoots: electronFileRoots.value,
permissionCeilings,
administratorPermissionGrants: administratorGrants(permissionCeilings),
permissionProjectRoot: parsedEnvironment.data.REA_PERMISSION_PROJECT_ROOT,
+329 -1
View File
@@ -1,4 +1,5 @@
import type { ToolContract } from "./toolContracts.js";
import { z } from "zod";
import { evidenceSchema } from "../domain/evidence.js";
import {
browserTargetListSchema,
@@ -6,8 +7,30 @@ import {
listBrowserTargetsInputSchema,
webPageInspectionSchema,
} from "../domain/browserObservation.js";
import {
analyzeWebBundleInputSchema,
webBundleAnalysisSchema,
} from "../domain/webBundleAnalysis.js";
import {
observeWebSessionInputSchema,
webObservationSessionSchema,
} from "../domain/browserSession.js";
import {
discoverWebMcpToolsInputSchema,
webMcpDiscoverySchema,
} from "../domain/webMcpDiscovery.js";
import {
compareWebCapturesInputSchema,
webCaptureDiffSchema,
} from "../domain/webCaptureDiff.js";
import {
captureWebScreenshotInputSchema,
compareWebScreenshotsInputSchema,
webScreenshotDiffSchema,
webScreenshotSchema,
} from "../domain/webScreenshot.js";
const evidenceResult = (schema: typeof browserTargetListSchema) =>
const evidenceResult = <Schema extends z.ZodType>(schema: Schema) =>
evidenceSchema
.omit({ normalized_result: true })
.extend({ normalized_result: schema });
@@ -15,6 +38,16 @@ const listOutputSchema = evidenceResult(browserTargetListSchema);
const inspectionOutputSchema = evidenceSchema
.omit({ normalized_result: true })
.extend({ normalized_result: webPageInspectionSchema });
const bundleOutputSchema = evidenceSchema
.omit({ normalized_result: true })
.extend({ normalized_result: webBundleAnalysisSchema });
const observationSessionOutputSchema = evidenceSchema
.omit({ normalized_result: true })
.extend({ normalized_result: webObservationSessionSchema });
const webMcpOutputSchema = evidenceResult(webMcpDiscoverySchema);
const captureDiffOutputSchema = evidenceResult(webCaptureDiffSchema);
const screenshotOutputSchema = evidenceResult(webScreenshotSchema);
const screenshotDiffOutputSchema = evidenceResult(webScreenshotDiffSchema);
const endpoint = "http://127.0.0.1:9222";
const origin = "https://app.example.test";
@@ -69,12 +102,21 @@ export const BROWSER_TOOL_CONTRACTS = [
approved: true,
target_id: "TARGET_ID_FROM_LIST_BROWSER_TARGETS",
observation_ms: 500,
include_accessibility_text: false,
include_console_text: false,
console_text_approved: false,
include_json_body_shapes: false,
json_body_schema_approved: false,
include_websocket_shapes: false,
websocket_shape_approved: false,
include_script_sources: false,
include_storage_keys: false,
limits: {
max_frames: 200,
max_dom_nodes: 2_000,
max_ax_nodes: 2_000,
max_ax_text_field_bytes: 1_024,
max_total_ax_text_bytes: 65_536,
max_scripts: 200,
max_resources: 2_000,
max_workers: 500,
@@ -83,10 +125,296 @@ export const BROWSER_TOOL_CONTRACTS = [
max_total_script_source_bytes: 4_194_304,
max_network_events: 1_000,
max_console_events: 200,
max_console_text_field_bytes: 1_024,
max_total_console_text_bytes: 65_536,
max_json_body_bytes: 1_048_576,
max_total_json_body_bytes: 4_194_304,
max_json_shape_nodes: 5_000,
max_json_shape_depth: 20,
max_websocket_events: 500,
max_websocket_shape_bytes: 65_536,
max_total_websocket_shape_bytes: 1_048_576,
},
},
},
],
},
{
name: "analyze_web_bundle",
description:
"Capture explicitly approved JavaScript source from one approved CDP page and statically derive a bounded chunk graph, route and endpoint candidates, vendor fingerprints, page-declared WebMCP metadata, and optional separately approved source-map evidence. JavaScript is parsed but never executed.",
kind: "browser-provider",
inputSchema: analyzeWebBundleInputSchema,
outputSchema: bundleOutputSchema,
annotations: {
readOnlyHint: true,
destructiveHint: false,
idempotentHint: true,
openWorldHint: true,
},
examples: [
{
title: "Analyze an approved page bundle",
input: {
cdp_endpoint: endpoint,
allowed_origins: [origin],
approved: true,
target_id: "TARGET_ID_FROM_LIST_BROWSER_TARGETS",
observation_ms: 500,
include_accessibility_text: false,
include_script_sources: true,
include_storage_keys: false,
source_capture_approved: true,
fetch_source_maps: false,
source_map_fetch_approved: false,
analysis_limits: {
max_findings: 1_000,
max_ast_nodes: 250_000,
max_source_maps: 100,
max_source_map_bytes: 4_194_304,
max_total_source_map_bytes: 16_777_216,
max_source_map_mappings: 10_000,
max_original_sources: 2_000,
},
},
},
],
},
{
name: "observe_web_session",
description:
"Arm a bounded CDP observation window while the user operates the page. Allows approved same-origin reload and SPA navigation, records ordered navigation, redirect, lifecycle, and failure metadata, and stops before retaining an out-of-policy destination.",
kind: "browser-provider",
inputSchema: observeWebSessionInputSchema,
outputSchema: observationSessionOutputSchema,
annotations: {
readOnlyHint: true,
destructiveHint: false,
idempotentHint: false,
openWorldHint: true,
},
examples: [
{
title: "Observe one external user action",
input: {
cdp_endpoint: endpoint,
allowed_origins: [origin],
target_id: "TARGET_ID_FROM_LIST_BROWSER_TARGETS",
approved: true,
observation_ms: 10_000,
max_timeline_events: 2_000,
},
},
],
},
{
name: "discover_webmcp_tools",
description:
"Passively inventory page-registered WebMCP tools using the experimental CDP WebMCP domain. Metadata is bounded and page-declared-untrusted; REA never registers or invokes discovered tools.",
kind: "browser-provider",
inputSchema: discoverWebMcpToolsInputSchema,
outputSchema: webMcpOutputSchema,
annotations: {
readOnlyHint: true,
destructiveHint: false,
idempotentHint: true,
openWorldHint: true,
},
examples: [
{
title: "Discover current WebMCP declarations",
input: {
cdp_endpoint: endpoint,
allowed_origins: [origin],
target_id: "TARGET_ID_FROM_LIST_BROWSER_TARGETS",
approved: true,
observation_ms: 100,
max_tools: 500,
max_schema_bytes: 262_144,
max_schema_nodes: 5_000,
max_schema_depth: 20,
},
},
],
},
{
name: "compare_web_captures",
description:
"Compare two normalized web captures across DOM, scripts, resources, network, safe metadata, and optional WebMCP inventories. Stable observed changes are distinguished from unknown absence caused by incomplete capture coverage.",
kind: "browser-provider",
inputSchema: compareWebCapturesInputSchema,
outputSchema: captureDiffOutputSchema,
annotations: {
readOnlyHint: true,
destructiveHint: false,
idempotentHint: true,
openWorldHint: false,
},
examples: [
{
title: "Compare two normalized captures",
input: {
before: { inspection: exampleInspection() },
after: { inspection: exampleInspection() },
max_changes: 2_000,
},
},
],
},
{
name: "capture_web_screenshot",
description:
"Capture the current visible viewport of one approved page as a bounded, content-addressed PNG artifact. Screenshot capture requires separate explicit approval and never scrolls, navigates, or evaluates page JavaScript.",
kind: "browser-provider",
inputSchema: captureWebScreenshotInputSchema,
outputSchema: screenshotOutputSchema,
annotations: {
readOnlyHint: true,
destructiveHint: false,
idempotentHint: false,
openWorldHint: true,
},
examples: [
{
title: "Capture an approved viewport",
input: {
cdp_endpoint: endpoint,
allowed_origins: [origin],
target_id: "TARGET_ID_FROM_LIST_BROWSER_TARGETS",
approved: true,
screenshot_approved: true,
maximum_image_bytes: 4_194_304,
},
},
],
},
{
name: "compare_web_screenshots",
description:
"Compare two self-verifying PNG screenshot artifacts with bounded local pixel metrics. Returns exact changed-pixel ratios and channel deltas without OCR or external services.",
kind: "browser-provider",
inputSchema: compareWebScreenshotsInputSchema,
outputSchema: screenshotDiffOutputSchema,
annotations: {
readOnlyHint: true,
destructiveHint: false,
idempotentHint: true,
openWorldHint: false,
},
examples: [
{
title: "Compare two screenshot artifacts",
input: {
before: exampleScreenshot(),
after: exampleScreenshot(),
channel_threshold: 0,
maximum_pixels: 16_000_000,
},
},
],
},
] as const satisfies readonly ToolContract[];
function exampleCompleteness() {
return {
status: "complete_within_window",
conditions: ["complete_within_window"],
policy_filtered_sections: [],
attach_limited_sections: [],
truncated_sections: [],
unavailable_sections: [],
excluded: [],
dropped_events: {
scripts: 0,
network_requests: 0,
console_events: 0,
websocket_connections: 0,
websocket_frames: 0,
webmcp_tools: 0,
timeline_events: 0,
total: 0,
},
};
}
function exampleInspection() {
return {
schema_version: 2,
browser: {
product: "Chrome/1",
protocol_version: "1.3",
revision: "example",
user_agent: "Chrome",
js_version: "1",
},
target: {
target_id: "page-1",
type: "page",
title: "Example",
url: `${origin}/`,
origin,
attached: false,
},
capture_window: {
started_at: "2026-07-14T00:00:00.000Z",
ended_at: "2026-07-14T00:00:01.000Z",
observation_ms: 1_000,
},
completeness: exampleCompleteness(),
frames: [],
dom: { total_nodes: 0, nodes: [] },
accessibility: {
total_nodes: 0,
text_capture: {
status: "not_approved",
retained_bytes: 0,
excluded_fields: 0,
truncated_fields: 0,
},
nodes: [],
},
scripts: { total: 0, items: [] },
resources: [],
network: {
requests: [],
websocket_events: [],
coverage_started_at: "2026-07-14T00:00:00.000Z",
prior_activity_available: false,
},
console: {
events: [],
coverage_started_at: "2026-07-14T00:00:00.000Z",
prior_activity_available: false,
},
workers: [],
metadata: {
responses: [],
dom_urls: [],
agent_hints: [],
excluded_dom_urls: 0,
headers_allowlisted: true,
},
storage: {
origin,
usage_bytes: null,
quota_bytes: null,
local_storage_keys: [],
session_storage_keys: [],
indexed_db_names: [],
cache_names: [],
values_redacted: true,
},
limitations: [],
};
}
function exampleScreenshot() {
return {
uri: "rea://web-screenshot/sha256/153cf6c9a526a63053a37b10234c2fd85df38887c2dc0a800d90abfa6631d01c",
sha256: "153cf6c9a526a63053a37b10234c2fd85df38887c2dc0a800d90abfa6631d01c",
bytes: 70,
media_type: "image/png",
data_base64:
"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8/5+hHgAHggJ/PzWvWQAAAABJRU5ErkJggg==",
};
}
+84
View File
@@ -0,0 +1,84 @@
import type { ToolContract } from "./toolContracts.js";
import { evidenceSchema } from "../domain/evidence.js";
import {
electronPageInspectionSchema,
electronTargetListSchema,
inspectElectronPageInputSchema,
listElectronTargetsInputSchema,
} from "../domain/electronObservation.js";
const listOutputSchema = evidenceSchema
.omit({ normalized_result: true })
.extend({ normalized_result: electronTargetListSchema });
const inspectionOutputSchema = evidenceSchema
.omit({ normalized_result: true })
.extend({ normalized_result: electronPageInspectionSchema });
const endpoint = "http://127.0.0.1:9223";
const root = "/Applications/Example.app/Contents/Resources";
/** Root-confined Electron file-page discovery and inspection contracts. */
export const ELECTRON_TOOL_CONTRACTS = [
{
name: "list_electron_targets",
description:
"List Electron file:// page targets from an approved user-owned loopback CDP endpoint. Every path is canonicalized and must remain contained by an approved filesystem root, including after symlink resolution.",
kind: "electron-provider",
inputSchema: listElectronTargetsInputSchema,
outputSchema: listOutputSchema,
annotations: {
readOnlyHint: true,
destructiveHint: false,
idempotentHint: true,
openWorldHint: true,
},
examples: [
{
title: "List approved Electron file pages",
input: {
cdp_endpoint: endpoint,
allowed_file_roots: [root],
approved: true,
offset: 0,
limit: 100,
},
},
],
},
{
name: "inspect_electron_page",
description:
"Passively inspect one approved Electron file page through CDP. Returns root-confined frames, DOM structure, resource paths, and scripts without evaluating renderer JavaScript or invoking Electron APIs; script contents require separate approval.",
kind: "electron-provider",
inputSchema: inspectElectronPageInputSchema,
outputSchema: inspectionOutputSchema,
annotations: {
readOnlyHint: true,
destructiveHint: false,
idempotentHint: true,
openWorldHint: true,
},
examples: [
{
title: "Inspect an approved Electron page",
input: {
cdp_endpoint: endpoint,
allowed_file_roots: [root],
target_id: "TARGET_ID_FROM_LIST_ELECTRON_TARGETS",
approved: true,
observation_ms: 100,
include_script_sources: false,
source_capture_approved: false,
limits: {
max_frames: 200,
max_dom_nodes: 2_000,
max_scripts: 500,
max_resources: 2_000,
max_script_source_bytes: 1_048_576,
max_total_script_source_bytes: 4_194_304,
},
},
},
],
},
] as const satisfies readonly ToolContract[];
+1
View File
@@ -23,6 +23,7 @@ export type ToolKind =
| "native-provider"
| "artifact-provider"
| "browser-provider"
| "electron-provider"
| "session";
/** Single source of truth for a public MCP tool. */
+2
View File
@@ -27,6 +27,7 @@ import { TOOL_EXAMPLE_OVERRIDES } from "./toolContractExamples.js";
import { NATIVE_TOOL_CONTRACTS } from "./nativeToolContracts.js";
import { ARTIFACT_TOOL_CONTRACTS } from "./artifactToolContracts.js";
import { BROWSER_TOOL_CONTRACTS } from "./browserToolContracts.js";
import { ELECTRON_TOOL_CONTRACTS } from "./electronToolContracts.js";
import {
closeBinaryInputSchema,
openBinaryInputSchema,
@@ -520,5 +521,6 @@ export const TOOL_CONTRACTS = [
...NATIVE_TOOL_CONTRACTS,
...ARTIFACT_TOOL_CONTRACTS,
...BROWSER_TOOL_CONTRACTS,
...ELECTRON_TOOL_CONTRACTS,
...SESSION_TOOL_CONTRACTS,
] as const;
+142
View File
@@ -0,0 +1,142 @@
import { z } from "zod";
/** Stable browser sections used by coverage and omission accounting. */
const browserSectionSchema = z.enum([
"frames",
"dom",
"accessibility",
"scripts",
"script_sources",
"resources",
"network_requests",
"network_initiators",
"console_events",
"console_text",
"websocket_connections",
"websocket_frames",
"websocket_shapes",
"json_body_shapes",
"workers",
"storage",
"storage_keys",
"metadata",
"webmcp_tools",
"screenshot",
"source_maps",
"timeline",
]);
export type BrowserSection = z.infer<typeof browserSectionSchema>;
/** Why an eligible browser observation was not retained. */
const browserExclusionReasonSchema = z.enum([
"disallowed_origin",
"unsupported_url",
"unattributed_origin",
"not_approved",
"out_of_target_scope",
"provider_unavailable",
"invalid_protocol_value",
]);
export type BrowserExclusionReason = z.infer<
typeof browserExclusionReasonSchema
>;
const browserExclusionSchema = z.object({
section: browserSectionSchema,
reason: browserExclusionReasonSchema,
count: z.number().int().min(0).nullable(),
});
export type BrowserExclusion = z.infer<typeof browserExclusionSchema>;
const browserDroppedEventsSchema = z.object({
scripts: z.number().int().min(0),
network_requests: z.number().int().min(0),
console_events: z.number().int().min(0),
websocket_connections: z.number().int().min(0),
websocket_frames: z.number().int().min(0),
webmcp_tools: z.number().int().min(0),
timeline_events: z.number().int().min(0),
total: z.number().int().min(0),
});
export type BrowserDroppedEvents = z.infer<typeof browserDroppedEventsSchema>;
export type BrowserDroppedEventKind = Exclude<
keyof BrowserDroppedEvents,
"total"
>;
const browserCompletenessConditionSchema = z.enum([
"complete_within_window",
"policy_filtered",
"attach_limited",
"truncated",
]);
/** Explicit, non-lossy coverage semantics for a bounded browser observation. */
export const browserCompletenessSchema = z.object({
status: browserCompletenessConditionSchema,
conditions: z.array(browserCompletenessConditionSchema).min(1).max(3),
policy_filtered_sections: z.array(browserSectionSchema),
attach_limited_sections: z.array(browserSectionSchema),
truncated_sections: z.array(browserSectionSchema),
unavailable_sections: z.array(browserSectionSchema),
excluded: z.array(browserExclusionSchema),
dropped_events: browserDroppedEventsSchema,
});
export type BrowserCompleteness = z.infer<typeof browserCompletenessSchema>;
/** Build canonical completeness output from one adapter-owned capture ledger. */
export const classifyBrowserCompleteness = (input: {
readonly policyFilteredSections: ReadonlySet<BrowserSection>;
readonly attachLimitedSections: ReadonlySet<BrowserSection>;
readonly truncatedSections: ReadonlySet<BrowserSection>;
readonly unavailableSections: ReadonlySet<BrowserSection>;
readonly excluded: readonly BrowserExclusion[];
readonly droppedEvents: Omit<BrowserDroppedEvents, "total">;
}): BrowserCompleteness => {
const policyFiltered = input.policyFilteredSections.size > 0;
const truncated =
input.truncatedSections.size > 0 ||
Object.values(input.droppedEvents).some((count) => count > 0);
const attachLimited =
input.attachLimitedSections.size > 0 || input.unavailableSections.size > 0;
const status = truncated
? "truncated"
: policyFiltered
? "policy_filtered"
: attachLimited
? "attach_limited"
: "complete_within_window";
const conditions = [
...(attachLimited ? (["attach_limited"] as const) : []),
...(policyFiltered ? (["policy_filtered"] as const) : []),
...(truncated ? (["truncated"] as const) : []),
];
const droppedTotal = Object.values(input.droppedEvents).reduce(
(sum, count) => sum + count,
0,
);
return browserCompletenessSchema.parse({
status,
conditions:
conditions.length === 0 ? ["complete_within_window"] : conditions,
policy_filtered_sections: sortedSections(input.policyFilteredSections),
attach_limited_sections: sortedSections(input.attachLimitedSections),
truncated_sections: sortedSections(input.truncatedSections),
unavailable_sections: sortedSections(input.unavailableSections),
excluded: canonicalExclusions(input.excluded),
dropped_events: { ...input.droppedEvents, total: droppedTotal },
});
};
const sortedSections = (
sections: ReadonlySet<BrowserSection>,
): BrowserSection[] => [...sections].sort();
const canonicalExclusions = (
exclusions: readonly BrowserExclusion[],
): BrowserExclusion[] =>
[...exclusions].sort(
(left, right) =>
left.section.localeCompare(right.section) ||
left.reason.localeCompare(right.reason),
);
+257 -12
View File
@@ -1,5 +1,9 @@
import { z } from "zod";
import { browserCompletenessSchema } from "./browserCompleteness.js";
import { webTextArtifactSchema } from "./webContentArtifact.js";
import { jsonShapeSchema } from "./jsonShape.js";
const parseExactOrigin = (value: string): string | undefined => {
let url: URL;
try {
@@ -78,7 +82,7 @@ export const browserEndpointSchema = z
return url.origin;
});
const allowedOriginsSchema = z
export const browserAllowedOriginsSchema = z
.array(browserOriginSchema)
.min(1)
.max(32)
@@ -86,7 +90,7 @@ const allowedOriginsSchema = z
const approvedBrowserInput = {
cdp_endpoint: browserEndpointSchema,
allowed_origins: allowedOriginsSchema,
allowed_origins: browserAllowedOriginsSchema,
approved: z
.literal(true)
.describe("Explicit approval for browser observation"),
@@ -103,6 +107,18 @@ const browserInspectionLimitsSchema = z.object({
max_frames: z.number().int().min(1).max(1_000).default(200),
max_dom_nodes: z.number().int().min(1).max(10_000).default(2_000),
max_ax_nodes: z.number().int().min(1).max(10_000).default(2_000),
max_ax_text_field_bytes: z
.number()
.int()
.min(1)
.max(16 * 1_024)
.default(1_024),
max_total_ax_text_bytes: z
.number()
.int()
.min(1)
.max(1_024 * 1_024)
.default(64 * 1_024),
max_scripts: z.number().int().min(1).max(1_000).default(200),
max_resources: z.number().int().min(1).max(10_000).default(2_000),
max_workers: z.number().int().min(1).max(5_000).default(500),
@@ -121,13 +137,53 @@ const browserInspectionLimitsSchema = z.object({
.default(4 * 1_024 * 1_024),
max_network_events: z.number().int().min(1).max(10_000).default(1_000),
max_console_events: z.number().int().min(1).max(2_000).default(200),
max_console_text_field_bytes: z
.number()
.int()
.min(1)
.max(16 * 1_024)
.default(1_024),
max_total_console_text_bytes: z
.number()
.int()
.min(1)
.max(1_024 * 1_024)
.default(64 * 1_024),
max_json_body_bytes: z
.number()
.int()
.min(1)
.max(4 * 1_024 * 1_024)
.default(1_024 * 1_024),
max_total_json_body_bytes: z
.number()
.int()
.min(1)
.max(16 * 1_024 * 1_024)
.default(4 * 1_024 * 1_024),
max_json_shape_nodes: z.number().int().min(1).max(100_000).default(5_000),
max_json_shape_depth: z.number().int().min(1).max(100).default(20),
max_websocket_events: z.number().int().min(1).max(5_000).default(500),
max_websocket_shape_bytes: z
.number()
.int()
.min(1)
.max(1_024 * 1_024)
.default(64 * 1_024),
max_total_websocket_shape_bytes: z
.number()
.int()
.min(1)
.max(16 * 1_024 * 1_024)
.default(1_024 * 1_024),
});
const DEFAULT_BROWSER_INSPECTION_LIMITS = {
max_frames: 200,
max_dom_nodes: 2_000,
max_ax_nodes: 2_000,
max_ax_text_field_bytes: 1_024,
max_total_ax_text_bytes: 64 * 1_024,
max_scripts: 200,
max_resources: 2_000,
max_workers: 500,
@@ -136,7 +192,15 @@ const DEFAULT_BROWSER_INSPECTION_LIMITS = {
max_total_script_source_bytes: 4 * 1_024 * 1_024,
max_network_events: 1_000,
max_console_events: 200,
max_console_text_field_bytes: 1_024,
max_total_console_text_bytes: 64 * 1_024,
max_json_body_bytes: 1_024 * 1_024,
max_total_json_body_bytes: 4 * 1_024 * 1_024,
max_json_shape_nodes: 5_000,
max_json_shape_depth: 20,
max_websocket_events: 500,
max_websocket_shape_bytes: 64 * 1_024,
max_total_websocket_shape_bytes: 1_024 * 1_024,
} as const;
/** Public input for one passive, bounded inspection of an existing page. */
@@ -145,6 +209,13 @@ export const inspectWebPageInputSchema = z
...approvedBrowserInput,
target_id: z.string().trim().min(1).max(256),
observation_ms: z.number().int().min(0).max(10_000).default(500),
include_accessibility_text: z.boolean().default(false),
include_console_text: z.boolean().default(false),
console_text_approved: z.boolean().default(false),
include_json_body_shapes: z.boolean().default(false),
json_body_schema_approved: z.boolean().default(false),
include_websocket_shapes: z.boolean().default(false),
websocket_shape_approved: z.boolean().default(false),
include_script_sources: z.boolean().default(false),
include_storage_keys: z.boolean().default(false),
limits: browserInspectionLimitsSchema.default(
@@ -161,6 +232,66 @@ export const inspectWebPageInputSchema = z
path: ["limits", "max_script_source_bytes"],
message: "Per-script source limit cannot exceed the total source limit",
});
for (const [include, approved, path, message] of [
[
input.include_console_text,
input.console_text_approved,
"console_text_approved",
"Console text capture requires separate approval",
],
[
input.include_json_body_shapes,
input.json_body_schema_approved,
"json_body_schema_approved",
"JSON body schema capture requires separate approval",
],
[
input.include_websocket_shapes,
input.websocket_shape_approved,
"websocket_shape_approved",
"WebSocket shape capture requires separate approval",
],
] as const)
if (include && !approved)
context.addIssue({ code: "custom", path: [path], message });
if (
input.limits.max_console_text_field_bytes >
input.limits.max_total_console_text_bytes
)
context.addIssue({
code: "custom",
path: ["limits", "max_console_text_field_bytes"],
message:
"Per-field console text limit cannot exceed the total text limit",
});
if (
input.limits.max_json_body_bytes > input.limits.max_total_json_body_bytes
)
context.addIssue({
code: "custom",
path: ["limits", "max_json_body_bytes"],
message: "Per-body JSON limit cannot exceed the total body limit",
});
if (
input.limits.max_websocket_shape_bytes >
input.limits.max_total_websocket_shape_bytes
)
context.addIssue({
code: "custom",
path: ["limits", "max_websocket_shape_bytes"],
message:
"Per-frame WebSocket limit cannot exceed the total frame limit",
});
if (
input.limits.max_ax_text_field_bytes >
input.limits.max_total_ax_text_bytes
)
context.addIssue({
code: "custom",
path: ["limits", "max_ax_text_field_bytes"],
message:
"Per-field accessibility text limit cannot exceed the total text limit",
});
});
export type ListBrowserTargetsInput = z.infer<
@@ -280,13 +411,22 @@ const browserScriptSourceSchema = z.discriminatedUnion("included", [
z.object({ included: z.literal(false), reason: z.string() }),
z.object({
included: z.literal(true),
sha256: z.string().regex(/^[a-f0-9]{64}$/u),
bytes: z.number().int().min(0),
content: z.string(),
artifact: webTextArtifactSchema,
}),
]);
const browserResourceReconciliationSchema = z.discriminatedUnion("status", [
z.object({ status: z.literal("exact"), resource_key: z.string() }),
z.object({
status: z.literal("ambiguous"),
candidate_resource_keys: z.array(z.string()),
}),
z.object({
status: z.literal("unmatched"),
reason: z.literal("no_exact_sanitized_url"),
}),
]);
const browserScriptSchema = z.object({
script_id: z.string(),
script_key: z.string().regex(/^scr_[a-f0-9]{64}(?:_[0-9]+)?$/u),
url: z.string(),
origin: z.string().nullable(),
cdp_hash: z.string(),
@@ -294,9 +434,11 @@ const browserScriptSchema = z.object({
is_module: z.boolean(),
language: z.string().nullable(),
source_map_url: z.string().nullable(),
resource_reconciliation: browserResourceReconciliationSchema,
source: browserScriptSourceSchema,
});
const browserResourceSchema = z.object({
resource_key: z.string().regex(/^res_[a-f0-9]{64}$/u),
url: z.string(),
origin: z.string().nullable(),
type: z.string(),
@@ -318,6 +460,17 @@ const browserNetworkRequestSchema = z.object({
line: z.number().int().min(0).nullable(),
column: z.number().int().min(0).nullable(),
}),
body_shapes: z.object({
status: z.enum([
"not_approved",
"included",
"partial",
"unavailable",
"truncated",
]),
request: jsonShapeSchema.nullable(),
response: jsonShapeSchema.nullable(),
}),
});
const browserConsoleEventSchema = z.object({
type: z.string(),
@@ -326,12 +479,31 @@ const browserConsoleEventSchema = z.object({
url: z.string().nullable(),
line: z.number().int().min(0).nullable(),
column: z.number().int().min(0).nullable(),
text_capture: z.object({
status: z.enum(["not_approved", "included", "truncated"]),
values: z.array(
z.object({
argument_index: z.number().int().min(0),
type: z.string(),
text: z.string(),
}),
),
retained_bytes: z.number().int().min(0),
truncated_values: z.number().int().min(0),
}),
});
const browserWebSocketEventSchema = z.object({
request_id: z.string(),
direction: z.enum(["sent", "received"]),
opcode: z.number().int().min(0),
payload_bytes: z.number().int().min(0),
payload_shape: z
.object({
format: z.enum(["json", "text", "binary"]),
json_shape: jsonShapeSchema.nullable(),
truncated: z.boolean(),
})
.nullable(),
});
const browserWorkerSchema = z.object({
target_id: z.string(),
@@ -340,10 +512,80 @@ const browserWorkerSchema = z.object({
origin: z.string().nullable(),
attached: z.boolean(),
});
const browserCspSourceSchema = z.object({
kind: z.enum([
"keyword",
"scheme",
"approved_origin",
"external_origin",
"other",
]),
value: z.string().nullable(),
});
const browserLinkMetadataSchema = z.object({
href: z.string().nullable(),
destination_scope: z.enum(["approved", "outside_policy", "unsupported"]),
rel: z.array(z.string()),
as: z.string().nullable(),
type: z.string().nullable(),
crossorigin: z.string().nullable(),
});
const browserAgentHintSchema = z.object({
mechanism: z.enum([
"link_rel",
"dom_link_rel",
"well_known_resource",
"response_header",
]),
declaration: z.string(),
url: z.string().nullable(),
trust: z.literal("page-declared-untrusted"),
});
const browserSafeMetadataSchema = z.object({
responses: z.array(
z.object({
request_id: z.string(),
url: z.string(),
mime_type: z.string().nullable(),
content_length: z.number().int().min(0).nullable(),
content_encoding: z.string().nullable(),
csp: z.object({
directives: z.array(
z.object({
name: z.string(),
sources: z.array(browserCspSourceSchema),
}),
),
nonce_count: z.number().int().min(0),
hash_count: z.number().int().min(0),
}),
links: z.array(browserLinkMetadataSchema),
policies: z.object({
coop: z.string().nullable(),
coep: z.string().nullable(),
corp: z.string().nullable(),
referrer_policy: z.string().nullable(),
x_content_type_options: z.string().nullable(),
permissions_policy_features: z.array(z.string()),
}),
}),
),
dom_urls: z.array(
z.object({
node_index: z.number().int().min(0),
attribute: z.enum(["href", "src", "action", "formaction", "poster"]),
url: z.string().nullable(),
destination_scope: z.enum(["approved", "outside_policy", "unsupported"]),
}),
),
agent_hints: z.array(browserAgentHintSchema),
excluded_dom_urls: z.number().int().min(0),
headers_allowlisted: z.literal(true),
});
/** Provider-neutral normalized result for one passive web-page observation. */
export const webPageInspectionSchema = z.object({
schema_version: z.literal(1),
schema_version: z.literal(2),
browser: browserVersionSchema,
target: browserTargetSchema,
capture_window: z.object({
@@ -351,11 +593,7 @@ export const webPageInspectionSchema = z.object({
ended_at: z.iso.datetime(),
observation_ms: z.number().int().min(0),
}),
completeness: z.object({
status: z.enum(["complete", "truncated"]),
truncated_sections: z.array(z.string()),
dropped_events: z.number().int().min(0),
}),
completeness: browserCompletenessSchema,
frames: z.array(browserFrameSchema),
dom: z.object({
total_nodes: z.number().int().min(0),
@@ -363,6 +601,12 @@ export const webPageInspectionSchema = z.object({
}),
accessibility: z.object({
total_nodes: z.number().int().min(0),
text_capture: z.object({
status: z.enum(["not_approved", "included", "truncated", "unavailable"]),
retained_bytes: z.number().int().min(0),
excluded_fields: z.number().int().min(0),
truncated_fields: z.number().int().min(0),
}),
nodes: z.array(browserAxNodeSchema),
}),
scripts: z.object({
@@ -382,6 +626,7 @@ export const webPageInspectionSchema = z.object({
prior_activity_available: z.literal(false),
}),
workers: z.array(browserWorkerSchema),
metadata: browserSafeMetadataSchema,
storage: z.object({
origin: z.string(),
usage_bytes: z.number().min(0).nullable(),
+74
View File
@@ -0,0 +1,74 @@
import { z } from "zod";
import {
browserAllowedOriginsSchema,
browserEndpointSchema,
} from "./browserObservation.js";
import { browserCompletenessSchema } from "./browserCompleteness.js";
/** Input for a bounded, navigation-aware browser observation window. */
export const observeWebSessionInputSchema = z.object({
cdp_endpoint: browserEndpointSchema,
allowed_origins: browserAllowedOriginsSchema,
target_id: z.string().trim().min(1).max(256),
approved: z.literal(true),
observation_ms: z.number().int().min(1).max(60_000).default(10_000),
max_timeline_events: z.number().int().min(1).max(20_000).default(2_000),
});
export type ObserveWebSessionInput = z.infer<
typeof observeWebSessionInputSchema
>;
const timelineEventSchema = z.object({
sequence: z.number().int().min(1),
type: z.enum([
"navigation_requested",
"navigation_committed",
"same_origin_reload",
"same_document_navigation",
"redirect",
"load_failed",
"lifecycle",
"target_terminated",
]),
timestamp: z.number().min(0),
frame_id: z.string().nullable(),
loader_id: z.string().nullable(),
request_id: z.string().nullable(),
url: z.string().nullable(),
destination_scope: z
.enum(["approved", "outside_policy", "unsupported"])
.nullable(),
detail: z.string().nullable(),
});
/** Navigation-aware session result for external user actions. */
export const webObservationSessionSchema = z.object({
schema_version: z.literal(1),
browser: z.object({
product: z.string(),
protocol_version: z.string(),
revision: z.string(),
user_agent: z.string(),
js_version: z.string(),
}),
target: z.object({
target_id: z.string(),
initial_url: z.string(),
final_url: z.string().nullable(),
}),
window: z.object({
armed_at: z.iso.datetime(),
ended_at: z.iso.datetime(),
requested_ms: z.number().int().min(1),
end_reason: z.enum([
"window_elapsed",
"target_left_scope",
"target_terminated",
]),
}),
timeline: z.array(timelineEventSchema),
completeness: browserCompletenessSchema,
limitations: z.array(z.string()),
});
export type WebObservationSession = z.infer<typeof webObservationSessionSchema>;
+194
View File
@@ -0,0 +1,194 @@
import { isAbsolute } from "node:path";
import { z } from "zod";
import { browserCompletenessSchema } from "./browserCompleteness.js";
import { browserEndpointSchema } from "./browserObservation.js";
import { webTextArtifactSchema } from "./webContentArtifact.js";
/** Absolute operator-approved filesystem roots for Electron file pages. */
export const electronFileRootsSchema = z
.array(
z
.string()
.min(1)
.max(16_384)
.refine(isAbsolute, "Electron file roots must be absolute paths"),
)
.min(1)
.max(32)
.transform((roots) => [...new Set(roots)].sort());
const approvedElectronInput = {
cdp_endpoint: browserEndpointSchema,
allowed_file_roots: electronFileRootsSchema,
approved: z.literal(true),
};
/** Input for listing root-confined file:// page targets from Electron CDP. */
export const listElectronTargetsInputSchema = z.object({
...approvedElectronInput,
offset: z.number().int().min(0).default(0),
limit: z.number().int().min(1).max(200).default(100),
});
export type ListElectronTargetsInput = z.infer<
typeof listElectronTargetsInputSchema
>;
/** Input for passive structural inspection of one Electron file page. */
export const inspectElectronPageInputSchema = z
.object({
...approvedElectronInput,
target_id: z.string().trim().min(1).max(256),
observation_ms: z.number().int().min(0).max(10_000).default(100),
include_script_sources: z.boolean().default(false),
source_capture_approved: z.boolean().default(false),
limits: z
.object({
max_frames: z.number().int().min(1).max(1_000).default(200),
max_dom_nodes: z.number().int().min(1).max(10_000).default(2_000),
max_scripts: z.number().int().min(1).max(2_000).default(500),
max_resources: z.number().int().min(1).max(10_000).default(2_000),
max_script_source_bytes: z
.number()
.int()
.min(1)
.max(4 * 1_024 * 1_024)
.default(1_024 * 1_024),
max_total_script_source_bytes: z
.number()
.int()
.min(1)
.max(16 * 1_024 * 1_024)
.default(4 * 1_024 * 1_024),
})
.default({
max_frames: 200,
max_dom_nodes: 2_000,
max_scripts: 500,
max_resources: 2_000,
max_script_source_bytes: 1_024 * 1_024,
max_total_script_source_bytes: 4 * 1_024 * 1_024,
}),
})
.superRefine((input, context) => {
if (input.include_script_sources && !input.source_capture_approved)
context.addIssue({
code: "custom",
path: ["source_capture_approved"],
message: "Electron script source capture requires separate approval",
});
if (
input.limits.max_script_source_bytes >
input.limits.max_total_script_source_bytes
)
context.addIssue({
code: "custom",
path: ["limits", "max_script_source_bytes"],
message: "Per-script source limit cannot exceed the total source limit",
});
});
export type InspectElectronPageInput = z.infer<
typeof inspectElectronPageInputSchema
>;
const browserVersionSchema = z.object({
product: z.string(),
protocol_version: z.string(),
revision: z.string(),
user_agent: z.string(),
js_version: z.string(),
});
const electronTargetSchema = z.object({
target_id: z.string(),
type: z.string(),
title: z.string(),
file_path: z.string(),
attached: z.boolean(),
});
/** Root-filtered Electron file target inventory. */
export const electronTargetListSchema = z.object({
schema_version: z.literal(1),
browser: browserVersionSchema,
targets: z.object({
items: z.array(electronTargetSchema),
offset: z.number().int().min(0),
limit: z.number().int().min(1),
total: z.number().int().min(0),
next_offset: z.number().int().min(0).nullable(),
has_more: z.boolean(),
}),
excluded: z.object({
outside_root: z.number().int().min(0),
unsupported_url: z.number().int().min(0),
non_page: z.number().int().min(0),
}),
limitations: z.array(z.string()),
});
export type ElectronTargetList = z.infer<typeof electronTargetListSchema>;
const electronSourceSchema = z.discriminatedUnion("included", [
z.object({ included: z.literal(false), reason: z.string() }),
z.object({ included: z.literal(true), artifact: webTextArtifactSchema }),
]);
/** Provider-neutral passive Electron file-page structure and script inventory. */
export const electronPageInspectionSchema = z.object({
schema_version: z.literal(1),
browser: browserVersionSchema,
target: electronTargetSchema,
capture_window: z.object({
started_at: z.iso.datetime(),
ended_at: z.iso.datetime(),
observation_ms: z.number().int().min(0),
}),
completeness: browserCompletenessSchema,
frames: z.array(
z.object({
frame_id: z.string(),
parent_frame_id: z.string().nullable(),
file_path: z.string(),
}),
),
dom: z.object({
total_nodes: z.number().int().min(0),
nodes: z.array(
z.object({
index: z.number().int().min(0),
parent_index: z.number().int().min(-1),
node_type: z.number().int().min(0),
node_name: z.string(),
node_value_length: z.number().int().min(0),
attribute_names: z.array(z.string()),
}),
),
}),
scripts: z.object({
total: z.number().int().min(0),
items: z.array(
z.object({
script_key: z.string().regex(/^electron_script_[a-f0-9]{64}$/u),
file_path: z.string(),
cdp_hash: z.string(),
length: z.number().int().min(0),
is_module: z.boolean(),
language: z.string().nullable(),
source: electronSourceSchema,
}),
),
}),
resources: z.array(
z.object({
resource_key: z.string().regex(/^electron_resource_[a-f0-9]{64}$/u),
file_path: z.string(),
type: z.string(),
mime_type: z.string(),
content_size: z.number().min(0).nullable(),
}),
),
limitations: z.array(z.string()),
});
export type ElectronPageInspection = z.infer<
typeof electronPageInspectionSchema
>;
+14 -1
View File
@@ -144,12 +144,25 @@ export class ProviderAdapterError extends AnalysisError {
}
}
/** Public browser and Electron operations that can fail at a CDP boundary. */
export type BrowserObservationOperation =
| "list_browser_targets"
| "inspect_web_page"
| "analyze_web_bundle"
| "observe_web_session"
| "discover_webmcp_tools"
| "compare_web_captures"
| "capture_web_screenshot"
| "compare_web_screenshots"
| "list_electron_targets"
| "inspect_electron_page";
/** A bounded passive browser observation failed at its CDP boundary. */
export class BrowserObservationError extends AnalysisError {
readonly _tag = "BrowserObservationError";
constructor(
readonly operation: "list_browser_targets" | "inspect_web_page",
readonly operation: BrowserObservationOperation,
readonly reason:
| "endpoint_unreachable"
| "invalid_endpoint_response"
+151
View File
@@ -0,0 +1,151 @@
import { z } from "zod";
const jsonValueTypeSchema = z.enum([
"object",
"array",
"string",
"number",
"boolean",
"null",
]);
type JsonValueType = z.infer<typeof jsonValueTypeSchema>;
/** Value-free structural summary of one approved JSON payload. */
export const jsonShapeSchema = z.object({
root_type: jsonValueTypeSchema,
node_count: z.number().int().min(1),
max_depth_observed: z.number().int().min(0),
properties: z.array(
z.object({
path: z.string(),
types: z.array(jsonValueTypeSchema).min(1),
observations: z.number().int().min(1),
}),
),
truncated: z.boolean(),
});
export type JsonShape = z.infer<typeof jsonShapeSchema>;
export interface JsonShapeLimits {
readonly maximumBytes: number;
readonly maximumNodes: number;
readonly maximumDepth: number;
}
/** Parse approved JSON and immediately discard values after iterative shape inference. */
export const inferJsonShape = (
text: string,
limits: JsonShapeLimits,
): JsonShape | null => {
if (Buffer.byteLength(text) > limits.maximumBytes) return null;
let root: unknown;
try {
root = JSON.parse(text);
} catch {
return null;
}
const properties = new Map<
string,
{ readonly types: Set<JsonValueType>; observations: number }
>();
const pending: Array<{
readonly value: unknown;
readonly path: string;
readonly depth: number;
}> = [{ value: root, path: "", depth: 0 }];
let nodeCount = 0;
let maxDepthObserved = 0;
let truncated = false;
while (pending.length > 0) {
const current = pending.pop();
if (current === undefined) break;
if (nodeCount >= limits.maximumNodes) {
truncated = true;
break;
}
nodeCount += 1;
maxDepthObserved = Math.max(maxDepthObserved, current.depth);
if (current.depth >= limits.maximumDepth) {
if (hasChildren(current.value)) truncated = true;
continue;
}
if (Array.isArray(current.value)) {
const capacity = Math.max(
0,
limits.maximumNodes - nodeCount - pending.length,
);
const retained = Math.min(current.value.length, capacity);
if (retained < current.value.length) truncated = true;
for (let index = retained - 1; index >= 0; index -= 1)
pending.push({
value: current.value[index],
path: `${current.path}/*`,
depth: current.depth + 1,
});
continue;
}
if (!isRecord(current.value)) continue;
const allEntries = Object.entries(current.value);
const capacity = Math.max(
0,
limits.maximumNodes - nodeCount - pending.length,
);
const entries = allEntries.slice(0, capacity);
if (entries.length < allEntries.length) truncated = true;
for (let index = entries.length - 1; index >= 0; index -= 1) {
const entry = entries[index];
if (entry === undefined) continue;
const [name, value] = entry;
const path = `${current.path}/${pointerName(name)}`.slice(0, 2_048);
const type = jsonValueType(value);
const existing = properties.get(path);
if (existing === undefined)
properties.set(path, { types: new Set([type]), observations: 1 });
else {
existing.types.add(type);
existing.observations += 1;
}
pending.push({ value, path, depth: current.depth + 1 });
}
}
return jsonShapeSchema.parse({
root_type: jsonValueType(root),
node_count: nodeCount,
max_depth_observed: maxDepthObserved,
properties: [...properties.entries()]
.map(([path, value]) => ({
path,
types: [...value.types].sort(),
observations: value.observations,
}))
.sort((left, right) => left.path.localeCompare(right.path))
.slice(0, limits.maximumNodes),
truncated,
});
};
const jsonValueType = (value: unknown): JsonValueType => {
if (value === null) return "null";
if (Array.isArray(value)) return "array";
switch (typeof value) {
case "string":
return "string";
case "number":
return "number";
case "boolean":
return "boolean";
default:
return "object";
}
};
const hasChildren = (value: unknown): boolean =>
Array.isArray(value)
? value.length > 0
: isRecord(value) && Object.keys(value).length > 0;
const isRecord = (value: unknown): value is Readonly<Record<string, unknown>> =>
typeof value === "object" && value !== null && !Array.isArray(value);
const pointerName = (value: string): string =>
value.slice(0, 256).replaceAll("~", "~0").replaceAll("/", "~1");
+1
View File
@@ -6,6 +6,7 @@ import { err, ok, type Result } from "./result.js";
export type PermissionCapability =
| "process_capture"
| "browser_observe"
| "electron_observe"
| "evidence_read"
| "evidence_write"
| "investigation_input"
+228
View File
@@ -0,0 +1,228 @@
import { z } from "zod";
import { inspectWebPageInputSchema } from "./browserObservation.js";
import { webTextArtifactSchema } from "./webContentArtifact.js";
const webBundleLimitsSchema = z.object({
max_findings: z.number().int().min(1).max(10_000).default(1_000),
max_ast_nodes: z.number().int().min(1).max(2_000_000).default(250_000),
max_source_maps: z.number().int().min(1).max(1_000).default(100),
max_source_map_bytes: z
.number()
.int()
.min(1)
.max(16 * 1_024 * 1_024)
.default(4 * 1_024 * 1_024),
max_total_source_map_bytes: z
.number()
.int()
.min(1)
.max(64 * 1_024 * 1_024)
.default(16 * 1_024 * 1_024),
max_source_map_mappings: z.number().int().min(1).max(100_000).default(10_000),
max_original_sources: z.number().int().min(1).max(20_000).default(2_000),
});
/** Capture-and-analyze input with separate source and source-map approvals. */
export const analyzeWebBundleInputSchema = inspectWebPageInputSchema
.safeExtend({
include_script_sources: z.literal(true).default(true),
source_capture_approved: z.literal(true),
fetch_source_maps: z.boolean().default(false),
source_map_fetch_approved: z.boolean().default(false),
analysis_limits: webBundleLimitsSchema.default({
max_findings: 1_000,
max_ast_nodes: 250_000,
max_source_maps: 100,
max_source_map_bytes: 4 * 1_024 * 1_024,
max_total_source_map_bytes: 16 * 1_024 * 1_024,
max_source_map_mappings: 10_000,
max_original_sources: 2_000,
}),
})
.superRefine((input, context) => {
if (input.fetch_source_maps && !input.source_map_fetch_approved)
context.addIssue({
code: "custom",
path: ["source_map_fetch_approved"],
message: "Source-map network fetch requires separate approval",
});
if (
input.analysis_limits.max_source_map_bytes >
input.analysis_limits.max_total_source_map_bytes
)
context.addIssue({
code: "custom",
path: ["analysis_limits", "max_source_map_bytes"],
message:
"Per-map source-map byte limit cannot exceed the aggregate limit",
});
});
export type AnalyzeWebBundleInput = z.infer<typeof analyzeWebBundleInputSchema>;
const sourceLocationSchema = z.object({
script_key: z.string(),
line: z.number().int().min(1).nullable(),
column: z.number().int().min(0).nullable(),
});
const basisSchema = z.object({
script_key: z.string(),
artifact_sha256: z.string().regex(/^[a-f0-9]{64}$/u),
line: z.number().int().min(1).nullable(),
column: z.number().int().min(0).nullable(),
detector: z.string(),
});
const findingSchema = z.object({
value: z.string(),
mechanism: z.string(),
location: sourceLocationSchema,
});
const sourceMapSchema = z.object({
script_key: z.string(),
declared_url: z.string(),
status: z.enum([
"included",
"fetch_failed",
"invalid",
"policy_filtered",
"truncated",
]),
artifact: webTextArtifactSchema.nullable(),
original_sources: z.array(
z.object({
source: z.string(),
artifact: webTextArtifactSchema.nullable(),
}),
),
original_module_edges: z.array(
z.object({
from_source: z.string(),
kind: z.enum(["static_import", "dynamic_import", "require"]),
specifier: z.string(),
resolved_source: z.string().nullable(),
}),
),
mappings: z.array(
z.object({
generated_line: z.number().int().min(1),
generated_column: z.number().int().min(0),
source: z.string(),
original_line: z.number().int().min(1),
original_column: z.number().int().min(0),
name: z.string().nullable(),
}),
),
limitation: z.string().nullable(),
});
const webTextArtifactSummarySchema = z.object({
uri: z.string().regex(/^rea:\/\/web-content\/sha256\/[a-f0-9]{64}$/u),
sha256: z.string().regex(/^[a-f0-9]{64}$/u),
bytes: z.number().int().min(0),
media_type: z.string().min(1).max(256),
charset: z.literal("utf-8"),
text_available: z.literal(true),
});
const sourceMapsSchema = z
.object({
status: z.enum([
"not_requested",
"included",
"partial",
"unavailable",
"truncated",
]),
requested: z.number().int().min(0),
processed: z.number().int().min(0),
dropped: z.number().int().min(0),
dropped_script_keys: z.array(z.string()),
items: z.array(sourceMapSchema),
})
.superRefine((sourceMaps, context) => {
if (
sourceMaps.requested !== sourceMaps.processed + sourceMaps.dropped ||
sourceMaps.processed !== sourceMaps.items.length ||
sourceMaps.dropped !== sourceMaps.dropped_script_keys.length
)
context.addIssue({
code: "custom",
message: "Source-map coverage counts are inconsistent",
});
});
/** Provider-neutral result of bounded JavaScript bundle reverse engineering. */
export const webBundleAnalysisSchema = z.object({
schema_version: z.literal(1),
capture: z.object({
target_url: z.string(),
scripts_observed: z.number().int().min(0),
scripts_analyzed: z.number().int().min(0),
source_artifacts: z.array(webTextArtifactSummarySchema),
}),
observations: z.object({
chunks: z.object({
nodes: z.array(
z.object({
script_key: z.string(),
url: z.string(),
artifact_sha256: z.string().regex(/^[a-f0-9]{64}$/u),
bytes: z.number().int().min(0),
}),
),
edges: z.array(
z.object({
from_script_key: z.string(),
kind: z.enum([
"static_import",
"dynamic_import",
"require",
"worker_import",
]),
specifier: z.string(),
resolved_url: z.string().nullable(),
location: sourceLocationSchema,
}),
),
}),
routes: z.array(findingSchema),
endpoints: z.array(findingSchema),
webmcp_declarations: z.array(
z.object({
name: z.string().nullable(),
description: z.string().nullable(),
schema_property_names: z.array(z.string()),
trust: z.literal("page-declared-untrusted"),
location: sourceLocationSchema,
}),
),
source_maps: sourceMapsSchema,
}),
inferences: z.array(
z.object({
kind: z.enum(["vendor_fingerprint", "route_framework", "bundle_runtime"]),
value: z.string(),
confidence: z.enum(["low", "medium", "high"]),
basis: z.array(basisSchema).min(1),
}),
),
unknowns: z.array(
z.object({
dimension: z.string(),
reason: z.string(),
affected_script_keys: z.array(z.string()),
}),
),
completeness: z.object({
status: z.enum(["complete_within_limits", "truncated", "partial"]),
parsed_scripts: z.number().int().min(0),
parse_failures: z.number().int().min(0),
visited_ast_nodes: z.number().int().min(0),
dropped_findings: z.number().int().min(0),
}),
limitations: z.array(z.string()),
});
export type WebBundleAnalysis = z.infer<typeof webBundleAnalysisSchema>;
+640
View File
@@ -0,0 +1,640 @@
import { parse } from "@babel/parser";
import * as t from "@babel/types";
import type { WebPageInspection } from "./browserObservation.js";
import { sanitizeBrowserUrl } from "./browserObservation.js";
import {
webBundleAnalysisSchema,
type AnalyzeWebBundleInput,
type WebBundleAnalysis,
} from "./webBundleAnalysis.js";
type BundleObservations = WebBundleAnalysis["observations"];
type ChunkEdge = BundleObservations["chunks"]["edges"][number];
type Finding = BundleObservations["routes"][number];
type WebMcpDeclaration = BundleObservations["webmcp_declarations"][number];
type Inference = WebBundleAnalysis["inferences"][number];
type BrowserScript = WebPageInspection["scripts"]["items"][number];
type IncludedSource = Extract<BrowserScript["source"], { included: true }>;
type IncludedScript = Omit<BrowserScript, "source"> & {
readonly source: IncludedSource;
};
interface AnalysisAccumulator {
readonly edges: ChunkEdge[];
readonly routes: Finding[];
readonly endpoints: Finding[];
readonly webMcp: WebMcpDeclaration[];
readonly inferences: Inference[];
readonly seen: Set<string>;
visitedNodes: number;
parsedScripts: number;
parseFailures: number;
droppedFindings: number;
astLimitReached: boolean;
}
/** Analyze captured, explicitly approved JavaScript source without execution. */
export const analyzeCapturedWebBundle = (
inspection: WebPageInspection,
input: AnalyzeWebBundleInput,
sourceMaps: BundleObservations["source_maps"] = {
status: "not_requested",
requested: 0,
processed: 0,
dropped: 0,
dropped_script_keys: [],
items: [],
},
): WebBundleAnalysis => {
const accumulator = emptyAccumulator();
const sourceScripts = inspection.scripts.items.filter(isIncludedScript);
for (const script of sourceScripts) analyzeScript(script, input, accumulator);
const unavailable = inspection.scripts.items
.filter((script) => !script.source.included)
.map(({ script_key }) => script_key);
const sourceMapIncomplete =
sourceMaps.status === "partial" || sourceMaps.status === "unavailable";
const partial =
accumulator.parseFailures > 0 ||
unavailable.length > 0 ||
sourceMapIncomplete;
const truncated =
accumulator.astLimitReached ||
accumulator.droppedFindings > 0 ||
sourceMaps.status === "truncated";
return webBundleAnalysisSchema.parse({
schema_version: 1,
capture: {
target_url: inspection.target.url,
scripts_observed: inspection.scripts.total,
scripts_analyzed: sourceScripts.length,
source_artifacts: sourceScripts.map((script) => {
if (!script.source.included)
throw new TypeError("Filtered source changed");
const { text: _text, ...artifact } = script.source.artifact;
return { ...artifact, text_available: true };
}),
},
observations: {
chunks: {
nodes: sourceScripts.map((script) => {
if (!script.source.included)
throw new TypeError("Filtered source changed");
return {
script_key: script.script_key,
url: script.url,
artifact_sha256: script.source.artifact.sha256,
bytes: script.source.artifact.bytes,
};
}),
edges: accumulator.edges,
},
routes: accumulator.routes,
endpoints: accumulator.endpoints,
webmcp_declarations: accumulator.webMcp,
source_maps: sourceMaps,
},
inferences: accumulator.inferences,
unknowns: [
...(unavailable.length === 0
? []
: [
{
dimension: "script_source",
reason: "Source artifact was not captured within approved limits",
affected_script_keys: unavailable,
},
]),
...(accumulator.parseFailures === 0
? []
: [
{
dimension: "javascript_ast",
reason: "One or more source artifacts could not be parsed",
affected_script_keys: sourceScripts.map(
({ script_key }) => script_key,
),
},
]),
...(sourceMaps.status === "not_requested" ||
sourceMaps.status === "included"
? []
: [
{
dimension: "source_maps",
reason:
sourceMaps.status === "truncated"
? "Source-map evidence was truncated by approved limits"
: "One or more requested source maps were unavailable or incomplete",
affected_script_keys: [
...new Set([
...sourceMaps.items
.filter(({ status }) => status !== "included")
.map(({ script_key }) => script_key),
...sourceMaps.dropped_script_keys,
]),
].sort(),
},
]),
],
completeness: {
status: truncated
? "truncated"
: partial
? "partial"
: "complete_within_limits",
parsed_scripts: accumulator.parsedScripts,
parse_failures: accumulator.parseFailures,
visited_ast_nodes: accumulator.visitedNodes,
dropped_findings: accumulator.droppedFindings,
},
limitations: [
"Static bundle findings are observations or bounded inferences; REA does not execute captured JavaScript.",
"String-built routes and endpoints, encrypted configuration, and server-side behavior may remain unknown.",
"Page-declared WebMCP metadata is untrusted and is never registered or invoked as an REA tool.",
],
});
};
const analyzeScript = (
script: IncludedScript,
input: AnalyzeWebBundleInput,
accumulator: AnalysisAccumulator,
): void => {
let file: ReturnType<typeof parse>;
try {
file = parse(script.source.artifact.text, {
sourceType: "unambiguous",
errorRecovery: true,
plugins: ["jsx", "typescript"],
});
} catch {
accumulator.parseFailures += 1;
return;
}
accumulator.parsedScripts += 1;
detectVendorFingerprints(
script,
accumulator,
input.analysis_limits.max_findings,
);
t.traverseFast(file, (node) => {
accumulator.visitedNodes += 1;
if (accumulator.visitedNodes > input.analysis_limits.max_ast_nodes) {
accumulator.astLimitReached = true;
return t.traverseFast.stop;
}
inspectNode(script, node, accumulator, input.analysis_limits.max_findings);
return undefined;
});
};
const inspectNode = (
script: IncludedScript,
node: t.Node,
accumulator: AnalysisAccumulator,
maximumFindings: number,
): void => {
if (
(t.isImportDeclaration(node) || t.isExportAllDeclaration(node)) &&
node.source !== undefined
)
addEdge(
script,
node.source.value,
"static_import",
node,
accumulator,
maximumFindings,
);
else if (t.isExportNamedDeclaration(node) && node.source != null)
addEdge(
script,
node.source.value,
"static_import",
node,
accumulator,
maximumFindings,
);
else if (t.isImportExpression(node) && t.isStringLiteral(node.source))
addEdge(
script,
node.source.value,
"dynamic_import",
node,
accumulator,
maximumFindings,
);
if (t.isObjectProperty(node))
inspectRouteProperty(script, node, accumulator, maximumFindings);
if (t.isCallExpression(node) || t.isNewExpression(node))
inspectCall(script, node, accumulator, maximumFindings);
};
const inspectCall = (
script: IncludedScript,
node: t.CallExpression | t.NewExpression,
accumulator: AnalysisAccumulator,
maximumFindings: number,
): void => {
const name = calleeName(node.callee);
const first = stringArgument(node.arguments[0]);
if ((name === "require" || name.endsWith(".require")) && first !== undefined)
addEdge(script, first, "require", node, accumulator, maximumFindings);
if (
(name === "importScripts" || name.endsWith(".importScripts")) &&
first !== undefined
)
addEdge(script, first, "worker_import", node, accumulator, maximumFindings);
if (
routeCallNames.some(
(candidate) => name === candidate || name.endsWith(`.${candidate}`),
)
) {
if (first !== undefined)
addFinding(
accumulator.routes,
script,
first,
`call:${name}`,
node,
accumulator,
maximumFindings,
);
addRouteFrameworkInference(
script,
name,
node,
accumulator,
maximumFindings,
);
}
const endpoint = endpointArgument(name, node.arguments);
if (endpoint !== undefined)
addFinding(
accumulator.endpoints,
script,
endpoint,
`call:${name}`,
node,
accumulator,
maximumFindings,
);
if (
name.endsWith("modelContext.registerTool") ||
name === "modelContext.registerTool"
)
addWebMcpDeclaration(script, node, accumulator, maximumFindings);
};
const inspectRouteProperty = (
script: IncludedScript,
node: t.ObjectProperty,
accumulator: AnalysisAccumulator,
maximumFindings: number,
): void => {
const key = propertyName(node.key);
if ((key === "path" || key === "route") && t.isStringLiteral(node.value))
addFinding(
accumulator.routes,
script,
node.value.value,
`property:${key}`,
node,
accumulator,
maximumFindings,
);
};
const addEdge = (
script: IncludedScript,
specifier: string,
kind: ChunkEdge["kind"],
node: t.Node,
accumulator: AnalysisAccumulator,
maximumFindings: number,
): void => {
const bounded = specifier.slice(0, 4_096);
const key = `edge\0${script.script_key}\0${kind}\0${bounded}`;
addBounded(accumulator, key, maximumFindings, () =>
accumulator.edges.push({
from_script_key: script.script_key,
kind,
specifier: bounded,
resolved_url: resolveSpecifier(bounded, script.url),
location: location(script.script_key, node),
}),
);
};
const addFinding = (
collection: Finding[],
script: IncludedScript,
rawValue: string,
mechanism: string,
node: t.Node,
accumulator: AnalysisAccumulator,
maximumFindings: number,
): void => {
const value = sanitizeCandidate(rawValue);
const key = `finding\0${mechanism}\0${script.script_key}\0${value}`;
addBounded(accumulator, key, maximumFindings, () =>
collection.push({
value,
mechanism,
location: location(script.script_key, node),
}),
);
};
const addWebMcpDeclaration = (
script: IncludedScript,
node: t.CallExpression | t.NewExpression,
accumulator: AnalysisAccumulator,
maximumFindings: number,
): void => {
const declaration = node.arguments[0];
if (!t.isObjectExpression(declaration)) return;
const name = objectString(declaration, "name");
const description = objectString(declaration, "description");
const schema =
objectValue(declaration, "inputSchema") ??
objectValue(declaration, "input_schema");
const declaredProperties = t.isObjectExpression(schema)
? objectValue(schema, "properties")
: undefined;
const propertyObject = t.isObjectExpression(declaredProperties)
? declaredProperties
: schema;
const schemaPropertyNames = t.isObjectExpression(propertyObject)
? propertyObject.properties.flatMap((property) =>
t.isObjectProperty(property)
? [propertyName(property.key)].filter(Boolean)
: [],
)
: [];
const key = `webmcp\0${script.script_key}\0${name ?? ""}\0${schemaPropertyNames.join("\0")}`;
addBounded(accumulator, key, maximumFindings, () =>
accumulator.webMcp.push({
name: name?.slice(0, 256) ?? null,
description: description?.slice(0, 2_048) ?? null,
schema_property_names: [...new Set(schemaPropertyNames)]
.sort()
.slice(0, 256),
trust: "page-declared-untrusted",
location: location(script.script_key, node),
}),
);
};
const detectVendorFingerprints = (
script: IncludedScript,
accumulator: AnalysisAccumulator,
maximumFindings: number,
): void => {
const text = script.source.artifact.text;
for (const detector of vendorDetectors) {
if (!detector.patterns.some((pattern) => text.includes(pattern))) continue;
const key = `vendor\0${script.script_key}\0${detector.value}`;
addBounded(accumulator, key, maximumFindings, () =>
accumulator.inferences.push({
kind: detector.kind,
value: detector.value,
confidence: detector.confidence,
basis: [basis(script, detector.detector)],
}),
);
}
};
const addRouteFrameworkInference = (
script: IncludedScript,
name: string,
node: t.Node,
accumulator: AnalysisAccumulator,
maximumFindings: number,
): void => {
const value =
name.endsWith("useRoutes") || name.endsWith("createBrowserRouter")
? "React Router-compatible route API"
: "Generic route registration API";
const key = `route-framework\0${script.script_key}\0${value}`;
addBounded(accumulator, key, maximumFindings, () =>
accumulator.inferences.push({
kind: "route_framework",
value,
confidence: "medium",
basis: [{ ...basis(script, `call:${name}`), ...locationFields(node) }],
}),
);
};
const addBounded = (
accumulator: AnalysisAccumulator,
key: string,
maximum: number,
add: () => void,
): void => {
if (accumulator.seen.has(key)) return;
const findings =
accumulator.edges.length +
accumulator.routes.length +
accumulator.endpoints.length +
accumulator.webMcp.length +
accumulator.inferences.length;
if (findings >= maximum) {
accumulator.droppedFindings += 1;
return;
}
accumulator.seen.add(key);
add();
};
const endpointArgument = (
name: string,
args: readonly (
| t.Expression
| t.SpreadElement
| t.JSXNamespacedName
| t.ArgumentPlaceholder
)[],
): string | undefined => {
if (name === "fetch" || name.endsWith(".fetch") || name === "WebSocket")
return stringArgument(args[0]);
if (name.endsWith(".open") && stringArgument(args[1]) !== undefined)
return stringArgument(args[1]);
if (
["get", "post", "put", "patch", "delete", "request"].some(
(method) => name === method || name.endsWith(`.${method}`),
)
)
return stringArgument(args[0]);
return undefined;
};
const calleeName = (callee: t.Node): string => {
if (t.isIdentifier(callee)) return callee.name;
if (t.isImport(callee)) return "import";
if (t.isMemberExpression(callee) || t.isOptionalMemberExpression(callee)) {
const object = t.isExpression(callee.object)
? calleeName(callee.object)
: "";
const property = propertyName(callee.property);
return object === "" ? property : `${object}.${property}`;
}
return "";
};
const stringArgument = (
value: t.Node | null | undefined,
): string | undefined => (t.isStringLiteral(value) ? value.value : undefined);
const propertyName = (value: t.Node): string => {
if (t.isIdentifier(value)) return value.name;
if (t.isStringLiteral(value)) return value.value;
return "";
};
const objectValue = (
object: t.ObjectExpression,
name: string,
): t.ObjectProperty["value"] | undefined => {
for (const property of object.properties)
if (t.isObjectProperty(property) && propertyName(property.key) === name)
return property.value;
return undefined;
};
const objectString = (
object: t.ObjectExpression,
name: string,
): string | undefined => {
const value = objectValue(object, name);
return t.isStringLiteral(value) ? value.value : undefined;
};
const resolveSpecifier = (specifier: string, base: string): string | null => {
try {
const resolved = new URL(specifier, base);
if (resolved.protocol !== "http:" && resolved.protocol !== "https:")
return null;
return sanitizeBrowserUrl(resolved.href).url;
} catch {
return null;
}
};
const sanitizeCandidate = (value: string): string => {
const bounded = value.slice(0, 4_096);
try {
const parsed = new URL(bounded, "https://rea.invalid");
const sanitized = sanitizeBrowserUrl(parsed.href).url;
return parsed.origin === "https://rea.invalid"
? sanitized.replace("https://rea.invalid", "")
: sanitized;
} catch {
return bounded.split("#", 1)[0]?.split("?", 1)[0] ?? "";
}
};
const location = (scriptKey: string, node: t.Node) => ({
script_key: scriptKey,
...locationFields(node),
});
const locationFields = (node: t.Node) => ({
line: node.loc?.start.line ?? null,
column: node.loc?.start.column ?? null,
});
const basis = (script: IncludedScript, detector: string) => ({
script_key: script.script_key,
artifact_sha256: script.source.artifact.sha256,
line: null,
column: null,
detector,
});
const emptyAccumulator = (): AnalysisAccumulator => ({
edges: [],
routes: [],
endpoints: [],
webMcp: [],
inferences: [],
seen: new Set(),
visitedNodes: 0,
parsedScripts: 0,
parseFailures: 0,
droppedFindings: 0,
astLimitReached: false,
});
const isIncludedScript = (script: BrowserScript): script is IncludedScript =>
script.source.included;
const routeCallNames = [
"route",
"addRoute",
"useRoutes",
"createBrowserRouter",
"createHashRouter",
] as const;
const vendorDetectors: readonly {
readonly value: string;
readonly detector: string;
readonly patterns: readonly string[];
readonly confidence: Inference["confidence"];
readonly kind: Inference["kind"];
}[] = [
{
value: "webpack",
detector: "webpack-runtime",
patterns: ["__webpack_require__"],
confidence: "high",
kind: "bundle_runtime",
},
{
value: "Vite",
detector: "vite-runtime",
patterns: ["__vite__", "import.meta.hot"],
confidence: "medium",
kind: "bundle_runtime",
},
{
value: "React",
detector: "react-runtime",
patterns: ["__REACT_DEVTOOLS_GLOBAL_HOOK__", "React.createElement"],
confidence: "medium",
kind: "vendor_fingerprint",
},
{
value: "Vue",
detector: "vue-runtime",
patterns: ["__VUE__", "createApp("],
confidence: "medium",
kind: "vendor_fingerprint",
},
{
value: "Next.js",
detector: "next-runtime",
patterns: ["__NEXT_DATA__", "/_next/"],
confidence: "high",
kind: "vendor_fingerprint",
},
{
value: "Angular",
detector: "angular-runtime",
patterns: ["ɵɵdefineComponent"],
confidence: "high",
kind: "vendor_fingerprint",
},
{
value: "Svelte",
detector: "svelte-runtime",
patterns: ["svelte/internal"],
confidence: "medium",
kind: "vendor_fingerprint",
},
];
+286
View File
@@ -0,0 +1,286 @@
import { createHash } from "node:crypto";
import canonicalize from "canonicalize";
import { z } from "zod";
import {
webPageInspectionSchema,
type WebPageInspection,
} from "./browserObservation.js";
import {
webMcpDiscoverySchema,
type WebMcpDiscovery,
} from "./webMcpDiscovery.js";
const captureSnapshotSchema = z.object({
inspection: webPageInspectionSchema,
webmcp: webMcpDiscoverySchema.nullable().default(null),
});
/** Input for deterministic comparison of two normalized web captures. */
export const compareWebCapturesInputSchema = z.object({
before: captureSnapshotSchema,
after: captureSnapshotSchema,
max_changes: z.number().int().min(1).max(20_000).default(2_000),
});
export type CompareWebCapturesInput = z.infer<
typeof compareWebCapturesInputSchema
>;
const changeSchema = z.object({
identity: z.string(),
change: z.enum(["added", "removed", "modified"]),
});
const dimensionSchema = z.object({
status: z.enum(["changed", "unchanged", "unknown"]),
total_changes: z.number().int().min(0),
changes: z.array(changeSchema),
omitted_changes: z.number().int().min(0),
reason: z.string().nullable(),
});
/** Completeness-aware changes across stable browser evidence dimensions. */
export const webCaptureDiffSchema = z.object({
schema_version: z.literal(1),
overall_status: z.enum(["changed", "unchanged", "unknown"]),
before_target: z.object({ target_id: z.string(), url: z.string() }),
after_target: z.object({ target_id: z.string(), url: z.string() }),
dimensions: z.object({
dom_structure: dimensionSchema,
scripts: dimensionSchema,
resources: dimensionSchema,
network: dimensionSchema,
metadata: dimensionSchema,
webmcp: dimensionSchema,
}),
limitations: z.array(z.string()),
});
export type WebCaptureDiff = z.infer<typeof webCaptureDiffSchema>;
type Dimension =
WebCaptureDiff["dimensions"][keyof WebCaptureDiff["dimensions"]];
type Change = Dimension["changes"][number];
/** Compare normalized observations without treating incomplete absence as proof. */
export const compareWebCaptures = (
input: CompareWebCapturesInput,
): WebCaptureDiff => {
let remaining = input.max_changes;
const dimension = (
before: ReadonlyMap<string, string>,
after: ReadonlyMap<string, string>,
complete: boolean,
reason: string,
): Dimension => {
const all = compareIdentities(before, after);
const retained = all.slice(0, remaining);
remaining -= retained.length;
return {
status: all.length > 0 ? "changed" : complete ? "unchanged" : "unknown",
total_changes: all.length,
changes: retained,
omitted_changes: all.length - retained.length,
reason: all.length === 0 && !complete ? reason : null,
};
};
const before = input.before.inspection;
const after = input.after.inspection;
const dimensions = {
dom_structure: dimension(
singleton("document", digest(domProjection(before))),
singleton("document", digest(domProjection(after))),
sectionsComplete(before, ["frames", "dom"]) &&
sectionsComplete(after, ["frames", "dom"]),
"DOM or frame capture was incomplete in at least one observation.",
),
scripts: dimension(
keyed(
before.scripts.items.map(scriptProjection),
(item) => item.script_key,
),
keyed(
after.scripts.items.map(scriptProjection),
(item) => item.script_key,
),
sectionsComplete(before, ["scripts"]) &&
sectionsComplete(after, ["scripts"]),
"Script inventory was incomplete in at least one observation.",
),
resources: dimension(
keyed(before.resources, (item) => item.resource_key),
keyed(after.resources, (item) => item.resource_key),
sectionsComplete(before, ["resources"]) &&
sectionsComplete(after, ["resources"]),
"Resource inventory was incomplete in at least one observation.",
),
network: dimension(
networkMap(before),
networkMap(after),
sectionsComplete(before, ["network_requests"]) &&
sectionsComplete(after, ["network_requests"]),
"Network capture is attach-window limited or incomplete.",
),
metadata: dimension(
singleton("metadata", digest(metadataProjection(before))),
singleton("metadata", digest(metadataProjection(after))),
sectionsComplete(before, ["metadata"]) &&
sectionsComplete(after, ["metadata"]),
"Safe metadata capture was incomplete in at least one observation.",
),
webmcp: dimension(
webMcpMap(input.before.webmcp),
webMcpMap(input.after.webmcp),
webMcpComplete(input.before.webmcp) && webMcpComplete(input.after.webmcp),
"WebMCP discovery was unavailable or incomplete in at least one capture.",
),
};
const statuses = Object.values(dimensions).map(({ status }) => status);
return webCaptureDiffSchema.parse({
schema_version: 1,
overall_status: statuses.includes("changed")
? "changed"
: statuses.includes("unknown")
? "unknown"
: "unchanged",
before_target: {
target_id: before.target.target_id,
url: before.target.url,
},
after_target: {
target_id: after.target.target_id,
url: after.target.url,
},
dimensions,
limitations: [
"A changed status proves an observed difference; an unknown status means absence could not be established from capture completeness.",
"Network comparison covers only activity observed after each CDP attachment.",
],
});
};
const compareIdentities = (
before: ReadonlyMap<string, string>,
after: ReadonlyMap<string, string>,
): Change[] => {
const changes: Change[] = [];
for (const [identity, fingerprint] of before) {
const next = after.get(identity);
if (next === undefined) changes.push({ identity, change: "removed" });
else if (next !== fingerprint)
changes.push({ identity, change: "modified" });
}
for (const identity of after.keys())
if (!before.has(identity)) changes.push({ identity, change: "added" });
return changes.sort(
(left, right) =>
left.identity.localeCompare(right.identity) ||
left.change.localeCompare(right.change),
);
};
const keyed = <T>(
values: readonly T[],
identity: (value: T) => string,
): ReadonlyMap<string, string> =>
new Map(values.map((value) => [identity(value), digest(value)]));
const networkMap = (
inspection: WebPageInspection,
): ReadonlyMap<string, string> => {
const grouped = new Map<string, unknown[]>();
for (const request of inspection.network.requests) {
const identity = `net_${digest({
method: request.method,
url: request.url,
resource_type: request.resource_type,
})}`;
const values = grouped.get(identity) ?? [];
values.push({
status: request.status,
mime_type: request.mime_type,
encoded_data_length: request.encoded_data_length,
initiator: request.initiator,
body_shapes: request.body_shapes,
});
grouped.set(identity, values);
}
return new Map(
[...grouped].map(([identity, values]) => [
identity,
digest(values.map((value) => digest(value)).sort()),
]),
);
};
const webMcpMap = (
discovery: WebMcpDiscovery | null,
): ReadonlyMap<string, string> =>
discovery === null
? new Map()
: keyed(discovery.tools.items, (tool) => tool.tool_key);
const webMcpComplete = (discovery: WebMcpDiscovery | null): boolean =>
discovery !== null &&
discovery.status === "available" &&
!incompleteSections(discovery.completeness).has("webmcp_tools");
const sectionsComplete = (
inspection: WebPageInspection,
sections: readonly string[],
): boolean => {
const incomplete = incompleteSections(inspection.completeness);
return sections.every((section) => !incomplete.has(section));
};
const incompleteSections = (completeness: {
readonly policy_filtered_sections: readonly string[];
readonly attach_limited_sections: readonly string[];
readonly truncated_sections: readonly string[];
readonly unavailable_sections: readonly string[];
}): ReadonlySet<string> =>
new Set([
...completeness.policy_filtered_sections,
...completeness.attach_limited_sections,
...completeness.truncated_sections,
...completeness.unavailable_sections,
]);
const domProjection = (inspection: WebPageInspection) => ({
frames: inspection.frames
.map(({ url, origin }) => ({ url, origin }))
.sort((left, right) => left.url.localeCompare(right.url)),
nodes: inspection.dom.nodes.map(({ index: _index, ...node }) => node),
});
const scriptProjection = (
script: WebPageInspection["scripts"]["items"][number],
) => ({
script_key: script.script_key,
url: script.url,
cdp_hash: script.cdp_hash,
length: script.length,
is_module: script.is_module,
language: script.language,
source_map_url: script.source_map_url,
});
const metadataProjection = (inspection: WebPageInspection) => ({
responses: inspection.metadata.responses
.map(({ request_id: _requestId, ...response }) => response)
.map((value) => digest(value))
.sort(),
dom_urls: inspection.metadata.dom_urls.map((value) => digest(value)).sort(),
agent_hints: inspection.metadata.agent_hints
.map((value) => digest(value))
.sort(),
excluded_dom_urls: inspection.metadata.excluded_dom_urls,
headers_allowlisted: inspection.metadata.headers_allowlisted,
});
const singleton = (key: string, value: string): ReadonlyMap<string, string> =>
new Map([[key, value]]);
const digest = (value: unknown): string => {
const encoded = canonicalize(value);
if (encoded === undefined) throw new TypeError("Expected canonical JSON");
return createHash("sha256").update(encoded).digest("hex");
};
+56
View File
@@ -0,0 +1,56 @@
import { createHash } from "node:crypto";
import { z } from "zod";
/** Self-verifying text artifact used by web-source analysis boundaries. */
export const webTextArtifactSchema = z
.object({
uri: z.string().regex(/^rea:\/\/web-content\/sha256\/[a-f0-9]{64}$/u),
sha256: z.string().regex(/^[a-f0-9]{64}$/u),
bytes: z.number().int().min(0),
media_type: z.string().min(1).max(256),
charset: z.literal("utf-8"),
text: z.string(),
})
.superRefine((artifact, context) => {
const bytes = Buffer.byteLength(artifact.text);
const digest = createHash("sha256").update(artifact.text).digest("hex");
if (artifact.bytes !== bytes)
context.addIssue({
code: "custom",
path: ["bytes"],
message: "Byte count mismatch",
});
if (artifact.sha256 !== digest)
context.addIssue({
code: "custom",
path: ["sha256"],
message: "Digest mismatch",
});
if (artifact.uri !== webContentUri(digest))
context.addIssue({
code: "custom",
path: ["uri"],
message: "Content URI mismatch",
});
});
export type WebTextArtifact = z.infer<typeof webTextArtifactSchema>;
/** Build an immutable content-addressed UTF-8 text artifact. */
export const createWebTextArtifact = (
text: string,
mediaType: string,
): WebTextArtifact => {
const sha256 = createHash("sha256").update(text).digest("hex");
return {
uri: webContentUri(sha256),
sha256,
bytes: Buffer.byteLength(text),
media_type: mediaType,
charset: "utf-8",
text,
};
};
const webContentUri = (sha256: string): string =>
`rea://web-content/sha256/${sha256}`;
+120
View File
@@ -0,0 +1,120 @@
import { createHash } from "node:crypto";
import canonicalize from "canonicalize";
export interface WebInventoryResourceInput {
readonly url: string;
readonly origin: string | null;
readonly type: string;
readonly mime_type: string;
readonly content_size: number | null;
}
export interface WebInventoryScriptInput {
readonly url: string;
readonly cdp_hash: string;
readonly length: number;
readonly is_module: boolean;
readonly language: string | null;
readonly source_map_url: string | null;
}
export interface StableWebResource extends WebInventoryResourceInput {
readonly resource_key: string;
}
export type WebResourceReconciliation =
| { readonly status: "exact"; readonly resource_key: string }
| { readonly status: "ambiguous"; readonly candidate_resource_keys: string[] }
| { readonly status: "unmatched"; readonly reason: "no_exact_sanitized_url" };
/** Assign deterministic identities to a bounded resource inventory. */
export const stableWebResources = (
resources: readonly WebInventoryResourceInput[],
): StableWebResource[] => {
const keyed = resources.map((resource) => ({
...resource,
resource_key: `res_${digest(resource)}`,
}));
return [
...new Map(
keyed.map((resource) => [resource.resource_key, resource]),
).values(),
].sort(
(left, right) =>
left.resource_key.localeCompare(right.resource_key) ||
left.url.localeCompare(right.url),
);
};
/** Reconcile one script with resource-tree observations without fuzzy matching. */
export const reconcileWebScript = (
script: WebInventoryScriptInput,
resources: readonly StableWebResource[],
): WebResourceReconciliation => {
const candidates = resources.filter(
(resource) => resource.url === script.url,
);
if (candidates.length === 1)
return { status: "exact", resource_key: candidates[0]?.resource_key ?? "" };
if (candidates.length > 1)
return {
status: "ambiguous",
candidate_resource_keys: uniqueResourceKeys(candidates),
};
return { status: "unmatched", reason: "no_exact_sanitized_url" };
};
/** Prefer exact transient raw-URL matching, then report sanitized ambiguity. */
export const reconcileCapturedWebScript = (
script: WebInventoryScriptInput & { readonly rawUrl: string },
rawResources: readonly (WebInventoryResourceInput & {
readonly rawUrl: string;
})[],
resources: readonly StableWebResource[],
): WebResourceReconciliation => {
const exact = rawResources.filter(
(resource) => resource.rawUrl === script.rawUrl,
);
if (exact.length === 1) {
const resource = exact[0];
if (resource !== undefined) {
const { rawUrl: _rawUrl, ...publicResource } = resource;
const stable = stableWebResources([publicResource])[0];
if (stable !== undefined)
return { status: "exact", resource_key: stable.resource_key };
}
}
const candidates = resources.filter(
(resource) => resource.url === script.url,
);
if (candidates.length > 0)
return {
status: "ambiguous",
candidate_resource_keys: uniqueResourceKeys(candidates),
};
return { status: "unmatched", reason: "no_exact_sanitized_url" };
};
/** Stable script identity excludes target IDs, CDP script IDs, and timestamps. */
export const stableWebScriptKey = (script: WebInventoryScriptInput): string =>
`scr_${digest({
url: script.url,
cdp_hash: script.cdp_hash,
length: script.length,
is_module: script.is_module,
language: script.language,
source_map_url: script.source_map_url,
})}`;
const uniqueResourceKeys = (
resources: readonly StableWebResource[],
): string[] =>
[...new Set(resources.map(({ resource_key }) => resource_key))].sort();
const digest = (value: unknown): string => {
const encoded = canonicalize(value);
if (encoded === undefined)
throw new TypeError("Web inventory value is not canonical JSON");
return createHash("sha256").update(encoded).digest("hex");
};
+80
View File
@@ -0,0 +1,80 @@
import { z } from "zod";
import { browserCompletenessSchema } from "./browserCompleteness.js";
import {
browserAllowedOriginsSchema,
browserEndpointSchema,
} from "./browserObservation.js";
import { jsonShapeSchema } from "./jsonShape.js";
/** Input for passive discovery of page-registered WebMCP tools. */
export const discoverWebMcpToolsInputSchema = z.object({
cdp_endpoint: browserEndpointSchema,
allowed_origins: browserAllowedOriginsSchema,
target_id: z.string().trim().min(1).max(256),
approved: z.literal(true),
observation_ms: z.number().int().min(0).max(10_000).default(100),
max_tools: z.number().int().min(1).max(5_000).default(500),
max_schema_bytes: z
.number()
.int()
.min(1)
.max(1_024 * 1_024)
.default(256 * 1_024),
max_schema_nodes: z.number().int().min(1).max(100_000).default(5_000),
max_schema_depth: z.number().int().min(1).max(100).default(20),
});
export type DiscoverWebMcpToolsInput = z.infer<
typeof discoverWebMcpToolsInputSchema
>;
const browserVersionSchema = z.object({
product: z.string(),
protocol_version: z.string(),
revision: z.string(),
user_agent: z.string(),
js_version: z.string(),
});
const webMcpToolSchema = z.object({
tool_key: z.string().regex(/^webmcp_[a-f0-9]{64}$/u),
name: z.string(),
description: z.string(),
frame_id: z.string(),
frame_url: z.string(),
owner_origin: z.string(),
declaration_kind: z.enum(["declarative", "imperative"]),
input_schema_shape: jsonShapeSchema.nullable(),
annotations: z.object({
read_only: z.boolean().nullable(),
untrusted_content: z.boolean().nullable(),
autosubmit: z.boolean().nullable(),
}),
registration_source: z
.object({
url: z.string(),
line: z.number().int().min(0).nullable(),
column: z.number().int().min(0).nullable(),
})
.nullable(),
trust: z.literal("page-declared-untrusted"),
});
/** Passive WebMCP inventory; it intentionally has no invocation surface. */
export const webMcpDiscoverySchema = z.object({
schema_version: z.literal(1),
browser: browserVersionSchema,
target: z.object({
target_id: z.string(),
url: z.string(),
origin: z.string(),
}),
status: z.enum(["available", "unavailable"]),
tools: z.object({
total: z.number().int().min(0),
items: z.array(webMcpToolSchema),
}),
completeness: browserCompletenessSchema,
limitations: z.array(z.string()),
});
export type WebMcpDiscovery = z.infer<typeof webMcpDiscoverySchema>;
+142
View File
@@ -0,0 +1,142 @@
import { createHash } from "node:crypto";
import { z } from "zod";
import { browserCompletenessSchema } from "./browserCompleteness.js";
import {
browserAllowedOriginsSchema,
browserEndpointSchema,
} from "./browserObservation.js";
const MAX_SCREENSHOT_BYTES = 8 * 1_024 * 1_024;
/** Self-verifying inline PNG artifact for CLI/MCP parity. */
const webScreenshotArtifactSchema = z
.object({
uri: z.string().regex(/^rea:\/\/web-screenshot\/sha256\/[a-f0-9]{64}$/u),
sha256: z.string().regex(/^[a-f0-9]{64}$/u),
bytes: z.number().int().min(1).max(MAX_SCREENSHOT_BYTES),
media_type: z.literal("image/png"),
data_base64: z.string().max(Math.ceil((MAX_SCREENSHOT_BYTES * 4) / 3) + 4),
})
.superRefine((artifact, context) => {
const bytes = decodeCanonicalBase64(artifact.data_base64);
if (bytes === undefined) {
context.addIssue({ code: "custom", message: "Invalid canonical base64" });
return;
}
const sha256 = createHash("sha256").update(bytes).digest("hex");
if (
bytes.byteLength !== artifact.bytes ||
sha256 !== artifact.sha256 ||
artifact.uri !== `rea://web-screenshot/sha256/${sha256}`
)
context.addIssue({
code: "custom",
message: "Screenshot artifact digest or size mismatch",
});
});
export type WebScreenshotArtifact = z.infer<typeof webScreenshotArtifactSchema>;
/** Input for one read-only visible-viewport screenshot. */
export const captureWebScreenshotInputSchema = z.object({
cdp_endpoint: browserEndpointSchema,
allowed_origins: browserAllowedOriginsSchema,
target_id: z.string().trim().min(1).max(256),
approved: z.literal(true),
screenshot_approved: z.literal(true),
maximum_image_bytes: z
.number()
.int()
.min(1)
.max(MAX_SCREENSHOT_BYTES)
.default(4 * 1_024 * 1_024),
});
export type CaptureWebScreenshotInput = z.infer<
typeof captureWebScreenshotInputSchema
>;
const browserVersionSchema = z.object({
product: z.string(),
protocol_version: z.string(),
revision: z.string(),
user_agent: z.string(),
js_version: z.string(),
});
/** Bounded screenshot observation with embedded immutable artifact bytes. */
export const webScreenshotSchema = z.object({
schema_version: z.literal(1),
browser: browserVersionSchema,
target: z.object({
target_id: z.string(),
url: z.string(),
origin: z.string(),
}),
captured_at: z.iso.datetime(),
viewport: z.object({
width: z.number().int().min(1),
height: z.number().int().min(1),
}),
artifact: webScreenshotArtifactSchema,
completeness: browserCompletenessSchema,
limitations: z.array(z.string()),
});
export type WebScreenshot = z.infer<typeof webScreenshotSchema>;
/** Input for bounded local pixel comparison of two screenshot artifacts. */
export const compareWebScreenshotsInputSchema = z.object({
before: webScreenshotArtifactSchema,
after: webScreenshotArtifactSchema,
channel_threshold: z.number().int().min(0).max(255).default(0),
maximum_pixels: z.number().int().min(1).max(32_000_000).default(16_000_000),
});
export type CompareWebScreenshotsInput = z.infer<
typeof compareWebScreenshotsInputSchema
>;
/** Value-only visual difference metrics; no OCR or image mutation. */
export const webScreenshotDiffSchema = z.object({
schema_version: z.literal(1),
status: z.enum(["identical", "different", "dimension_mismatch"]),
before: z.object({
width: z.number().int().min(1),
height: z.number().int().min(1),
}),
after: z.object({
width: z.number().int().min(1),
height: z.number().int().min(1),
}),
channel_threshold: z.number().int().min(0).max(255),
compared_pixels: z.number().int().min(0),
changed_pixels: z.number().int().min(0).nullable(),
changed_ratio: z.number().min(0).max(1).nullable(),
maximum_channel_delta: z.number().int().min(0).max(255).nullable(),
mean_absolute_channel_delta: z.number().min(0).max(255).nullable(),
limitations: z.array(z.string()),
});
export type WebScreenshotDiff = z.infer<typeof webScreenshotDiffSchema>;
/** Create a content-addressed PNG artifact from already bounded bytes. */
export const createWebScreenshotArtifact = (
bytes: Buffer,
): WebScreenshotArtifact => {
const sha256 = createHash("sha256").update(bytes).digest("hex");
return webScreenshotArtifactSchema.parse({
uri: `rea://web-screenshot/sha256/${sha256}`,
sha256,
bytes: bytes.byteLength,
media_type: "image/png",
data_base64: bytes.toString("base64"),
});
};
/** Strict canonical base64 decoder used before digest validation. */
export const decodeCanonicalBase64 = (value: string): Buffer | undefined => {
if (value.length === 0 || value.length % 4 !== 0 || !BASE64.test(value))
return undefined;
const decoded = Buffer.from(value, "base64");
return decoded.toString("base64") === value ? decoded : undefined;
};
const BASE64 = /^(?:[A-Za-z\d+/]{4})*(?:[A-Za-z\d+/]{2}==|[A-Za-z\d+/]{3}=)?$/u;
+2 -2
View File
@@ -1,9 +1,9 @@
/** Generated from package.json and package-lock.json; do not edit. */
export const PACKAGE_METADATA = {
name: "rea-agents",
version: "1.4.0",
version: "1.5.0",
serverSdkVersion: "2.0.0-beta.4",
clientSdkVersion: "2.0.0-beta.4",
coreSdkVersion: "2.0.0-beta.4",
skillVersion: "13",
skillVersion: "14",
} as const;
+7
View File
@@ -16,6 +16,7 @@ import { projectAnalysisError } from "./domain/errors.js";
import { readProjectPermissionStore } from "./application/ProjectPermissionStore.js";
import { loadConfiguredPermissionAuthority } from "./application/PermissionConfiguration.js";
import { CdpBrowserProvider } from "./browser/CdpBrowserProvider.js";
import { CdpElectronProvider } from "./browser/CdpElectronProvider.js";
const MCP_CONNECTION_LOST =
"REA lost its MCP client connection. Restart REA from your MCP client.";
@@ -82,6 +83,7 @@ export const run = async (
const session = createBinarySession(config.value, logger);
const browserObservation = new CdpBrowserProvider();
const electronObservation = new CdpElectronProvider();
if (config.value.hopperTargetPath !== undefined) {
const opened = await session.open(config.value.hopperTargetPath, {
targetKind: config.value.hopperTargetKind,
@@ -129,6 +131,7 @@ export const run = async (
analysisSnapshotFilePolicy: runtimeSnapshotPolicy,
permissionAuthority: permissionAuthority.value,
browserObservation,
electronObservation,
artifactIntegrityContinueEnabled: () =>
currentConfig.artifactIntegrityContinueEnabled,
availabilityPolicy: () => ({
@@ -138,6 +141,10 @@ export const run = async (
currentConfig.browserObservationEnabled &&
currentConfig.browserCdpEndpoints.length > 0 &&
currentConfig.browserAllowedOrigins.length > 0,
electronObservationEnabled:
currentConfig.electronObservationEnabled &&
currentConfig.electronCdpEndpoints.length > 0 &&
currentConfig.electronFileRoots.length > 0,
}),
});
liveServers.add(server);
+10
View File
@@ -17,6 +17,8 @@ import { createServerIdentity } from "../serverIdentity.js";
import type { PermissionAuthority } from "../application/PermissionAuthority.js";
import type { BrowserObservationPort } from "../application/BrowserObservationPort.js";
import { registerBrowserTools } from "./registerBrowserTools.js";
import type { ElectronObservationPort } from "../application/ElectronObservationPort.js";
import { registerElectronTools } from "./registerElectronTools.js";
export interface CreateServerOptions {
readonly logger?: Logger;
@@ -26,11 +28,13 @@ export interface CreateServerOptions {
readonly analysisSnapshotFilePolicy?: EvidenceFilePolicy;
readonly permissionAuthority?: PermissionAuthority;
readonly browserObservation?: BrowserObservationPort;
readonly electronObservation?: ElectronObservationPort;
readonly artifactIntegrityContinueEnabled?: () => boolean;
readonly availabilityPolicy?: () => {
readonly processCaptureEnabled: boolean;
readonly evidenceFileRoots: number;
readonly browserObservationEnabled?: boolean;
readonly electronObservationEnabled?: boolean;
};
}
@@ -146,6 +150,12 @@ export const createServer = (
permissionAuthority: options.permissionAuthority,
recordEvidence,
});
registerElectronTools(server, {
logger: toolLogger,
electron: options.electronObservation,
permissionAuthority: options.permissionAuthority,
recordEvidence,
});
registerGuidedPrompts(server, analysis, session);
if (session !== undefined) {
registerEvidenceResources(server, session);
+144 -1
View File
@@ -4,14 +4,28 @@ import type { BrowserObservationPort } from "../application/BrowserObservationPo
import type { BinarySessionPort } from "../application/BinarySession.js";
import type { PermissionAuthority } from "../application/PermissionAuthority.js";
import {
analyzeWebBundle,
captureWebScreenshot,
compareWebCaptureEvidence,
compareWebScreenshotEvidence,
discoverWebMcpTools,
inspectWebPage,
listBrowserTargets,
observeWebSession,
} from "../application/BrowserObservationService.js";
import { BROWSER_TOOL_CONTRACTS } from "../contracts/browserToolContracts.js";
import {
inspectWebPageInputSchema,
listBrowserTargetsInputSchema,
} from "../domain/browserObservation.js";
import { analyzeWebBundleInputSchema } from "../domain/webBundleAnalysis.js";
import { observeWebSessionInputSchema } from "../domain/browserSession.js";
import { discoverWebMcpToolsInputSchema } from "../domain/webMcpDiscovery.js";
import { compareWebCapturesInputSchema } from "../domain/webCaptureDiff.js";
import {
captureWebScreenshotInputSchema,
compareWebScreenshotsInputSchema,
} from "../domain/webScreenshot.js";
import type { Logger } from "../logger.js";
import { logToolExecution } from "./toolLogging.js";
import { toCallToolResult } from "./toolResult.js";
@@ -30,7 +44,16 @@ export const registerBrowserTools = (
server: McpServer,
options: BrowserToolRegistration,
): void => {
const [listContract, inspectContract] = BROWSER_TOOL_CONTRACTS;
const [
listContract,
inspectContract,
analyzeContract,
sessionContract,
webMcpContract,
captureDiffContract,
screenshotContract,
screenshotDiffContract,
] = BROWSER_TOOL_CONTRACTS;
server.registerTool(
listContract.name,
toolRegistrationOptions(listContract),
@@ -69,6 +92,126 @@ export const registerBrowserTools = (
return evidenceResult(options, inspectContract, result.value);
},
);
server.registerTool(
analyzeContract.name,
toolRegistrationOptions(analyzeContract),
async (input, context) => {
const parsed = analyzeWebBundleInputSchema.parse(input);
const result = await logToolExecution(
options.logger,
analyzeContract.name,
() =>
analyzeWebBundle(
options.browser,
options.permissionAuthority,
parsed,
{
signal: context.mcpReq.signal,
progress: mcpProgressReporter(context),
},
),
);
if (!result.ok) return toCallToolResult(result, analyzeContract);
return evidenceResult(options, analyzeContract, result.value);
},
);
server.registerTool(
sessionContract.name,
toolRegistrationOptions(sessionContract),
async (input, context) => {
const parsed = observeWebSessionInputSchema.parse(input);
const result = await logToolExecution(
options.logger,
sessionContract.name,
() =>
observeWebSession(
options.browser,
options.permissionAuthority,
parsed,
{
signal: context.mcpReq.signal,
progress: mcpProgressReporter(context),
},
),
);
if (!result.ok) return toCallToolResult(result, sessionContract);
return evidenceResult(options, sessionContract, result.value);
},
);
server.registerTool(
webMcpContract.name,
toolRegistrationOptions(webMcpContract),
async (input, context) => {
const parsed = discoverWebMcpToolsInputSchema.parse(input);
const result = await logToolExecution(
options.logger,
webMcpContract.name,
() =>
discoverWebMcpTools(
options.browser,
options.permissionAuthority,
parsed,
{
signal: context.mcpReq.signal,
progress: mcpProgressReporter(context),
},
),
);
if (!result.ok) return toCallToolResult(result, webMcpContract);
return evidenceResult(options, webMcpContract, result.value);
},
);
server.registerTool(
captureDiffContract.name,
toolRegistrationOptions(captureDiffContract),
async (input) => {
const parsed = compareWebCapturesInputSchema.parse(input);
const result = await logToolExecution(
options.logger,
captureDiffContract.name,
() => compareWebCaptureEvidence(options.browser, parsed),
);
if (!result.ok) return toCallToolResult(result, captureDiffContract);
return evidenceResult(options, captureDiffContract, result.value);
},
);
server.registerTool(
screenshotContract.name,
toolRegistrationOptions(screenshotContract),
async (input, context) => {
const parsed = captureWebScreenshotInputSchema.parse(input);
const result = await logToolExecution(
options.logger,
screenshotContract.name,
() =>
captureWebScreenshot(
options.browser,
options.permissionAuthority,
parsed,
{
signal: context.mcpReq.signal,
progress: mcpProgressReporter(context),
},
),
);
if (!result.ok) return toCallToolResult(result, screenshotContract);
return evidenceResult(options, screenshotContract, result.value);
},
);
server.registerTool(
screenshotDiffContract.name,
toolRegistrationOptions(screenshotDiffContract),
async (input) => {
const parsed = compareWebScreenshotsInputSchema.parse(input);
const result = await logToolExecution(
options.logger,
screenshotDiffContract.name,
() => compareWebScreenshotEvidence(options.browser, parsed),
);
if (!result.ok) return toCallToolResult(result, screenshotDiffContract);
return evidenceResult(options, screenshotDiffContract, result.value);
},
);
};
const evidenceResult = (
+90
View File
@@ -0,0 +1,90 @@
import type { McpServer } from "@modelcontextprotocol/server";
import type { BinarySessionPort } from "../application/BinarySession.js";
import type { ElectronObservationPort } from "../application/ElectronObservationPort.js";
import {
inspectElectronPage,
listElectronTargets,
} from "../application/ElectronObservationService.js";
import type { PermissionAuthority } from "../application/PermissionAuthority.js";
import { ELECTRON_TOOL_CONTRACTS } from "../contracts/electronToolContracts.js";
import {
inspectElectronPageInputSchema,
listElectronTargetsInputSchema,
} from "../domain/electronObservation.js";
import type { Logger } from "../logger.js";
import { logToolExecution } from "./toolLogging.js";
import { toCallToolResult } from "./toolResult.js";
import { toolRegistrationOptions } from "./toolRegistrationOptions.js";
import { mcpProgressReporter } from "./mcpProgress.js";
interface ElectronToolRegistration {
readonly logger: Logger;
readonly electron: ElectronObservationPort | undefined;
readonly permissionAuthority: PermissionAuthority | undefined;
readonly recordEvidence: BinarySessionPort["recordEvidence"] | undefined;
}
/** Register Electron tools even when provider or permission policy is absent. */
export const registerElectronTools = (
server: McpServer,
options: ElectronToolRegistration,
): void => {
const [listContract, inspectContract] = ELECTRON_TOOL_CONTRACTS;
server.registerTool(
listContract.name,
toolRegistrationOptions(listContract),
async (input, context) => {
const parsed = listElectronTargetsInputSchema.parse(input);
const result = await logToolExecution(
options.logger,
listContract.name,
() =>
listElectronTargets(
options.electron,
options.permissionAuthority,
parsed,
{ signal: context.mcpReq.signal },
),
);
if (!result.ok) return toCallToolResult(result, listContract);
return evidenceResult(options, listContract, result.value);
},
);
server.registerTool(
inspectContract.name,
toolRegistrationOptions(inspectContract),
async (input, context) => {
const parsed = inspectElectronPageInputSchema.parse(input);
const result = await logToolExecution(
options.logger,
inspectContract.name,
() =>
inspectElectronPage(
options.electron,
options.permissionAuthority,
parsed,
{
signal: context.mcpReq.signal,
progress: mcpProgressReporter(context),
},
),
);
if (!result.ok) return toCallToolResult(result, inspectContract);
return evidenceResult(options, inspectContract, result.value);
},
);
};
const evidenceResult = (
options: ElectronToolRegistration,
contract: (typeof ELECTRON_TOOL_CONTRACTS)[number],
evidence: import("../domain/evidence.js").Evidence,
) => {
const recorded = options.recordEvidence?.(evidence);
return recorded !== undefined && !recorded.ok
? toCallToolResult(recorded, contract)
: toCallToolResult({ ok: true, value: evidence }, contract, {
evidenceResourcesAvailable: recorded !== undefined,
});
};
+1
View File
@@ -20,6 +20,7 @@ export const registerSessionStatusTool = (
readonly processCaptureEnabled: boolean;
readonly evidenceFileRoots: number;
readonly browserObservationEnabled?: boolean;
readonly electronObservationEnabled?: boolean;
},
): void => {
server.registerTool(
+3
View File
@@ -438,6 +438,7 @@ interface LifecycleToolRegistration {
readonly processCaptureEnabled: boolean;
readonly evidenceFileRoots: number;
readonly browserObservationEnabled?: boolean;
readonly electronObservationEnabled?: boolean;
};
readonly permissionAuthority?: PermissionAuthority;
}
@@ -599,6 +600,7 @@ export interface SessionToolOptions {
readonly processCaptureEnabled: boolean;
readonly evidenceFileRoots: number;
readonly browserObservationEnabled?: boolean;
readonly electronObservationEnabled?: boolean;
};
}
@@ -646,6 +648,7 @@ export const registerSessionTools = (
processCaptureEnabled: processPolicy.enabled,
evidenceFileRoots: evidenceFilePolicy.roots.length,
browserObservationEnabled: false,
electronObservationEnabled: false,
})),
...(options.permissionAuthority === undefined
? {}