Skip to content

armgit / rea

Public Imported from GitHub
0

Reverse engineer anything with agents, from app behavior down to native binaries.

fix: reject directory paths for the Wakaru limiter and mitmdump

Fazmina67dbf6a488c
diff --git a/src/browser/history/MitmproxyCaptureAdapter.ts b/src/browser/history/MitmproxyCaptureAdapter.ts
index 2e71720c7..791884341 100644
--- a/src/browser/history/MitmproxyCaptureAdapter.ts
+++ b/src/browser/history/MitmproxyCaptureAdapter.ts
@@ -1,4 +1,4 @@
-import { access } from "node:fs/promises";
+import { access, stat } from "node:fs/promises";
import { constants } from "node:fs";
import { isAbsolute } from "node:path";
import { fileURLToPath } from "node:url";
@@ -32,6 +32,8 @@ export class MitmproxyCaptureAdapter implements HistoricalCaptureFormatAdapter {
);
try {
await access(command, constants.R_OK | constants.X_OK);
+ if (!(await stat(command)).isFile())
+ throw new TypeError("Configured path is not a regular file");
} catch (cause: unknown) {
const reason = `Configured mitmdump executable is unavailable: ${command}.`;
throw new AnalysisCapabilityUnavailableError(
diff --git a/src/javascript/recovery/WakaruCommand.ts b/src/javascript/recovery/WakaruCommand.ts
index e02447481..fe4500ba9 100644
--- a/src/javascript/recovery/WakaruCommand.ts
+++ b/src/javascript/recovery/WakaruCommand.ts
@@ -1,6 +1,6 @@
import { randomUUID } from "node:crypto";
import { constants } from "node:fs";
-import { access, realpath } from "node:fs/promises";
+import { access, realpath, stat } from "node:fs/promises";
import { isAbsolute } from "node:path";
import {
AnalysisCancelledError,
@@ -87,6 +87,8 @@ export const resolveWakaruCommand = async (
if (!isAbsolute(limiter))
throw new TypeError("REA_JAVASCRIPT_PRLIMIT_COMMAND must be absolute");
await access(limiter, constants.X_OK);
+ if (!(await stat(limiter)).isFile())
+ throw new TypeError(`Resource limiter is not a regular file: ${limiter}`);
return { command, limiter, ...fingerprint };
} catch (cause: unknown) {
const reason = `Configured Wakaru or util-linux prlimit is unavailable: ${recoveryFailureMessage(cause)}`;
diff --git a/tests/boundary/browser/historicalCaptureDecoder.test.ts b/tests/boundary/browser/historicalCaptureDecoder.test.ts
index 23c4604a3..92b26d5df 100644
--- a/tests/boundary/browser/historicalCaptureDecoder.test.ts
+++ b/tests/boundary/browser/historicalCaptureDecoder.test.ts
@@ -2,8 +2,10 @@ import { PrivateRuntimeRoot } from "../../../src/process/PrivateRuntimeRoot.js";
import {
access,
chmod,
+ mkdir,
mkdtemp,
rm,
+ symlink,
truncate,
writeFile,
} from "node:fs/promises";
@@ -11,6 +13,7 @@ import { tmpdir } from "node:os";
import { join } from "node:path";
import { expect, it, onTestFinished } from "vitest";
import { HistoricalCaptureDecoder } from "../../../src/browser/history/HistoricalCaptureDecoder.js";
+import { MitmproxyCaptureAdapter } from "../../../src/browser/history/MitmproxyCaptureAdapter.js";
import { HAR_CAPTURE_PROVIDER_IDENTITY } from "../../../src/browser/history/CaptureRelease.js";
import {
inspectWebNetworkCaptureInputSchema,
@@ -20,6 +23,41 @@ import { historicalHar } from "../../fixtures/historicalHar.js";
import { decodeHarCapture } from "../../../src/browser/history/HarCapture.js";
import { projectAnalysisError } from "../../../src/domain/analysisErrorProjection.js";
+const linux = it.skipIf(process.platform !== "linux");
+
+linux("refuses a directory configured as the mitmdump executable", async () => {
+ const root = await mkdtemp(join(tmpdir(), "rea-mitmdump-command-"));
+ onTestFinished(() => rm(root, { recursive: true, force: true }));
+ const command = join(root, "mitmdump");
+ await mkdir(command);
+
+ await expect(
+ new MitmproxyCaptureAdapter({ REA_MITMDUMP_COMMAND: command }).command(
+ join(root, "request.json"),
+ join(root, "runtime"),
+ ),
+ ).rejects.toMatchObject({
+ _tag: "AnalysisCapabilityUnavailableError",
+ reason: expect.stringContaining(command),
+ });
+});
+
+linux("allows a symlink to a regular mitmdump executable", async () => {
+ const root = await mkdtemp(join(tmpdir(), "rea-mitmdump-command-symlink-"));
+ onTestFinished(() => rm(root, { recursive: true, force: true }));
+ const command = join(root, "mitmdump");
+ const target = join(root, "mitmdump-bin");
+ await writeFile(target, "#!/bin/sh\n", { mode: 0o700 });
+ await symlink(target, command);
+
+ await expect(
+ new MitmproxyCaptureAdapter({ REA_MITMDUMP_COMMAND: command }).command(
+ join(root, "request.json"),
+ join(root, "runtime"),
+ ),
+ ).resolves.toMatchObject({ command });
+});
+
it.each([true, false])(
"binds decoder replies to the complete selected provider identity (matching name: %s)",
async (matching) => {
diff --git a/tests/boundary/javascript-recovery/wakaruCommandConfiguration.test.ts b/tests/boundary/javascript-recovery/wakaruCommandConfiguration.test.ts
new file mode 100644
index 000000000..ce9845be5
--- /dev/null
+++ b/tests/boundary/javascript-recovery/wakaruCommandConfiguration.test.ts
@@ -0,0 +1,47 @@
+import { mkdir, symlink, writeFile } from "node:fs/promises";
+import { join } from "node:path";
+import { expect, it } from "vitest";
+import { resolveWakaruCommand } from "../../../src/javascript/recovery/WakaruCommand.js";
+import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
+
+const linuxX64 = it.skipIf(
+ process.platform !== "linux" || process.arch !== "x64",
+);
+
+linuxX64(
+ "refuses a directory configured as the Wakaru resource limiter",
+ async () => {
+ const root = await createTestTempDirectory("rea-wakaru-limiter-");
+ const command = join(root, "wakaru");
+ const limiter = join(root, "prlimit");
+ await writeFile(command, "#!/bin/sh\n", { mode: 0o700 });
+ await mkdir(limiter);
+
+ await expect(
+ resolveWakaruCommand({
+ REA_WAKARU_COMMAND: command,
+ REA_JAVASCRIPT_PRLIMIT_COMMAND: limiter,
+ }),
+ ).rejects.toMatchObject({
+ _tag: "AnalysisCapabilityUnavailableError",
+ reason: expect.stringContaining(limiter),
+ });
+ },
+);
+
+linuxX64("allows a symlink to a regular Wakaru resource limiter", async () => {
+ const root = await createTestTempDirectory("rea-wakaru-limiter-symlink-");
+ const command = join(root, "wakaru");
+ const limiter = join(root, "prlimit");
+ const target = join(root, "prlimit-bin");
+ await writeFile(command, "#!/bin/sh\n", { mode: 0o700 });
+ await writeFile(target, "#!/bin/sh\n", { mode: 0o700 });
+ await symlink(target, limiter);
+
+ await expect(
+ resolveWakaruCommand({
+ REA_WAKARU_COMMAND: command,
+ REA_JAVASCRIPT_PRLIMIT_COMMAND: limiter,
+ }),
+ ).resolves.toMatchObject({ limiter });
+});