clawft-plugin 0.6.1

Plugin trait definitions for clawft
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
//! Per-agent sandbox policy definitions.
//!
//! The [`SandboxPolicy`] struct defines the runtime security restrictions for
//! an agent or plugin. It maps from per-agent config (`~/.clawft/agents/<id>/config.toml`)
//! to enforceable sandbox rules.
//!
//! The [`SandboxType`] enum determines which isolation mechanism is used:
//! - `Wasm` -- WASM sandbox (cross-platform, default for WASM plugins)
//! - `OsSandbox` -- seccomp + landlock on Linux (default for native on Linux)
//! - `Combined` -- both WASM + OS sandbox layers
//!
//! **Secure by default**: The default sandbox type is NOT `None`. WASM plugins
//! get `Wasm`, native execution on Linux gets `OsSandbox`.

use serde::{Deserialize, Serialize};
use std::collections::HashSet;
use std::path::PathBuf;

/// Sandbox isolation mechanism.
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum SandboxType {
    /// WASM sandbox via wasmtime WASI capabilities (cross-platform).
    Wasm,
    /// OS-level sandbox: seccomp + landlock on Linux.
    OsSandbox,
    /// Both WASM and OS-level sandbox layers.
    Combined,
}

impl Default for SandboxType {
    fn default() -> Self {
        // Secure by default: use OS sandbox on Linux, WASM elsewhere.
        if cfg!(target_os = "linux") {
            Self::OsSandbox
        } else {
            Self::Wasm
        }
    }
}

/// Network access policy for a sandboxed agent.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct NetworkPolicy {
    /// Whether network access is allowed at all.
    #[serde(default)]
    pub allow_network: bool,

    /// Allowed domain patterns (exact or wildcard `*.example.com`).
    #[serde(default)]
    pub allowed_domains: Vec<String>,

    /// Blocked domain patterns (takes precedence over allowed).
    #[serde(default)]
    pub blocked_domains: Vec<String>,

    /// Maximum outbound connections per minute.
    #[serde(default = "default_max_connections")]
    pub max_connections_per_minute: u32,
}

fn default_max_connections() -> u32 {
    30
}

/// Filesystem access policy for a sandboxed agent.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct FilesystemPolicy {
    /// Paths the agent can read from.
    #[serde(default)]
    pub readable_paths: Vec<PathBuf>,

    /// Paths the agent can write to.
    #[serde(default)]
    pub writable_paths: Vec<PathBuf>,

    /// Whether the agent can create new files.
    #[serde(default)]
    pub allow_create: bool,

    /// Whether the agent can delete files.
    #[serde(default)]
    pub allow_delete: bool,

    /// Maximum individual file size in bytes (default: 8MB).
    #[serde(default = "default_max_file_size")]
    pub max_file_size: u64,
}

fn default_max_file_size() -> u64 {
    8 * 1024 * 1024
}

/// Process execution policy for a sandboxed agent.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct ProcessPolicy {
    /// Whether the agent can execute shell commands.
    #[serde(default)]
    pub allow_shell: bool,

    /// Allowed command names (empty = none allowed unless `allow_shell` is true).
    #[serde(default)]
    pub allowed_commands: Vec<String>,

    /// Blocked command patterns (takes precedence over allowed).
    #[serde(default)]
    pub blocked_commands: Vec<String>,

    /// Maximum execution time per command in seconds.
    #[serde(default = "default_max_exec_time")]
    pub max_execution_seconds: u32,
}

fn default_max_exec_time() -> u32 {
    30
}

/// Environment variable access policy.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct EnvPolicy {
    /// Allowed environment variable names.
    #[serde(default)]
    pub allowed_vars: Vec<String>,

    /// Variables that are never accessible (hardcoded deny list).
    #[serde(default)]
    pub denied_vars: Vec<String>,
}

/// Per-agent sandbox policy.
///
/// Created from an agent's configuration and enforced at runtime by the
/// sandbox enforcement layer. Each agent's tool restrictions map to a
/// `SandboxPolicy`.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SandboxPolicy {
    /// Agent or plugin identifier.
    pub agent_id: String,

    /// Sandbox isolation type.
    #[serde(default)]
    pub sandbox_type: SandboxType,

    /// Network access policy.
    #[serde(default)]
    pub network: NetworkPolicy,

    /// Filesystem access policy.
    #[serde(default)]
    pub filesystem: FilesystemPolicy,

    /// Process execution policy.
    #[serde(default)]
    pub process: ProcessPolicy,

    /// Environment variable access policy.
    #[serde(default)]
    pub env: EnvPolicy,

    /// Tools this agent is allowed to use (empty = all tools allowed).
    #[serde(default)]
    pub allowed_tools: Vec<String>,

    /// Tools explicitly denied to this agent.
    #[serde(default)]
    pub denied_tools: Vec<String>,

    /// Whether audit logging is enabled for this agent.
    #[serde(default = "default_true")]
    pub audit_logging: bool,
}

