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
//! WasmGuard and WasmGuardFactory — host-side wrappers that implement the Guard
//! trait by delegating to a sandboxed WASM plugin module.

use std::sync::Arc;

use async_trait::async_trait;
use serde_json::Value;

use crate::error::Result;
use crate::guards::{CustomGuardFactory, Guard, GuardAction, GuardContext, GuardResult, Severity};
use crate::plugins::manifest::{PluginCapabilities, PluginResourceLimits};
use crate::plugins::runtime::{
    execute_wasm_guard_bytes, WasmGuardInputEnvelope, WasmGuardRuntimeOptions,
};

/// A security guard backed by a sandboxed WASM module.
///
/// The compiled WASM bytes are cached via `Arc` so that cloning the guard (or
/// building multiple instances from the same factory) does not duplicate the
/// underlying allocation.
pub struct WasmGuard {
    /// Human-readable guard identifier (e.g. `acme.secret-scan`).
    guard_name: String,
    /// Raw WASM module bytes, shared across clones.
    wasm_bytes: Arc<Vec<u8>>,
    /// Declared action types this guard handles. Empty means "all".
    handles_actions: Vec<String>,
    /// Sandbox capabilities granted to the plugin.
    capabilities: PluginCapabilities,
    /// Resource limits for the WASM runtime.
    resources: PluginResourceLimits,
    /// Per-instance configuration forwarded into the WASM envelope.
    config: Value,
}

impl WasmGuard {
    /// Create a new WASM-backed guard.
    pub fn new(
        guard_name: String,
        wasm_bytes: Arc<Vec<u8>>,
        handles_actions: Vec<String>,
        capabilities: PluginCapabilities,
        resources: PluginResourceLimits,
        config: Value,
    ) -> Self {
        Self {
            guard_name,
            wasm_bytes,
            handles_actions,
            capabilities,
            resources,
            config,
        }
    }

    /// Map a `GuardAction` to its string action-type name.
    fn action_type_str(action: &GuardAction<'_>) -> &'static str {
        match action {
            GuardAction::FileAccess(_) => "file_access",
            GuardAction::FileWrite(_, _) => "file_write",
            GuardAction::NetworkEgress(_, _) => "network_egress",
            GuardAction::ShellCommand(_) => "shell_command",
            GuardAction::McpTool(_, _) => "mcp_tool",
            GuardAction::Patch(_, _) => "patch",
            GuardAction::Custom(_, _) => "custom",
        }
    }

    /// Serialize a `GuardAction` into the JSON payload expected by WASM guards.
    fn action_to_payload(action: &GuardAction<'_>, context: &GuardContext) -> Value {
        let data = match action {
            GuardAction::FileAccess(path) => serde_json::json!({
                "type": "file_access",
                "path": path,
            }),
            GuardAction::FileWrite(path, content) => serde_json::json!({
                "type": "file_write",
                "path": path,
                "content_length": content.len(),
                "content_utf8": String::from_utf8_lossy(content),
            }),
            GuardAction::NetworkEgress(host, port) => serde_json::json!({
                "type": "network_egress",
                "host": host,
                "port": port,
            }),
            GuardAction::ShellCommand(cmd) => serde_json::json!({
                "type": "shell_command",
                "command": cmd,
            }),
            GuardAction::McpTool(name, args) => serde_json::json!({
                "type": "mcp_tool",
                "toolName": name,
                "parameters": args,
            }),
            GuardAction::Patch(file, diff) => serde_json::json!({
                "type": "patch",
                "file": file,
                "diff": diff,
            }),
            GuardAction::Custom(kind, payload) => serde_json::json!({
                "type": kind,
                "data": payload,
            }),
        };

        serde_json::json!({
            "eventType": Self::action_type_str(action),
            "data": data,
            "context": {
                "cwd": context.cwd,
                "session_id": context.session_id,
                "agent_id": context.agent_id,
            },
        })
    }
}

#[async_trait]
impl Guard for WasmGuard {
    fn name(&self) -> &str {
        &self.guard_name
    }

    fn handles(&self, action: &GuardAction<'_>) -> bool {
        if self.handles_actions.is_empty() {
            return true;
        }
        let action_str = Self::action_type_str(action);
        self.handles_actions.iter().any(|h| h == action_str)
    }

