File size: 2,509 Bytes
7a1ad33
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
81
82
83
84
85
86
87
88
89
90
91
/**
 * @license
 * Copyright 2026 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */

import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import { createPolicyEngineConfig } from './config.js';
import { PolicyEngine } from './policy-engine.js';
import { PolicyDecision, ApprovalMode } from './types.js';
import { Storage } from '../config/storage.js';

describe('PolicyEngine - Core Tools Mapping', () => {
  beforeEach(() => {
    vi.spyOn(Storage, 'getUserPoliciesDir').mockReturnValue(
      '/mock/user/policies',
    );
    vi.spyOn(Storage, 'getSystemPoliciesDir').mockReturnValue(
      '/mock/system/policies',
    );
  });

  afterEach(() => {
    vi.restoreAllMocks();
  });

  it('should allow tools explicitly listed in settings.tools.core', async () => {
    const settings = {
      tools: {
        core: ['run_shell_command(ls)', 'run_shell_command(git status)'],
      },
    };

    const config = await createPolicyEngineConfig(
      settings,
      ApprovalMode.DEFAULT,
      undefined,
      true, // interactive
    );

    const engine = new PolicyEngine(config);

    // Test simple tool name
    const result1 = await engine.check(
      { name: 'run_shell_command', args: { command: 'ls' } },
      undefined,
    );
    expect(result1.decision).toBe(PolicyDecision.ALLOW);

    // Test tool name with args
    const result2 = await engine.check(
      { name: 'run_shell_command', args: { command: 'git status' } },
      undefined,
    );
    expect(result2.decision).toBe(PolicyDecision.ALLOW);

    // Test tool not in core list
    const result3 = await engine.check(
      { name: 'run_shell_command', args: { command: 'npm test' } },
      undefined,
    );
    // Should be DENIED because of strict allowlist
    expect(result3.decision).toBe(PolicyDecision.DENY);
  });

  it('should allow tools in tools.core even if they are restricted by default policies', async () => {
    // By default run_shell_command is ASK_USER.
    // Putting it in tools.core should make it ALLOW.
    const settings = {
      tools: {
        core: ['run_shell_command'],
      },
    };

    const config = await createPolicyEngineConfig(
      settings,
      ApprovalMode.DEFAULT,
      undefined,
      true,
    );

    const engine = new PolicyEngine(config);

    const result = await engine.check(
      { name: 'run_shell_command', args: { command: 'any command' } },
      undefined,
    );
    expect(result.decision).toBe(PolicyDecision.ALLOW);
  });
});