Files
reverse-engineering-anything/scripts/verify-native-ui.mjs
T
morluto cb49383269 refactor(browser): take the host in Chromium, Electron and capture adapters (#11130)
* refactor(browser): take the host in Chromium, Electron and capture adapters

chromiumSandboxEnabled and safeElectronEnvironment take a required platform. The scenario browser, native module resolver, Electron provider and capture cleanup, mitmproxy adapter and web script artifacts take an optional platform seam (default HOST_PLATFORM); Electron fuses read the host architecture from HOST_PLATFORM. Their tests inject the platform instead of redefining process.platform. src/browser no longer reads process.platform or process.arch.

* refactor(pcap): take the host platform in the packet tools, native bound and batch launch (#11141)

* refactor(pcap): take the host platform in the packet tools, native bound and batch launch

* test(pcap): cover the selected host in batch launch and capture-host support

* refactor(apktool): take the host platform in launcher selection, version probe and decode (#11164)

* refactor(apktool): take the host platform in launcher selection, version probe and decode

* test(apktool): cover the provider's selected host in launcher selection

* refactor(hopper): give the launcher the provider's host platform

* refactor(native): inject the host into the pwntools decoders and native capability defaults (#11173)

* refactor(native): inject the host into the pwntools decoders and native capability defaults

* fix(native): give composed pwntools services the capability host and drop empty test hooks

* refactor(adb): take the host platform in binary selection and pulled file names (#11176)

* refactor(adb): take the host platform in binary selection and pulled file names

* refactor(cli): pass the resolved host platform through rea mcp add (#11179)

* refactor(cli): pass the resolved host platform through rea mcp add

* refactor(composition): default composition entry points to HOST_PLATFORM and pass it to Hopper and IDA (#11183)

* refactor(kuna): require the host platform in admission and installation, and give it to the session client (#11186)

* refactor(kuna): require the host platform in admission and installation, and give it to the session client

* fix(kuna): pass the host platform from the JavaScript installation callers

verify-real-kuna, both native oracle helpers and the command-lifetime child script call inspectKunaInstallation from dist, where TypeScript cannot see the now-required platform. They passed undefined and got unsupported_host on supported hosts.

* refactor(server): give createServer a host option and pass it to session tools, sigrok and EVM (#11203)

* refactor(server): give createServer a host option and pass it to session tools, sigrok and EVM

createServer read process.platform and process.arch live for its capability services, and registerSessionTools read process.platform for process capture. A host option (default HOST_PLATFORM) now selects both, and composition passes the capability host to the sigrok provider and the EVM provider (execution gate, limiter probe and environment rules), as it does for pwntools.

hostRestrictedAvailabilityMcp selects its host through the option instead of redefining process.platform and process.arch, and moves to the MCP boundary lane since it no longer touches process-global state. evmCleanupRetry passes SIMULATED_LINUX_X64 to the provider instead of 52 property redefinitions; the fixture moves to tests/fixtures/simulatedHost.ts.

* refactor(desktop): require the host platform in build computer and secret folder paths (#11210)

* refactor(desktop): require the host platform in build computer and secret folder paths

buildComputerArchiveDirectory, defaultBuildComputerArchive, resolveBuildComputerEnvironment and buildSecretsDirectory defaulted to process.platform. Every caller but DesktopBuildComputers' secret folder already passed the platform; it now passes HOST_PLATFORM.platform, which it reads for the build environment.

* refactor(filesystem): default caller paths and ChangeTime settling to HOST_PLATFORM (#11212)

appendCallerPath, absoluteCallerPath and the ChangeTime settle seams defaulted to a live process.platform read. Their defaults now come from HOST_PLATFORM: the 59 callers that omit the platform join or settle paths they open on this host, and callers describing another host's layout already pass it.

The webScriptExport case that redefined process.platform to reach the Windows settle budget moves to its own file, which selects Windows by mocking HostPlatform.

* fix(server): report process capture availability for the server's host

sessionAvailabilityPolicy imported platform from node:process, so with a selected host binary_session reported capture_process_scenario from the running host while planning and capture used the selected one. It takes the platform from createServer and registerSessionTools. hostRestrictedAvailabilityMcp checks discovery against a dry-run plan on win32 and linux hosts.

* build(guards): count platform and arch imports from node:process and node:os as ambient host reads

sessionAvailabilityPolicy's named import of platform from node:process escaped the ambient-host-platform guard. The guards now also match named platform/arch imports from node:process or node:os and os.platform()/os.arch(). The one other site, DesktopApplicationCapture's os.arch(), records the capturing host and now reads HOST_PLATFORM.architecture.

* refactor(config): take the host platform in parseConfig and give debugger managers the capability host (#11237)

* refactor(config): take the host platform in parseConfig and give debugger managers the capability host

parseConfig read process.platform for the default Hopper launcher; it takes the platform (default HOST_PLATFORM). GdbSessionManager and RizinDebugSessionManager default their platform to HOST_PLATFORM, and composition passes the capability host that already decides their availability. GDB's /proc descriptor check reads HOST_PLATFORM explicitly: it inspects this host's /proc whatever host the manager models.

* refactor(firmware): give the firmware and Wakaru providers the capability host (#11239)

* refactor(firmware): give the firmware and Wakaru providers the capability host

resolveFirmwareCommand and resolveWakaruCommand read process.platform (and process.arch) for their host gate. They take the platform or host as a parameter; FirmwareProvider and WakaruProvider take a host (default HOST_PLATFORM) for the gate and their environment snapshots, and composition passes the capability host that already decides their availability. The JavaScript analysis source transfer defaults to HOST_PLATFORM.

artifactSelectedCommand passes SIMULATED_LINUX_X64 to both providers instead of redefining process.platform and process.arch, and the Wakaru limiter configuration cases now run on any POSIX host.

* refactor(captures): give tshark the capability host and default Cutter to HOST_PLATFORM (#11240)

* refactor(captures): give tshark the capability host and default Cutter to HOST_PLATFORM

TsharkCaptureProvider and CutterBridgeClient defaulted their platform to process.platform. Both default to HOST_PLATFORM now, and composition passes the capability host to the packet capture service, which already decides its availability.

* refactor(platform): read HOST_PLATFORM explicitly in host-only path checks (#11241)

* refactor(platform): read HOST_PLATFORM explicitly in host-only path checks

Electron's readlink target split, IDA's host path comparison, the inspector's reported runtime path, the server identity check and WABT's executable names all describe paths on the running host. They read HOST_PLATFORM instead of process.platform.

* test(native): select the native UI verifier's host through HostPlatform (#11242)

verify-native-ui.mjs reads its macOS requirement from HOST_PLATFORM, and its action test resolves the script's HostPlatform import to a simulated darwin host instead of redefining process.platform in the child preload. Simulating linux there fails the macOS requirement, so the redirect is effective.

* test(mcp): check that composition forwards the selected host to execution

hostRestrictedAvailabilityMcp ran execution only on darwin, where the runner's own host also refuses. The new case calls the firmware, Wakaru, tshark, sigrok and EVM tools under a darwin and a linux x64 host and requires different outcomes, so dropping a provider's host forwarding fails on any runner (checked by removing the firmware forwarding).

* fix(composition): give the pcap, Apktool and ADB providers the capability host

createServer's selected host decided pcap capture discovery, but the default pcap, Apktool and ADB factories built their providers with HOST_PLATFORM. The factories take the platform (default HOST_PLATFORM) and capabilityServices passes its context platform, so availability and execution follow one host.

* test: import moved resources owners in host-platform fixtures

* refactor(config): drop the unused host parameter from parseConfig
2026-11-16 04:27:07 +08:00

338 lines
12 KiB
JavaScript

import { execFile, spawn } from "node:child_process";
import assert from "node:assert/strict";
import { mkdir, writeFile, readFile } from "node:fs/promises";
import { join } from "node:path";
import { fileURLToPath } from "node:url";
import { promisify } from "node:util";
import { parseBinaryTarget } from "../dist/application/BinaryTargetResolver.js";
import { observeNativeUi } from "../dist/native/NativeUiObservation.js";
import { NATIVE_UI_HELPER_MAX_BUFFER } from "../dist/native/NativeUiOutputBudget.js";
import { artifactMcpResult, withArtifactMcp } from "./lib/artifact-e2e.mjs";
import { observeProcessStartIdentity } from "../dist/process/ProcessOwnershipObservation.js";
import { HOST_PLATFORM } from "../dist/platform/HostPlatform.js";
import { VerifierFixture } from "./lib/verifier-fixture.mjs";
if (HOST_PLATFORM.platform !== "darwin")
throw new Error(
"Native UI verification requires macOS, an interactive desktop and Xcode command-line tools",
);
const rootFixture = await VerifierFixture.open({
prefix: "rea-ui-fixture-",
private: false,
});
const root = rootFixture.path;
let child;
let childExit;
let fixturePid;
let fixtureIdentity;
let report;
// `open -W` exits only after the app exits, so its own exit is the proof that
// the fixture no longer runs from the root. Without it the root is retained.
let retainedRoot;
const FIXTURE_EXIT_GRACE_MS = 10_000;
try {
const childRetrievalTest = join(root, "child-retrieval-test");
await promisify(execFile)("/usr/bin/xcrun", [
"swiftc",
fileURLToPath(
new URL("../bridge/native/NativeUIChildren.swift", import.meta.url),
),
fileURLToPath(
new URL("../bridge/native/NativeUIValues.swift", import.meta.url),
),
fileURLToPath(
new URL(
"../tests/conformance/native/native-ui-children/main.swift",
import.meta.url,
),
),
"-o",
childRetrievalTest,
]);
const childRetrieval = await promisify(execFile)(childRetrievalTest, []);
const [slashRichProducerJson, numericProducerJson] = childRetrieval.stdout
.trimEnd()
.split("\n");
if (
slashRichProducerJson === undefined ||
numericProducerJson === undefined
) {
throw new Error(
"Native UI serializer seam did not emit its producer bytes",
);
}
const slashRichConsumerJson = JSON.stringify(
JSON.parse(slashRichProducerJson),
);
if (slashRichProducerJson !== slashRichConsumerJson) {
throw new Error(
"Swift helper JSON bytes differ from the normalized JavaScript consumer representation",
);
}
const numericConsumerJson = JSON.stringify(JSON.parse(numericProducerJson));
if (
Buffer.byteLength(numericProducerJson) <=
Buffer.byteLength(numericConsumerJson)
) {
throw new Error(
"Foundation numeric fixture no longer exercises larger producer JSON bytes",
);
}
if (Buffer.byteLength(numericProducerJson) > NATIVE_UI_HELPER_MAX_BUFFER) {
throw new Error("Foundation numeric fixture exceeds the raw helper budget");
}
const contents = join(root, "Fixture.app", "Contents");
await mkdir(join(contents, "MacOS"), { recursive: true });
await writeFile(
join(contents, "Info.plist"),
`<?xml version="1.0"?><plist version="1.0"><dict><key>CFBundleExecutable</key><string>fixture</string><key>CFBundleIdentifier</key><string>io.rea.source-owned-ui-fixture</string><key>CFBundleName</key><string>REA UI Fixture</string><key>CFBundlePackageType</key><string>APPL</string></dict></plist>`,
);
const executable = join(contents, "MacOS", "fixture");
await promisify(execFile)("/usr/bin/xcrun", [
"swiftc",
"-module-cache-path",
join(root, "modules"),
fileURLToPath(
new URL("../tests/conformance/native/ui.swift", import.meta.url),
),
"-o",
executable,
]);
const coordinates = join(root, "scope.json");
await writeFile(coordinates, "");
child = spawn(
"/usr/bin/open",
["-W", "-n", "-g", "--stdout", coordinates, join(root, "Fixture.app")],
{ stdio: "ignore" },
);
childExit = new Promise((resolve) => child.once("exit", resolve));
let scope;
const deadline = Date.now() + 20000;
while (Date.now() < deadline) {
const output = await readFile(coordinates, "utf8");
if (output.includes("\n")) {
scope = JSON.parse(output.split("\n")[0]);
break;
}
await new Promise((resolve) => setTimeout(resolve, 100));
}
if (scope === undefined)
throw new Error(
"Source-owned UI fixture failed to expose one window within 20 seconds",
);
fixturePid = scope.pid;
fixtureIdentity = await observeProcessStartIdentity(fixturePid);
assert.equal(
fixtureIdentity?.state,
"readable",
"Fixture OS start identity is unavailable",
);
const target = await parseBinaryTarget(executable);
if (!target.ok) throw target.error;
const observation = await observeNativeUi(
target.value,
"observe_native_ui",
{
...scope,
},
{ environment: process.env },
);
if (
!observation.ok &&
!/accessibility-denied|screen-recording-denied/u.test(
observation.error.message,
)
)
throw observation.error;
if (!observation.ok && !process.argv.includes("--allow-permission-denial"))
throw new Error(
`Native UI E2E requires successful capture; permissions prevented coverage: ${observation.error.message}. Use --allow-permission-denial only for the separate permission diagnostic lane.`,
);
let scenarioStatus = "not-run-permission-denied";
if (observation.ok) {
const button = observation.value.initial.nodes.find(
(node) => node.title === "Increment REA fixture",
);
if (button === undefined)
throw new Error("Source-owned fixture button is absent from AX capture");
const scenario = await observeNativeUi(
target.value,
"capture_native_ui_scenario",
{
...scope,
screenshot: false,
steps: [
{ kind: "click", path: button.path },
{ kind: "wait", milliseconds: 200 },
],
},
{ environment: process.env },
);
if (!scenario.ok) throw scenario.error;
if (
scenario.value.steps.some((step) => step.outcome !== "completed") ||
!scenario.value.steps
.at(-1)
?.after?.nodes.some(
(node) => node.title === "REA fixture incremented 1",
)
)
throw new Error(
`Source-owned AX scenario failed: ${JSON.stringify(scenario.value.steps.map(({ outcome, reason }) => ({ outcome, reason })))}`,
);
scenarioStatus = "selected-AX-button-action-observed";
const cli = await promisify(execFile)(
process.execPath,
[
fileURLToPath(new URL("./rea.mjs", import.meta.url)),
"capture-native-ui-scenario",
executable,
"--pid",
String(scope.pid),
"--window-id",
String(scope.window_id),
"--steps",
JSON.stringify([
{ kind: "click", path: button.path },
{ kind: "wait", milliseconds: 200 },
]),
"--json",
],
{ maxBuffer: 32 * 1024 * 1024, timeout: 180000 },
);
const evidence = JSON.parse(cli.stdout);
if (
evidence.normalized_result?.steps?.some(
(step) => step.outcome !== "completed",
) ||
evidence.normalized_result?.steps?.length !== 2 ||
!evidence.normalized_result?.steps
?.at(-1)
?.after?.nodes.some(
(node) => node.title === "REA fixture incremented 2",
)
)
throw new Error(
`Native UI CLI did not preserve the selected scenario: ${JSON.stringify(evidence.error ?? evidence.normalized_result?.steps?.map(({ outcome, reason }) => ({ outcome, reason })))}`,
);
scenarioStatus = "selected-AX-button-action-and-CLI-parity-observed";
await withArtifactMcp(executable, async (client) => {
const observed = await artifactMcpResult(client, "observe_native_ui", {
...scope,
screenshot: false,
});
if (
!observed.initial.nodes.some(
(node) =>
node.role === "AXButton" &&
JSON.stringify(node.path) === JSON.stringify(button.path),
)
)
throw new Error("MCP omitted the selected fixture's actual AX button");
const captured = await artifactMcpResult(
client,
"capture_native_ui_scenario",
{
...scope,
screenshot: false,
steps: [
{ kind: "click", path: button.path },
{ kind: "wait", milliseconds: 200 },
],
},
);
if (
captured.steps.length !== 2 ||
captured.steps.some((step) => step.outcome !== "completed") ||
!captured.steps
.at(-1)
?.after?.nodes.some(
(node) => node.title === "REA fixture incremented 3",
)
)
throw new Error("MCP did not observe the selected fixture's AX action");
});
scenarioStatus = "selected-AX-button-action-and-CLI-MCP-parity-observed";
}
const mismatch = await observeNativeUi(
{ ...target.value, sha256: "0".repeat(64) },
"observe_native_ui",
scope,
{ environment: process.env },
);
// A selection failure carries its helper code in the issue, not in its message.
if (
mismatch.ok ||
!mismatch.error.issues?.some(({ message }) =>
message.startsWith("target-mismatch:"),
)
)
throw new Error("Real helper did not reject changed target bytes");
report = {
ok: true,
verification_status: observation.ok ? "passed" : "permission-boundary-only",
positive_e2e: observation.ok,
observation: observation.ok
? "captured-selected-fixture-window"
: "os-permission-denial-verified",
scenario: scenarioStatus,
permission_failure: observation.ok ? null : observation.error.message,
mismatch_rejected: true,
target_owned: true,
};
} finally {
if (fixturePid !== undefined && fixtureIdentity?.state === "readable") {
const current = await observeProcessStartIdentity(fixturePid);
if (current !== undefined) {
assert.ok(
current.state === "readable" &&
current.identity === fixtureIdentity.identity,
"Fixture PID identity changed; refusing to signal another process",
);
try {
process.kill(fixturePid, "SIGTERM");
} catch (cause) {
if (cause.code !== "ESRCH") throw cause;
}
}
}
const fixtureExited =
child === undefined || (await openExitedWithFixture(child, childExit));
if (fixtureExited) await rootFixture.release();
else {
// The app may have been reparented away from `open`; stopping `open`
// does not prove the fixture exited, so its root stays in place.
if (child.exitCode === null && child.signalCode === null) {
child.kill("SIGTERM");
await childExit;
}
retainedRoot = root;
process.stderr.write(
`Source-owned UI fixture exit was not observed; retained ${root}\n`,
);
}
}
if (retainedRoot !== undefined)
throw new Error(
`Source-owned UI fixture exit was not observed; retained ${retainedRoot}`,
);
process.stdout.write(`${JSON.stringify(report)}\n`);
// `open -W` exits on its own only after the app exits (or never launched it).
// A signaled `open`, or none within the grace period, leaves the app unproven.
async function openExitedWithFixture(launcher, exit) {
if (launcher.exitCode === null && launcher.signalCode === null) {
let timer;
const exited = await Promise.race([
exit.then(() => true),
new Promise((resolve) => {
timer = setTimeout(() => resolve(false), FIXTURE_EXIT_GRACE_MS);
}),
]);
clearTimeout(timer);
if (!exited) return false;
}
return launcher.exitCode !== null && launcher.signalCode === null;
}