claude-code-acp-rs 0.1.22

Use Claude Code from any ACP client - A Rust implementation of Claude Code ACP Agent
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
//! Permission checker implementation
//!
//! Checks tool permissions against settings rules.

use std::path::{Path, PathBuf};

use super::manager::Settings;
use super::rule::{ParsedRule, PermissionCheckResult};
use crate::command_safety::extract_command_basename;

/// Permission checker that evaluates tool permissions against settings rules
#[derive(Debug)]
pub struct PermissionChecker {
    /// Merged settings with permission rules
    settings: Settings,
    /// Working directory for path resolution
    cwd: PathBuf,
    /// Parsed and cached allow rules
    allow_rules: Vec<(String, ParsedRule)>,
    /// Parsed and cached deny rules
    deny_rules: Vec<(String, ParsedRule)>,
    /// Parsed and cached ask rules
    ask_rules: Vec<(String, ParsedRule)>,
}

impl PermissionChecker {
    /// Create a new permission checker
    pub fn new(settings: Settings, cwd: impl AsRef<Path>) -> Self {
        let cwd = cwd.as_ref().to_path_buf();

        // Pre-parse rules for efficiency
        let allow_rules = Self::parse_rules(
            settings.permissions.as_ref().and_then(|p| p.allow.as_ref()),
            &cwd,
        );
        let deny_rules = Self::parse_rules(
            settings.permissions.as_ref().and_then(|p| p.deny.as_ref()),
            &cwd,
        );
        let ask_rules = Self::parse_rules(
            settings.permissions.as_ref().and_then(|p| p.ask.as_ref()),
            &cwd,
        );

        Self {
            settings,
            cwd,
            allow_rules,
            deny_rules,
            ask_rules,
        }
    }

    /// Parse a list of rule strings into ParsedRule objects
    fn parse_rules(rules: Option<&Vec<String>>, cwd: &Path) -> Vec<(String, ParsedRule)> {
        rules
            .map(|rules| {
                rules
                    .iter()
                    .map(|rule| (rule.clone(), ParsedRule::parse_with_glob(rule, cwd)))
                    .collect()
            })
            .unwrap_or_default()
    }

    /// Check permission for a tool invocation
    ///
    /// Priority: deny > allow > ask
    ///
    /// Returns the permission decision and matching rule (if any).
    pub fn check_permission(
        &self,
        tool_name: &str,
        tool_input: &serde_json::Value,
    ) -> PermissionCheckResult {
        // Check deny rules first (highest priority)
        for (rule_str, parsed) in &self.deny_rules {
            if parsed.matches(tool_name, tool_input, &self.cwd) {
                tracing::debug!("Tool {} denied by rule: {}", tool_name, rule_str);
                return PermissionCheckResult::deny(rule_str);
            }
        }

        // Check allow rules
        for (rule_str, parsed) in &self.allow_rules {
            if parsed.matches(tool_name, tool_input, &self.cwd) {
                tracing::debug!("Tool {} allowed by rule: {}", tool_name, rule_str);
                return PermissionCheckResult::allow(rule_str);
            }
        }

        // Check ask rules
        for (rule_str, parsed) in &self.ask_rules {
            if parsed.matches(tool_name, tool_input, &self.cwd) {
                tracing::debug!(
                    "Tool {} requires permission (ask rule): {}",
                    tool_name,
                    rule_str
                );
                return PermissionCheckResult::ask_with_rule(rule_str);
            }
        }

        // Default: ask
        tracing::debug!("Tool {} has no matching rule, defaulting to ask", tool_name);
        PermissionCheckResult::ask()
    }

    /// Get the settings
    pub fn settings(&self) -> &Settings {
        &self.settings
    }

    /// Get the working directory
    pub fn cwd(&self) -> &Path {
        &self.cwd
    }

    /// Check if there are any permission rules configured
    pub fn has_rules(&self) -> bool {
        !self.allow_rules.is_empty() || !self.deny_rules.is_empty() || !self.ask_rules.is_empty()
    }

    /// Add a runtime allow rule (e.g., from user's "Always Allow" choice)
    pub fn add_allow_rule(&mut self, rule: &str) {
        let parsed = ParsedRule::parse_with_glob(rule, &self.cwd);
        self.allow_rules.push((rule.to_string(), parsed));
    }

    /// Add a runtime allow rule for "Always Allow" permission decision
    ///
    /// For Bash tool: extracts the command name (first word) and generates
    /// a rule like `Bash(find:*)` that allows all invocations of that command
    /// regardless of arguments or paths.
    ///
    /// For file operations (Read/Edit/Write): generates a directory-based rule
    /// that allows operations in the same directory tree.
    ///
    /// This provides reasonable granularity:
    /// - `find /path1` → rule `Bash(find:*)` → allows all `find` commands
    /// - `ls /path` → not matched → needs separate permission
    pub fn add_allow_rule_for_tool_call(
        &mut self,
        tool_name: &str,
        tool_input: &serde_json::Value,
    ) {
        // Strip mcp__acp__ prefix for consistent rule matching
        let stripped = tool_name.strip_prefix("mcp__acp__").unwrap_or(tool_name);

        let rule = match stripped {
            "Bash" => {
                // Extract command name (first word only) for Bash
                if let Some(cmd) = tool_input.get("command").and_then(|v| v.as_str()) {
                    let cmd_name = Self::extract_command_name(cmd);
                    if cmd_name.is_empty() {
                        stripped.to_string()
                    } else {
                        format!("Bash({}:*)", cmd_name) // e.g., "Bash(find:*)"
                    }
                } else {
                    stripped.to_string()
                }
            }
            "Read" | "Grep" | "Glob" | "LS" => {
                // For read operations, generate directory-based rule
                Self::generate_file_rule("Read", tool_input, &self.cwd)
            }
            "Edit" | "Write" => {
                // For write operations, generate directory-based rule
                Self::generate_file_rule(stripped, tool_input, &self.cwd)
            }
            _ => stripped.to_string(),
        };

        tracing::info!(
            tool_name = %tool_name,
            generated_rule = %rule,
            "Adding allow rule for Always Allow"
        );

        let parsed = ParsedRule::parse_with_glob(&rule, &self.cwd);
        self.allow_rules.push((rule, parsed));
    }

    /// Extract command name (basename only) from a shell command
    ///
    /// Supports both simple commands and full path commands:
    /// - `find /path -name "*.rs"` → `find`
    /// - `/usr/bin/find . -name "*.rs"` → `find`
    /// - `ls -la /tmp` → `ls`
    fn extract_command_name(cmd: &str) -> String {
        extract_command_basename(cmd).to_string()
    }

    /// Generate a file-based permission rule
    fn generate_file_rule(tool_name: &str, tool_input: &serde_json::Value, cwd: &Path) -> String {
        if let Some(path) = tool_input.get("file_path").and_then(|v| v.as_str()) {
            let path = Path::new(path);

            // Try to get the directory
            if let Some(dir) = path.parent() {
                // Make path relative to cwd if possible
                let dir_str = if let Ok(relative) = dir.strip_prefix(cwd) {
                    format!("./{}", relative.display())
                } else {
                    dir.to_string_lossy().to_string()
                };

                // Generate glob rule for the directory
                if dir_str.is_empty() || dir_str == "." {
                    format!("{}(./*)", tool_name)
                } else {
                    format!("{}({}/**)", tool_name, dir_str)
                }
            } else {
                tool_name.to_string()
            }
        } else {
            tool_name.to_string()
        }
    }

    /// Add a runtime deny rule
    pub fn add_deny_rule(&mut self, rule: &str) {
        let parsed = ParsedRule::parse_with_glob(rule, &self.cwd);
        self.deny_rules.push((rule.to_string(), parsed));
    }

    /// Get the default permission mode from settings
    pub fn default_mode(&self) -> Option<&str> {
        self.settings
            .permissions
            .as_ref()
            .and_then(|p| p.default_mode.as_deref())
    }

    /// Get additional directories from settings
    pub fn additional_directories(&self) -> Option<&Vec<String>> {
        self.settings
            .permissions
            .as_ref()
            .and_then(|p| p.additional_directories.as_ref())
    }
}

