feat(browser): trace captured website module imports (#879)

* feat(browser): trace captured website module imports

Compose verified exported scripts, inert native import syntax and a narrow
Chromium resolution port. Preserve query/fragment identity, import-map context,
all reported capture candidates and explicit unknown execution. Share named
CLI/MCP contracts and retain native reports, diagnostics and cleanup failures.

Add focused boundary regressions and a real module-loading oracle to the
existing conditional Chrome lane. No captured application code is executed
by module tracing, and no selected website asset is refetched.

Refs #851, #774

Assisted-by: Yukari (gensokyo-skills:yukari-boundary-analysis)
Assisted-by: Kogasa (gensokyo-skills:kogasa-surprise-testing)

* fix(browser): preserve module contexts and source bindings

Keep capture completeness inline, validate actual source text against its
digest, and separate unsupported URL contexts from malformed input.
Use the importer origin for native module resolution and the selected map
base independently, including mixed HTTP/HTTPS and escaped HTML attributes.

Refs #851, #774

Assisted-by: Yukari (gensokyo-skills:yukari-boundary-analysis)
Assisted-by: Kogasa (gensokyo-skills:kogasa-surprise-testing)
This commit is contained in:
ysopwnable
2026-10-07 14:59:52 +08:00
committed by GitHub
parent 507e8b3c7b
commit 01be082e9e
48 changed files with 2911 additions and 21 deletions
@@ -2,6 +2,35 @@ import { describe, expect, it } from "vitest";
import { buildCapabilityInventory } from "./CapabilityInventory.js";
it("reports module syntax and native resolution availability separately", () => {
const input = { open: false, capabilities: [] };
const unavailable = buildCapabilityInventory(input, {
processCaptureEnabled: false,
}).find((tool) => tool.name === "trace_web_module_imports");
expect(unavailable).toMatchObject({
available: true,
default_mode_available: false,
modes: [
expect.objectContaining({
name: "sources-without-literal-imports",
available: true,
}),
expect.objectContaining({
name: "native-literal-resolution",
available: false,
}),
],
});
const enabled = buildCapabilityInventory(input, {
processCaptureEnabled: false,
webModuleResolutionEnabled: true,
}).find((tool) => tool.name === "trace_web_module_imports");
expect(enabled).toMatchObject({
available: true,
default_mode_available: true,
});
});
const enabledPolicy: Parameters<typeof buildCapabilityInventory>[1] = {
processCaptureEnabled: true,
browserObservationEnabled: true,
+29
View File
@@ -37,6 +37,7 @@ export type AvailabilityPolicy = {
readonly firmwareExtractionEnabled?: boolean;
readonly androidAnalysisEnabled?: boolean;
readonly javascriptRecoveryEnabled?: boolean;
readonly webModuleResolutionEnabled?: boolean;
readonly browserObservationEnabled?: boolean;
readonly browserScenarioEnabled?: boolean;
readonly electronObservationEnabled?: boolean;
@@ -237,6 +238,34 @@ const workflowAvailabilityFor = ({
kind,
policy,
}: AvailabilityContext): Availability | null => {
if (name === "trace_web_module_imports")
return {
reason: "available",
remediation: null,
defaultModeAvailable: policy.webModuleResolutionEnabled === true,
modes: [
{
name: "sources-without-literal-imports",
available: true,
missing_operations: [],
remediation: null,
},
policy.webModuleResolutionEnabled === true
? {
name: "native-literal-resolution",
available: true,
missing_operations: [],
remediation: null,
}
: {
name: "native-literal-resolution",
available: false,
missing_operations: ["native-module-resolver"],
remediation:
"Provide an absolute REA_BROWSER_EXECUTABLE supporting import.meta.resolve; no binary target is required.",
},
],
};
if (name === "recover_javascript_sources")
return policy.javascriptRecoveryEnabled === true
? { reason: "available", remediation: null }
+57
View File
@@ -0,0 +1,57 @@
import type { AnalysisError } from "../domain/analysisErrorBase.js";
import type { JsonValue } from "../domain/jsonValue.js";
import type { Result } from "../domain/result.js";
import type {
WebModuleFile,
WebModuleResolution,
WebModuleTraceInput,
} from "../domain/webModuleTrace.js";
import type { webScriptExportManifestSchema } from "../domain/webScriptExport.js";
import type { z } from "zod";
import type { ExecutionOptions } from "./AnalysisProvider.js";
type JsonObject = Record<string, JsonValue>;
/** Verified selected source and capture metadata; provider protocols stay outside. */
export interface WebModuleArtifacts {
readonly manifest: z.output<typeof webScriptExportManifestSchema>;
readonly manifestFile: WebModuleFile;
readonly sourceFile: WebModuleFile;
readonly source: string;
readonly importMap: {
readonly file: WebModuleFile;
readonly baseUrl: string;
readonly value: JsonObject;
} | null;
}
/** Read selected local artifacts without refetching website assets. */
export interface WebModuleArtifactPort {
load(
input: WebModuleTraceInput,
options?: ExecutionOptions,
): Promise<Result<WebModuleArtifacts, AnalysisError>>;
}
/** Resolve literal specifiers in a selected URL/map context without executing the selected application. */
export interface WebModuleResolutionBatch {
readonly engine: { readonly id: string; readonly version: string };
readonly resolutions: readonly WebModuleResolution[];
readonly diagnostics: readonly string[];
readonly rawResult: JsonObject;
}
/** Replaceable native resolution engine, with no provider protocol in callers. */
export interface WebModuleResolutionPort {
resolve(
input: {
readonly importerUrl: string;
readonly importMap: {
readonly baseUrl: string;
readonly value: JsonObject;
} | null;
readonly specifiers: readonly string[];
},
options?: ExecutionOptions,
): Promise<Result<WebModuleResolutionBatch, AnalysisError>>;
}
@@ -0,0 +1,199 @@
import { describe, expect, it } from "vitest";
import { WebModuleTraceService } from "./WebModuleTraceService.js";
import { ok } from "../domain/result.js";
import { projectAnalysisError } from "../domain/analysisErrorProjection.js";
import {
webModuleTraceResultSchema,
type WebModuleResolution,
} from "../domain/webModuleTrace.js";
import {
webModuleArtifactsFixture,
webModuleResolverFixture,
} from "../../tests/fixtures/webModuleTrace.js";
const args = { manifest_path: "/analysis/manifest.json", script_index: 0 };
it.each([
{ ...args, importer_url: "file:///tmp/main.js" },
{ ...args, import_map: { path: "/map.json", base_url: "file:///tmp/maps/" } },
])(
"distinguishes valid unsupported URL contexts from malformed input: %j",
async (input) => {
const result = await new WebModuleTraceService(
{
load: () => {
throw new Error("must not acquire artifacts");
},
},
webModuleResolverFixture,
).trace(input);
if (result.ok) throw new Error("expected unsupported context");
expect(projectAnalysisError(result.error)).toMatchObject({
code: "capability_unavailable",
message: expect.stringContaining("file:"),
});
},
);
it("rejects source text changed across the artifact port while retaining the original digest", async () => {
const artifacts = {
...webModuleArtifactsFixture("import(name)"),
source: "import(other)",
};
const result = await new WebModuleTraceService(
{ load: () => Promise.resolve(ok(artifacts)) },
{
resolve: () => {
throw new Error("must not acquire engine");
},
},
).trace(args);
expect(result.ok).toBe(false);
if (!result.ok) expect(result.error._tag).toBe("AnalysisOutputError");
});
it("rejects sparse resolver arrays rather than relabeling literal imports as computed unknowns", async () => {
const artifacts = webModuleArtifactsFixture('import "./dep.js";');
const service = new WebModuleTraceService(
{ load: () => Promise.resolve(ok(artifacts)) },
{
resolve: () =>
Promise.resolve(
ok({
engine: { id: "fixture", version: "1" },
resolutions: new Array<WebModuleResolution>(1),
diagnostics: [],
rawResult: {},
}),
),
},
);
const result = await service.trace(args);
expect(result.ok).toBe(false);
if (!result.ok) expect(result.error._tag).toBe("AnalysisOutputError");
});
describe("selected module trace workflow", () => {
it("composes selected source, derived resolution and explicit computed/execution unknowns", async () => {
const artifacts = webModuleArtifactsFixture();
const service = new WebModuleTraceService(
{ load: () => Promise.resolve(ok(artifacts)) },
webModuleResolverFixture,
);
const response = await service.trace(args);
if (!response.ok) throw response.error;
const result = webModuleTraceResultSchema.parse(
response.value.normalized_result,
);
expect(result.importer).toEqual({
url: artifacts.manifest.scripts[0]?.url,
basis: "reported-source-url",
});
expect(result.manifest.capture_completeness).toEqual(
artifacts.manifest.capture_completeness,
);
expect(result.imports[0]?.resolution).toEqual({
state: "resolved",
url: "https://app.test/dep.js",
});
expect(result.imports[1]?.resolution).toEqual({
state: "unknown",
reason: "computed-specifier",
});
expect(result.imports.every((item) => item.execution === "unknown")).toBe(
true,
);
expect(response.value.subject).toMatchObject({
local_path: artifacts.sourceFile.path,
digest: { sha256: artifacts.sourceFile.sha256 },
});
});
it.each([
{ ...args, manifest_path: "relative.json" },
{
...args,
import_map: { path: "relative.json", base_url: "https://app.test/" },
},
{ ...args, importer_url: "not a URL" },
{ ...args, import_map: { path: "/map.json", base_url: "relative" } },
])(
"rejects malformed context before acquiring artifacts: %j",
async (input) => {
const service = new WebModuleTraceService(
{
load: () => {
throw new Error("must not acquire");
},
},
webModuleResolverFixture,
);
const response = await service.trace(input);
if (response.ok) throw new Error("expected invalid input");
expect(projectAnalysisError(response.error)).toMatchObject({
code: "invalid_request",
details: {
issues: [expect.objectContaining({ reason: "invalid_format" })],
},
});
},
);
it("handles computed-only sources without acquiring the optional engine", async () => {
const artifacts = webModuleArtifactsFixture("import(name)");
const service = new WebModuleTraceService(
{ load: () => Promise.resolve(ok(artifacts)) },
{
resolve: () => {
throw new Error("engine must not start");
},
},
);
const result = await service.trace(args);
if (!result.ok) throw result.error;
expect(
webModuleTraceResultSchema.parse(result.value.normalized_result).engine,
).toBeNull();
});
it("rejects incomplete resolution rather than fabricating unknown values", async () => {
const artifacts = webModuleArtifactsFixture();
const service = new WebModuleTraceService(
{ load: () => Promise.resolve(ok(artifacts)) },
{
resolve: () =>
Promise.resolve(
ok({
engine: { id: "fixture", version: "1" },
resolutions: [],
diagnostics: [],
rawResult: {},
}),
),
},
);
const response = await service.trace(args);
expect(response.ok).toBe(false);
if (!response.ok) expect(response.error._tag).toBe("AnalysisOutputError");
});
it("preserves an explicit selected importer context", async () => {
const artifacts = webModuleArtifactsFixture();
const result = await new WebModuleTraceService(
{ load: () => Promise.resolve(ok(artifacts)) },
webModuleResolverFixture,
).trace({ ...args, importer_url: "https://selected.test/base/main.js" });
if (!result.ok) throw result.error;
expect(
webModuleTraceResultSchema.parse(result.value.normalized_result)
.imports[0]?.resolution,
).toEqual({ state: "resolved", url: "https://selected.test/base/dep.js" });
});
it("honors cancellation before any artifacts are acquired", async () => {
const controller = new AbortController();
controller.abort();
const service = new WebModuleTraceService(
{
load: () => {
throw new Error("must not acquire");
},
},
webModuleResolverFixture,
);
const result = await service.trace(args, { signal: controller.signal });
expect(result.ok).toBe(false);
if (!result.ok) expect(result.error._tag).toBe("AnalysisCancelledError");
});
});
+327
View File
@@ -0,0 +1,327 @@
import { isAbsolute } from "node:path";
import { createHash } from "node:crypto";
import { z } from "zod";
import type { AnalysisError } from "../domain/analysisErrorBase.js";
import {
AnalysisCancelledError,
AnalysisCapabilityUnavailableError,
AnalysisInputError,
AnalysisOutputError,
} from "../domain/analysisErrorCore.js";
import { createEvidence, type Evidence } from "../domain/evidence.js";
import { jsonObjectSchema } from "../domain/jsonValue.js";
import { projectInputIssues } from "../domain/inputIssueProjection.js";
import { err, ok, type Result } from "../domain/result.js";
import {
collectWebModuleImports,
matchCapturedModules,
} from "../domain/webModuleImports.js";
import {
webModuleTraceInputSchema,
webModuleFileSchema,
webModuleResolutionSchema,
webModuleTraceResultSchema,
type WebModuleResolution,
type WebModuleTraceResult,
} from "../domain/webModuleTrace.js";
import { webScriptExportManifestSchema } from "../domain/webScriptExport.js";
import type { ExecutionOptions } from "./AnalysisProvider.js";
import type {
WebModuleArtifactPort,
WebModuleResolutionPort,
WebModuleResolutionBatch,
} from "./WebModulePorts.js";
const OPERATION = "trace_web_module_imports";
const artifactsSchema = z.strictObject({
manifest: webScriptExportManifestSchema,
manifestFile: webModuleFileSchema,
sourceFile: webModuleFileSchema,
source: z.string(),
importMap: z
.strictObject({
file: webModuleFileSchema,
baseUrl: z.string().min(1),
value: jsonObjectSchema,
})
.nullable(),
});
const resolutionBatchSchema = z.strictObject({
engine: z.strictObject({ id: z.string().min(1), version: z.string().min(1) }),
resolutions: z.array(webModuleResolutionSchema),
diagnostics: z.array(z.string()),
rawResult: jsonObjectSchema,
});
const LIMITATIONS = [
"Resolution is derived under the selected importer URL and optional import map; it does not establish that the map was installed in the captured page.",
"Reported source URLs do not establish an inline script's document base. Select importer_url explicitly when that context differs or is unknown.",
"Captured candidates retain reported capture metadata; their bytes are not reverified by this trace unless selected as the source.",
"A resolved URL or captured source does not prove loading, execution, UI causality, or module namespace identity. Execution remains unknown.",
"Only native ES module syntax is resolved. Bundler module IDs, computed imports, workers and non-HTTP(S) importer contexts are outside this trace.",
"Source-map declarations do not establish retained map bytes or executed original-source coverage.",
"Selected application code is parsed as data and never executed. The owned resolver executes only REA's trusted stub; its page requests are locally fulfilled or blocked.",
"Manifest, source and import-map limits are 32 MiB, 16 MiB and 4 MiB. Native resolution has a 20-second deadline; cleanup may extend it. One renderer is requested; no aggregate host memory limit is claimed.",
];
/** Compose verified captures, inert syntax and a replaceable URL resolution engine. */
export class WebModuleTraceService {
constructor(
readonly artifacts: WebModuleArtifactPort,
readonly resolver: WebModuleResolutionPort,
) {}
async trace(
rawInput: unknown,
options?: ExecutionOptions,
): Promise<Result<Evidence, AnalysisError>> {
if (isAborted(options?.signal))
return err(new AnalysisCancelledError(OPERATION));
const parsed = webModuleTraceInputSchema.safeParse(rawInput);
if (!parsed.success)
return err(
new AnalysisInputError(
OPERATION,
{ cause: parsed.error },
projectInputIssues(parsed.error.issues, rawInput),
),
);
const input = parsed.data;
for (const [path, value] of [
["manifest_path", input.manifest_path],
["import_map.path", input.import_map?.path],
] as const)
if (value !== undefined && !isAbsolute(value))
return invalid(
path.split("."),
"Expected an absolute filesystem path on this host.",
);
for (const [path, value] of [
["importer_url", input.importer_url],
["import_map.base_url", input.import_map?.base_url],
] as const)
if (value !== undefined) {
const admission = admitContext(value, path.split("."));
if (!admission.ok) return admission;
}
const loaded = await this.artifacts.load(input, options);
if (!loaded.ok) return loaded;
const verified = artifactsSchema.safeParse(loaded.value);
if (!verified.success)
return err(
new AnalysisOutputError(
OPERATION,
"Artifact port returned a malformed source/manifest report.",
),
);
const data = verified.data;
if (
(input.import_map?.path ?? null) !==
(data.importMap?.file.path ?? null) ||
(input.import_map?.base_url ?? null) !== (data.importMap?.baseUrl ?? null)
)
return err(
new AnalysisOutputError(
OPERATION,
"Artifact port changed the selected import-map identity/context.",
),
);
const selected = data.manifest.scripts[input.script_index];
if (
selected === undefined ||
selected.content.state !== "exported" ||
selected.content.sha256 !== data.sourceFile.sha256 ||
selected.content.bytes !== data.sourceFile.bytes ||
Buffer.byteLength(data.source) !== data.sourceFile.bytes ||
createHash("sha256").update(data.source, "utf8").digest("hex") !==
data.sourceFile.sha256 ||
data.manifestFile.path !== input.manifest_path
)
return err(
new AnalysisOutputError(
OPERATION,
"Artifact port did not bind the selected source and manifest identities.",
),
);
const importerUrl = input.importer_url ?? selected.url;
const importerAdmission = admitContext(importerUrl, ["importer_url"]);
if (!importerAdmission.ok) return importerAdmission;
const syntax = collectWebModuleImports(data.source);
const literals = syntax.imports
.filter((reference) => reference.specifier !== null)
.map((reference) => reference.specifier ?? "");
let resolution: WebModuleResolutionBatch | null = null;
if (literals.length > 0) {
const execution = await this.resolver.resolve(
{
importerUrl,
importMap:
data.importMap === null
? null
: {
baseUrl: data.importMap.baseUrl,
value: data.importMap.value,
},
specifiers: literals,
},
options,
);
if (!execution.ok) return execution;
const report = resolutionBatchSchema.safeParse(execution.value);
if (!report.success)
return err(
new AnalysisOutputError(
OPERATION,
"Resolver returned malformed or incomplete native evidence.",
),
);
resolution = report.data;
if (resolution.resolutions.length !== literals.length)
return err(
new AnalysisOutputError(
OPERATION,
"Resolver did not return every selected literal specifier.",
),
);
}
if (isAborted(options?.signal))
return err(new AnalysisCancelledError(OPERATION));
let literalIndex = 0;
const imports: WebModuleTraceResult["imports"] = [];
for (const reference of syntax.imports) {
let resolved:
| WebModuleResolution
| { state: "unknown"; reason: "computed-specifier" };
if (reference.specifier === null)
resolved = { state: "unknown", reason: "computed-specifier" };
else {
const reported = resolution?.resolutions[literalIndex++];
if (reported === undefined)
return err(
new AnalysisOutputError(
OPERATION,
"Resolver omitted a selected literal specifier.",
),
);
resolved = reported;
}
imports.push({
...reference,
resolution: resolved,
captured_candidates:
resolved.state === "resolved"
? matchCapturedModules(resolved.url, data.manifest.scripts)
: [],
execution: "unknown",
});
}
const normalized = webModuleTraceResultSchema.safeParse({
manifest: {
...data.manifestFile,
reported_output_directory: data.manifest.output_directory,
capture_sha256: data.manifest.capture_sha256,
capture_path: data.manifest.capture_path,
capture_kind: data.manifest.capture_kind,
capture_completeness: data.manifest.capture_completeness,
source_evidence_id: data.manifest.source_evidence_id,
},
source: {
...data.sourceFile,
script_index: input.script_index,
script: selected,
},
importer: {
url: importerUrl,
basis:
input.importer_url === undefined
? "reported-source-url"
: "caller-selected",
},
import_map:
data.importMap === null
? null
: { ...data.importMap.file, base_url: data.importMap.baseUrl },
parser: {
state: syntax.state,
diagnostics: syntax.diagnostics,
location_units: "UTF-16 offsets and columns; one-based lines",
},
engine: resolution?.engine ?? null,
imports,
diagnostics: resolution?.diagnostics ?? [],
limitations: LIMITATIONS,
});
if (!normalized.success)
return err(new AnalysisOutputError(OPERATION, normalized.error.message));
return ok(
createEvidence(
{
path: data.sourceFile.path,
sha256: data.sourceFile.sha256,
format: "file",
},
{
id: "rea-web-module-trace",
name: "REA captured module relationship trace",
version: "1",
},
{
operation: OPERATION,
parameters: jsonObjectSchema.parse(input),
result: normalized.data,
rawResult: resolution?.rawResult ?? null,
confidence: "derived",
authority: "historical-reference",
limitations: LIMITATIONS,
locations: [
{ kind: "artifact-path", path: data.sourceFile.path },
{ kind: "artifact-path", path: input.manifest_path },
],
evidenceLinks:
data.manifest.source_evidence_id === null
? []
: [data.manifest.source_evidence_id],
},
),
);
}
}
const admitContext = (
value: string,
path: readonly string[],
): Result<null, AnalysisError> => {
let url: URL;
try {
url = new URL(value);
} catch (cause: unknown) {
void cause;
return invalid(
path,
"Expected an absolute context URL. Select importer_url explicitly when the reported source URL is unknown.",
);
}
if (url.username !== "" || url.password !== "")
return invalid(
path,
"Module context URLs must not contain username/password userinfo.",
);
if (url.protocol !== "http:" && url.protocol !== "https:")
return err(
new AnalysisCapabilityUnavailableError(
"web-module-resolution",
OPERATION,
"unsupported_context_scheme",
{
userMessage: `${path.join(".")} ${value} uses unsupported ${url.protocol} context. This trace currently supports HTTP(S) importer/map contexts; select a supported context explicitly when appropriate.`,
},
),
);
return ok(null);
};
const isAborted = (signal?: AbortSignal): boolean => signal?.aborted === true;
const invalid = (path: readonly string[], message: string) =>
err(
new AnalysisInputError(OPERATION, undefined, [
{ path, reason: "invalid_format", message },
]),
);
+84
View File
@@ -0,0 +1,84 @@
import { createHash } from "node:crypto";
import { constants } from "node:fs";
import { lstat, open } from "node:fs/promises";
import { ArtifactReaderFailure } from "./ArtifactReader.js";
/** Read one bounded regular file and reject replacement or in-place changes. */
export const readStableArtifact = async (
path: string,
maximumBytes: number,
signal?: AbortSignal,
): Promise<{ readonly bytes: Buffer; readonly sha256: string }> => {
signal?.throwIfAborted();
const before = await lstat(path, { bigint: true });
if (!before.isFile() || before.isSymbolicLink())
throw new ArtifactReaderFailure(
"path",
`Expected a regular file without a symlink: ${path}`,
);
if (before.size > BigInt(maximumBytes))
throw new ArtifactReaderFailure(
"limit",
`Selected artifact exceeds ${String(maximumBytes)} bytes: ${path}`,
);
const file = await open(
path,
constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK,
);
try {
const initial = await file.stat({ bigint: true });
if (
!initial.isFile() ||
initial.dev !== before.dev ||
initial.ino !== before.ino
)
throw new ArtifactReaderFailure(
"integrity",
`Artifact changed before open: ${path}`,
);
const chunks: Buffer[] = [];
let count = 0;
for await (const chunk of file.createReadStream({
autoClose: false,
highWaterMark: 65536,
})) {
signal?.throwIfAborted();
const value: unknown = chunk;
if (!Buffer.isBuffer(value))
throw new ArtifactReaderFailure(
"format",
`Artifact stream did not return bytes: ${path}`,
);
const bytes = value;
count += bytes.length;
if (count > maximumBytes)
throw new ArtifactReaderFailure(
"limit",
`Selected artifact grew beyond its byte limit: ${path}`,
);
chunks.push(bytes);
}
const after = await file.stat({ bigint: true });
const current = await lstat(path, { bigint: true });
if (
[after, current].some(
(stat) =>
!stat.isFile() ||
stat.dev !== initial.dev ||
stat.ino !== initial.ino ||
stat.size !== initial.size ||
stat.mtimeNs !== initial.mtimeNs ||
stat.ctimeNs !== initial.ctimeNs,
) ||
BigInt(count) !== initial.size
)
throw new ArtifactReaderFailure(
"integrity",
`Artifact changed during read: ${path}`,
);
const bytes = Buffer.concat(chunks, count);
return { bytes, sha256: createHash("sha256").update(bytes).digest("hex") };
} finally {
await file.close();
}
};
@@ -0,0 +1,91 @@
import { describe, expect, it } from "vitest";
import { NativeModuleResolver } from "./NativeModuleResolver.js";
import { projectAnalysisError } from "../../domain/analysisErrorProjection.js";
const input = {
importerUrl: "https://app.test/main.js",
importMap: null,
specifiers: ["./dep.js"],
};
describe("optional native resolution acquisition", () => {
it("awaits owned cleanup when cancellation arrives during acquisition", async () => {
const controller = new AbortController();
let closed = false;
const provider = new NativeModuleResolver(
{ REA_BROWSER_EXECUTABLE: "/browser" },
() => {
controller.abort();
return Promise.resolve({
version: () => "fixture",
newContext: () => {
throw new Error("must not acquire context");
},
close: () => {
closed = true;
return Promise.resolve();
},
});
},
);
const result = await provider.resolve(input, { signal: controller.signal });
expect(closed).toBe(true);
if (result.ok) throw new Error("expected cancelled result");
expect(result.error._tag).toBe("AnalysisCancelledError");
});
it("returns cleanup_incomplete when owned cleanup fails after an engine error", async () => {
const provider = new NativeModuleResolver(
{ REA_BROWSER_EXECUTABLE: "/browser" },
() =>
Promise.resolve({
version: () => "fixture",
newContext: () =>
Promise.reject(new Error("fixture context failure")),
close: () =>
Promise.reject(new Error("fixture owned cleanup refusal")),
}),
);
const result = await provider.resolve(input);
if (result.ok) throw new Error("expected cleanup failure");
expect(projectAnalysisError(result.error).code).toBe("cleanup_incomplete");
expect(result.error.cleanupIncomplete).toBe(true);
});
it("does not load the browser engine when the selected operation lacks its configuration", async () => {
const provider = new NativeModuleResolver({}, () => {
throw new Error("must not launch");
});
const result = await provider.resolve(input);
expect(result.ok).toBe(false);
if (!result.ok)
expect(projectAnalysisError(result.error)).toMatchObject({
code: "capability_unavailable",
message: expect.stringContaining("REA_BROWSER_EXECUTABLE"),
});
});
it("does not acquire a browser after caller cancellation", async () => {
const controller = new AbortController();
controller.abort();
const provider = new NativeModuleResolver(
{ REA_BROWSER_EXECUTABLE: "/browser" },
() => {
throw new Error("must not launch");
},
);
const result = await provider.resolve(input, { signal: controller.signal });
if (result.ok) throw new Error("expected cancellation");
expect(result.error._tag).toBe("AnalysisCancelledError");
});
it("retains the selected executable and actual startup diagnostic", async () => {
const provider = new NativeModuleResolver(
{ REA_BROWSER_EXECUTABLE: "/missing/browser" },
() => Promise.reject(new Error("spawn EACCES fixture")),
);
const result = await provider.resolve(input);
if (result.ok) throw new Error("expected startup failure");
expect(projectAnalysisError(result.error).details).toMatchObject({
diagnostics: {
executable: "/missing/browser",
error_message: "spawn EACCES fixture",
},
});
});
});
+282
View File
@@ -0,0 +1,282 @@
import { isAbsolute } from "node:path";
import { z } from "zod";
import type { Browser, LaunchOptions } from "playwright-core";
import type { ExecutionOptions } from "../../application/AnalysisProvider.js";
import type {
WebModuleResolutionPort,
WebModuleResolutionBatch,
} from "../../application/WebModulePorts.js";
import type { AnalysisError } from "../../domain/analysisErrorBase.js";
import {
AnalysisCancelledError,
AnalysisCapabilityUnavailableError,
AnalysisOutputError,
AnalysisTimeoutError,
} from "../../domain/analysisErrorCore.js";
import { BrowserObservationError } from "../../domain/browserObservationError.js";
import { jsonObjectSchema } from "../../domain/jsonValue.js";
import { ProviderAdapterError } from "../../domain/providerAdapterError.js";
import { err, ok, type Result } from "../../domain/result.js";
import { webModuleResolutionSchema } from "../../domain/webModuleTrace.js";
import { ProviderStartupDeadline } from "../../process/ProviderDeadline.js";
const OPERATION = "trace_web_module_imports";
const TIMEOUT_MS = 20000;
const reportSchema = z.strictObject({
importer_url: z.string(),
entries: z.array(
z.strictObject({
specifier: z.string(),
resolution: webModuleResolutionSchema,
}),
),
});
/** Production launch seam: only the adapter knows the browser API. */
export type ModuleResolverLauncher = (
options: LaunchOptions,
) => Promise<Pick<Browser, "newContext" | "version" | "close">>;
const launch: ModuleResolverLauncher = async (options) =>
(await import("playwright-core")).chromium.launch(options);
/** Use native import.meta.resolve in an owned context with locally fulfilled requests. */
export class NativeModuleResolver implements WebModuleResolutionPort {
constructor(
readonly environment: Readonly<Record<string, string | undefined>>,
readonly launcher: ModuleResolverLauncher = launch,
) {}
async resolve(
input: Parameters<WebModuleResolutionPort["resolve"]>[0],
options?: ExecutionOptions,
): ReturnType<WebModuleResolutionPort["resolve"]> {
if (options?.signal?.aborted === true)
return err(new AnalysisCancelledError(OPERATION));
const executable = this.environment.REA_BROWSER_EXECUTABLE;
if (executable === undefined || !isAbsolute(executable))
return err(
new AnalysisCapabilityUnavailableError(
"native-module-resolver",
OPERATION,
"missing_browser_executable",
{
userMessage:
"Provide an absolute REA_BROWSER_EXECUTABLE for a caller-supplied Chromium browser supporting import.meta.resolve. REA installs no browser.",
},
),
);
const deadline = new ProviderStartupDeadline(TIMEOUT_MS, options?.signal);
let browser: Pick<Browser, "newContext" | "version" | "close"> | undefined;
let cleanup: Promise<void> | undefined;
const close = (): Promise<void> => {
if (cleanup === undefined && browser !== undefined)
cleanup = browser.close();
return cleanup ?? Promise.resolve();
};
const onAbort = (): void => {
if (browser !== undefined) {
const pending = close();
void pending.catch(() => undefined);
}
};
deadline.signal.addEventListener("abort", onAbort, { once: true });
let result: Result<WebModuleResolutionBatch, AnalysisError>;
try {
browser = await this.launcher({
executablePath: executable,
headless: true,
timeout: Math.max(1, deadline.remainingMs()),
handleSIGINT: false,
handleSIGTERM: false,
handleSIGHUP: false,
args: [
"--disable-dev-shm-usage",
"--disable-background-networking",
"--renderer-process-limit=1",
"--js-flags=--max-old-space-size=128",
...(this.environment.REA_BROWSER_NO_SANDBOX === "true"
? ["--no-sandbox"]
: []),
],
});
deadline.signal.throwIfAborted();
const context = await browser.newContext({
serviceWorkers: "block",
acceptDownloads: false,
});
const page = await context.newPage();
const diagnostics: string[] = [];
let diagnosticBytes = 0;
page.on("console", (message) => {
const text = message.text();
diagnosticBytes += Buffer.byteLength(text);
if (diagnosticBytes <= 1024 * 1024)
diagnostics.push(`${message.type()}: ${text}`);
});
const requests: {
url: string;
resource_type: string;
effect: "fulfilled" | "blocked";
}[] = [];
const importerUrl = new URL(input.importerUrl).href;
const documentUrl = new URL("/.rea-module-resolver/document", importerUrl)
.href;
const transportUrl = new URL(importerUrl);
transportUrl.hash = "";
let stubSent = false;
await context.route("**/*", async (route) => {
const request = route.request();
if (
request.resourceType() === "document" &&
request.url() === documentUrl.split("#")[0]
) {
requests.push({
url: request.url(),
resource_type: "document",
effect: "fulfilled",
});
await route.fulfill({
status: 200,
contentType: "text/html",
body: `<!doctype html>${input.importMap === null ? "" : `<base href="${htmlAttribute(new URL(input.importMap.baseUrl).href)}"><script type="importmap">${scriptJson(input.importMap.value)}</script>`}`,
});
} else if (
!stubSent &&
request.resourceType() === "script" &&
request.url() === transportUrl.href
) {
stubSent = true;
requests.push({
url: request.url(),
resource_type: "script",
effect: "fulfilled",
});
await route.fulfill({
status: 200,
contentType: "text/javascript",
headers: { "Access-Control-Allow-Origin": "*" },
body: `if (typeof import.meta.resolve !== "function") {globalThis.__reaModuleUnsupported = true;} else {globalThis.__reaModuleResult = {importer_url: import.meta.url, entries: ${scriptJson(input.specifiers)}.map(specifier => {try {return {specifier, resolution: {state: "resolved", url: import.meta.resolve(specifier)}}} catch (error) {return {specifier, resolution: {state: "rejected", message: String(error)}}}})};}`,
});
} else {
requests.push({
url: request.url(),
resource_type: request.resourceType(),
effect: "blocked",
});
await route.abort();
}
});
await page.goto(documentUrl, {
timeout: Math.max(1, deadline.remainingMs()),
});
await page.evaluate(
`{const script = document.createElement("script"); script.type = "module"; script.src = ${scriptJson(importerUrl)}; script.onerror = () => {globalThis.__reaModuleFailure = "Trusted resolver stub could not be loaded in the selected context.";}; document.head.append(script);}`,
);
await page.waitForFunction(
() =>
Reflect.has(globalThis, "__reaModuleResult") ||
Reflect.has(globalThis, "__reaModuleUnsupported") ||
Reflect.has(globalThis, "__reaModuleFailure"),
undefined,
{ timeout: Math.max(1, deadline.remainingMs()) },
);
const unsupported: unknown = await page.evaluate(
() => Reflect.get(globalThis, "__reaModuleUnsupported") as unknown,
);
if (unsupported === true)
throw new AnalysisCapabilityUnavailableError(
"native-module-resolver",
OPERATION,
"native_resolution_unsupported",
{
userMessage: `Configured Chromium ${browser.version()} does not supply import.meta.resolve; use a browser supporting native module resolution.`,
},
);
const raw: unknown = await page.evaluate(
() => Reflect.get(globalThis, "__reaModuleResult") as unknown,
);
const failure: unknown = await page.evaluate(
() => Reflect.get(globalThis, "__reaModuleFailure") as unknown,
);
if (typeof failure === "string")
throw new AnalysisOutputError(
OPERATION,
`${failure} Native diagnostics: ${diagnostics.join("\n")}`,
);
const parsed = reportSchema.safeParse(raw);
if (
!parsed.success ||
parsed.data.importer_url !== importerUrl ||
parsed.data.entries.length !== input.specifiers.length ||
parsed.data.entries.some(
(entry, index) => entry.specifier !== input.specifiers[index],
)
)
throw new AnalysisOutputError(
OPERATION,
"Native resolver report has invalid shape or does not match the selected importer/specifiers.",
);
if (diagnosticBytes > 1024 * 1024)
throw new BrowserObservationError(OPERATION, "payload_limit");
deadline.signal.throwIfAborted();
const engine = {
id: "chromium-native-module-resolver",
version: browser.version(),
};
result = ok({
engine,
resolutions: parsed.data.entries.map((entry) => entry.resolution),
diagnostics,
rawResult: jsonObjectSchema.parse({
native_report: parsed.data,
requests,
diagnostics,
engine,
executable_path: executable,
}),
});
} catch (cause: unknown) {
result = err(
deadline.interruption === "cancelled"
? new AnalysisCancelledError(OPERATION)
: deadline.interruption === "timeout"
? new AnalysisTimeoutError(OPERATION, TIMEOUT_MS)
: cause instanceof AnalysisOutputError ||
cause instanceof AnalysisCapabilityUnavailableError ||
cause instanceof BrowserObservationError
? cause
: new ProviderAdapterError("native-module-resolver", OPERATION, {
cause,
diagnostics: {
executable,
error_message:
cause instanceof Error ? cause.message : String(cause),
},
}),
);
} finally {
deadline.signal.removeEventListener("abort", onAbort);
deadline.dispose();
try {
await close();
} catch (cause: unknown) {
result = err(
new BrowserObservationError(OPERATION, "cleanup_failed", { cause }),
);
}
}
if (result.ok && options?.signal?.aborted)
return err(new AnalysisCancelledError(OPERATION));
return result;
}
}
const scriptJson = (value: unknown): string =>
JSON.stringify(value).replaceAll("<", "\\u003c");
const htmlAttribute = (value: string): string =>
value
.replaceAll("&", "&amp;")
.replaceAll('"', "&quot;")
.replaceAll("<", "&lt;")
.replaceAll(">", "&gt;");
+230
View File
@@ -0,0 +1,230 @@
import { lstat, realpath } from "node:fs/promises";
import { dirname, isAbsolute, join, relative, sep } from "node:path";
import { z } from "zod";
import { ArtifactReaderFailure } from "../../artifacts/ArtifactReader.js";
import { readStableArtifact } from "../../artifacts/readStableArtifact.js";
import { normalizeArtifactPath } from "../../artifacts/ArtifactPaths.js";
import type {
WebModuleArtifactPort,
WebModuleArtifacts,
} from "../../application/WebModulePorts.js";
import type { ExecutionOptions } from "../../application/AnalysisProvider.js";
import {
AnalysisCancelledError,
AnalysisInputError,
AnalysisCapabilityUnavailableError,
} from "../../domain/analysisErrorCore.js";
import { ArtifactOperationError } from "../../domain/artifactOperationError.js";
import { ProviderAdapterError } from "../../domain/providerAdapterError.js";
import { jsonObjectSchema } from "../../domain/jsonValue.js";
import { safeParseJson } from "../../domain/safeJson.js";
import { err, ok } from "../../domain/result.js";
import type { WebModuleTraceInput } from "../../domain/webModuleTrace.js";
import { webScriptExportManifestSchema } from "../../domain/webScriptExport.js";
/** Load verified exported source bytes relative to the current manifest location. */
export class LocalWebModuleArtifacts implements WebModuleArtifactPort {
/** Verify exactly the selected manifest, source and optional local map. */
async load(input: WebModuleTraceInput, options?: ExecutionOptions) {
let field: readonly (string | number)[] = ["manifest_path"];
let targetPath = input.manifest_path;
try {
const manifestBytes = await readStableArtifact(
input.manifest_path,
32 * 1024 * 1024,
options?.signal,
);
const manifest = webScriptExportManifestSchema.parse(
parseJson(manifestBytes.bytes),
);
field = ["script_index"];
const selected = manifest.scripts[input.script_index];
if (selected === undefined)
throw new ModuleArtifactFormatFailure(
`No script at index ${String(input.script_index)}; manifest contains ${String(manifest.scripts.length)} scripts.`,
);
if (selected.content.state !== "exported")
return err(
new AnalysisCapabilityUnavailableError(
"web-module-artifacts",
"trace_web_module_imports",
"selected_source_unavailable",
{
userMessage: `Selected script ${String(input.script_index)} has unavailable source bytes (${selected.content.reason}): ${selected.content.message} Capture source bytes before tracing this script.`,
},
),
);
const portable = normalizeArtifactPath(selected.content.relative_path);
if (portable !== selected.content.relative_path)
throw new ArtifactReaderFailure(
"path",
`Selected source path changes during normalization: ${selected.content.relative_path}`,
);
const root = dirname(input.manifest_path);
const sourcePath = join(root, "files", portable);
targetPath = sourcePath;
await assertContained(root, sourcePath);
const sourceBytes = await readStableArtifact(
sourcePath,
16 * 1024 * 1024,
options?.signal,
);
await assertContained(root, sourcePath);
if (
sourceBytes.sha256 !== selected.content.sha256 ||
sourceBytes.bytes.length !== selected.content.bytes
)
throw new ArtifactReaderFailure(
"integrity",
`Selected source identity mismatch: ${sourcePath}; expected sha256=${selected.content.sha256}, bytes=${String(selected.content.bytes)}; observed sha256=${sourceBytes.sha256}, bytes=${String(sourceBytes.bytes.length)}.`,
undefined,
{
logicalPath: selected.content.relative_path,
declaredSha256: selected.content.sha256,
calculatedSha256: sourceBytes.sha256,
unpacked: false,
},
);
const source = decode(sourceBytes.bytes);
let importMap: WebModuleArtifacts["importMap"] = null;
if (input.import_map !== undefined) {
field = ["import_map", "path"];
targetPath = input.import_map.path;
const bytes = await readStableArtifact(
input.import_map.path,
4 * 1024 * 1024,
options?.signal,
);
importMap = {
file: {
path: input.import_map.path,
sha256: bytes.sha256,
bytes: bytes.bytes.length,
},
baseUrl: input.import_map.base_url,
value: jsonObjectSchema.parse(parseJson(bytes.bytes)),
};
}
return ok({
manifest,
manifestFile: {
path: input.manifest_path,
sha256: manifestBytes.sha256,
bytes: manifestBytes.bytes.length,
},
sourceFile: {
path: sourcePath,
sha256: sourceBytes.sha256,
bytes: sourceBytes.bytes.length,
},
source,
importMap,
});
} catch (cause: unknown) {
if (options?.signal?.aborted === true)
return err(new AnalysisCancelledError("trace_web_module_imports"));
if (cause instanceof ArtifactReaderFailure)
return err(
new ArtifactOperationError(
"trace_web_module_imports",
cause.reason,
cause.details,
cause.message,
),
);
if (
cause instanceof Error &&
"code" in cause &&
typeof cause.code === "string" &&
"syscall" in cause &&
typeof cause.syscall === "string"
)
return err(
new ArtifactOperationError(
"trace_web_module_imports",
"io",
undefined,
`${field.join(".")}: ${cause.code}: ${cause.message}`,
),
);
if (
cause instanceof ModuleArtifactFormatFailure ||
cause instanceof z.ZodError
)
return err(
new AnalysisInputError("trace_web_module_imports", { cause }, [
{
path: field,
reason: "invalid_format",
message: `${targetPath}: ${cause.message}`,
},
]),
);
return err(
new ProviderAdapterError(
"web-module-artifacts",
"trace_web_module_imports",
{
cause,
diagnostics: {
target_path: targetPath,
field: field.join("."),
error_message:
cause instanceof Error ? cause.message : String(cause),
},
},
),
);
}
}
}
class ModuleArtifactFormatFailure extends Error {}
const decode = (bytes: Buffer): string => {
try {
return new TextDecoder("utf-8", { fatal: true, ignoreBOM: true }).decode(
bytes,
);
} catch (cause: unknown) {
throw new ModuleArtifactFormatFailure(
"Selected artifact is not valid UTF-8.",
{ cause },
);
}
};
const parseJson = (bytes: Buffer): unknown => {
const parsed = safeParseJson(decode(bytes));
if (!parsed.ok) throw new ModuleArtifactFormatFailure(parsed.error);
return parsed.value;
};
const assertContained = async (root: string, path: string): Promise<void> => {
const canonicalRoot = await realpath(root);
const canonicalFile = await realpath(path);
const fromRoot = relative(canonicalRoot, canonicalFile);
if (
fromRoot === ".." ||
fromRoot.startsWith(`..${sep}`) ||
isAbsolute(fromRoot)
)
throw new ArtifactReaderFailure(
"path",
`Selected source escapes its manifest directory: ${path}`,
);
let directory = dirname(path);
while (directory !== root) {
const stat = await lstat(directory);
if (!stat.isDirectory() || stat.isSymbolicLink())
throw new ArtifactReaderFailure(
"path",
`Selected source directory is a symlink or not a directory: ${directory}`,
);
const parent = dirname(directory);
if (parent === directory)
throw new ArtifactReaderFailure(
"path",
"Selected source has no containing manifest directory.",
);
directory = parent;
}
};
+2
View File
@@ -13,6 +13,7 @@ import { registerEvidenceCommands } from "./cliEvidenceCommands.js";
import { registerProcessCommands } from "./cli/processCommands.js";
import { registerBrowserCommands } from "./cli/browserCommands.js";
import { registerWebScriptCommands } from "./cli/webScriptCommands.js";
import { registerWebModuleCommands } from "./cli/webModuleCommands.js";
import { registerJavaScriptRecoveryCommands } from "./cli/javascriptRecoveryCommands.js";
import { registerAdvancedBrowserCommands } from "./cli/browserAdvancedCommands.js";
import { registerBrowserScenarioCommands } from "./cliBrowserScenarioCommands.js";
@@ -57,6 +58,7 @@ export const createCli = (
registerProcessCommands(cli, logger, environment);
registerBrowserCommands(cli, logger);
registerWebScriptCommands(cli, logger);
registerWebModuleCommands(cli, logger, environment);
registerJavaScriptRecoveryCommands(cli, logger, environment);
registerAdvancedBrowserCommands(cli, logger);
registerBrowserScenarioCommands(cli, logger);
+70
View File
@@ -0,0 +1,70 @@
import { z } from "incur";
import { createWebModuleTraceService } from "../composition/webModules.js";
import { CLI_COMMANDS } from "../cliCommandNames.js";
import { logCliCommand } from "../cliLogging.js";
import { projectAnalysisError } from "../domain/analysisErrorProjection.js";
import type { Logger } from "../logger.js";
import type { CliInstance } from "./types.js";
import { withCommandCancellation } from "./commandCancellation.js";
/** Expose the same selected-context module trace through the CLI. */
export const registerWebModuleCommands = (
cli: CliInstance,
logger: Logger,
environment: Readonly<Record<string, string | undefined>>,
): void => {
const service = createWebModuleTraceService(environment);
cli.command(CLI_COMMANDS.traceWebModuleImports, {
description:
"Trace one captured script's native imports to URLs and captured source candidates",
args: z.object({
manifestPath: z
.string()
.describe("Absolute export_web_scripts manifest path"),
scriptIndex: z.coerce
.number()
.int()
.min(0)
.describe("Zero-based manifest script index"),
}),
options: z.object({
importerUrl: z
.string()
.optional()
.describe("Explicit HTTP(S) importer URL"),
importMapPath: z
.string()
.optional()
.describe("Absolute local import-map JSON"),
importMapBaseUrl: z
.string()
.optional()
.describe("HTTP(S) import-map base URL"),
}),
run: ({ args, options }) =>
withCommandCancellation((signal) =>
logCliCommand(logger, CLI_COMMANDS.traceWebModuleImports, async () => {
const result = await service.trace(
{
manifest_path: args.manifestPath,
script_index: args.scriptIndex,
...(options.importerUrl === undefined
? {}
: { importer_url: options.importerUrl }),
...(options.importMapPath === undefined &&
options.importMapBaseUrl === undefined
? {}
: {
import_map: {
path: options.importMapPath,
base_url: options.importMapBaseUrl,
},
}),
},
{ signal },
);
return result.ok ? result.value : projectAnalysisError(result.error);
}),
),
});
};
+1
View File
@@ -1,5 +1,6 @@
/** Canonical names used by every one-shot CLI command registration. */
export const CLI_COMMANDS = Object.freeze({
traceWebModuleImports: "trace-web-module-imports",
analyze: "analyze",
inspect: "inspect",
decompile: "decompile",
+12
View File
@@ -0,0 +1,12 @@
import { WebModuleTraceService } from "../application/WebModuleTraceService.js";
import { LocalWebModuleArtifacts } from "../browser/modules/WebModuleArtifacts.js";
import { NativeModuleResolver } from "../browser/modules/NativeModuleResolver.js";
/** Assemble optional module tracing without acquiring a browser or reading files. */
export const createWebModuleTraceService = (
environment: Readonly<Record<string, string | undefined>> = process.env,
): WebModuleTraceService =>
new WebModuleTraceService(
new LocalWebModuleArtifacts(),
new NativeModuleResolver(environment),
);
+1
View File
@@ -83,6 +83,7 @@ const PUBLISHED_TOOL_NAME_FLOOR = [
"export_evidence_bundle",
"extract_artifact",
"export_web_scripts",
"trace_web_module_imports",
"find_changed_behavior",
"find_xrefs_to_name",
"get_call_graph",
+6
View File
@@ -41,6 +41,12 @@ const sessionEvidence = effects({ mutatesSession: true });
/** Explicit effect audit for every public tool. */
export const TOOL_EFFECTS: Readonly<Record<string, ToolEffects>> = {
trace_web_module_imports: effects({
mutatesSession: true,
writesFilesystem: true,
launchesProcess: true,
accessesNetwork: true,
}),
inspect_firmware_regions: effects({
mutatesSession: true,
launchesProcess: true,
+26
View File
@@ -3,11 +3,37 @@ import {
webScriptExportResultSchema,
} from "../domain/webScriptExport.js";
import type { ToolContract } from "./toolContractTypes.js";
import {
webModuleTraceInputSchema,
webModuleTraceResultSchema,
} from "../domain/webModuleTrace.js";
import { toolContractMetadata } from "./toolEffects.js";
import { evidenceResultOf } from "./toolOutputSchemas.js";
/** Local captured-script publication contracts shared by CLI and MCP. */
export const WEB_SCRIPT_TOOL_CONTRACTS = [
{
name: "trace_web_module_imports",
...toolContractMetadata("trace_web_module_imports"),
description:
"Trace one exported website script's outgoing native ES imports/re-exports, using exact URL and optional selected import-map context. Reads a local export_web_scripts manifest and verifies the selected source's SHA-256/size; returns source positions, native resolved URLs or exact errors, every matching captured candidate and explicit unknown execution. Query/fragment identities are preserved. Computed imports and bundler IDs remain unknown. Select importer_url for unknown inline/document-base context. Caller-supplied Chromium executes only a trusted REA resolver stub in an owned context with page requests locally fulfilled/blocked; no captured application execution or asset refetch. Requires absolute REA_BROWSER_EXECUTABLE for literal imports. 32 MiB manifest, 16 MiB source, 4 MiB map and 20-second native deadline; cleanup may extend the deadline.",
kind: "application",
inputSchema: webModuleTraceInputSchema,
outputSchema: evidenceResultOf(webModuleTraceResultSchema),
examples: [
{
title: "Trace an exported module under a selected import map",
input: {
manifest_path: "/analysis/exported-scripts/manifest.json",
script_index: 0,
import_map: {
path: "/analysis/import-map.json",
base_url: "https://example.test/app/",
},
},
},
],
},
{
name: "export_web_scripts",
...toolContractMetadata("export_web_scripts"),
+1
View File
@@ -13,6 +13,7 @@ export class ArtifactOperationError extends AnalysisError {
| "inspect_asset_catalog"
| "inspect_keyed_archive"
| "export_web_scripts"
| "trace_web_module_imports"
| "analyze_javascript_application",
readonly reason:
| "cancelled"
+1
View File
@@ -3,6 +3,7 @@ export type BrowserObservationOperation =
| "list_browser_targets"
| "inspect_web_page"
| "analyze_web_bundle"
| "trace_web_module_imports"
| "observe_web_session"
| "discover_webmcp_tools"
| "compare_web_captures"
+120
View File
@@ -0,0 +1,120 @@
import { describe, expect, it } from "vitest";
import {
collectWebModuleImports,
matchCapturedModules,
} from "./webModuleImports.js";
import { exportedWebScriptSchema } from "./webScriptExport.js";
describe("native module syntax observations", () => {
it("collects native imports and re-exports, preserving empty strings and computed unknowns", () => {
const source =
'import ""; import x from "./x.js"; export * from "./all.js"; export {x} from "pkg"; import(`./literal.js`); import(`./${name}.js`); import(expr);';
const result = collectWebModuleImports(source);
expect(result.state).toBe("parsed");
expect(result.imports.map((item) => [item.kind, item.specifier])).toEqual([
["static-import", ""],
["static-import", "./x.js"],
["re-export", "./all.js"],
["re-export", "pkg"],
["dynamic-import", "./literal.js"],
["dynamic-import", null],
["dynamic-import", null],
]);
for (const item of result.imports)
expect(source.slice(item.start.offset, item.end.offset)).toBe(
item.expression,
);
});
it("excludes erased TypeScript imports/exports and bundler inference", () => {
const result = collectWebModuleImports(
'import type {T} from "types"; import {type X} from "also-types"; export type {T} from "export-types"; export {type Y} from "only-types"; import {type T, value} from "mixed"; __webpack_require__.e(4); __vitePreload(() => 1, ["chunk.js"]);',
);
expect(result.imports.map((item) => item.specifier)).toEqual(["mixed"]);
});
it("uses UTF-16 source locations after astral characters", () => {
const source = 'const emoji = "😀";\nimport "./x.js";';
const item = collectWebModuleImports(source).imports[0];
expect(item?.start).toEqual({
offset: source.indexOf('"./x.js"'),
line: 2,
column: 7,
});
});
it("reports unparseable input without asserting an empty complete graph", () => {
const result = collectWebModuleImports('import "./ok.js"; function {');
expect(result).toMatchObject({ state: "unparseable", imports: [] });
expect(result.diagnostics.length).toBeGreaterThan(0);
});
});
const script = (
url: string,
kind: "page-script" | "scenario-response",
sha256 = "a".repeat(64),
) =>
exportedWebScriptSchema.parse({
url,
source:
kind === "page-script"
? {
kind,
script_key: "script1",
frame_id: null,
is_module: true,
language: "JavaScript",
source_map_url: null,
}
: {
kind,
transaction_id: "txn",
request_sequence: 1,
response_sequence: 2,
status: 200,
},
content: {
state: "exported",
relative_path: "modules/a.js",
layout: "isolated",
layout_reason: "fixture",
sha256,
bytes: 1,
media_type: "text/javascript",
redacted: false,
representation:
kind === "page-script"
? "debugger-source-utf8"
: "browser-decoded-response-bytes",
},
});
describe("captured module candidates", () => {
it("keeps query versions separate and response fragment matching weaker", () => {
const scripts = [
script("https://app.test/a.js?v=1#one", "page-script"),
script("https://app.test/a.js?v=1#two", "page-script"),
script("https://app.test/a.js?v=1", "scenario-response"),
script("https://app.test/a.js?v=2", "scenario-response"),
];
expect(
matchCapturedModules("https://app.test/a.js?v=1#one", scripts).map(
(item) => [item.script_index, item.match],
),
).toEqual([
[0, "exact-reported-url"],
[2, "response-url-without-fragment"],
]);
});
it("preserves every captured version at the same URL and never picks one", () => {
const url = "https://app.test/a.js";
const candidates = matchCapturedModules(url, [
script(url, "scenario-response"),
script(url, "scenario-response", "b".repeat(64)),
]);
expect(candidates).toHaveLength(2);
expect(
matchCapturedModules(
"https://app.test/lazy.js",
candidates.map((item) => item.script),
),
).toEqual([]);
});
});
+132
View File
@@ -0,0 +1,132 @@
import { traverseFast, isStringLiteral, isTemplateLiteral } from "@babel/types";
import type { Node } from "@babel/types";
import { parseJavaScriptSource } from "./javascriptSourceParser.js";
import type { WebModuleImport } from "./webModuleTrace.js";
import type { ExportedWebScript } from "./webScriptExport.js";
/** Collect native import syntax, excluding erased type-only references. */
export const collectWebModuleImports = (
source: string,
): {
readonly imports: readonly WebModuleImport[];
readonly state: "parsed" | "unparseable";
readonly diagnostics: readonly string[];
} => {
const ast = parseJavaScriptSource(source);
if (ast === null)
return {
imports: [],
state: "unparseable",
diagnostics: ["Source could not be parsed; imports are unknown."],
};
const imports: WebModuleImport[] = [];
const add = (kind: WebModuleImport["kind"], expression: Node): void => {
if (
expression.start === null ||
expression.start === undefined ||
expression.end === null ||
expression.end === undefined ||
expression.loc === null ||
expression.loc === undefined
)
return;
const specifier = isStringLiteral(expression)
? expression.value
: isTemplateLiteral(expression) && expression.expressions.length === 0
? (expression.quasis[0]?.value.cooked ?? null)
: null;
imports.push({
kind,
specifier,
expression: source.slice(expression.start, expression.end),
start: {
offset: expression.start,
line: expression.loc.start.line,
column: expression.loc.start.column,
},
end: {
offset: expression.end,
line: expression.loc.end.line,
column: expression.loc.end.column,
},
});
};
traverseFast(ast, (node) => {
if (node.type === "ImportDeclaration") {
if (node.importKind === "type" || node.importKind === "typeof") return;
if (
node.specifiers.length > 0 &&
node.specifiers.every(
(s) =>
s.type === "ImportSpecifier" &&
(s.importKind === "type" || s.importKind === "typeof"),
)
)
return;
add("static-import", node.source);
} else if (
node.type === "ExportAllDeclaration" ||
node.type === "ExportNamedDeclaration"
) {
if (
node.exportKind === "type" ||
node.source === null ||
node.source === undefined
)
return;
if (
node.type === "ExportNamedDeclaration" &&
node.specifiers.length > 0 &&
node.specifiers.every(
(s) => s.type === "ExportSpecifier" && s.exportKind === "type",
)
)
return;
add("re-export", node.source);
} else if (node.type === "ImportExpression")
add("dynamic-import", node.source);
else if (
node.type === "CallExpression" &&
node.callee.type === "Import" &&
node.arguments[0] !== undefined
)
add("dynamic-import", node.arguments[0]);
});
imports.sort((a, b) => a.start.offset - b.start.offset);
return {
imports,
state: "parsed",
diagnostics: ast.errors?.map((error) => error.message) ?? [],
};
};
/** Match reported URLs without collapsing query or module fragment identity. */
export const matchCapturedModules = (
url: string,
scripts: readonly ExportedWebScript[],
) => {
const candidates: {
script_index: number;
match: "exact-reported-url" | "response-url-without-fragment";
script: ExportedWebScript;
}[] = [];
scripts.forEach((script, script_index) => {
if (script.url === url)
candidates.push({ script_index, match: "exact-reported-url", script });
else if (script.source.kind === "scenario-response") {
try {
const transport = new URL(url);
transport.hash = "";
if (transport.href === script.url)
candidates.push({
script_index,
match: "response-url-without-fragment",
script,
});
} catch (cause: unknown) {
void cause;
}
}
});
return candidates;
};
+126
View File
@@ -0,0 +1,126 @@
import { z } from "zod";
import { digestSchema } from "./digests.js";
import {
exportedWebScriptSchema,
webScriptExportManifestSchema,
} from "./webScriptExport.js";
/** Inspect one captured script's native ES module relationships. */
export const webModuleTraceInputSchema = z.strictObject({
manifest_path: z
.string()
.min(1)
.describe("Absolute path to an export_web_scripts manifest"),
script_index: z
.number()
.int()
.min(0)
.describe("Zero-based index in manifest.scripts"),
importer_url: z
.string()
.min(1)
.optional()
.describe(
"Explicit HTTP(S) module URL; useful when captured inline/base context is unknown",
),
import_map: z
.strictObject({
path: z
.string()
.min(1)
.describe("Absolute local import-map JSON path; no fetch"),
base_url: z
.string()
.min(1)
.describe("Explicit HTTP(S) base for the selected map"),
})
.optional(),
});
export type WebModuleTraceInput = z.output<typeof webModuleTraceInputSchema>;
/** Byte identity of one selected local artifact. */
export const webModuleFileSchema = z.strictObject({
path: z.string().min(1),
sha256: digestSchema,
bytes: z.number().int().min(0),
});
export type WebModuleFile = z.output<typeof webModuleFileSchema>;
const positionSchema = z.strictObject({
offset: z.number().int().min(0),
line: z.number().int().min(1),
column: z.number().int().min(0),
});
/** Parser observations do not assert that a dependency was loaded. */
export const webModuleImportSchema = z.strictObject({
kind: z.enum(["static-import", "re-export", "dynamic-import"]),
specifier: z.string().nullable(),
expression: z.string(),
start: positionSchema,
end: positionSchema,
});
export type WebModuleImport = z.output<typeof webModuleImportSchema>;
/** Native resolver output, including the engine's exact failure text. */
export const webModuleResolutionSchema = z.discriminatedUnion("state", [
z.strictObject({ state: z.literal("resolved"), url: z.string().min(1) }),
z.strictObject({ state: z.literal("rejected"), message: z.string() }),
]);
export type WebModuleResolution = z.output<typeof webModuleResolutionSchema>;
const candidateSchema = z.strictObject({
script_index: z.number().int().min(0),
match: z.enum(["exact-reported-url", "response-url-without-fragment"]),
script: exportedWebScriptSchema,
});
/** Complete outgoing imports with captured candidates and explicit unknowns. */
export const webModuleTraceResultSchema = z.strictObject({
manifest: webModuleFileSchema.extend({
reported_output_directory: z.string(),
...webScriptExportManifestSchema.pick({
capture_path: true,
capture_sha256: true,
capture_kind: true,
capture_completeness: true,
source_evidence_id: true,
}).shape,
}),
source: webModuleFileSchema.extend({
script_index: z.number().int().min(0),
script: exportedWebScriptSchema,
}),
importer: z.strictObject({
url: z.string().min(1),
basis: z.enum(["caller-selected", "reported-source-url"]),
}),
import_map: webModuleFileSchema
.extend({ base_url: z.string().min(1) })
.nullable(),
parser: z.strictObject({
state: z.enum(["parsed", "unparseable"]),
diagnostics: z.array(z.string()),
location_units: z.literal("UTF-16 offsets and columns; one-based lines"),
}),
engine: z
.strictObject({ id: z.string().min(1), version: z.string().min(1) })
.nullable(),
imports: z.array(
webModuleImportSchema.extend({
resolution: z.union([
webModuleResolutionSchema,
z.strictObject({
state: z.literal("unknown"),
reason: z.literal("computed-specifier"),
}),
]),
captured_candidates: z.array(candidateSchema),
execution: z.literal("unknown"),
}),
),
diagnostics: z.array(z.string()),
limitations: z.array(z.string()),
});
export type WebModuleTraceResult = z.output<typeof webModuleTraceResultSchema>;
File diff suppressed because one or more lines are too long
+13
View File
@@ -16,6 +16,9 @@ import { registerArtifactTools } from "./registerArtifactTools.js";
import { registerBrowserScenarioTool } from "./registerBrowserScenarioTool.js";
import { registerBrowserTools } from "./registerBrowserTools.js";
import { registerWebScriptTool } from "./registerWebScriptTool.js";
import { registerWebModuleTool } from "./registerWebModuleTool.js";
import type { WebModuleTraceService } from "../application/WebModuleTraceService.js";
import { createWebModuleTraceService } from "../composition/webModules.js";
import { registerJavaScriptRecoveryTool } from "./registerJavaScriptRecoveryTool.js";
import { JavaScriptRecoveryService } from "../application/JavaScriptRecoveryService.js";
import type { JavaScriptRecoveryPort } from "../application/JavaScriptRecoveryPort.js";
@@ -50,6 +53,7 @@ export interface CreateServerOptions {
readonly logger?: Logger;
readonly firmwareAnalysis?: FirmwareAnalysisPort;
readonly javascriptRecovery?: JavaScriptRecoveryPort;
readonly webModuleTrace?: WebModuleTraceService;
readonly androidAnalysis?: AndroidAnalysisPort;
readonly browserObservation?: BrowserObservationPort;
readonly browserScenarioCapture?: BrowserScenarioCapturePort;
@@ -69,6 +73,9 @@ const installSessionToolAvailability = (
const policy = sessionAvailabilityPolicy(options.availabilityPolicy, {
optionalProviderLoadFailures: options.optionalProviderLoadFailures,
optionalFeatures: {
webModuleResolutionEnabled:
options.webModuleTrace !== undefined ||
isAbsolute(process.env.REA_BROWSER_EXECUTABLE ?? ""),
firmwareInspectionEnabled:
options.firmwareAnalysis !== undefined ||
(process.platform === "linux" &&
@@ -165,6 +172,12 @@ export const createServer = (
recordEvidence,
);
registerObservationTools(toolContext);
registerWebModuleTool(
server,
options.webModuleTrace ?? createWebModuleTraceService(),
toolLogger,
recordEvidence,
);
registerJavaScriptRecoveryTool(
server,
new JavaScriptRecoveryService(
+32
View File
@@ -0,0 +1,32 @@
import type { McpServer } from "@modelcontextprotocol/server";
import type { WebModuleTraceService } from "../application/WebModuleTraceService.js";
import type { EvidenceWriter } from "../application/investigation/InvestigationRecordPort.js";
import { toolContract } from "../contracts/toolContracts.js";
import type { Logger } from "../logger.js";
import { logToolExecution } from "./toolLogging.js";
import { toolRegistrationOptions } from "./toolRegistrationOptions.js";
import { toCallToolResult } from "./toolResult.js";
/** Bind the module relationship workflow to its exact canonical contract. */
export const registerWebModuleTool = (
server: McpServer,
service: WebModuleTraceService,
logger: Logger,
recordEvidence?: EvidenceWriter["recordEvidence"],
): void => {
const contract = toolContract("trace_web_module_imports");
server.registerTool(
contract.name,
toolRegistrationOptions(contract),
async (input, context) => {
const result = await logToolExecution(logger, contract.name, () =>
service.trace(input, { signal: context.mcpReq.signal }),
);
if (!result.ok) return toCallToolResult(result, contract);
const recorded = recordEvidence?.(result.value);
return recorded !== undefined && !recorded.ok
? toCallToolResult(recorded, contract)
: toCallToolResult(result, contract);
},
);
};
+3
View File
@@ -17,6 +17,7 @@ export interface SessionAvailabilityDefaults {
| "v8InspectorObservationEnabled"
| "androidAnalysisEnabled"
| "javascriptRecoveryEnabled"
| "webModuleResolutionEnabled"
| "firmwareInspectionEnabled"
| "firmwareExtractionEnabled"
>;
@@ -39,6 +40,8 @@ export const sessionAvailabilityPolicy = (
defaults.optionalFeatures?.androidAnalysisEnabled ?? false,
javascriptRecoveryEnabled:
defaults.optionalFeatures?.javascriptRecoveryEnabled ?? false,
webModuleResolutionEnabled:
defaults.optionalFeatures?.webModuleResolutionEnabled ?? false,
browserObservationEnabled:
defaults.optionalFeatures?.browserObservationEnabled ?? false,
browserScenarioEnabled: