mirror of
https://github.com/morluto/rea.git
synced 2026-11-16 19:53:50 +00:00
* 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
778 lines
26 KiB
TypeScript
778 lines
26 KiB
TypeScript
import { createTestArtifactResourceScope } from "../../tests/fixtures/artifactResourceScope.js";
|
|
import { cleanupOwnedProcessGroup } from "../process/ProcessOwnership.js";
|
|
import { EventEmitter } from "node:events";
|
|
import { chmod, mkdir, writeFile } from "node:fs/promises";
|
|
import { join } from "node:path";
|
|
import { PassThrough } from "node:stream";
|
|
|
|
import { expect, it as test, vi } from "vitest";
|
|
|
|
import { createTestTempDirectory } from "../../tests/fixtures/temporaryDirectory.js";
|
|
import { AnalysisCapabilityUnavailableError } from "../domain/analysisErrorCore.js";
|
|
import { projectAnalysisError } from "../domain/analysisErrorProjection.js";
|
|
import { FIRMWARE_LIMITS } from "./FirmwareRelease.js";
|
|
import { firmwareSignalLimitError } from "./FirmwareFailures.js";
|
|
import { FirmwareFileAccess } from "./FirmwareFiles.js";
|
|
import {
|
|
resolveFirmwareCommand,
|
|
runFirmwareCommand,
|
|
type FirmwareLauncher,
|
|
} from "./FirmwareCommand.js";
|
|
import {
|
|
spawnOwnedProviderProcess,
|
|
type ProviderProcessHandle,
|
|
type ProviderProcessSupervisor,
|
|
type OwnedProviderProcessSpawnOptions,
|
|
} from "../process/ProviderProcess.js";
|
|
import { AnalysisError } from "../domain/analysisErrorBase.js";
|
|
import {
|
|
ProcessOwnershipInspectionCancelled,
|
|
ProcessOwnershipIdentityBatchFailure,
|
|
} from "../process/ProcessInspectionCancellation.js";
|
|
|
|
const it = test.skipIf(process.platform !== "linux");
|
|
|
|
it.each([
|
|
{ source: "analysis", wrapped: false },
|
|
{ source: "caller", wrapped: false },
|
|
{ source: "analysis", wrapped: true },
|
|
{ source: "caller", wrapped: true },
|
|
] as const)(
|
|
"classifies $source cancellation during launcher admission (wrapped: $wrapped)",
|
|
async ({ source, wrapped }) => {
|
|
const analysis = new AbortController();
|
|
const caller = new AbortController();
|
|
const reason =
|
|
source === "analysis"
|
|
? new DOMException("selected deadline", "TimeoutError")
|
|
: { caller: "opaque admission cancellation" };
|
|
const retainCleanup = vi.fn();
|
|
const failure: unknown = await runFirmwareCommand({
|
|
resources: createTestArtifactResourceScope(),
|
|
fileSizeCeiling: Number.MAX_SAFE_INTEGER,
|
|
engine: "binwalk",
|
|
operation: "inspect_firmware_regions",
|
|
command: "/usr/bin/true",
|
|
limiter: "/usr/bin/true",
|
|
args: ["--version"],
|
|
cwd: await createTestTempDirectory("rea-firmware-admission-"),
|
|
environment: process.env,
|
|
signal: AbortSignal.any([analysis.signal, caller.signal]),
|
|
deadline: {
|
|
phase: "analysis",
|
|
signal: analysis.signal,
|
|
timeoutMs: 1234,
|
|
expiresAtMs: performance.now() + 1234,
|
|
},
|
|
launcher: async (options) => {
|
|
// Real launcher is paused at its first ownership-preparation await;
|
|
// it must reject before acquiring a child after this cancellation.
|
|
const admission = wrapped
|
|
? undefined
|
|
: spawnOwnedProviderProcess(options);
|
|
(source === "analysis" ? analysis : caller).abort(reason);
|
|
if (admission !== undefined) return admission;
|
|
throw new ProcessOwnershipInspectionCancelled(reason);
|
|
},
|
|
retainCleanup,
|
|
}).catch((cause: unknown) => cause);
|
|
expect(failure).toBeInstanceOf(AnalysisError);
|
|
if (!(failure instanceof AnalysisError))
|
|
throw new Error("Expected typed admission interruption");
|
|
expect(failure.cause).toBe(reason);
|
|
expect(projectAnalysisError(failure)).toMatchObject(
|
|
source === "analysis"
|
|
? {
|
|
code: "provider_timeout",
|
|
details: { timeout_ms: 1234, diagnostics: { phase: "analysis" } },
|
|
remediation: {
|
|
action: expect.stringContaining("analysis_timeout_ms"),
|
|
},
|
|
}
|
|
: { code: "cancelled" },
|
|
);
|
|
expect(retainCleanup).not.toHaveBeenCalled();
|
|
},
|
|
);
|
|
|
|
test.each([
|
|
"direct",
|
|
"batch",
|
|
"deadline-batch",
|
|
"physical-identity",
|
|
"physical-name",
|
|
] as const)(
|
|
"classifies %s by the ownership producer rather than abort resemblance",
|
|
async (kind) => {
|
|
const controller = new AbortController();
|
|
const reason = { caller: "typed admission cancellation" };
|
|
const cancelled = new ProcessOwnershipInspectionCancelled(reason);
|
|
const physical = new Error("physical admission failure", { cause: reason });
|
|
physical.name = "AbortError";
|
|
const batch = new ProcessOwnershipIdentityBatchFailure(
|
|
[{ pid: 42, requestIndex: 0, cause: cancelled }],
|
|
new Map([
|
|
[
|
|
44,
|
|
{
|
|
state: "unavailable" as const,
|
|
reason: "completed sibling observation",
|
|
},
|
|
],
|
|
]),
|
|
);
|
|
const source =
|
|
kind === "direct" ? cancelled : kind.endsWith("batch") ? batch : physical;
|
|
const failure = await runFirmwareCommand({
|
|
resources: createTestArtifactResourceScope(),
|
|
fileSizeCeiling: Number.MAX_SAFE_INTEGER,
|
|
engine: "binwalk",
|
|
operation: "inspect_firmware_regions",
|
|
command: "/unused/binwalk",
|
|
limiter: "/unused/prlimit",
|
|
args: [],
|
|
cwd: "/unused",
|
|
environment: {},
|
|
signal: controller.signal,
|
|
deadline: {
|
|
phase: "analysis",
|
|
signal:
|
|
kind === "deadline-batch"
|
|
? controller.signal
|
|
: new AbortController().signal,
|
|
timeoutMs: 30_000,
|
|
expiresAtMs: performance.now() + 30_000,
|
|
},
|
|
launcher: async () => {
|
|
if (kind.startsWith("physical"))
|
|
controller.abort(kind === "physical-identity" ? physical : reason);
|
|
else if (kind === "deadline-batch") controller.abort(reason);
|
|
throw source;
|
|
},
|
|
retainCleanup: vi.fn(),
|
|
}).catch((cause: unknown) => cause);
|
|
if (!(failure instanceof AnalysisError)) throw failure;
|
|
if (kind.startsWith("physical")) {
|
|
expect(failure).toMatchObject({
|
|
_tag: "ProviderAdapterError",
|
|
cause: physical,
|
|
});
|
|
expect(failure.cause).toBe(physical);
|
|
} else if (kind === "deadline-batch") {
|
|
expect(failure).toMatchObject({
|
|
_tag: "AnalysisTimeoutError",
|
|
cause: reason,
|
|
});
|
|
const [cancelled, ...extra] = failure.secondaryFailures;
|
|
expect(extra).toEqual([]);
|
|
if (cancelled === undefined || !("inspectionFailure" in cancelled))
|
|
throw new Error("Missing deadline inspection origin");
|
|
expect(cancelled.cause).toBe(reason);
|
|
expect(cancelled.inspectionFailure).toBe(batch);
|
|
} else {
|
|
expect(failure).toMatchObject({
|
|
_tag: "AnalysisCancelledError",
|
|
cause: reason,
|
|
});
|
|
expect(failure.cause).toBe(reason);
|
|
if (!("inspectionFailure" in failure))
|
|
throw new Error("Missing ownership inspection origin");
|
|
expect(failure.inspectionFailure).toBe(source);
|
|
}
|
|
},
|
|
);
|
|
|
|
it("does not relabel an unrelated launch failure when a deadline also fires", async () => {
|
|
const analysis = new AbortController();
|
|
const failure = new Error("distinct launch refusal");
|
|
await expect(
|
|
runFirmwareCommand({
|
|
resources: createTestArtifactResourceScope(),
|
|
fileSizeCeiling: Number.MAX_SAFE_INTEGER,
|
|
engine: "binwalk",
|
|
operation: "inspect_firmware_regions",
|
|
command: "/usr/bin/true",
|
|
limiter: "/usr/bin/true",
|
|
args: [],
|
|
cwd: await createTestTempDirectory("rea-firmware-admission-error-"),
|
|
environment: process.env,
|
|
signal: analysis.signal,
|
|
deadline: {
|
|
phase: "analysis",
|
|
signal: analysis.signal,
|
|
timeoutMs: 1234,
|
|
expiresAtMs: performance.now() + 1234,
|
|
},
|
|
launcher: async () => {
|
|
analysis.abort();
|
|
throw failure;
|
|
},
|
|
retainCleanup: vi.fn(),
|
|
}),
|
|
).rejects.toMatchObject({ _tag: "ProviderAdapterError", cause: failure });
|
|
});
|
|
|
|
test("names the host and tool requirement of an unconfigured engine", async () => {
|
|
const refusal: unknown = await resolveFirmwareCommand(
|
|
{},
|
|
"binwalk",
|
|
"inspect_firmware_regions",
|
|
new FirmwareFileAccess(),
|
|
"linux",
|
|
).catch((cause: unknown) => cause);
|
|
|
|
expect(refusal).toMatchObject({
|
|
_tag: "AnalysisCapabilityUnavailableError",
|
|
});
|
|
if (!(refusal instanceof AnalysisCapabilityUnavailableError))
|
|
throw new Error("Expected a capability refusal");
|
|
expect(projectAnalysisError(refusal)).toMatchObject({
|
|
code: "capability_unavailable",
|
|
message: refusal.reason,
|
|
remediation: { action: refusal.reason },
|
|
});
|
|
expect(refusal.reason).toContain("REA_BINWALK_COMMAND");
|
|
});
|
|
|
|
it("rejects a directory configured as the firmware engine executable", async () => {
|
|
const root = await createTestTempDirectory("rea-firmware-command-");
|
|
const command = join(root, "binwalk");
|
|
const limiter = await executableFixture(root, "prlimit");
|
|
await mkdir(command);
|
|
|
|
await expect(
|
|
resolveFirmwareCommand(
|
|
{
|
|
REA_BINWALK_COMMAND: command,
|
|
REA_FIRMWARE_PRLIMIT_COMMAND: limiter,
|
|
},
|
|
"binwalk",
|
|
"inspect_firmware_regions",
|
|
new FirmwareFileAccess(),
|
|
"linux",
|
|
),
|
|
).rejects.toMatchObject({
|
|
_tag: "AnalysisCapabilityUnavailableError",
|
|
reason: expect.stringContaining(
|
|
`REA_BINWALK_COMMAND is not a readable executable regular file: ${command}: Configured path is not a regular file`,
|
|
),
|
|
});
|
|
});
|
|
|
|
it("classifies an unusable firmware engine as an unavailable capability", async () => {
|
|
const root = await createTestTempDirectory("rea-firmware-command-");
|
|
const command = join(root, "binwalk");
|
|
const limiter = await executableFixture(root, "prlimit");
|
|
await writeFile(command, "not executable", { mode: 0o600 });
|
|
|
|
await expect(
|
|
resolveFirmwareCommand(
|
|
{
|
|
REA_BINWALK_COMMAND: command,
|
|
REA_FIRMWARE_PRLIMIT_COMMAND: limiter,
|
|
},
|
|
"binwalk",
|
|
"inspect_firmware_regions",
|
|
new FirmwareFileAccess(),
|
|
"linux",
|
|
),
|
|
).rejects.toMatchObject({
|
|
_tag: "AnalysisCapabilityUnavailableError",
|
|
reason: expect.stringContaining(command),
|
|
});
|
|
});
|
|
|
|
it("rejects a directory configured as the resource limiter executable", async () => {
|
|
const root = await createTestTempDirectory("rea-firmware-limiter-");
|
|
const command = await executableFixture(root, "binwalk");
|
|
const limiter = join(root, "prlimit");
|
|
await mkdir(limiter);
|
|
|
|
await expect(
|
|
resolveFirmwareCommand(
|
|
{
|
|
REA_BINWALK_COMMAND: command,
|
|
REA_FIRMWARE_PRLIMIT_COMMAND: limiter,
|
|
},
|
|
"binwalk",
|
|
"inspect_firmware_regions",
|
|
new FirmwareFileAccess(),
|
|
"linux",
|
|
),
|
|
).rejects.toMatchObject({
|
|
_tag: "AnalysisCapabilityUnavailableError",
|
|
reason: expect.stringContaining(
|
|
`Resource limiter is not an executable regular file: ${limiter}: Configured path is not a regular file; provide util-linux prlimit`,
|
|
),
|
|
});
|
|
});
|
|
|
|
// A launched producer that writes its output, then stops on a limit signal.
|
|
const signalledLauncher = (
|
|
signal: NodeJS.Signals,
|
|
output: { stdout: string; stderr: string },
|
|
): FirmwareLauncher => {
|
|
const stdout = new PassThrough();
|
|
const stderr = new PassThrough();
|
|
const emitter = new EventEmitter();
|
|
const child: ProviderProcessHandle = {
|
|
on: emitter.on.bind(emitter),
|
|
once: emitter.once.bind(emitter),
|
|
off: emitter.off.bind(emitter),
|
|
pid: 4242,
|
|
stdout,
|
|
stderr,
|
|
exitCode: null,
|
|
signalCode: signal,
|
|
kill: () => false,
|
|
};
|
|
let ended = 0;
|
|
const close = () => {
|
|
ended += 1;
|
|
if (ended === 2) emitter.emit("close", null, signal);
|
|
};
|
|
stdout.once("end", close);
|
|
stderr.once("end", close);
|
|
setTimeout(() => {
|
|
stdout.end(output.stdout);
|
|
stderr.end(output.stderr);
|
|
}, 15);
|
|
return () =>
|
|
Promise.resolve({
|
|
process: child,
|
|
ownership: {
|
|
runId: "rea-firmware-signal",
|
|
leaderPid: 4242,
|
|
processGroupId: 4242,
|
|
},
|
|
cleanup: () => Promise.resolve({ cleaned: true, signaled: false }),
|
|
});
|
|
};
|
|
|
|
const analysisDeadline = (signal: AbortSignal) => ({
|
|
phase: "analysis" as const,
|
|
signal,
|
|
timeoutMs: 30_000,
|
|
expiresAtMs: performance.now() + 30_000,
|
|
});
|
|
|
|
it("keeps captured tool output when firmware stops on SIGXCPU", async () => {
|
|
const analysis = new AbortController();
|
|
const failure: unknown = await runFirmwareCommand({
|
|
resources: createTestArtifactResourceScope(),
|
|
fileSizeCeiling: Number.MAX_SAFE_INTEGER,
|
|
engine: "binwalk",
|
|
operation: "inspect_firmware_regions",
|
|
command: "/opt/binwalk",
|
|
limiter: "/usr/bin/prlimit",
|
|
args: ["--quiet"],
|
|
cwd: await createTestTempDirectory("rea-firmware-signal-"),
|
|
environment: process.env,
|
|
signal: analysis.signal,
|
|
deadline: analysisDeadline(analysis.signal),
|
|
launcher: signalledLauncher("SIGXCPU", {
|
|
stdout: "partial scan\n",
|
|
stderr: "cpu limit\n",
|
|
}),
|
|
retainCleanup: vi.fn(),
|
|
}).catch((cause: unknown) => cause);
|
|
expect(failure).toMatchObject({
|
|
_tag: "AnalysisResourceConstraintError",
|
|
resource: "cpu",
|
|
capturedOutput: {
|
|
stdout: "partial scan\n",
|
|
stderr: "cpu limit\n",
|
|
signal: "SIGXCPU",
|
|
exit_code: null,
|
|
truncated: false,
|
|
},
|
|
});
|
|
if (!(failure instanceof AnalysisError))
|
|
throw new Error("Expected a firmware resource constraint");
|
|
expect(projectAnalysisError(failure).details?.captured_output).toMatchObject({
|
|
stdout: "partial scan\n",
|
|
stderr: "cpu limit\n",
|
|
signal: "SIGXCPU",
|
|
});
|
|
});
|
|
|
|
const sixteenMiB = 16 * 1024 * 1024;
|
|
it.each([
|
|
{
|
|
inherited: Number.MAX_SAFE_INTEGER,
|
|
limit: sixteenMiB + 1,
|
|
adjustable: true,
|
|
},
|
|
{ inherited: sixteenMiB + 1, limit: sixteenMiB + 1, adjustable: false },
|
|
])(
|
|
"recommends a larger max_output_bytes on SIGXFSZ only when it raises the limit (inherited $inherited)",
|
|
async ({ inherited, limit, adjustable }) => {
|
|
const root = await createTestTempDirectory("rea-firmware-fsize-signal-");
|
|
const analysis = new AbortController();
|
|
const failure: unknown = await runFirmwareCommand({
|
|
resources: createTestArtifactResourceScope(),
|
|
fileSizeCeiling: inherited,
|
|
engine: "unblob",
|
|
operation: "extract_firmware",
|
|
command: "/opt/unblob",
|
|
limiter: "/usr/bin/prlimit",
|
|
args: [],
|
|
cwd: root,
|
|
environment: process.env,
|
|
signal: analysis.signal,
|
|
deadline: analysisDeadline(analysis.signal),
|
|
launcher: signalledLauncher("SIGXFSZ", { stdout: "", stderr: "" }),
|
|
outputBudget: { root, bytes: sixteenMiB, entries: 10 },
|
|
retainCleanup: vi.fn(),
|
|
}).catch((cause: unknown) => cause);
|
|
expect(failure).toMatchObject({
|
|
_tag: "AnalysisResourceConstraintError",
|
|
resource: "file-size",
|
|
reportedLimits: { file_size_bytes: limit },
|
|
retryable: false,
|
|
});
|
|
// Compare with the recovery owner instead of its wording; the two branches
|
|
// must differ for the comparison to pin the selected one.
|
|
const recovery = (fileSizeAdjustable: boolean) =>
|
|
firmwareSignalLimitError(
|
|
"extract_firmware",
|
|
"SIGXFSZ",
|
|
limit,
|
|
fileSizeAdjustable,
|
|
)?.remediationAction;
|
|
expect(recovery(true)).not.toBe(recovery(false));
|
|
expect(failure).toHaveProperty("remediationAction", recovery(adjustable));
|
|
},
|
|
);
|
|
|
|
const executableFixture = async (root: string, name: string) => {
|
|
const path = join(root, name);
|
|
await writeFile(path, "#!/bin/sh\n");
|
|
await chmod(path, 0o700);
|
|
return path;
|
|
};
|
|
|
|
test.each([
|
|
{
|
|
selected: 1,
|
|
inherited: Number.MAX_SAFE_INTEGER,
|
|
expected: 8 * 1024 * 1024 + 1,
|
|
},
|
|
{
|
|
selected: 16 * 1024 * 1024,
|
|
inherited: Number.MAX_SAFE_INTEGER,
|
|
expected: 16 * 1024 * 1024 + 1,
|
|
},
|
|
{
|
|
selected: 16 * 1024 * 1024,
|
|
inherited: 16 * 1024 * 1024,
|
|
expected: 16 * 1024 * 1024,
|
|
},
|
|
{ selected: 16 * 1024 * 1024, inherited: 1024, expected: 1024 },
|
|
])(
|
|
"separates payload $selected from metadata file headroom and inherited ceiling $inherited",
|
|
async ({ selected, inherited, expected }) => {
|
|
const root = await createTestTempDirectory("rea-firmware-file-policy-");
|
|
const refused = new Error("stop after inspecting launch policy");
|
|
const launcher = vi.fn(
|
|
async (options: OwnedProviderProcessSpawnOptions) => {
|
|
expect(options.arguments).toContain(`--fsize=${expected}:${expected}`);
|
|
expect(options.arguments).toContain(
|
|
`--as=${FIRMWARE_LIMITS.addressSpaceBytes}:${FIRMWARE_LIMITS.addressSpaceBytes}`,
|
|
);
|
|
throw refused;
|
|
},
|
|
);
|
|
const failure: unknown = await runFirmwareCommand({
|
|
resources: createTestArtifactResourceScope(),
|
|
engine: "unblob",
|
|
operation: "extract_firmware",
|
|
command: "/synthetic/unblob",
|
|
limiter: "/synthetic/prlimit",
|
|
fileSizeCeiling: inherited,
|
|
args: [],
|
|
cwd: root,
|
|
environment: {},
|
|
signal: new AbortController().signal,
|
|
deadline: {
|
|
phase: "analysis",
|
|
signal: new AbortController().signal,
|
|
timeoutMs: 120_000,
|
|
expiresAtMs: performance.now() + 120_000,
|
|
},
|
|
launcher,
|
|
outputBudget: { root, bytes: selected, entries: 10 },
|
|
retainCleanup: vi.fn(),
|
|
}).catch((cause: unknown) => cause);
|
|
expect(failure).toMatchObject({ _tag: "ProviderAdapterError" });
|
|
if (!(failure instanceof AnalysisError)) throw failure;
|
|
expect(failure.cause).toBe(refused);
|
|
expect(failure.cleanupResources).toEqual([]);
|
|
expect(launcher).toHaveBeenCalledOnce();
|
|
},
|
|
);
|
|
|
|
test.each([
|
|
{ exitCode: 0, cleanupFails: false },
|
|
{ exitCode: 7, cleanupFails: false },
|
|
{ exitCode: 7, cleanupFails: true },
|
|
])(
|
|
"preserves completed exit $exitCode through cancellation during real owned cleanup (fails: $cleanupFails)",
|
|
async ({ exitCode, cleanupFails }) => {
|
|
const root = await createTestTempDirectory("rea-firmware-finalization-");
|
|
const caller = new AbortController();
|
|
const reason = { caller: "cancel during final cleanup" };
|
|
let cleanupCalls = 0;
|
|
const retained: ProviderProcessSupervisor[] = [];
|
|
const failure: unknown = await runFirmwareCommand({
|
|
resources: createTestArtifactResourceScope(),
|
|
engine: "binwalk",
|
|
operation: "inspect_firmware_regions",
|
|
command: process.execPath,
|
|
limiter: process.execPath,
|
|
fileSizeCeiling: Number.MAX_SAFE_INTEGER,
|
|
args: [],
|
|
cwd: root,
|
|
environment: process.env,
|
|
signal: caller.signal,
|
|
deadline: {
|
|
phase: "analysis",
|
|
signal: new AbortController().signal,
|
|
timeoutMs: 30_000,
|
|
expiresAtMs: performance.now() + 30_000,
|
|
},
|
|
launcher: async (options) => {
|
|
const owned = await spawnOwnedProviderProcess({
|
|
...options,
|
|
command: process.execPath,
|
|
arguments: [
|
|
"-e",
|
|
`process.stdout.write("scan evidence\\n"); process.stderr.write("producer diagnostics\\n"); process.exitCode = ${exitCode}`,
|
|
],
|
|
});
|
|
return {
|
|
...owned,
|
|
cleanup: async () => {
|
|
cleanupCalls += 1;
|
|
expect(owned.process.exitCode).toBe(exitCode);
|
|
caller.abort(reason);
|
|
const cleaned = await cleanupOwnedProcessGroup(owned.ownership);
|
|
if (cleanupFails && cleanupCalls === 1)
|
|
return {
|
|
cleaned: false as const,
|
|
reason: "controlled cleanup refusal after real release",
|
|
};
|
|
return cleaned;
|
|
},
|
|
};
|
|
},
|
|
retainCleanup: (owner) => {
|
|
retained.push(owner);
|
|
},
|
|
}).catch((cause: unknown) => cause);
|
|
expect(cleanupCalls).toBe(1);
|
|
expect(failure).toBeInstanceOf(AnalysisError);
|
|
if (!(failure instanceof AnalysisError))
|
|
throw new Error("Expected typed failure");
|
|
if (exitCode === 0) {
|
|
expect(failure).toMatchObject({
|
|
_tag: "AnalysisCancelledError",
|
|
cause: reason,
|
|
});
|
|
} else if (cleanupFails) {
|
|
expect(retained).toHaveLength(1);
|
|
expect(await retained[0]!.stop()).toMatchObject({
|
|
status: "verified-cleanup",
|
|
});
|
|
expect(cleanupCalls).toBe(2);
|
|
retained[0]!.dispose();
|
|
expect(failure).toMatchObject({
|
|
diagnostics: {
|
|
previous_error: {
|
|
code: "execution_failure",
|
|
details: { diagnostics: { exit_code: 7 } },
|
|
},
|
|
},
|
|
capturedOutput: {
|
|
stdout: "scan evidence\n",
|
|
stderr: "producer diagnostics\n",
|
|
exit_code: 7,
|
|
},
|
|
});
|
|
} else {
|
|
expect(failure).toMatchObject({
|
|
_tag: "ProviderAdapterError",
|
|
diagnostics: {
|
|
exit_code: 7,
|
|
stdout: "scan evidence\n",
|
|
stderr: "producer diagnostics\n",
|
|
},
|
|
});
|
|
expect(projectAnalysisError(failure).code).toBe("execution_failure");
|
|
}
|
|
},
|
|
);
|
|
|
|
// The injected launcher runs real owned Node processes on every POSIX host;
|
|
// Linux firmware binaries and prlimit are deliberately outside this lifecycle seam.
|
|
test.skipIf(process.platform === "win32").each([
|
|
{
|
|
overflow: false,
|
|
exitCode: 7,
|
|
acceptFailure: false,
|
|
cancelBeforeExit: false,
|
|
expected: "ProviderAdapterError",
|
|
},
|
|
{
|
|
overflow: false,
|
|
exitCode: 0,
|
|
acceptFailure: false,
|
|
cancelBeforeExit: false,
|
|
expected: "AnalysisCancelledError",
|
|
},
|
|
{
|
|
overflow: false,
|
|
exitCode: 1,
|
|
acceptFailure: true,
|
|
cancelBeforeExit: false,
|
|
expected: "AnalysisCancelledError",
|
|
},
|
|
{
|
|
overflow: false,
|
|
exitCode: 7,
|
|
acceptFailure: false,
|
|
cancelBeforeExit: true,
|
|
expected: "AnalysisCancelledError",
|
|
},
|
|
{
|
|
overflow: true,
|
|
exitCode: 7,
|
|
acceptFailure: false,
|
|
cancelBeforeExit: false,
|
|
expected: "ProviderAdapterError",
|
|
},
|
|
])(
|
|
"retains root exit $exitCode while a handshaken descendant holds output (accepted: $acceptFailure, earlier cancellation: $cancelBeforeExit, overflow: $overflow)",
|
|
async ({ exitCode, acceptFailure, cancelBeforeExit, expected, overflow }) => {
|
|
const root = await createTestTempDirectory(
|
|
"rea-firmware-inherited-output-",
|
|
);
|
|
const caller = new AbortController();
|
|
const reason = { caller: "cancel with inherited output still open" };
|
|
let cleanupCalls = 0;
|
|
let closed = false;
|
|
let exited = false;
|
|
const failure: unknown = await runFirmwareCommand({
|
|
resources: createTestArtifactResourceScope(),
|
|
engine: "binwalk",
|
|
operation: "inspect_firmware_regions",
|
|
command: process.execPath,
|
|
limiter: process.execPath,
|
|
fileSizeCeiling: Number.MAX_SAFE_INTEGER,
|
|
args: [],
|
|
cwd: root,
|
|
environment: process.env,
|
|
signal: caller.signal,
|
|
deadline: {
|
|
phase: "analysis",
|
|
signal: new AbortController().signal,
|
|
timeoutMs: 30_000,
|
|
expiresAtMs: performance.now() + 30_000,
|
|
},
|
|
acceptReportedExtractionFailure: acceptFailure,
|
|
launcher: async (options) => {
|
|
const exitRelease = join(root, "exit-release");
|
|
const tailRelease = join(root, "tail-release");
|
|
const descendant = `
|
|
const { existsSync } = require("node:fs");
|
|
setInterval(() => {}, 1000);
|
|
const tail = setInterval(() => {
|
|
if (!existsSync(${JSON.stringify(tailRelease)})) return;
|
|
clearInterval(tail);
|
|
process.stderr.write(Buffer.alloc(${overflow ? FIRMWARE_LIMITS.diagnosticBytes : 0}, 120), () => {
|
|
process.stderr.write("descendant-tail\\n");
|
|
});
|
|
}, 10);
|
|
process.send("ready");
|
|
`;
|
|
const owned = await spawnOwnedProviderProcess({
|
|
...options,
|
|
command: process.execPath,
|
|
arguments: [
|
|
"-e",
|
|
`
|
|
const { spawn } = require("node:child_process");
|
|
const child = spawn(process.execPath, ["-e", ${JSON.stringify(descendant)}], {
|
|
stdio: ["ignore", "inherit", "inherit", "ipc"],
|
|
});
|
|
child.once("message", () => {
|
|
process.stderr.write("descendant-ready\\n");
|
|
child.disconnect();
|
|
child.unref();
|
|
if (${cancelBeforeExit}) {
|
|
const release = setInterval(() => {
|
|
if (!require("node:fs").existsSync(${JSON.stringify(exitRelease)})) return;
|
|
clearInterval(release);
|
|
process.exitCode = ${exitCode};
|
|
}, 10);
|
|
} else process.exitCode = ${exitCode};
|
|
});
|
|
`,
|
|
],
|
|
});
|
|
owned.process.once("close", () => {
|
|
closed = true;
|
|
});
|
|
let diagnostics = "";
|
|
owned.process.stderr?.on("data", (chunk: Buffer) => {
|
|
diagnostics += chunk.toString();
|
|
if (cancelBeforeExit && !caller.signal.aborted) {
|
|
expect(exited).toBe(false);
|
|
caller.abort(reason);
|
|
void writeFile(exitRelease, "release");
|
|
} else if (
|
|
!cancelBeforeExit &&
|
|
diagnostics.includes("descendant-tail")
|
|
) {
|
|
expect(exited).toBe(true);
|
|
expect(closed).toBe(false);
|
|
caller.abort(reason);
|
|
}
|
|
});
|
|
owned.process.once("exit", () => {
|
|
exited = true;
|
|
// Release descendant diagnostics only after the supervisor has also
|
|
// observed the root's exit, leaving inherited pipes open until cleanup.
|
|
queueMicrotask(() => {
|
|
void writeFile(tailRelease, "release");
|
|
});
|
|
});
|
|
return {
|
|
...owned,
|
|
cleanup: async () => {
|
|
cleanupCalls += 1;
|
|
return cleanupOwnedProcessGroup(owned.ownership);
|
|
},
|
|
};
|
|
},
|
|
retainCleanup: () => {
|
|
throw new Error("Real descendant cleanup must complete");
|
|
},
|
|
}).catch((cause: unknown) => cause);
|
|
expect(exited).toBe(true);
|
|
expect(cleanupCalls).toBe(1);
|
|
expect(failure).toMatchObject({ _tag: expected });
|
|
if (expected === "ProviderAdapterError")
|
|
expect(failure).toMatchObject({
|
|
diagnostics: {
|
|
exit_code: 7,
|
|
stderr: overflow
|
|
? expect.stringMatching(/^descendant-ready\nxxx/)
|
|
: "descendant-ready\ndescendant-tail\n",
|
|
truncated: overflow,
|
|
},
|
|
});
|
|
else expect(failure).toMatchObject({ cause: reason });
|
|
},
|
|
);
|