impl Default for PermissionChecker {
    fn default() -> Self {
        Self::new(Settings::default(), PathBuf::from("."))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::settings::{PermissionDecision, PermissionSettings};
    use serde_json::json;

    fn settings_with_permissions(permissions: PermissionSettings) -> Settings {
        Settings {
            permissions: Some(permissions),
            ..Default::default()
        }
    }

    #[test]
    fn test_empty_rules_default_to_ask() {
        let checker = PermissionChecker::default();
        let result = checker.check_permission("Read", &json!({"file_path": "/tmp/test.txt"}));

        assert_eq!(result.decision, PermissionDecision::Ask);
        assert!(result.rule.is_none());
    }

    #[test]
    fn test_allow_rule() {
        let permissions = PermissionSettings {
            allow: Some(vec!["Read".to_string()]),
            ..Default::default()
        };
        let checker = PermissionChecker::new(settings_with_permissions(permissions), "/tmp");

        let result = checker.check_permission("Read", &json!({"file_path": "/tmp/test.txt"}));
        assert_eq!(result.decision, PermissionDecision::Allow);
        assert_eq!(result.rule, Some("Read".to_string()));
    }

    #[test]
    fn test_deny_rule() {
        let permissions = PermissionSettings {
            deny: Some(vec!["Bash".to_string()]),
            ..Default::default()
        };
        let checker = PermissionChecker::new(settings_with_permissions(permissions), "/tmp");

        let result = checker.check_permission("Bash", &json!({"command": "rm -rf /"}));
        assert_eq!(result.decision, PermissionDecision::Deny);
    }

    #[test]
    fn test_deny_takes_priority_over_allow() {
        let permissions = PermissionSettings {
            allow: Some(vec!["Bash".to_string()]),
            deny: Some(vec!["Bash".to_string()]),
            ..Default::default()
        };
        let checker = PermissionChecker::new(settings_with_permissions(permissions), "/tmp");

        let result = checker.check_permission("Bash", &json!({"command": "ls"}));
        assert_eq!(result.decision, PermissionDecision::Deny);
    }

    #[test]
    fn test_allow_takes_priority_over_ask() {
        let permissions = PermissionSettings {
            allow: Some(vec!["Read".to_string()]),
            ask: Some(vec!["Read".to_string()]),
            ..Default::default()
        };
        let checker = PermissionChecker::new(settings_with_permissions(permissions), "/tmp");

        let result = checker.check_permission("Read", &json!({}));
        assert_eq!(result.decision, PermissionDecision::Allow);
    }

    #[test]
    fn test_bash_wildcard_rule() {
        let permissions = PermissionSettings {
            allow: Some(vec!["Bash(npm run:*)".to_string()]),
            ..Default::default()
        };
        let checker = PermissionChecker::new(settings_with_permissions(permissions), "/tmp");

        // Should allow npm run commands
        assert_eq!(
            checker
                .check_permission("Bash", &json!({"command": "npm run build"}))
                .decision,
            PermissionDecision::Allow
        );

        // Should not allow npm install
        assert_eq!(
            checker
                .check_permission("Bash", &json!({"command": "npm install"}))
                .decision,
            PermissionDecision::Ask
        );

        // Should block command chaining
        assert_eq!(
            checker
                .check_permission("Bash", &json!({"command": "npm run build && rm -rf /"}))
                .decision,
            PermissionDecision::Ask
        );
    }

    #[test]
    fn test_read_group_matching() {
        let permissions = PermissionSettings {
            allow: Some(vec!["Read".to_string()]),
            ..Default::default()
        };
        let checker = PermissionChecker::new(settings_with_permissions(permissions), "/tmp");

        // Read rule should allow Read, Grep, Glob, LS
        assert_eq!(
            checker.check_permission("Read", &json!({})).decision,
            PermissionDecision::Allow
        );
        assert_eq!(
            checker.check_permission("Grep", &json!({})).decision,
            PermissionDecision::Allow
        );
        assert_eq!(
            checker.check_permission("Glob", &json!({})).decision,
            PermissionDecision::Allow
        );
        assert_eq!(
            checker.check_permission("LS", &json!({})).decision,
            PermissionDecision::Allow
        );

        // Should not allow Write
        assert_eq!(
            checker.check_permission("Write", &json!({})).decision,
            PermissionDecision::Ask
        );
    }

    #[test]
    fn test_add_runtime_rule() {
        let mut checker = PermissionChecker::default();

        // Initially should ask
        assert_eq!(
            checker.check_permission("Read", &json!({})).decision,
            PermissionDecision::Ask
        );

        // Add allow rule at runtime
        checker.add_allow_rule("Read");

        // Now should allow
        assert_eq!(
            checker.check_permission("Read", &json!({})).decision,
            PermissionDecision::Allow
        );
    }

    #[test]
    fn test_acp_prefix_stripped() {
        let permissions = PermissionSettings {
            allow: Some(vec!["Read".to_string()]),
            ..Default::default()
        };
        let checker = PermissionChecker::new(settings_with_permissions(permissions), "/tmp");

        // Should work with or without ACP prefix
        assert_eq!(
            checker.check_permission("Read", &json!({})).decision,
            PermissionDecision::Allow
        );
        assert_eq!(
            checker
                .check_permission("mcp__acp__Read", &json!({}))
                .decision,
            PermissionDecision::Allow
        );
    }

    #[test]
    fn test_has_rules() {
        let checker = PermissionChecker::default();
        assert!(!checker.has_rules());

        let permissions = PermissionSettings {
            allow: Some(vec!["Read".to_string()]),
            ..Default::default()
        };
        let checker = PermissionChecker::new(settings_with_permissions(permissions), "/tmp");
        assert!(checker.has_rules());
    }

    #[test]
    fn test_add_allow_rule_for_bash_command() {
        let mut checker = PermissionChecker::new(Settings::default(), "/tmp");

        // Add rule for specific bash command (find)
        checker
            .add_allow_rule_for_tool_call("Bash", &json!({"command": "find /path1 -name '*.rs'"}));

        // Should allow ANY find command (same command name)
        assert_eq!(
            checker
                .check_permission("Bash", &json!({"command": "find /different/path -type f"}))
                .decision,
            PermissionDecision::Allow
        );

        // Should allow find with different arguments
        assert_eq!(
            checker
                .check_permission("Bash", &json!({"command": "find . -name '*.txt' -delete"}))
                .decision,
            PermissionDecision::Allow
        );

        // Should NOT allow different commands (ls, rm, etc.)
        assert_eq!(
            checker
                .check_permission("Bash", &json!({"command": "ls -la /tmp"}))
                .decision,
            PermissionDecision::Ask
        );

        assert_eq!(
            checker
                .check_permission("Bash", &json!({"command": "rm -rf /"}))
                .decision,
            PermissionDecision::Ask
        );
    }

    #[test]
    fn test_add_allow_rule_for_file_operation() {
        let mut checker = PermissionChecker::new(Settings::default(), "/tmp");

        // Add rule for specific file
        checker.add_allow_rule_for_tool_call(
            "Read",
            &json!({"file_path": "/tmp/project/src/main.rs"}),
        );

        // Should allow files in the same directory
        assert_eq!(
            checker
                .check_permission("Read", &json!({"file_path": "/tmp/project/src/lib.rs"}))
                .decision,
            PermissionDecision::Allow
        );

        // Should allow subdirectories (glob **)
        assert_eq!(
            checker
                .check_permission(
                    "Read",
                    &json!({"file_path": "/tmp/project/src/utils/helper.rs"})
                )
                .decision,
            PermissionDecision::Allow
        );

        // Should NOT allow different directories
        assert_eq!(
            checker
                .check_permission("Read", &json!({"file_path": "/etc/passwd"}))
                .decision,
            PermissionDecision::Ask
        );
    }

    #[test]
    fn test_add_allow_rule_for_mcp_prefixed_tool() {
        let mut checker = PermissionChecker::new(Settings::default(), "/tmp");

        // Add rule with MCP prefix
        checker
            .add_allow_rule_for_tool_call("mcp__acp__Bash", &json!({"command": "npm run build"}));

        // Should work for both prefixed and non-prefixed tool names
        assert_eq!(
            checker
                .check_permission("Bash", &json!({"command": "npm run test"}))
                .decision,
            PermissionDecision::Allow
        );
        assert_eq!(
            checker
                .check_permission("mcp__acp__Bash", &json!({"command": "npm run lint"}))
                .decision,
            PermissionDecision::Allow
        );
    }

    #[test]
    fn test_extract_command_name() {
        // Should extract only the command name (basename)
        assert_eq!(
            PermissionChecker::extract_command_name("cargo build --release"),
            "cargo"
        );
        assert_eq!(
            PermissionChecker::extract_command_name("find /path -name '*.rs'"),
            "find"
        );
        assert_eq!(PermissionChecker::extract_command_name("ls -la /tmp"), "ls");
        assert_eq!(PermissionChecker::extract_command_name("npm"), "npm");
        assert_eq!(PermissionChecker::extract_command_name(""), "");
        // Full path commands should return just the basename
        assert_eq!(
            PermissionChecker::extract_command_name("/usr/bin/find . -name '*.rs'"),
            "find"
        );
        assert_eq!(
            PermissionChecker::extract_command_name("/usr/local/bin/cargo build"),
            "cargo"
        );
    }
}