    async fn check(&self, action: &GuardAction<'_>, context: &GuardContext) -> GuardResult {
        let envelope = WasmGuardInputEnvelope {
            guard: self.guard_name.clone(),
            action_type: Some(Self::action_type_str(action).to_string()),
            payload: Self::action_to_payload(action, context),
            config: self.config.clone(),
        };

        let options = WasmGuardRuntimeOptions {
            capabilities: self.capabilities.clone(),
            resources: self.resources.clone(),
        };

        // Execute WASM in a blocking context since wasmtime is synchronous.
        let wasm_bytes = Arc::clone(&self.wasm_bytes);
        let result = tokio::task::spawn_blocking(move || {
            execute_wasm_guard_bytes(&wasm_bytes, &envelope, &options)
        })
        .await;

        match result {
            Ok(Ok(execution)) => execution.result,
            Ok(Err(e)) => {
                // WASM runtime error → fail-closed
                GuardResult {
                    allowed: false,
                    guard: self.guard_name.clone(),
                    severity: Severity::Error,
                    message: format!("WASM guard runtime error: {e}"),
                    details: Some(serde_json::json!({
                        "runtime_fault": "execution_error",
                    })),
                }
            }
            Err(e) => {
                // Tokio join error (panic in blocking task) → fail-closed
                GuardResult {
                    allowed: false,
                    guard: self.guard_name.clone(),
                    severity: Severity::Error,
                    message: format!("WASM guard task panicked: {e}"),
                    details: Some(serde_json::json!({
                        "runtime_fault": "task_panic",
                    })),
                }
            }
        }
    }
}

/// Factory that produces [`WasmGuard`] instances from cached WASM bytes.
///
/// Registered in the [`crate::guards::CustomGuardRegistry`] and invoked
/// when a policy references the guard by id.
pub struct WasmGuardFactory {
    /// Guard identifier (matches the `id` used in policy YAML).
    guard_id: String,
    /// Shared WASM module bytes.
    wasm_bytes: Arc<Vec<u8>>,
    /// Declared action types the guard handles.
    handles_actions: Vec<String>,
    /// Capabilities granted to the plugin sandbox.
    capabilities: PluginCapabilities,
    /// Resource limits for the WASM runtime.
    resources: PluginResourceLimits,
}

impl WasmGuardFactory {
    /// Create a new factory for a WASM guard plugin.
    pub fn new(
        guard_id: String,
        wasm_bytes: Vec<u8>,
        handles_actions: Vec<String>,
        capabilities: PluginCapabilities,
        resources: PluginResourceLimits,
    ) -> Self {
        Self {
            guard_id,
            wasm_bytes: Arc::new(wasm_bytes),
            handles_actions,
            capabilities,
            resources,
        }
    }
}

impl CustomGuardFactory for WasmGuardFactory {
    fn id(&self) -> &str {
        &self.guard_id
    }

    fn build(&self, config: Value) -> Result<Box<dyn Guard>> {
        Ok(Box::new(WasmGuard::new(
            self.guard_id.clone(),
            Arc::clone(&self.wasm_bytes),
            self.handles_actions.clone(),
            self.capabilities.clone(),
            self.resources.clone(),
            config,
        )))
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

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

    fn default_capabilities() -> PluginCapabilities {
        PluginCapabilities::default()
    }

    fn default_resources() -> PluginResourceLimits {
        PluginResourceLimits::default()
    }

    /// Compile a WAT string into WASM bytes for testing.
    fn compile_wat(wat_source: &str) -> Vec<u8> {
        wat::parse_str(wat_source).expect("valid WAT")
    }

    /// A minimal WASM guard that always allows.
    fn allow_guard_wat() -> &'static str {
        // Output: {"allowed":true,"severity":"low","message":"Allowed by wasm"} = 61 bytes
        r#"(module
            (import "clawdstrike_host" "set_output" (func $set_output (param i32 i32) (result i32)))
            (import "clawdstrike_host" "request_capability" (func $cap (param i32) (result i32)))
            (memory (export "memory") 1)
            (data (i32.const 64) "{\"allowed\":true,\"severity\":\"low\",\"message\":\"Allowed by wasm\"}")
            (func (export "clawdstrike_guard_init") (result i32)
              i32.const 1)
            (func (export "clawdstrike_guard_handles") (param i32 i32) (result i32)
              i32.const 1)
            (func (export "clawdstrike_guard_check") (param i32 i32) (result i32)
              i32.const 64
              i32.const 61
              call $set_output
              drop
              i32.const 0)
        )"#
    }