fn default_true() -> bool {
    true
}

impl Default for SandboxPolicy {
    fn default() -> Self {
        Self {
            agent_id: String::new(),
            sandbox_type: SandboxType::default(),
            network: NetworkPolicy::default(),
            filesystem: FilesystemPolicy::default(),
            process: ProcessPolicy::default(),
            env: EnvPolicy::default(),
            allowed_tools: Vec::new(),
            denied_tools: Vec::new(),
            audit_logging: true,
        }
    }
}

impl SandboxPolicy {
    /// Create a new sandbox policy for the given agent.
    pub fn new(agent_id: impl Into<String>) -> Self {
        Self {
            agent_id: agent_id.into(),
            ..Default::default()
        }
    }

    /// Check whether a specific tool is allowed by this policy.
    pub fn is_tool_allowed(&self, tool_name: &str) -> bool {
        // Denied tools always take precedence.
        if self.denied_tools.iter().any(|t| t == tool_name) {
            return false;
        }
        // If allowed_tools is empty, all tools are allowed.
        if self.allowed_tools.is_empty() {
            return true;
        }
        self.allowed_tools.iter().any(|t| t == tool_name)
    }

    /// Check whether a domain is allowed by the network policy.
    pub fn is_domain_allowed(&self, domain: &str) -> bool {
        if !self.network.allow_network {
            return false;
        }
        // Check blocked domains first (takes precedence).
        for blocked in &self.network.blocked_domains {
            if domain_matches(domain, blocked) {
                return false;
            }
        }
        // If no allowed domains specified, all are allowed.
        if self.network.allowed_domains.is_empty() {
            return true;
        }
        self.network.allowed_domains.iter().any(|a| domain_matches(domain, a))
    }

    /// Check whether a file path is readable.
    pub fn is_path_readable(&self, path: &std::path::Path) -> bool {
        self.filesystem.readable_paths.iter().any(|allowed| {
            path.starts_with(allowed)
        })
    }

    /// Check whether a file path is writable.
    pub fn is_path_writable(&self, path: &std::path::Path) -> bool {
        self.filesystem.writable_paths.iter().any(|allowed| {
            path.starts_with(allowed)
        })
    }

    /// Check whether a command is allowed by the process policy.
    pub fn is_command_allowed(&self, command: &str) -> bool {
        if !self.process.allow_shell {
            return false;
        }
        // Blocked commands take precedence.
        if self.process.blocked_commands.iter().any(|b| b == command) {
            return false;
        }
        // If allowed_commands is empty but allow_shell is true, all allowed.
        if self.process.allowed_commands.is_empty() {
            return true;
        }
        self.process.allowed_commands.iter().any(|a| a == command)
    }

    /// Collect the set of all effective tool names that are allowed.
    pub fn effective_tools(&self) -> HashSet<String> {
        let mut tools: HashSet<String> = self.allowed_tools.iter().cloned().collect();
        for denied in &self.denied_tools {
            tools.remove(denied);
        }
        tools
    }

    /// Return the platform-appropriate sandbox type.
    ///
    /// On macOS, downgrades `OsSandbox` and `Combined` to `Wasm` with a
    /// warning, since seccomp/landlock are Linux-only.
    pub fn effective_sandbox_type(&self) -> SandboxType {
        if cfg!(target_os = "linux") {
            return self.sandbox_type.clone();
        }
        // Non-Linux: WASM-only fallback.
        match &self.sandbox_type {
            SandboxType::OsSandbox | SandboxType::Combined => {
                tracing::warn!(
                    agent = %self.agent_id,
                    "OS sandbox unavailable on this platform; \
                     falling back to WASM-only sandbox"
                );
                SandboxType::Wasm
            }
            other => other.clone(),
        }
    }
}

/// Check whether a domain matches a pattern (exact or wildcard).
fn domain_matches(domain: &str, pattern: &str) -> bool {
    let domain_lower = domain.to_lowercase();
    let pattern_lower = pattern.to_lowercase();

    if pattern_lower == "*" {
        return true;
    }
    if let Some(suffix) = pattern_lower.strip_prefix("*.") {
        return domain_lower.ends_with(&format!(".{suffix}"))
            || domain_lower == suffix;
    }
    domain_lower == pattern_lower
}

