File size: 2,326 Bytes
e249c6d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { afterEach, describe, expect, it, vi } from "vitest";
import {
  makeExecutable,
  makeExecApprovalsTempDir,
} from "../../infra/exec-approvals-test-helpers.js";
import { loadExecApprovals, saveExecApprovals } from "../../infra/exec-approvals.js";
import type { CliBackendToolPermissionResult } from "../../plugins/cli-backend.types.js";
import { callGatewayTool } from "../tools/gateway.js";
import {
  closePluginTestAdmissions,
  createExecution,
  runPlugin,
  SUCCESS_RESULT,
} from "./execute-plugin.test-support.js";

vi.mock("../tools/gateway.js", () => ({ callGatewayTool: vi.fn() }));
const mockCallGatewayTool = vi.mocked(callGatewayTool);

afterEach(() => {
  vi.unstubAllEnvs();
  closePluginTestAdmissions();
  mockCallGatewayTool.mockReset();
});

describe("native Bash execution policy", () => {
  it.each([
    ["allowlist", "on-miss", "allow"],
    ["deny", "on-miss", "deny"],
    ["allowlist", "off", "deny"],
  ] as const)(
    "applies %s/%s to native Bash with configured PATH",
    async (security, ask, behavior) => {
      const dir = makeExecApprovalsTempDir();
      vi.stubEnv("OPENCLAW_STATE_DIR", dir);
      const binary = makeExecutable(dir, "gog");
      saveExecApprovals({ version: 1, agents: { main: { allowlist: [{ pattern: binary }] } } });
      const { context } = await createExecution({
        config: { tools: { exec: { security, ask, pathPrepend: [dir] } } },
        nativeTools: ["Bash"],
      });
      let decision: CliBackendToolPermissionResult | undefined;
      const runExit = await runPlugin(context, async function* (execution) {
        decision = await execution.requestToolPermission({
          toolName: "Bash",
          toolInput: { command: "gog calendar list" },
          cwd: dir,
        });
        yield SUCCESS_RESULT;
      });
      expect(decision?.behavior, JSON.stringify({ decision, runExit })).toBe(behavior);
      expect(mockCallGatewayTool).not.toHaveBeenCalled();
      if (decision?.behavior === "allow") {
        expect(decision.updatedInput?.command).toContain(binary);
        expect(loadExecApprovals().agents?.main?.allowlist?.[0]?.lastUsedAt).toEqual(
          expect.any(Number),
        );
      } else {
        expect(loadExecApprovals().agents?.main?.allowlist?.[0]?.lastUsedAt).toBeUndefined();
      }
    },
  );
});