cc-agent-sdk 0.1.7

claude agent sdk
Documentation
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
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
//! # Skills Security Auditor
//!
//! This module provides security auditing capabilities for agent skills,
//! detecting potentially dangerous patterns in skill code and configurations.
//!
//! ## Features
//!
//! - Network access detection
//! - Dangerous command detection (eval, exec, system)
//! - File access pattern analysis
//! - Risk level assessment
//!
//! ## Example
//!
//! ```no_run
//! use claude_agent_sdk::skills::auditor::{SkillAuditor, AuditConfig, RiskLevel};
//! use claude_agent_sdk::skills::skill_md::SkillMdFile;
//!
//! let config = AuditConfig {
//!     strict_mode: true,
//!     allow_network: false,
//!     check_scripts: true,
//!     check_resources: true,
//! };
//!
//! let auditor = SkillAuditor::new(config);
//! let skill = SkillMdFile::parse("path/to/SKILL.md")?;
//! let report = auditor.audit(&skill)?;
//!
//! if report.risk_level == RiskLevel::Safe {
//!     println!("Skill is safe to use");
//! } else {
//!     println!("Skill has risks: {:?}", report.issues);
//! }
//! # Ok::<(), Box<dyn std::error::Error>>(())
//! ```

use crate::skills::skill_md::SkillMdFile;
use std::path::Path;

/// Risk level for a skill
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Default)]
pub enum RiskLevel {
    /// Safe - only from trusted sources
    #[default]
    Safe,
    /// Low - minor issues
    Low,
    /// Medium - needs review
    Medium,
    /// High - dangerous, should not run
    High,
    /// Critical - malicious, block execution
    Critical,
}

impl std::fmt::Display for RiskLevel {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            RiskLevel::Safe => write!(f, "Safe"),
            RiskLevel::Low => write!(f, "Low"),
            RiskLevel::Medium => write!(f, "Medium"),
            RiskLevel::High => write!(f, "High"),
            RiskLevel::Critical => write!(f, "Critical"),
        }
    }
}

/// Individual security issue found during audit
#[derive(Debug, Clone)]
pub struct SkillAuditIssue {
    /// Type of issue
    pub issue_type: IssueType,
    /// Severity level
    pub severity: RiskLevel,
    /// File where issue was found (if applicable)
    pub file: Option<String>,
    /// Line number (if applicable)
    pub line: Option<usize>,
    /// Description of the issue
    pub message: String,
    /// Code snippet that triggered the issue
    pub snippet: Option<String>,
}

/// Type of security issue
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum IssueType {
    /// Network access detected
    NetworkAccess,
    /// Dangerous command (eval, exec, system)
    DangerousCommand,
    /// File system access
    FileAccess,
    /// Code execution
    CodeExecution,
    /// External command execution
    ExternalCommand,
    /// Sensitive data access
    SensitiveDataAccess,
    /// Other security concern
    Other,
}

impl std::fmt::Display for IssueType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            IssueType::NetworkAccess => write!(f, "Network Access"),
            IssueType::DangerousCommand => write!(f, "Dangerous Command"),
            IssueType::FileAccess => write!(f, "File Access"),
            IssueType::CodeExecution => write!(f, "Code Execution"),
            IssueType::ExternalCommand => write!(f, "External Command"),
            IssueType::SensitiveDataAccess => write!(f, "Sensitive Data Access"),
            IssueType::Other => write!(f, "Other"),
        }
    }
}

/// Audit report for a skill
#[derive(Debug, Clone, Default)]
pub struct SkillAuditReport {
    /// Overall safety assessment
    pub safe: bool,
    /// All issues found
    pub issues: Vec<SkillAuditIssue>,
    /// Warnings (non-critical issues)
    pub warnings: Vec<String>,
    /// Errors (critical issues)
    pub errors: Vec<String>,
    /// Overall risk level
    pub risk_level: RiskLevel,
    /// Number of files scanned
    pub files_scanned: usize,
}

impl SkillAuditReport {
    /// Check if the report has any issues
    pub fn has_issues(&self) -> bool {
        !self.issues.is_empty()
    }

    /// Get issues by severity
    pub fn issues_by_severity(&self, severity: RiskLevel) -> Vec<&SkillAuditIssue> {
        self.issues
            .iter()
            .filter(|issue| issue.severity == severity)
            .collect()
    }

    /// Get all high and critical issues
    pub fn critical_issues(&self) -> Vec<&SkillAuditIssue> {
        self.issues
            .iter()
            .filter(|issue| issue.severity >= RiskLevel::High)
            .collect()
    }
}

