agentd 0.1.2

Agent daemon for secure capability execution with pluggable isolation backends
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
//! Security Error Handling Tests for Smith Executor
//!
//! Tests that verify proper error handling, logging, and recovery
//! when security violations or failures occur.

use anyhow::Result;
use serde_json::json;
use std::{collections::HashMap, time::Duration};
use tempfile::TempDir;

use agentd::{
    capabilities::register_builtin_capabilities,
    capability::{ExecCtx, ExecutionScope, SandboxConfig},
    runners::{create_exec_context, MemoryOutputSink, RunnerRegistry, Scope},
    ExecutionLimits,
};
use smith_protocol::ExecutionLimits as CapabilityExecutionLimits;
use smith_protocol::{Capability as ProtoCapability, ExecutionStatus, Intent, SandboxMode};

/// Test error handling for validation failures
#[tokio::test]
async fn test_validation_error_handling() -> Result<()> {
    let registry = register_builtin_capabilities();

    let capability = registry
        .get("shell.exec.v1")
        .expect("shell.exec.v1 should be registered");

    // Test various validation errors and ensure proper error types
    let validation_test_cases = vec![
        (json!({ "command": "" }), "empty command"),
        (json!({}), "missing command"),
        (
            json!({ "command": "echo", "timeout_ms": 0 }),
            "invalid timeout",
        ),
        (
            json!({ "command": "echo", "timeout_ms": 999_999 }),
            "timeout too large",
        ),
        (
            json!({ "command": "echo", "forbidden_param": "value" }),
            "unsupported param",
        ),
        (json!({ "command": 123 }), "invalid command type"),
    ];

    for (params, description) in validation_test_cases {
        let intent = Intent::new(
            ProtoCapability::ShellExec,
            "validation-error-test".to_string(),
            params.clone(),
            30000,
            "test-user".to_string(),
        );

        let validation_result = capability.validate(&intent);

        match validation_result {
            Err(e) => {
                // Verify error contains meaningful information
                let error_msg = format!("{:?}", e);
                assert!(
                    !error_msg.is_empty(),
                    "Error message should not be empty for {}",
                    description
                );
                assert!(
                    !error_msg.contains("Unknown error"),
                    "Error should be descriptive for {}",
                    description
                );

                // Verify error is not leaking sensitive information
                assert!(
                    !error_msg.contains("super_secret_token"),
                    "Error should not leak secret values"
                );
                assert!(
                    !error_msg.contains("password="),
                    "Error should not leak credential-like material"
                );

                println!("Validation error for {}: {}", description, error_msg);
            }
            Ok(_) => {
                panic!(
                    "Validation should have failed for {}: {:?}",
                    description, params
                );
            }
        }
    }

    Ok(())
}

/// Test error handling for execution failures under security constraints
#[tokio::test]
async fn test_execution_error_handling() -> Result<()> {
    let temp_dir = TempDir::new()?;
    let registry = RunnerRegistry::new(None);

    let runner = registry
        .get_runner("fs.read")
        .expect("fs.read runner should be registered");

    // Test execution errors with proper error propagation
    let execution_test_cases = vec![
        ("nonexistent.txt", "file not found"),
        ("../outside_scope.txt", "path outside scope"),
        ("", "empty filename"),
    ];

    for (filename, description) in execution_test_cases {
        let exec_context = create_exec_context(
            temp_dir.path(),
            ExecutionLimits {
                cpu_ms_per_100ms: 80,
                mem_bytes: 64 * 1024 * 1024,
                io_bytes: 10 * 1024 * 1024,
                pids_max: 5,
                timeout_ms: 5000,
            },
            Scope {
                paths: vec![temp_dir.path().to_string_lossy().to_string()],
                urls: vec![],
            },
            format!("error-test-{}", description.replace(' ', "-")),
        );

        let mut output_sink = MemoryOutputSink::new();
        let params = json!({ "path": filename, "len": 1024 });

        let result = runner
            .execute(&exec_context, params, &mut output_sink)
            .await;

        match result {
            Ok(exec_result) => {
                // Should complete but with error status for invalid operations
                if filename == "nonexistent.txt" {
                    // File not found is acceptable - depends on runner implementation
                    assert!(
                        exec_result.status == ExecutionStatus::Ok
                            || exec_result.status == ExecutionStatus::Error,
                        "Should handle non-existent file gracefully: {}",
                        description
                    );
                } else {
                    // Other errors should be handled properly
                    assert!(
                        exec_result.status == ExecutionStatus::Error,
                        "Should return error status for: {}",
                        description
                    );
                }
            }
            Err(e) => {
                // Execution errors are also acceptable - verify they're informative
                let error_msg = format!("{:?}", e);
                assert!(
                    !error_msg.is_empty(),
                    "Error message should not be empty for {}",
                    description
                );
                println!("Execution error for {}: {}", description, error_msg);
            }
        }

        // Verify error logging occurred
        if !output_sink.logs.is_empty() {
            let log_content = output_sink.logs.join("\n");
            println!("Logs for {}: {}", description, log_content);
        }
    }

    Ok(())
}

