refactor: harden safe-bin trusted dir diagnostics

This commit is contained in:
Peter Steinberger
2026-02-24 23:29:12 +00:00
parent 5c2a483375
commit 4355e08262
10 changed files with 391 additions and 7 deletions

View File

@@ -1,5 +1,7 @@
import fs from "node:fs/promises";
import os from "node:os";
import path from "node:path";
import { describe, expect, it } from "vitest";
import { describe, expect, it, vi } from "vitest";
import {
isInterpreterLikeSafeBin,
listInterpreterLikeSafeBins,
@@ -103,4 +105,34 @@ describe("exec safe-bin runtime policy", () => {
expect(optedIn.trustedSafeBinDirs.has(path.resolve("/opt/homebrew/bin"))).toBe(true);
expect(optedIn.trustedSafeBinDirs.has(path.resolve("/usr/local/bin"))).toBe(true);
});
it("emits runtime warning when explicitly trusted dir is writable", async () => {
if (process.platform === "win32") {
return;
}
const dir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-safe-bin-runtime-"));
try {
await fs.chmod(dir, 0o777);
const onWarning = vi.fn();
const policy = resolveExecSafeBinRuntimePolicy({
global: {
safeBinTrustedDirs: [dir],
},
onWarning,
});
expect(policy.writableTrustedSafeBinDirs).toEqual([
{
dir: path.resolve(dir),
groupWritable: true,
worldWritable: true,
},
]);
expect(onWarning).toHaveBeenCalledWith(expect.stringContaining(path.resolve(dir)));
expect(onWarning).toHaveBeenCalledWith(expect.stringContaining("world-writable"));
} finally {
await fs.chmod(dir, 0o755).catch(() => undefined);
await fs.rm(dir, { recursive: true, force: true }).catch(() => undefined);
}
});
});