/// Configuration for skill auditing
#[derive(Debug, Clone)]
pub struct AuditConfig {
    /// Enable strict mode (more conservative checks)
    pub strict_mode: bool,
    /// Allow network access (otherwise treats as high risk)
    pub allow_network: bool,
    /// Check scripts directory
    pub check_scripts: bool,
    /// Check resource files
    pub check_resources: bool,
}

impl Default for AuditConfig {
    fn default() -> Self {
        Self {
            strict_mode: false,
            allow_network: false,
            check_scripts: true,
            check_resources: true,
        }
    }
}

/// Skills security auditor
pub struct SkillAuditor {
    config: AuditConfig,
}

impl SkillAuditor {
    /// Create a new auditor with the given configuration
    pub fn new(config: AuditConfig) -> Self {
        Self { config }
    }

    /// Create an auditor with default configuration
    pub fn default_auditor() -> Self {
        Self::new(AuditConfig::default())
    }

    /// Audit a skill and return a security report
    pub fn audit(&self, skill: &SkillMdFile) -> Result<SkillAuditReport, AuditError> {
        let mut report = SkillAuditReport::default();

        // Scan main SKILL.md content
        self.check_content(&skill.content, "SKILL.md", &mut report);

        // Scan scripts if enabled
        if self.config.check_scripts {
            for script_path in &skill.scripts {
                if let Some(content) = self.read_file(script_path) {
                    self.check_script(
                        &content,
                        script_path.file_name().and_then(|n| n.to_str()).unwrap_or("unknown"),
                        &mut report,
                    );
                    report.files_scanned += 1;
                }
            }
        }

        // Scan resource files if enabled
        if self.config.check_resources {
            for resource_path in &skill.resources {
                if let Some(content) = self.read_file(resource_path) {
                    self.check_content(
                        &content,
                        resource_path.file_name().and_then(|n| n.to_str()).unwrap_or("unknown"),
                        &mut report,
                    );
                    report.files_scanned += 1;
                }
            }
        }

        // Calculate overall risk level
        report.risk_level = self.calculate_risk_level(&report);
        report.safe = report.risk_level <= RiskLevel::Low;

        Ok(report)
    }

    /// Read file content safely
    fn read_file(&self, path: &Path) -> Option<String> {
        std::fs::read_to_string(path).ok()
    }

    /// Calculate overall risk level from issues
    fn calculate_risk_level(&self, report: &SkillAuditReport) -> RiskLevel {
        if report.issues.is_empty() {
            return RiskLevel::Safe;
        }

        // Find the highest severity issue
        report
            .issues
            .iter()
            .map(|issue| issue.severity)
            .max()
            .unwrap_or(RiskLevel::Safe)
    }

    /// Check main markdown content
    fn check_content(&self, content: &str, file: &str, report: &mut SkillAuditReport) {
        // Check for network patterns in code blocks
        self.check_network_access(content, file, report);

        // Check for dangerous commands
        self.check_dangerous_commands(content, file, report);

        // Check for file access patterns
        self.check_file_access_patterns(content, file, report);
    }

    /// Check script file
    fn check_script(&self, content: &str, file: &str, report: &mut SkillAuditReport) {
        // Scripts get more stringent checks
        self.check_network_access(content, file, report);
        self.check_dangerous_commands(content, file, report);
        self.check_file_access_patterns(content, file, report);
        self.check_code_execution(content, file, report);
    }

    /// Check for network access patterns
    fn check_network_access(&self, content: &str, file: &str, report: &mut SkillAuditReport) {
        let patterns = [
            "http://",
            "https://",
            "ftp://",
            "requests.",
            "urllib.",
            "fetch(",
            "axios.",
            "socket.",
            "connect(",
            "wget",
            "curl",
        ];

        let mut found_patterns = Vec::new();

        for (line_num, line) in content.lines().enumerate() {
            for pattern in &patterns {
                if line.contains(pattern) {
                    let severity = if self.config.allow_network {
                        RiskLevel::Low
                    } else {
                        RiskLevel::Medium
                    };

                    found_patterns.push((line_num + 1, pattern, severity));
                }
            }
        }

        for (line_num, pattern, severity) in found_patterns {
            report.issues.push(SkillAuditIssue {
                issue_type: IssueType::NetworkAccess,
                severity,
                file: Some(file.to_string()),
                line: Some(line_num),
                message: format!("Network access pattern detected: {}", pattern),
                snippet: self.get_line_snippet(content, line_num),
            });
        }
    }

