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
@@ -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;
}
};