/// Audit log entry for a sandbox decision.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SandboxAuditEntry {
    /// Timestamp (ISO 8601).
    pub timestamp: String,
    /// Agent identifier.
    pub agent_id: String,
    /// Action attempted (e.g., "file_read", "network_connect", "tool_invoke").
    pub action: String,
    /// Target of the action (e.g., file path, URL, tool name).
    pub target: String,
    /// Whether the action was allowed.
    pub allowed: bool,
    /// Reason for denial (if denied).
    pub reason: Option<String>,
}

impl SandboxAuditEntry {
    /// Create a new audit entry for an allowed action.
    pub fn allowed(
        agent_id: impl Into<String>,
        action: impl Into<String>,
        target: impl Into<String>,
    ) -> Self {
        Self {
            timestamp: chrono::Utc::now().to_rfc3339(),
            agent_id: agent_id.into(),
            action: action.into(),
            target: target.into(),
            allowed: true,
            reason: None,
        }
    }

    /// Create a new audit entry for a denied action.
    pub fn denied(
        agent_id: impl Into<String>,
        action: impl Into<String>,
        target: impl Into<String>,
        reason: impl Into<String>,
    ) -> Self {
        Self {
            timestamp: chrono::Utc::now().to_rfc3339(),
            agent_id: agent_id.into(),
            action: action.into(),
            target: target.into(),
            allowed: false,
            reason: Some(reason.into()),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::path::Path;

    #[test]
    fn default_sandbox_type_is_not_none() {
        let st = SandboxType::default();
        // On Linux, should be OsSandbox; on other platforms, Wasm.
        // Either way, it is NOT "None".
        assert!(matches!(st, SandboxType::OsSandbox | SandboxType::Wasm));
    }

    #[test]
    fn default_policy_has_secure_defaults() {
        let policy = SandboxPolicy::default();
        assert!(!policy.network.allow_network);
        assert!(policy.filesystem.readable_paths.is_empty());
        assert!(policy.filesystem.writable_paths.is_empty());
        assert!(!policy.process.allow_shell);
        assert!(policy.audit_logging);
    }

    #[test]
    fn tool_allowed_when_list_empty() {
        let policy = SandboxPolicy::new("test-agent");
        assert!(policy.is_tool_allowed("any_tool"));
    }

    #[test]
    fn tool_denied_when_in_denied_list() {
        let policy = SandboxPolicy {
            agent_id: "test".into(),
            denied_tools: vec!["dangerous_tool".into()],
            ..Default::default()
        };
        assert!(!policy.is_tool_allowed("dangerous_tool"));
    }

    #[test]
    fn tool_allowed_only_when_in_allowed_list() {
        let policy = SandboxPolicy {
            agent_id: "test".into(),
            allowed_tools: vec!["read_file".into(), "grep".into()],
            ..Default::default()
        };
        assert!(policy.is_tool_allowed("read_file"));
        assert!(policy.is_tool_allowed("grep"));
        assert!(!policy.is_tool_allowed("bash"));
    }

    #[test]
    fn denied_takes_precedence_over_allowed() {
        let policy = SandboxPolicy {
            agent_id: "test".into(),
            allowed_tools: vec!["bash".into()],
            denied_tools: vec!["bash".into()],
            ..Default::default()
        };
        assert!(!policy.is_tool_allowed("bash"));
    }

    #[test]
    fn domain_not_allowed_when_network_disabled() {
        let policy = SandboxPolicy::new("test");
        assert!(!policy.is_domain_allowed("example.com"));
    }

    #[test]
    fn domain_allowed_with_exact_match() {
        let policy = SandboxPolicy {
            agent_id: "test".into(),
            network: NetworkPolicy {
                allow_network: true,
                allowed_domains: vec!["api.example.com".into()],
                ..Default::default()
            },
            ..Default::default()
        };
        assert!(policy.is_domain_allowed("api.example.com"));
        assert!(!policy.is_domain_allowed("evil.com"));
    }

    #[test]
    fn domain_wildcard_match() {
        let policy = SandboxPolicy {
            agent_id: "test".into(),
            network: NetworkPolicy {
                allow_network: true,
                allowed_domains: vec!["*.example.com".into()],
                ..Default::default()
            },
            ..Default::default()
        };
        assert!(policy.is_domain_allowed("sub.example.com"));
        assert!(policy.is_domain_allowed("example.com"));
        assert!(!policy.is_domain_allowed("evil.com"));
    }

    #[test]
    fn blocked_domain_takes_precedence() {
        let policy = SandboxPolicy {
            agent_id: "test".into(),
            network: NetworkPolicy {
                allow_network: true,
                allowed_domains: vec!["*.example.com".into()],
                blocked_domains: vec!["evil.example.com".into()],
                ..Default::default()
            },
            ..Default::default()
        };
        assert!(!policy.is_domain_allowed("evil.example.com"));
        assert!(policy.is_domain_allowed("good.example.com"));
    }

    #[test]
    fn path_readable_check() {
        let policy = SandboxPolicy {
            agent_id: "test".into(),
            filesystem: FilesystemPolicy {
                readable_paths: vec![PathBuf::from("/home/user/workspace")],
                ..Default::default()
            },
            ..Default::default()
        };
        assert!(policy.is_path_readable(Path::new("/home/user/workspace/file.rs")));
        assert!(!policy.is_path_readable(Path::new("/etc/passwd")));
    }

    #[test]
    fn path_writable_check() {
        let policy = SandboxPolicy {
            agent_id: "test".into(),
            filesystem: FilesystemPolicy {
                writable_paths: vec![PathBuf::from("/tmp/sandbox")],
                ..Default::default()
            },
            ..Default::default()
        };
        assert!(policy.is_path_writable(Path::new("/tmp/sandbox/output.txt")));
        assert!(!policy.is_path_writable(Path::new("/etc/config")));
    }

    #[test]
    fn command_not_allowed_when_shell_disabled() {
        let policy = SandboxPolicy::new("test");
        assert!(!policy.is_command_allowed("ls"));
    }

    #[test]
    fn command_allowed_when_shell_enabled() {
        let policy = SandboxPolicy {
            agent_id: "test".into(),
            process: ProcessPolicy {
                allow_shell: true,
                ..Default::default()
            },
            ..Default::default()
        };
        assert!(policy.is_command_allowed("ls"));
    }

    #[test]
    fn command_blocked_takes_precedence() {
        let policy = SandboxPolicy {
            agent_id: "test".into(),
            process: ProcessPolicy {
                allow_shell: true,
                allowed_commands: vec!["rm".into()],
                blocked_commands: vec!["rm".into()],
                ..Default::default()
            },
            ..Default::default()
        };
        assert!(!policy.is_command_allowed("rm"));
    }

    #[test]
    fn effective_tools_excludes_denied() {
        let policy = SandboxPolicy {
            agent_id: "test".into(),
            allowed_tools: vec!["read".into(), "write".into(), "bash".into()],
            denied_tools: vec!["bash".into()],
            ..Default::default()
        };
        let effective = policy.effective_tools();
        assert!(effective.contains("read"));
        assert!(effective.contains("write"));
        assert!(!effective.contains("bash"));
    }

    #[test]
    fn audit_entry_allowed() {
        let entry = SandboxAuditEntry::allowed("agent-1", "file_read", "/tmp/test.txt");
        assert!(entry.allowed);
        assert!(entry.reason.is_none());
    }

    #[test]
    fn audit_entry_denied() {
        let entry = SandboxAuditEntry::denied(
            "agent-1",
            "network_connect",
            "evil.com",
            "domain not in allowlist",
        );
        assert!(!entry.allowed);
        assert_eq!(entry.reason.as_deref(), Some("domain not in allowlist"));
    }

    #[test]
    fn domain_matches_star() {
        assert!(domain_matches("anything.com", "*"));
    }

    #[test]
    fn domain_matches_case_insensitive() {
        assert!(domain_matches("API.Example.COM", "api.example.com"));
    }

    #[test]
    fn sandbox_policy_serialization_roundtrip() {
        let policy = SandboxPolicy {
            agent_id: "test-agent".into(),
            sandbox_type: SandboxType::Combined,
            network: NetworkPolicy {
                allow_network: true,
                allowed_domains: vec!["*.example.com".into()],
                blocked_domains: vec!["evil.example.com".into()],
                max_connections_per_minute: 60,
            },
            filesystem: FilesystemPolicy {
                readable_paths: vec![PathBuf::from("/workspace")],
                writable_paths: vec![PathBuf::from("/tmp")],
                allow_create: true,
                allow_delete: false,
                max_file_size: 4 * 1024 * 1024,
            },
            process: ProcessPolicy {
                allow_shell: true,
                allowed_commands: vec!["git".into(), "cargo".into()],
                blocked_commands: vec!["rm".into()],
                max_execution_seconds: 60,
            },
            env: EnvPolicy {
                allowed_vars: vec!["HOME".into()],
                denied_vars: vec!["AWS_SECRET_ACCESS_KEY".into()],
            },
            allowed_tools: vec!["read_file".into()],
            denied_tools: vec!["bash".into()],
            audit_logging: true,
        };
        let json = serde_json::to_string(&policy).unwrap();
        let restored: SandboxPolicy = serde_json::from_str(&json).unwrap();
        assert_eq!(restored.agent_id, "test-agent");
        assert_eq!(restored.sandbox_type, SandboxType::Combined);
        assert!(restored.network.allow_network);
        assert!(restored.audit_logging);
    }
}