    /// Check for dangerous commands
    fn check_dangerous_commands(&self, content: &str, file: &str, report: &mut SkillAuditReport) {
        let dangerous_patterns = [
            ("eval(", RiskLevel::High),
            ("exec(", RiskLevel::High),
            ("system(", RiskLevel::High),
            ("subprocess.call", RiskLevel::Medium),
            ("subprocess.Popen", RiskLevel::Medium),
            ("os.system", RiskLevel::High),
            ("child_process", RiskLevel::Medium),
            ("spawn(", RiskLevel::High),
            ("Runtime.exec", RiskLevel::High),
            ("ProcessBuilder", RiskLevel::Medium),
        ];

        for (line_num, line) in content.lines().enumerate() {
            for (pattern, severity) in &dangerous_patterns {
                if line.contains(pattern) {
                    report.issues.push(SkillAuditIssue {
                        issue_type: IssueType::DangerousCommand,
                        severity: *severity,
                        file: Some(file.to_string()),
                        line: Some(line_num + 1),
                        message: format!("Dangerous command detected: {}", pattern),
                        snippet: Some(line.to_string()),
                    });
                }
            }
        }
    }

    /// Check for file access patterns
    fn check_file_access_patterns(&self, content: &str, file: &str, report: &mut SkillAuditReport) {
        let file_patterns = [
            ("open(", RiskLevel::Low),
            ("File.open", RiskLevel::Low),
            ("fs.readFile", RiskLevel::Low),
            ("fs.writeFile", RiskLevel::Low),
            ("Path.", RiskLevel::Low),
            ("/etc/", RiskLevel::Medium),
            ("/home/", RiskLevel::Medium),
            ("C:\\\\", RiskLevel::Medium),
        ];

        for (line_num, line) in content.lines().enumerate() {
            for (pattern, severity) in &file_patterns {
                if line.contains(pattern) {
                    report.issues.push(SkillAuditIssue {
                        issue_type: IssueType::FileAccess,
                        severity: *severity,
                        file: Some(file.to_string()),
                        line: Some(line_num + 1),
                        message: format!("File access pattern detected: {}", pattern),
                        snippet: Some(line.to_string()),
                    });
                }
            }
        }
    }

    /// Check for code execution patterns
    fn check_code_execution(&self, content: &str, file: &str, report: &mut SkillAuditReport) {
        let code_exec_patterns = [
            "compile(",
            "execfile(",
            "__import__",
            "importlib.",
            "getattr(__builtins__",
        ];

        for (line_num, line) in content.lines().enumerate() {
            for pattern in &code_exec_patterns {
                if line.contains(pattern) {
                    report.issues.push(SkillAuditIssue {
                        issue_type: IssueType::CodeExecution,
                        severity: RiskLevel::High,
                        file: Some(file.to_string()),
                        line: Some(line_num + 1),
                        message: format!("Code execution pattern detected: {}", pattern),
                        snippet: Some(line.to_string()),
                    });
                }
            }
        }
    }

    /// Get a snippet of the line for context
    fn get_line_snippet(&self, content: &str, line_num: usize) -> Option<String> {
        content
            .lines()
            .nth(line_num.saturating_sub(1))
            .map(|s| s.to_string())
    }
}