/// Test timeout handling under security constraints
#[tokio::test]
async fn test_timeout_error_handling() -> Result<()> {
    let temp_dir = TempDir::new()?;
    let registry = RunnerRegistry::new(None);

    let runner = registry
        .get_runner("fs.read")
        .expect("fs.read runner should be registered");

    // Create a legitimate file
    let test_file = temp_dir.path().join("timeout_test.txt");
    tokio::fs::write(&test_file, "timeout test content").await?;

    // Set very short timeout
    let exec_context = create_exec_context(
        temp_dir.path(),
        ExecutionLimits {
            cpu_ms_per_100ms: 80,
            mem_bytes: 64 * 1024 * 1024,
            io_bytes: 10 * 1024 * 1024,
            pids_max: 5,
            timeout_ms: 1, // 1ms timeout - should be too short
        },
        Scope {
            paths: vec![test_file.to_string_lossy().to_string()],
            urls: vec![],
        },
        "timeout-test".to_string(),
    );

    let mut output_sink = MemoryOutputSink::new();
    let params = json!({
        "path": test_file.file_name().unwrap().to_string_lossy(),
        "len": 1024
    });

    let start_time = std::time::Instant::now();
    let result = runner
        .execute(&exec_context, params, &mut output_sink)
        .await;
    let elapsed = start_time.elapsed();

    // Should complete quickly due to timeout, either successfully or with timeout error
    assert!(
        elapsed < Duration::from_secs(2),
        "Should not take longer than 2 seconds"
    );

    match result {
        Ok(exec_result) => {
            // May complete successfully if very fast, or with timeout error
            println!("Timeout test result: {:?}", exec_result.status);
        }
        Err(e) => {
            // Timeout error is acceptable
            let error_msg = e.to_string();
            println!("Timeout error (expected): {}", error_msg);
        }
    }

    Ok(())
}

/// Test resource limit error handling
#[tokio::test]
async fn test_resource_limit_error_handling() -> Result<()> {
    let temp_dir = TempDir::new()?;
    let registry = RunnerRegistry::new(None);

    let runner = registry
        .get_runner("fs.read")
        .expect("fs.read runner should be registered");

    // Create a large file that exceeds I/O limits
    let large_file = temp_dir.path().join("large.txt");
    let content = "A".repeat(2 * 1024 * 1024); // 2MB content
    tokio::fs::write(&large_file, &content).await?;

    // Set very restrictive I/O limits
    let exec_context = create_exec_context(
        temp_dir.path(),
        ExecutionLimits {
            cpu_ms_per_100ms: 80,
            mem_bytes: 64 * 1024 * 1024,
            io_bytes: 512 * 1024, // 512KB I/O limit - less than file size
            pids_max: 5,
            timeout_ms: 10000,
        },
        Scope {
            paths: vec![large_file.to_string_lossy().to_string()],
            urls: vec![],
        },
        "resource-limit-test".to_string(),
    );

    let mut output_sink = MemoryOutputSink::new();
    let params = json!({
        "path": large_file.file_name().unwrap().to_string_lossy(),
        "len": 2 * 1024 * 1024 // Request full file
    });

    let result = runner
        .execute(&exec_context, params, &mut output_sink)
        .await?;

    // Should handle resource limits gracefully
    assert!(result.status == ExecutionStatus::Ok || result.status == ExecutionStatus::Error);

    // Should not exceed I/O limits
    assert!(
        result.stdout_bytes <= 512 * 1024,
        "Should respect I/O limits, got {} bytes",
        result.stdout_bytes
    );

    // Check for appropriate logging
    if !output_sink.logs.is_empty() {
        let log_content = output_sink.logs.join("\n");
        println!("Resource limit logs: {}", log_content);
    }

    Ok(())
}

