mirror of
https://github.com/morluto/rea.git
synced 2026-11-16 19:53:50 +00:00
79 lines
3.3 KiB
TypeScript
79 lines
3.3 KiB
TypeScript
import { execFile } from "node:child_process";
|
|
import { writeFile } from "node:fs/promises";
|
|
import { join } from "node:path";
|
|
import { promisify } from "node:util";
|
|
import { expect, it } from "vitest";
|
|
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
|
|
|
|
const execute = promisify(execFile);
|
|
|
|
it("loads the Frida verifier help without starting instrumentation", async () => {
|
|
const result = await execute(process.execPath, [
|
|
"scripts/verify-frida.mjs",
|
|
"--help",
|
|
]);
|
|
expect(result.stdout).toContain(
|
|
"Without flags, verifies a local Node.js process",
|
|
);
|
|
});
|
|
|
|
it("requires marker identity to match the selected target", async () => {
|
|
const root = await createTestTempDirectory("rea-frida-verifier-");
|
|
const preload = join(root, "provider.mjs");
|
|
await writeFile(
|
|
preload,
|
|
`
|
|
import { registerHooks } from 'node:module';
|
|
const service = \`
|
|
export class FridaInstrumentationService {
|
|
async listProcesses() { return { ok: true, value: { processes: [{ pid: 41, name: 'selected' }] } }; }
|
|
async instrument() {
|
|
const pid = process.env.REA_PROBE_KIND === 'missing' ? undefined : ['correct', 'wrong-marker'].includes(process.env.REA_PROBE_KIND) ? 41 : 42;
|
|
return { ok: true, value: { cleanupError: null, evidence: { normalized_result: { pid, messages: [{ type: 'send', payload: { rea_frida_verification: 'ok', pid: process.env.REA_PROBE_KIND === 'wrong-marker' ? 42 : pid } }] } } } };
|
|
}
|
|
}\`;
|
|
const manager = \`export class FridaInstrumentationManager { async closeAll() { if (process.env.REA_PROBE_KIND === 'cleanup') throw new AggregateError([new Error('controlled EACCES removing the Frida fixture')], 'Frida cleanup failed'); } }\`;
|
|
registerHooks({ resolve(specifier, context, next) {
|
|
if (context.parentURL?.endsWith('/scripts/verify-frida.mjs')) {
|
|
if (specifier.endsWith('/FridaInstrumentationService.js')) return { shortCircuit: true, url: 'data:text/javascript,' + encodeURIComponent(service) };
|
|
if (specifier.endsWith('/FridaInstrumentationManager.js')) return { shortCircuit: true, url: 'data:text/javascript,' + encodeURIComponent(manager) };
|
|
}
|
|
return next(specifier, context);
|
|
}});
|
|
`,
|
|
);
|
|
const invoke = (kind: string, remote: boolean) =>
|
|
execute(
|
|
process.execPath,
|
|
[
|
|
"--import",
|
|
preload,
|
|
"scripts/verify-frida.mjs",
|
|
...(remote ? ["--address", "127.0.0.1:27042", "--pid", "41"] : []),
|
|
],
|
|
{ env: { ...process.env, REA_PROBE_KIND: kind }, timeout: 10_000 },
|
|
);
|
|
for (const [kind, remote, message] of [
|
|
["missing", false, "Frida returned no valid target PID"],
|
|
["wrong", true, "Frida observed a different target PID"],
|
|
["wrong-marker", true, "Selected target did not return the Frida marker"],
|
|
// The verification failure and the nested cleanup reason both reach the
|
|
// operator, not only the outer "cleanup failed" wrapper.
|
|
[
|
|
"cleanup",
|
|
true,
|
|
"Frida observed a different target PID; controlled EACCES removing the Frida fixture",
|
|
],
|
|
] as const) {
|
|
await expect(invoke(kind, remote)).rejects.toMatchObject({
|
|
stderr: expect.stringContaining(message),
|
|
});
|
|
}
|
|
const result = await invoke("correct", true);
|
|
expect(JSON.parse(result.stdout)).toMatchObject({
|
|
ok: true,
|
|
target_pid: 41,
|
|
script_message_observed: true,
|
|
});
|
|
});
|