/// Errors that can occur during auditing
#[derive(Debug, thiserror::Error)]
pub enum AuditError {
    #[error("IO error: {0}")]
    Io(#[from] std::io::Error),

    #[error("Failed to read skill content: {0}")]
    ReadError(String),
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs;
    use tempfile::TempDir;

    fn create_test_skill(content: &str) -> (TempDir, SkillMdFile) {
        let temp_dir = TempDir::new().unwrap();
        let skill_md = temp_dir.path().join("SKILL.md");

        let skill_content = format!(
            r#"---
name: test-skill
description: Test skill for auditing
---

# Test Skill

{}
"#,
            content
        );

        fs::write(&skill_md, skill_content).unwrap();

        let skill = SkillMdFile::parse(&skill_md).unwrap();
        (temp_dir, skill)
    }

    #[test]
    fn test_audit_safe_skill() {
        let content = r#"
This is a safe skill with no dangerous patterns.

## Features

- Simple text processing
- Data validation
"#;

        let (_temp, skill) = create_test_skill(content);
        let auditor = SkillAuditor::default_auditor();
        let report = auditor.audit(&skill).unwrap();

        assert!(report.safe);
        assert_eq!(report.risk_level, RiskLevel::Safe);
        assert!(!report.has_issues());
    }

    #[test]
    fn test_audit_network_access() {
        let content = r#"
## Fetch Data

```python
import requests
response = requests.get("https://api.example.com/data")
```
"#;

        let (_temp, skill) = create_test_skill(content);
        let config = AuditConfig {
            allow_network: false,
            ..Default::default()
        };
        let auditor = SkillAuditor::new(config);
        let report = auditor.audit(&skill).unwrap();

        assert!(!report.safe);
        assert_eq!(report.risk_level, RiskLevel::Medium);
        assert!(report.has_issues());

        // Check that network access was detected
        let network_issues: Vec<_> = report
            .issues
            .iter()
            .filter(|i| i.issue_type == IssueType::NetworkAccess)
            .collect();

        assert!(!network_issues.is_empty());
    }

    #[test]
    fn test_audit_dangerous_commands() {
        let content = r#"
## Execute Command

```python
import os
os.system("rm -rf /")
```
"#;

        let (_temp, skill) = create_test_skill(content);
        let auditor = SkillAuditor::default_auditor();
        let report = auditor.audit(&skill).unwrap();

        assert!(!report.safe);
        assert!(report.has_issues());

        // Check that dangerous command was detected
        let dangerous_issues: Vec<_> = report
            .issues
            .iter()
            .filter(|i| i.issue_type == IssueType::DangerousCommand)
            .collect();

        assert!(!dangerous_issues.is_empty());
        assert_eq!(dangerous_issues[0].severity, RiskLevel::High);
    }

    #[test]
    fn test_audit_file_access() {
        let content = r#"
## Read File

```python
with open("/etc/passwd", "r") as f:
    content = f.read()
```
"#;

        let (_temp, skill) = create_test_skill(content);
        let auditor = SkillAuditor::default_auditor();
        let report = auditor.audit(&skill).unwrap();

        assert!(report.has_issues());

        // Check that file access was detected
        let file_issues: Vec<_> = report
            .issues
            .iter()
            .filter(|i| i.issue_type == IssueType::FileAccess)
            .collect();

        assert!(!file_issues.is_empty());
    }

    #[test]
    fn test_audit_multiple_issues() {
        let content = r#"
## Multi-issue Skill

```python
import os
import requests

os.system("ls")
requests.get("https://example.com")

with open("/etc/passwd", "r") as f:
    pass
```
"#;

        let (_temp, skill) = create_test_skill(content);
        let auditor = SkillAuditor::default_auditor();
        let report = auditor.audit(&skill).unwrap();

        assert!(!report.safe);
        assert!(report.has_issues());

        // Should have multiple issues
        assert!(report.issues.len() >= 3);
    }

    #[test]
    fn test_audit_strict_mode() {
        let content = r#"
## Read File

```python
with open("data.txt", "r") as f:
    content = f.read()
```
"#;

        let (_temp, skill) = create_test_skill(content);

        // Non-strict mode should allow this (low risk)
        let config = AuditConfig {
            strict_mode: false,
            ..Default::default()
        };
        let auditor = SkillAuditor::new(config);
        let report = auditor.audit(&skill).unwrap();

        assert!(report.has_issues());
        assert_eq!(report.risk_level, RiskLevel::Low);

        // Strict mode might have different behavior
        // For now, strict mode mainly affects future extensibility
    }

    #[test]
    fn test_audit_critical_issues() {
        let content = r#"
## Critical Issues

```python
eval(__import__('os').system('rm -rf /'))
```
"#;

        let (_temp, skill) = create_test_skill(content);
        let auditor = SkillAuditor::default_auditor();
        let report = auditor.audit(&skill).unwrap();

        assert!(!report.safe);
        assert!(report.has_issues());

        // Check that we have critical issues
        let critical = report.critical_issues();
        assert!(!critical.is_empty());
    }

    #[test]
    fn test_risk_level_ordering() {
        assert!(RiskLevel::Safe < RiskLevel::Low);
        assert!(RiskLevel::Low < RiskLevel::Medium);
        assert!(RiskLevel::Medium < RiskLevel::High);
        assert!(RiskLevel::High < RiskLevel::Critical);
    }

    #[test]
    fn test_issue_type_display() {
        assert_eq!(IssueType::NetworkAccess.to_string(), "Network Access");
        assert_eq!(IssueType::DangerousCommand.to_string(), "Dangerous Command");
        assert_eq!(IssueType::FileAccess.to_string(), "File Access");
    }

    #[test]
    fn test_risk_level_display() {
        assert_eq!(RiskLevel::Safe.to_string(), "Safe");
        assert_eq!(RiskLevel::Critical.to_string(), "Critical");
    }
}