/// Test http.fetch runner is removed from agentd
#[tokio::test]
async fn test_network_error_handling() -> Result<()> {
    let registry = RunnerRegistry::new(None);

    assert!(
        registry.get_runner("http.fetch").is_none(),
        "http.fetch runner should not be registered in agentd"
    );
    assert!(
        registry.get_runner("http.fetch.v1").is_none(),
        "http.fetch.v1 runner should not be registered in agentd"
    );

    Ok(())
}

/// Test concurrent error handling with capability validation
#[tokio::test]
async fn test_concurrent_error_handling() -> Result<()> {
    let temp_dir = TempDir::new()?;
    let registry = std::sync::Arc::new(register_builtin_capabilities());

    // Create some test files
    let valid_file = temp_dir.path().join("valid.txt");
    tokio::fs::write(&valid_file, "valid content").await?;

    // Test concurrent validation with various error conditions
    let mut tasks = Vec::new();

    for i in 0..10 {
        let registry_ref = registry.clone();
        let _temp_dir_path = temp_dir.path().to_path_buf();

        let task = tokio::spawn(async move {
            let capability = registry_ref
                .get("shell.exec.v1")
                .expect("shell.exec.v1 should be registered");

            let params = if i % 2 == 0 {
                json!({ "command": "echo", "args": ["valid"] })
            } else {
                json!({ "command": "echo", "timeout_ms": 0 })
            };

            let intent = Intent::new(
                ProtoCapability::ShellExec,
                format!("concurrent-error-{}", i),
                params,
                30000,
                "test-user".to_string(),
            );

            let validation_result = capability.validate(&intent);
            (i, validation_result)
        });

        tasks.push(task);
    }

    // Wait for all tasks and verify error handling
    let results = futures::future::join_all(tasks).await;

    let mut success_count = 0;
    let mut error_count = 0;
    let mut panic_count = 0;

    for result in results {
        match result {
            Ok((task_id, validation_result)) => match validation_result {
                Ok(_) => {
                    success_count += 1;
                    println!("Task {} validation succeeded", task_id);
                }
                Err(e) => {
                    error_count += 1;
                    println!("Task {} validation failed safely: {:?}", task_id, e);
                }
            },
            Err(e) => {
                panic_count += 1;
                println!("Task panicked: {}", e);
            }
        }
    }

    // Verify system remained stable
    assert_eq!(
        panic_count, 0,
        "No tasks should panic during error handling"
    );
    assert!(
        success_count > 0,
        "At least some validations should succeed"
    );

    println!(
        "Concurrent error handling results - Success: {}, Errors: {}, Panics: {}",
        success_count, error_count, panic_count
    );

    Ok(())
}

/// Test error recovery and cleanup
#[tokio::test]
async fn test_error_recovery_and_cleanup() -> Result<()> {
    let temp_dir = TempDir::new()?;
    let registry = register_builtin_capabilities();

    let capability = registry
        .get("shell.exec.v1")
        .expect("shell.exec.v1 should be registered");

    // Test recovery after various error conditions
    let error_recovery_tests = vec![
        // Invalid intent that should be rejected
        Intent::new(
            ProtoCapability::ShellExec,
            "invalid-1".to_string(),
            json!({ "command": "" }),
            30000,
            "attacker".to_string(),
        ),
        // Another invalid intent
        Intent::new(
            ProtoCapability::ShellExec,
            "invalid-2".to_string(),
            json!({ "command": "echo", "timeout_ms": 0 }),
            30000,
            "attacker".to_string(),
        ),
    ];

    // Process invalid intents
    for invalid_intent in error_recovery_tests {
        let validation_result = capability.validate(&invalid_intent);
        assert!(
            validation_result.is_err(),
            "Invalid intent should be rejected"
        );
    }

    // After errors, system should still work for valid requests
    let valid_intent = Intent::new(
        ProtoCapability::ShellExec,
        "valid-after-errors".to_string(),
        json!({ "command": "echo", "args": ["Recovery test content"] }),
        30000,
        "legitimate-user".to_string(),
    );

    // Validation should succeed
    let validation_result = capability.validate(&valid_intent);
    assert!(
        validation_result.is_ok(),
        "Valid intent should succeed after error recovery"
    );

    // Execution should also work
    let exec_context = ExecCtx {
        workdir: temp_dir.path().to_path_buf(),
        limits: CapabilityExecutionLimits::default(),
        scope: ExecutionScope {
            paths: vec![],
            urls: vec![],
            env_vars: vec![],
            custom: HashMap::new(),
        },
        trace_id: "recovery-test".to_string(),
        sandbox: SandboxConfig {
            mode: SandboxMode::Full,
            landlock_enabled: true,
            seccomp_enabled: true,
            cgroups_enabled: true,
            namespaces_enabled: true,
        },
    };

    let execution_result = capability.execute(valid_intent, exec_context).await;
    assert!(
        execution_result.is_ok(),
        "Execution should succeed after error recovery"
    );

    let result = execution_result.unwrap();
    assert_eq!(
        result.status,
        ExecutionStatus::Ok,
        "Should execute successfully"
    );

    Ok(())
}

