clawdstrike 0.2.5

Security guards and policy engine for AI agent execution
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
//! Filesystem Inline Reference Monitor
//!
//! Monitors filesystem operations and enforces path-based access control.

use async_trait::async_trait;
use tracing::debug;

use crate::policy::Policy;

use super::{Decision, EventType, HostCall, Monitor};

/// Filesystem IRM
pub struct FilesystemIrm {
    name: String,
}

impl FilesystemIrm {
    /// Create a new filesystem IRM
    pub fn new() -> Self {
        Self {
            name: "filesystem_irm".to_string(),
        }
    }

    /// Check if a path is forbidden based on policy
    fn is_forbidden(&self, path: &str, policy: &Policy) -> Option<String> {
        let normalized = self.normalize_path(path);

        // Use forbidden_path guard config if available
        if let Some(config) = &policy.guards.forbidden_path {
            for pattern in config.effective_patterns() {
                // Simple prefix/contains check (full glob matching done by guard)
                if normalized.contains(pattern.trim_start_matches("**/").trim_end_matches("/**"))
                    || self.matches_simple_pattern(&normalized, &pattern)
                {
                    return Some(pattern);
                }
            }
        }

        // Default forbidden paths
        let default_forbidden = [
            "/.ssh/",
            "/id_rsa",
            "/id_ed25519",
            "/.aws/",
            "/.env",
            "/etc/shadow",
            "/etc/passwd",
            "/.gnupg/",
            "/.kube/",
        ];

        for forbidden in default_forbidden {
            if normalized.contains(forbidden) {
                return Some(forbidden.to_string());
            }
        }

        None
    }

    /// Check if write is allowed based on policy roots
    fn is_write_allowed(&self, path: &str, _policy: &Policy) -> bool {
        let normalized = self.normalize_path(path);

        // If policy has explicit allowed write roots, check them
        // For now, allow writes to common safe locations
        let safe_prefixes = ["/tmp/", "/workspace/", "/app/", "/home/"];

        for prefix in safe_prefixes {
            if normalized.starts_with(prefix) {
                return true;
            }
        }

        // Check if path is in current working directory (implied safe)
        if !normalized.starts_with('/') {
            return true;
        }

        // Default: deny writes to system paths
        let system_paths = ["/etc/", "/usr/", "/bin/", "/sbin/", "/lib/", "/var/"];
        for sys in system_paths {
            if normalized.starts_with(sys) {
                return false;
            }
        }

        true
    }

    /// Normalize a path for comparison
    fn normalize_path(&self, path: &str) -> String {
        // Expand tilde (simplified - in real code we'd use proper home dir)
        let expanded = if path.starts_with("~/") {
            format!("/home/user{}", &path[1..])
        } else {
            path.to_string()
        };

        // Remove trailing slashes
        let trimmed = expanded.trim_end_matches('/');

        // Resolve "." but preserve ".." so security checks do not silently change path meaning.
        let mut parts: Vec<&str> = Vec::new();
        for part in trimmed.split('/') {
            match part {
                "" | "." => {}
                ".." => parts.push(".."),
                other => {
                    parts.push(other);
                }
            }
        }

        if trimmed.starts_with('/') {
            format!("/{}", parts.join("/"))
        } else {
            parts.join("/")
        }
    }

    /// Simple pattern matching (for **/ and /** patterns)
    fn matches_simple_pattern(&self, path: &str, pattern: &str) -> bool {
        let pattern = pattern.replace("**", "");
        let pattern = pattern.trim_matches('/');

        if pattern.is_empty() {
            return false;
        }

        path.contains(pattern)
    }

