


























@@ -1,4 +1,7 @@
1-import { afterEach, describe, expect, it } from "vitest";
1+import fs from "node:fs/promises";
2+import os from "node:os";
3+import path from "node:path";
4+import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it } from "vitest";
25import type { OpenClawConfig } from "../config/config.js";
36import { saveExecApprovals } from "../infra/exec-approvals.js";
47import { collectExecRuntimeFindings } from "./audit.js";
@@ -27,11 +30,61 @@ function requireFinding(
2730return finding;
2831}
293230-afterEach(() => {
31-saveExecApprovals({ version: 1, agents: {} });
32-});
33-3433describe("security audit exec surface findings", () => {
34+// Redirect the OpenClaw home (OPENCLAW_HOME wins over HOME/USERPROFILE in
35+// `resolveRawHomeDir`) to a per-test tempdir so `saveExecApprovals` never
36+// touches the real `~/.openclaw/exec-approvals.json` on the host running
37+// the suite.
38+let previousOpenClawHome: string | undefined;
39+let previousHome: string | undefined;
40+let previousUserProfile: string | undefined;
41+let tempRoot = "";
42+let tempCaseIndex = 0;
43+44+beforeAll(async () => {
45+tempRoot = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-exec-approvals-"));
46+});
47+48+beforeEach(async () => {
49+previousOpenClawHome = process.env.OPENCLAW_HOME;
50+previousHome = process.env.HOME;
51+previousUserProfile = process.env.USERPROFILE;
52+const tempDir = path.join(tempRoot, `case-${++tempCaseIndex}`);
53+await fs.mkdir(path.join(tempDir, ".openclaw"), { recursive: true });
54+// OPENCLAW_HOME takes precedence over HOME/USERPROFILE in resolveRawHomeDir,
55+// so all three must point at the tempdir to neutralize whichever the host
56+// happens to have set.
57+process.env.OPENCLAW_HOME = tempDir;
58+process.env.HOME = tempDir;
59+// Windows uses USERPROFILE for os.homedir()
60+process.env.USERPROFILE = tempDir;
61+});
62+63+afterEach(() => {
64+saveExecApprovals({ version: 1, agents: {} });
65+if (previousOpenClawHome === undefined) {
66+delete process.env.OPENCLAW_HOME;
67+} else {
68+process.env.OPENCLAW_HOME = previousOpenClawHome;
69+}
70+if (previousHome === undefined) {
71+delete process.env.HOME;
72+} else {
73+process.env.HOME = previousHome;
74+}
75+if (previousUserProfile === undefined) {
76+delete process.env.USERPROFILE;
77+} else {
78+process.env.USERPROFILE = previousUserProfile;
79+}
80+});
81+82+afterAll(async () => {
83+if (tempRoot) {
84+await fs.rm(tempRoot, { recursive: true, force: true, maxRetries: 5, retryDelay: 20 });
85+}
86+});
87+3588it("warns when exec approvals enable autoAllowSkills", () => {
3689saveExecApprovals({
3790version: 1,
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。