/// Test error message sanitization
#[test]
fn test_error_message_sanitization() {
    let registry = register_builtin_capabilities();

    let capability = registry
        .get("shell.exec.v1")
        .expect("shell.exec.v1 should be registered");

    // Test that error messages don't leak sensitive information
    let sensitive_test_cases = vec![
        json!({ "command": "x".repeat(5000), "token": "secret_key=abc123" }),
        json!({ "command": "echo", "timeout_ms": 0, "password": "xyz789" }),
        json!({ "command": 123, "api_key": "top_secret" }),
    ];

    for params in sensitive_test_cases {
        let intent = Intent::new(
            ProtoCapability::ShellExec,
            "sanitization-test".to_string(),
            params,
            30000,
            "test-user".to_string(),
        );

        let validation_result = capability.validate(&intent);

        if let Err(e) = validation_result {
            let error_msg = format!("{:?}", e).to_lowercase();

            // Verify error doesn't contain sensitive patterns
            assert!(
                !error_msg.contains("passwd"),
                "Error should not contain 'passwd'"
            );
            assert!(!error_msg.contains("ssh"), "Error should not contain 'ssh'");
            assert!(
                !error_msg.contains("secret_key=abc123"),
                "Error should not leak secret token values"
            );
            assert!(
                !error_msg.contains("xyz789"),
                "Error should not leak password values"
            );

            // Error should be generic but informative
            assert!(
                error_msg.contains("invalid")
                    || error_msg.contains("forbidden")
                    || error_msg.contains("not allowed")
                    || error_msg.contains("violation"),
                "Error should be informative but generic. Actual error: '{}'",
                error_msg
            );
        }
    }
}

/// Test logging safety under error conditions
#[tokio::test]
async fn test_logging_safety_under_errors() -> Result<()> {
    let temp_dir = TempDir::new()?;
    let registry = RunnerRegistry::new(None);

    let runner = registry
        .get_runner("fs.read")
        .expect("fs.read runner should be registered");

    // Create exec context that will cause errors
    let exec_context = create_exec_context(
        temp_dir.path(),
        ExecutionLimits {
            cpu_ms_per_100ms: 80,
            mem_bytes: 64 * 1024 * 1024,
            io_bytes: 10 * 1024 * 1024,
            pids_max: 5,
            timeout_ms: 5000,
        },
        Scope {
            paths: vec![], // Empty scope - will cause access errors
            urls: vec![],
        },
        "logging-safety-test".to_string(),
    );

    let mut output_sink = MemoryOutputSink::new();
    let params = json!({
        "path": "nonexistent.txt",
        "len": 1024
    });

    let _result = runner
        .execute(&exec_context, params, &mut output_sink)
        .await;

    // Check that logs don't contain sensitive information
    for log_entry in &output_sink.logs {
        let log_lower = log_entry.to_lowercase();

        // Verify logs don't leak sensitive system information
        assert!(
            !log_lower.contains("/etc/passwd"),
            "Log should not contain sensitive paths"
        );
        assert!(
            !log_lower.contains("root:"),
            "Log should not contain sensitive user info"
        );
        assert!(
            !log_lower.contains("secret"),
            "Log should not contain secrets"
        );

        println!("Safe log entry: {}", log_entry);
    }

    // Check stdout/stderr for safety as well
    let stdout_content = String::from_utf8_lossy(&output_sink.stdout);
    let stderr_content = String::from_utf8_lossy(&output_sink.stderr);

    assert!(
        !stdout_content.contains("/etc/passwd"),
        "Stdout should be safe"
    );
    assert!(
        !stderr_content.contains("/etc/passwd"),
        "Stderr should be safe"
    );

    Ok(())
}