Spaces:
Runtime error
Runtime error
File size: 2,316 Bytes
086d1ff 2eb19c1 086d1ff 01d948e 7e26449 086d1ff 2eb19c1 7e26449 2eb19c1 7e26449 2eb19c1 01d948e 2eb19c1 7e26449 2eb19c1 7e26449 01d948e 7e26449 01d948e 086d1ff 7e26449 086d1ff 01d948e 086d1ff 48c7a9b 086d1ff | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 | import { exec } from "child_process";
import { promisify } from "util";
import { writeFile, access } from "fs/promises";
import { join } from "path";
import { logger } from "../../../logger.ts";
const execAsync = promisify(exec);
const DEFAULT_SANDBOX = process.env.SANDBOX_DIR || "/tmp/poc-sandbox";
const TIMEOUT_MS = 60_000;
export interface FoundryResult {
exitCode: number;
stdout: string;
stderr: string;
combined: string;
timedOut: boolean;
}
/**
* Garante que o sandbox Foundry existe e está inicializado.
*/
async function ensureSandbox(sandboxDir: string) {
try {
await access(join(sandboxDir, "foundry.toml"));
} catch {
logger.info(`[Tester] foundryRunner: Sandbox em ${sandboxDir} não encontrado. Inicializando...`);
// Caminho absoluto para o script de setup (assume execução da raiz do projeto)
await execAsync("./scripts/setup-sandbox.sh", { env: { ...process.env, SANDBOX_DIR: sandboxDir } });
}
}
export async function runFoundry(solidityCode: string, sandboxDir: string = DEFAULT_SANDBOX): Promise<FoundryResult> {
await ensureSandbox(sandboxDir);
// Ensure test directory exists
const testDir = join(sandboxDir, "test");
try {
await access(testDir);
} catch {
await execAsync(`mkdir -p "${testDir}"`);
}
// Escrever o arquivo no sandbox
const testPath = join(testDir, "Exploit.t.sol");
await writeFile(testPath, solidityCode, "utf-8");
try {
const { stdout, stderr } = await execAsync("forge test --match-contract ExploitTest -vvvv", {
cwd: sandboxDir,
timeout: TIMEOUT_MS,
env: { ...process.env, PATH: `${process.env.HOME}/.foundry/bin:${process.env.PATH}` },
});
return {
exitCode: 0,
stdout,
stderr,
combined: `STDOUT:\n${stdout}\nSTDERR:\n${stderr}`,
timedOut: false,
};
} catch (err: any) {
if (err.killed || err.signal === "SIGTERM") {
return {
exitCode: -1,
stdout: "",
stderr: "Forge timed out",
combined: `TIMEOUT após ${TIMEOUT_MS / 1000}s`,
timedOut: true,
};
}
return {
exitCode: err.code ?? 1,
stdout: err.stdout ?? "",
stderr: err.stderr ?? "",
combined: `STDOUT:\n${err.stdout ?? ""}\nSTDERR:\n${err.stderr ?? ""}`,
timedOut: false,
};
}
}
|