    /// Extract path from host call arguments
    fn extract_path(&self, call: &HostCall) -> Option<String> {
        let allow_bare_string_paths = call.function.contains("path");

        for arg in &call.args {
            if let Some(obj) = arg.as_object() {
                for key in ["path", "file_path", "target_path"] {
                    if let Some(path) = obj.get(key).and_then(|value| value.as_str()) {
                        let trimmed = path.trim();
                        if trimmed.is_empty() {
                            continue;
                        }
                        if self.looks_like_path(trimmed)
                            || self.has_parent_traversal(trimmed)
                            || self.looks_like_bare_filename(trimmed)
                        {
                            return Some(trimmed.to_string());
                        }
                    }
                }
            }
        }

        for arg in &call.args {
            if let Some(s) = arg.as_str() {
                let trimmed = s.trim();
                if self.looks_like_path(trimmed)
                    || (allow_bare_string_paths && self.looks_like_bare_filename(trimmed))
                {
                    return Some(trimmed.to_string());
                }
            }
        }

        None
    }

    fn looks_like_path(&self, value: &str) -> bool {
        if value.is_empty() {
            return false;
        }

        if value.starts_with('/') || value.starts_with("~/") || value.starts_with("./") {
            return true;
        }

        if value == ".." || value.starts_with("../") {
            return true;
        }

        if value.contains('\\') {
            return true;
        }

        value.contains('/') && !value.contains("://") && !self.looks_like_mime_type(value)
    }

    fn looks_like_mime_type(&self, value: &str) -> bool {
        let mut parts = value.split('/');
        let Some(kind) = parts.next() else {
            return false;
        };
        let Some(subtype) = parts.next() else {
            return false;
        };
        if parts.next().is_some() {
            return false;
        }
        if kind.is_empty() || subtype.is_empty() {
            return false;
        }
        // Preserve common relative file paths such as "image/logo.png".
        if subtype.contains('.') {
            return false;
        }

        matches!(
            kind.to_ascii_lowercase().as_str(),
            "application"
                | "audio"
                | "font"
                | "image"
                | "message"
                | "model"
                | "multipart"
                | "text"
                | "video"
        )
    }

    fn looks_like_bare_filename(&self, value: &str) -> bool {
        let value = value.trim();
        if value.is_empty() {
            return false;
        }

        if value.contains("://") {
            return false;
        }

        if value == "." || value == ".." {
            return false;
        }

        if value.contains('/') || value.contains('\\') {
            return false;
        }

        if value.bytes().all(|b| b.is_ascii_digit()) {
            return false;
        }

        !value.chars().any(|ch| ch.is_control())
    }

    fn has_parent_traversal(&self, path: &str) -> bool {
        path.replace('\\', "/").split('/').any(|seg| seg == "..")
    }
}

impl Default for FilesystemIrm {
    fn default() -> Self {
        Self::new()
    }
}

#[async_trait]
impl Monitor for FilesystemIrm {
    fn name(&self) -> &str {
        &self.name
    }

    fn handles(&self, event_type: EventType) -> bool {
        matches!(
            event_type,
            EventType::FsRead | EventType::FsWrite | EventType::ArtifactEmit
        )
    }