    /// A minimal WASM guard that always denies.
    fn deny_guard_wat() -> &'static str {
        r#"(module
            (import "clawdstrike_host" "set_output" (func $set_output (param i32 i32) (result i32)))
            (import "clawdstrike_host" "request_capability" (func $cap (param i32) (result i32)))
            (memory (export "memory") 1)
            (data (i32.const 64) "{\"allowed\":false,\"severity\":\"high\",\"message\":\"Denied by wasm\"}")
            (func (export "clawdstrike_guard_init") (result i32)
              i32.const 1)
            (func (export "clawdstrike_guard_handles") (param i32 i32) (result i32)
              i32.const 1)
            (func (export "clawdstrike_guard_check") (param i32 i32) (result i32)
              i32.const 64
              i32.const 62
              call $set_output
              drop
              i32.const 0)
        )"#
    }

    /// A WASM guard that returns 0 from handles (does not handle the action).
    fn skip_guard_wat() -> &'static str {
        r#"(module
            (import "clawdstrike_host" "set_output" (func $set_output (param i32 i32) (result i32)))
            (import "clawdstrike_host" "request_capability" (func $cap (param i32) (result i32)))
            (memory (export "memory") 1)
            (func (export "clawdstrike_guard_init") (result i32)
              i32.const 1)
            (func (export "clawdstrike_guard_handles") (param i32 i32) (result i32)
              i32.const 0)
            (func (export "clawdstrike_guard_check") (param i32 i32) (result i32)
              i32.const 0)
        )"#
    }

    #[tokio::test]
    async fn wasm_guard_check_allow() {
        let wasm = compile_wat(allow_guard_wat());
        let guard = WasmGuard::new(
            "test.allow".to_string(),
            Arc::new(wasm),
            vec![],
            default_capabilities(),
            default_resources(),
            serde_json::json!({}),
        );

        let ctx = GuardContext::new();
        let result = guard
            .check(&GuardAction::FileAccess("/tmp/safe"), &ctx)
            .await;
        assert!(result.allowed);
        assert_eq!(result.guard, "test.allow");
    }

    #[tokio::test]
    async fn wasm_guard_check_deny() {
        let wasm = compile_wat(deny_guard_wat());
        let guard = WasmGuard::new(
            "test.deny".to_string(),
            Arc::new(wasm),
            vec![],
            default_capabilities(),
            default_resources(),
            serde_json::json!({}),
        );

        let ctx = GuardContext::new();
        let result = guard
            .check(&GuardAction::ShellCommand("rm -rf /"), &ctx)
            .await;
        assert!(!result.allowed);
        assert_eq!(result.guard, "test.deny");
        assert_eq!(result.severity, Severity::Error);
    }

    #[tokio::test]
    async fn wasm_guard_handles_delegates_to_action_list() {
        let wasm = compile_wat(allow_guard_wat());

        // Guard that only handles file_access
        let guard = WasmGuard::new(
            "test.file_only".to_string(),
            Arc::new(wasm),
            vec!["file_access".to_string()],
            default_capabilities(),
            default_resources(),
            serde_json::json!({}),
        );

        assert!(guard.handles(&GuardAction::FileAccess("/tmp/x")));
        assert!(!guard.handles(&GuardAction::ShellCommand("ls")));
    }

    #[tokio::test]
    async fn wasm_guard_handles_all_when_empty() {
        let wasm = compile_wat(allow_guard_wat());
        let guard = WasmGuard::new(
            "test.all".to_string(),
            Arc::new(wasm),
            vec![],
            default_capabilities(),
            default_resources(),
            serde_json::json!({}),
        );

        assert!(guard.handles(&GuardAction::FileAccess("/tmp/x")));
        assert!(guard.handles(&GuardAction::ShellCommand("ls")));
        assert!(guard.handles(&GuardAction::NetworkEgress("example.com", 443)));
    }

    #[tokio::test]
    async fn wasm_guard_skip_when_not_handled_returns_allow() {
        // When the WASM module's clawdstrike_guard_handles returns 0,
        // the runtime returns an allow result and skips check().
        let wasm = compile_wat(skip_guard_wat());
        let guard = WasmGuard::new(
            "test.skip".to_string(),
            Arc::new(wasm),
            vec![],
            default_capabilities(),
            default_resources(),
            serde_json::json!({}),
        );

        let ctx = GuardContext::new();
        let result = guard
            .check(&GuardAction::FileAccess("/tmp/safe"), &ctx)
            .await;
        // The runtime returns allow when handles() says no
        assert!(result.allowed);
    }

    #[tokio::test]
    async fn wasm_guard_corrupted_bytes_fail_closed() {
        let guard = WasmGuard::new(
            "test.corrupt".to_string(),
            Arc::new(vec![0xFF, 0xFF, 0xFF]),
            vec![],
            default_capabilities(),
            default_resources(),
            serde_json::json!({}),
        );

        let ctx = GuardContext::new();
        let result = guard
            .check(&GuardAction::FileAccess("/tmp/safe"), &ctx)
            .await;
        assert!(!result.allowed, "corrupted WASM must fail-closed");
        assert!(result.message.contains("WASM guard runtime error"));
    }

    #[test]
    fn wasm_guard_factory_id() {
        let wasm = compile_wat(allow_guard_wat());
        let factory = WasmGuardFactory::new(
            "acme.guard".to_string(),
            wasm,
            vec![],
            default_capabilities(),
            default_resources(),
        );
        assert_eq!(factory.id(), "acme.guard");
    }

    #[tokio::test]
    async fn wasm_guard_factory_build_creates_working_guard() {
        let wasm = compile_wat(deny_guard_wat());
        let factory = WasmGuardFactory::new(
            "acme.deny".to_string(),
            wasm,
            vec![],
            default_capabilities(),
            default_resources(),
        );

        let guard = factory.build(serde_json::json!({})).expect("build");
        assert_eq!(guard.name(), "acme.deny");

        let ctx = GuardContext::new();
        let result = guard
            .check(&GuardAction::FileAccess("/tmp/test"), &ctx)
            .await;
        assert!(!result.allowed);
    }

    #[test]
    fn wasm_guard_factory_build_corrupted_still_creates_guard() {
        // Factory construction succeeds even with bad bytes;
        // the error surfaces at check() time (fail-closed).
        let factory = WasmGuardFactory::new(
            "acme.bad".to_string(),
            vec![0x00],
            vec![],
            default_capabilities(),
            default_resources(),
        );

        let guard = factory.build(serde_json::json!({})).expect("build");
        assert_eq!(guard.name(), "acme.bad");
    }
}