    async fn evaluate(&self, call: &HostCall, policy: &Policy) -> Decision {
        let path = match self.extract_path(call) {
            Some(p) => p,
            None => {
                let reason = format!(
                    "Cannot determine filesystem path for call {}",
                    call.function
                );
                debug!("FilesystemIrm: {}", reason);
                return Decision::Deny { reason };
            }
        };

        debug!("FilesystemIrm checking path: {}", path);

        if self.has_parent_traversal(&path) {
            return Decision::Deny {
                reason: format!("Path contains parent traversal segment: {}", path),
            };
        }

        // Check forbidden paths
        if let Some(pattern) = self.is_forbidden(&path, policy) {
            return Decision::Deny {
                reason: format!("Path {} matches forbidden pattern: {}", path, pattern),
            };
        }

        // For write operations, check if path is in allowed roots
        let is_write = call.function.contains("write")
            || call.function.contains("create")
            || call.function.contains("unlink")
            || call.function.contains("mkdir")
            || call.function.contains("rename");

        if is_write && !self.is_write_allowed(&path, policy) {
            return Decision::Deny {
                reason: format!("Write to {} not in allowed roots", path),
            };
        }

        Decision::Allow
    }
}

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

    #[test]
    fn test_normalize_path() {
        let irm = FilesystemIrm::new();

        assert_eq!(irm.normalize_path("/foo/bar"), "/foo/bar");
        assert_eq!(irm.normalize_path("/foo/bar/"), "/foo/bar");
        assert_eq!(irm.normalize_path("/foo/../bar"), "/foo/../bar");
        assert_eq!(irm.normalize_path("/foo/./bar"), "/foo/bar");
        assert_eq!(irm.normalize_path("~/test"), "/home/user/test");
    }

    #[test]
    fn test_is_forbidden_default() {
        let irm = FilesystemIrm::new();
        let policy = Policy::default();

        assert!(irm
            .is_forbidden("/home/user/.ssh/id_rsa", &policy)
            .is_some());
        assert!(irm.is_forbidden("/etc/shadow", &policy).is_some());
        assert!(irm
            .is_forbidden("/home/user/.aws/credentials", &policy)
            .is_some());
        assert!(irm.is_forbidden("/app/src/main.rs", &policy).is_none());
    }

    #[test]
    fn test_is_write_allowed() {
        let irm = FilesystemIrm::new();
        let policy = Policy::default();

        assert!(irm.is_write_allowed("/tmp/test.txt", &policy));
        assert!(irm.is_write_allowed("/workspace/output.txt", &policy));
        assert!(!irm.is_write_allowed("/etc/passwd", &policy));
        assert!(!irm.is_write_allowed("/usr/bin/test", &policy));
    }

    #[tokio::test]
    async fn test_forbidden_path_denied() {
        let irm = FilesystemIrm::new();
        let policy = Policy::default();

        let call = HostCall::new("fd_read", vec![serde_json::json!("/etc/shadow")]);
        let decision = irm.evaluate(&call, &policy).await;

        assert!(!decision.is_allowed());
    }

    #[tokio::test]
    async fn test_allowed_read() {
        let irm = FilesystemIrm::new();
        let policy = Policy::default();

        let call = HostCall::new("fd_read", vec![serde_json::json!("/workspace/foo.txt")]);
        let decision = irm.evaluate(&call, &policy).await;

        assert!(decision.is_allowed());
    }

    #[tokio::test]
    async fn test_write_outside_allowed_roots() {
        let irm = FilesystemIrm::new();
        let policy = Policy::default();

        let call = HostCall::new("fd_write", vec![serde_json::json!("/etc/test.conf")]);
        let decision = irm.evaluate(&call, &policy).await;

        assert!(!decision.is_allowed());
    }

    #[tokio::test]
    async fn test_write_in_allowed_roots() {
        let irm = FilesystemIrm::new();
        let policy = Policy::default();

        let call = HostCall::new("fd_write", vec![serde_json::json!("/workspace/output.txt")]);
        let decision = irm.evaluate(&call, &policy).await;

        assert!(decision.is_allowed());
    }

    #[test]
    fn test_extract_path() {
        let irm = FilesystemIrm::new();

        let call = HostCall::new("fd_read", vec![serde_json::json!("/etc/passwd")]);
        assert_eq!(irm.extract_path(&call), Some("/etc/passwd".to_string()));

        let call = HostCall::new("fd_read", vec![serde_json::json!({"path": "/app/main.rs"})]);
        assert_eq!(irm.extract_path(&call), Some("/app/main.rs".to_string()));

        let call = HostCall::new("fd_read", vec![serde_json::json!("../../etc/passwd")]);
        assert_eq!(
            irm.extract_path(&call),
            Some("../../etc/passwd".to_string())
        );

        let call = HostCall::new("path_open", vec![serde_json::json!("README.md")]);
        assert_eq!(irm.extract_path(&call), Some("README.md".to_string()));

        let call = HostCall::new("fd_read", vec![serde_json::json!("image/logo.png")]);
        assert_eq!(irm.extract_path(&call), Some("image/logo.png".to_string()));

        let call = HostCall::new(
            "fd_write",
            vec![serde_json::json!({"target_path": "config.json"})],
        );
        assert_eq!(irm.extract_path(&call), Some("config.json".to_string()));

        let call = HostCall::new(
            "fd_read",
            vec![
                serde_json::json!("text/plain"),
                serde_json::json!({"path": "../../etc/passwd"}),
            ],
        );
        assert_eq!(
            irm.extract_path(&call),
            Some("../../etc/passwd".to_string())
        );

        assert!(!irm.looks_like_path("text/plain"));
        assert!(irm.looks_like_path("image/logo.png"));
        assert!(irm.looks_like_path("src/main.rs"));

        let call = HostCall::new("fd_read", vec![serde_json::json!(123)]);
        assert_eq!(irm.extract_path(&call), None);
    }

    #[tokio::test]
    async fn filesystem_irm_allows_bare_filename_for_path_style_calls() {
        let irm = FilesystemIrm::new();
        let policy = Policy::default();
        let call = HostCall::new("path_open", vec![serde_json::json!("README.md")]);
        let decision = irm.evaluate(&call, &policy).await;

        assert!(
            decision.is_allowed(),
            "bare filename should be treated as a valid filesystem path in path-style calls"
        );
    }

    #[tokio::test]
    async fn filesystem_irm_denies_parent_traversal_relative_paths() {
        let irm = FilesystemIrm::new();
        let policy = Policy::default();

        let call = HostCall::new("fd_read", vec![serde_json::json!("../../etc/passwd")]);
        let decision = irm.evaluate(&call, &policy).await;
        assert!(
            !decision.is_allowed(),
            "string traversal path should be denied"
        );

        let call = HostCall::new(
            "fd_write",
            vec![serde_json::json!({"path": "./../..//etc/passwd"})],
        );
        let decision = irm.evaluate(&call, &policy).await;
        assert!(
            !decision.is_allowed(),
            "object traversal path should be denied"
        );

        let call = HostCall::new(
            "fd_read",
            vec![
                serde_json::json!("text/plain"),
                serde_json::json!({"path": "../../etc/passwd"}),
            ],
        );
        let decision = irm.evaluate(&call, &policy).await;
        assert!(
            !decision.is_allowed(),
            "object traversal path must not be bypassed by slash-containing non-path tokens"
        );
    }

    #[tokio::test]
    async fn filesystem_irm_prefers_object_path_over_pathlike_string_arg() {
        let irm = FilesystemIrm::new();
        let policy = Policy::default();
        let call = HostCall::new(
            "fd_read",
            vec![
                serde_json::json!("image/logo.png"),
                serde_json::json!({"path": "../../etc/passwd"}),
            ],
        );

        let decision = irm.evaluate(&call, &policy).await;
        match decision {
            Decision::Deny { reason } => {
                assert!(
                    reason.contains("parent traversal"),
                    "deny reason should use object path traversal, not a slash token: {reason}"
                );
            }
            other => panic!("expected deny, got {other:?}"),
        }
    }

    #[tokio::test]
    async fn filesystem_irm_denies_traversal_when_path_is_in_nonfirst_object_arg() {
        let irm = FilesystemIrm::new();
        let policy = Policy::default();
        let call = HostCall::new(
            "fd_read",
            vec![
                serde_json::json!({"fd": 3}),
                serde_json::json!({"path": "../../etc/passwd"}),
            ],
        );

        let decision = irm.evaluate(&call, &policy).await;
        match decision {
            Decision::Deny { reason } => {
                assert!(
                    reason.contains("parent traversal"),
                    "deny reason should explain traversal rejection: {reason}"
                );
            }
            other => panic!("expected deny, got {other:?}"),
        }
    }

    #[tokio::test]
    async fn filesystem_irm_denies_when_no_path_can_be_extracted() {
        let irm = FilesystemIrm::new();
        let policy = Policy::default();
        let call = HostCall::new("fd_read", vec![serde_json::json!({"fd": 3})]);
        let decision = irm.evaluate(&call, &policy).await;

        assert!(
            !decision.is_allowed(),
            "filesystem calls without extractable paths must fail closed"
        );
    }

    #[test]
    fn test_handles_event_types() {
        let irm = FilesystemIrm::new();

        assert!(irm.handles(EventType::FsRead));
        assert!(irm.handles(EventType::FsWrite));
        assert!(irm.handles(EventType::ArtifactEmit));
        assert!(!irm.handles(EventType::NetConnect));
        assert!(!irm.handles(EventType::CommandExec));
    }
}