symbi-runtime 1.12.0

Agent Runtime System for the Symbi platform
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
//! Docker container sandbox runner
//!
//! Executes code inside Docker containers for isolated agent execution.
//! Uses the `docker` CLI rather than a Rust Docker client library to
//! minimize dependencies and match the pattern of the native runner.

use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::process::Stdio;
use tokio::io::AsyncReadExt;
use tokio::process::Command;
use tokio::time::{timeout, Duration};

use super::{ExecutionResult, SandboxRunner};

/// Default maximum output size in bytes (10 MB)
const DEFAULT_MAX_OUTPUT_BYTES: usize = 10 * 1024 * 1024;

/// Configuration for Docker sandbox execution
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DockerConfig {
    /// Docker image to use (e.g. "python:3.12-slim", "node:20-alpine")
    pub image: String,
    /// Shell executable inside the container (default: "sh")
    pub shell: String,
    /// Maximum memory for the container (e.g. "512m", "2g")
    pub memory_limit: Option<String>,
    /// Maximum CPU quota (e.g. "1.0" = 1 CPU, "0.5" = half CPU)
    pub cpu_limit: Option<f64>,
    /// Maximum execution time before killing the container
    pub max_execution_time: Duration,
    /// Network mode: "none" (isolated), "bridge" (default Docker), "host"
    pub network_mode: String,
    /// Extra volumes to mount (host_path:container_path:mode)
    pub volumes: Vec<String>,
    /// Working directory inside the container
    pub working_dir: String,
    /// Whether to remove the container after execution
    pub auto_remove: bool,
    /// Docker binary path (default: "docker")
    pub docker_binary: String,
    /// Maximum output bytes per stream before truncation
    pub max_output_bytes: usize,
    /// Extra docker run flags
    pub extra_flags: Vec<String>,
}

impl Default for DockerConfig {
    fn default() -> Self {
        Self {
            image: "python:3.12-slim".to_string(),
            shell: "sh".to_string(),
            memory_limit: Some("512m".to_string()),
            cpu_limit: Some(1.0),
            max_execution_time: Duration::from_secs(300),
            network_mode: "none".to_string(),
            volumes: Vec::new(),
            working_dir: "/workspace".to_string(),
            auto_remove: true,
            docker_binary: "docker".to_string(),
            max_output_bytes: DEFAULT_MAX_OUTPUT_BYTES,
            extra_flags: Vec::new(),
        }
    }
}

impl DockerConfig {
    /// Create a config for a specific image with sensible defaults
    pub fn for_image(image: &str) -> Self {
        Self {
            image: image.to_string(),
            ..Default::default()
        }
    }

    /// Create a config with network access enabled
    pub fn with_network(mut self) -> Self {
        self.network_mode = "bridge".to_string();
        self
    }

    /// Set memory limit
    pub fn with_memory(mut self, limit: &str) -> Self {
        self.memory_limit = Some(limit.to_string());
        self
    }

    /// Set CPU limit
    pub fn with_cpu(mut self, limit: f64) -> Self {
        self.cpu_limit = Some(limit);
        self
    }

    /// Add a volume mount.
    ///
    /// Validates the host-side path against an obvious-danger blocklist
    /// (docker socket, host root filesystem, kernel interfaces, path
    /// traversal). Returns `Err` rather than silently accepting a mount
    /// that would punch a hole through the sandbox.
    pub fn with_volume(mut self, mount: &str) -> Result<Self, anyhow::Error> {
        validate_volume_mount(mount)?;
        self.volumes.push(mount.to_string());
        Ok(self)
    }

    /// Validate the configuration
    pub fn validate(&self) -> Result<(), anyhow::Error> {
        if self.image.is_empty() {
            anyhow::bail!("Docker image must not be empty");
        }
        // Validate every volume mount in case it was pushed directly into
        // `volumes` (e.g. by deserializing a config file rather than going
        // through `with_volume`).
        for vol in &self.volumes {
            validate_volume_mount(vol)?;
        }
        // Block host network in production. Uses the strict SYMBIONT_ENV
        // parser so aliases like `prod` don't slip through.
        if self.network_mode == "host" {
            let is_prod = crate::env::is_production()
                .map_err(|e| anyhow::anyhow!("SYMBIONT_ENV parse failed: {e}"))?;
            if is_prod {
                anyhow::bail!("SECURITY: host network mode is disabled in production");
            }
            tracing::warn!("SECURITY: Docker host network mode provides no network isolation");
        }
        Ok(())
    }
}

/// Host-side paths that must never be mounted into a sandbox container.
///
/// The list is intentionally conservative: anything rooted in these
/// directories would let a container read host secrets, escape via the
/// container runtime's control socket, or edit kernel state.
const DANGEROUS_HOST_PATHS: &[&str] = &[
    "/var/run/docker.sock",
    "/run/docker.sock",
    "/var/run/containerd",
    "/var/run/crio",
    "/proc",
    "/sys",
    "/boot",
    "/etc",
    "/root",
    "/var/lib/docker",
    "/var/lib/kubelet",
    "/var/lib/rancher",
];

/// Validate a docker `-v host:container[:mode]` string.
fn validate_volume_mount(mount: &str) -> Result<(), anyhow::Error> {
    let mut parts = mount.splitn(3, ':');
    let host = parts
        .next()
        .ok_or_else(|| anyhow::anyhow!("empty volume mount"))?;
    let _container = parts
        .next()
        .ok_or_else(|| anyhow::anyhow!("volume mount {:?} missing container path", mount))?;

    if host.is_empty() {
        anyhow::bail!("volume mount {:?} has empty host path", mount);
    }

    // Named volume references (no slash, not absolute) are left to docker;
    // we only police host-path mounts.
    if !host.starts_with('/') {
        return Ok(());
    }

    // Normalize lazily by rejecting `..` segments; canonicalize() would
    // require the path to exist, which isn't guaranteed at config time.
    if host.split('/').any(|seg| seg == "..") {
        anyhow::bail!(
            "volume mount {:?} contains '..' path segments; refusing",
            mount
        );
    }

    for dangerous in DANGEROUS_HOST_PATHS {
        if host == *dangerous || host.starts_with(&format!("{}/", dangerous)) {
            anyhow::bail!(
                "volume mount {:?} targets dangerous host path {:?}; refusing",
                mount,
                dangerous
            );
        }
    }

    if host == "/" {
        anyhow::bail!("volume mount {:?} targets host root filesystem", mount);
    }

    Ok(())
}

/// Docker sandbox runner
pub struct DockerRunner {
    config: DockerConfig,
}

impl DockerRunner {
    /// Create a new Docker runner with the given configuration
    pub fn new(config: DockerConfig) -> Result<Self, anyhow::Error> {
        config.validate()?;

        // Verify docker is available
        let check = std::process::Command::new(&config.docker_binary)
            .arg("version")
            .stdout(Stdio::null())
            .stderr(Stdio::null())
            .status();

        match check {
            Ok(status) if status.success() => {}
            _ => {
                anyhow::bail!(
                    "Docker is not available at '{}'. Install Docker or set docker_binary path.",
                    config.docker_binary
                );
            }
        }

        tracing::info!(
            "Docker sandbox initialized: image={}, network={}, memory={:?}, cpu={:?}",
            config.image,
            config.network_mode,
            config.memory_limit,
            config.cpu_limit
        );

        Ok(Self { config })
    }

    /// Build the `docker run` command with all configuration applied
    fn build_command(&self, code: &str, env: &HashMap<String, String>) -> Command {
        let mut cmd = Command::new(&self.config.docker_binary);
        cmd.arg("run");

        // Auto-remove container after execution
        if self.config.auto_remove {
            cmd.arg("--rm");
        }

        // Resource limits
        if let Some(ref mem) = self.config.memory_limit {
            cmd.arg("--memory").arg(mem);
            // Also set memory-swap equal to memory to disable swap
            cmd.arg("--memory-swap").arg(mem);
        }
        if let Some(cpu) = self.config.cpu_limit {
            cmd.arg("--cpus").arg(cpu.to_string());
        }

        // Network isolation
        cmd.arg("--network").arg(&self.config.network_mode);

        // Working directory
        cmd.arg("--workdir").arg(&self.config.working_dir);

        // Read-only root filesystem for security
        cmd.arg("--read-only");
        // But allow /tmp for scratch space
        cmd.arg("--tmpfs").arg("/tmp:rw,noexec,nosuid,size=100m");

        // No new privileges
        cmd.arg("--security-opt").arg("no-new-privileges");

        // Drop all capabilities, add back only what's needed
        cmd.arg("--cap-drop").arg("ALL");

        // Environment variables
        for (key, value) in env {
            cmd.arg("-e").arg(format!("{}={}", key, value));
        }

        // Volume mounts
        for vol in &self.config.volumes {
            cmd.arg("-v").arg(vol);
        }

        // Extra flags
        for flag in &self.config.extra_flags {
            cmd.arg(flag);
        }

        // Image and command
        cmd.arg(&self.config.image);
        cmd.arg(&self.config.shell);
        cmd.arg("-c");
        cmd.arg(code);

        // Stdio
        cmd.stdin(Stdio::null());
        cmd.stdout(Stdio::piped());
        cmd.stderr(Stdio::piped());

        cmd
    }

    /// Read output with size limit (same pattern as NativeRunner)
    async fn read_limited_output<R: AsyncReadExt + Unpin>(
        reader: &mut R,
        max_bytes: usize,
    ) -> (String, bool) {
        let mut buf = vec![0u8; max_bytes + 1];
        let mut total = 0usize;

        loop {
            match reader.read(&mut buf[total..]).await {
                Ok(0) => break,
                Ok(n) => {
                    total += n;
                    if total > max_bytes {
                        total = max_bytes;
                        break;
                    }
                }
                Err(_) => break,
            }
        }

        let truncated = total == max_bytes;
        let output = String::from_utf8_lossy(&buf[..total]).to_string();

        if truncated {
            let with_marker = format!("{}\n... [output truncated at {} bytes]", output, max_bytes);
            (with_marker, true)
        } else {
            (output, false)
        }
    }
}

#[async_trait]
impl SandboxRunner for DockerRunner {
    async fn execute(
        &self,
        code: &str,
        env: HashMap<String, String>,
    ) -> Result<ExecutionResult, anyhow::Error> {
        tracing::info!(
            "Docker sandbox executing: image={}, code_len={}, env_count={}",
            self.config.image,
            code.len(),
            env.len()
        );

        let mut command = self.build_command(code, &env);
        let start = std::time::Instant::now();
        let max_output = self.config.max_output_bytes;

        let mut child = command.spawn().map_err(|e| {
            anyhow::anyhow!("Failed to spawn docker process: {}. Is Docker running?", e)
        })?;

        let mut child_stdout = child.stdout.take();
        let mut child_stderr = child.stderr.take();

        let output_result = timeout(self.config.max_execution_time, async {
            let stdout_future = async {
                match child_stdout.as_mut() {
                    Some(stdout) => Self::read_limited_output(stdout, max_output).await,
                    None => (String::new(), false),
                }
            };

            let stderr_future = async {
                match child_stderr.as_mut() {
                    Some(stderr) => Self::read_limited_output(stderr, max_output).await,
                    None => (String::new(), false),
                }
            };

            let ((stdout, stdout_truncated), (stderr, stderr_truncated)) =
                tokio::join!(stdout_future, stderr_future);

            let status = child.wait().await;

            (stdout, stdout_truncated, stderr, stderr_truncated, status)
        })
        .await;

        let execution_time = start.elapsed();

        match output_result {
            Ok((stdout, stdout_truncated, stderr, stderr_truncated, Ok(status))) => {
                let exit_code = status.code().unwrap_or(-1);
                let success = status.success();

                tracing::info!(
                    "Docker execution completed: exit_code={}, success={}, duration={:?}",
                    exit_code,
                    success,
                    execution_time
                );

                Ok(ExecutionResult {
                    stdout,
                    stderr,
                    exit_code,
                    success,
                    execution_time_ms: execution_time.as_millis() as u64,
                    stdout_truncated,
                    stderr_truncated,
                })
            }
            Ok((_, _, _, _, Err(e))) => {
                tracing::error!("Docker execution failed: {}", e);
                Err(anyhow::anyhow!("Docker execution failed: {}", e))
            }
            Err(_) => {
                // Timeout — kill the container
                let _ = child.kill().await;
                tracing::error!(
                    "Docker execution timed out after {:?}",
                    self.config.max_execution_time
                );
                Err(anyhow::anyhow!(
                    "Docker execution timed out after {:?}",
                    self.config.max_execution_time
                ))
            }
        }
    }
}

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

    #[test]
    fn test_default_config() {
        let config = DockerConfig::default();
        assert_eq!(config.image, "python:3.12-slim");
        assert_eq!(config.network_mode, "none");
        assert_eq!(config.memory_limit, Some("512m".to_string()));
        assert!(config.auto_remove);
    }

    #[test]
    fn test_config_builder() {
        let config = DockerConfig::for_image("node:20-alpine")
            .with_network()
            .with_memory("1g")
            .with_cpu(2.0)
            .with_volume("/data:/data:ro")
            .expect("safe volume");

        assert_eq!(config.image, "node:20-alpine");
        assert_eq!(config.network_mode, "bridge");
        assert_eq!(config.memory_limit, Some("1g".to_string()));
        assert_eq!(config.cpu_limit, Some(2.0));
        assert_eq!(config.volumes, vec!["/data:/data:ro"]);
    }

    #[test]
    fn test_with_volume_refuses_docker_socket() {
        let cfg = DockerConfig::for_image("x").with_volume("/var/run/docker.sock:/sock");
        assert!(cfg.is_err());
    }

    #[test]
    fn test_with_volume_refuses_host_etc() {
        let cfg = DockerConfig::for_image("x").with_volume("/etc:/data:ro");
        assert!(cfg.is_err());
    }

    #[test]
    fn test_with_volume_refuses_traversal() {
        let cfg = DockerConfig::for_image("x").with_volume("/home/../etc:/mnt");
        assert!(cfg.is_err());
    }

    #[test]
    fn test_with_volume_refuses_proc() {
        let cfg = DockerConfig::for_image("x").with_volume("/proc:/proc");
        assert!(cfg.is_err());
    }

    #[test]
    fn test_with_volume_allows_named_volume() {
        let cfg = DockerConfig::for_image("x").with_volume("myvol:/data");
        assert!(cfg.is_ok());
    }

    #[test]
    fn test_with_volume_refuses_empty_container() {
        let cfg = DockerConfig::for_image("x").with_volume("/data");
        assert!(cfg.is_err());
    }

    #[test]
    fn test_validate_refuses_injected_dangerous_volume() {
        // Volumes pushed directly around the builder must still be caught
        // by validate().
        let mut config = DockerConfig::for_image("x");
        config
            .volumes
            .push("/var/run/docker.sock:/sock".to_string());
        assert!(config.validate().is_err());
    }

    #[test]
    fn test_empty_image_rejected() {
        let config = DockerConfig {
            image: String::new(),
            ..Default::default()
        };
        assert!(config.validate().is_err());
    }

    #[test]
    fn test_command_building() {
        let config = DockerConfig::default();
        let runner = DockerRunner { config };

        let mut env = HashMap::new();
        env.insert("FOO".to_string(), "bar".to_string());

        let cmd = runner.build_command("echo hello", &env);
        // Verify the command is constructed (we can't easily inspect it,
        // but it shouldn't panic)
        let _ = cmd;
    }

    // Integration tests that require Docker are below.
    // They're ignored by default since CI may not have Docker.

    #[tokio::test]
    #[ignore] // Requires Docker
    async fn test_docker_runner_creation() {
        let config = DockerConfig::for_image("alpine:latest");
        let runner = DockerRunner::new(config);
        // Only passes if Docker is installed
        if runner.is_err() {
            eprintln!("Skipping: Docker not available");
            return;
        }
        assert!(runner.is_ok());
    }

    #[tokio::test]
    #[ignore] // Requires Docker
    async fn test_docker_execution() {
        let config = DockerConfig::for_image("alpine:latest");
        let runner = match DockerRunner::new(config) {
            Ok(r) => r,
            Err(_) => return, // Docker not available
        };

        let result = runner
            .execute("echo 'Hello from Docker!'", HashMap::new())
            .await;

        if let Ok(output) = result {
            assert!(output.success);
            assert!(output.stdout.contains("Hello from Docker!"));
        }
    }

    #[tokio::test]
    #[ignore] // Requires Docker
    async fn test_docker_env_vars() {
        let config = DockerConfig::for_image("alpine:latest");
        let runner = match DockerRunner::new(config) {
            Ok(r) => r,
            Err(_) => return,
        };

        let mut env = HashMap::new();
        env.insert("TEST_VAR".to_string(), "docker_test".to_string());

        let result = runner.execute("echo $TEST_VAR", env).await;

        if let Ok(output) = result {
            assert!(output.success);
            assert!(output.stdout.contains("docker_test"));
        }
    }

    #[tokio::test]
    #[ignore] // Requires Docker
    async fn test_docker_network_isolation() {
        let config = DockerConfig {
            image: "alpine:latest".to_string(),
            network_mode: "none".to_string(),
            ..Default::default()
        };
        let runner = match DockerRunner::new(config) {
            Ok(r) => r,
            Err(_) => return,
        };

        // This should fail since network is disabled
        let result = runner
            .execute("wget -q -O- http://example.com", HashMap::new())
            .await;

        if let Ok(output) = result {
            assert!(!output.success);
        }
    }

    #[tokio::test]
    #[ignore] // Requires Docker
    async fn test_docker_timeout() {
        let config = DockerConfig {
            image: "alpine:latest".to_string(),
            max_execution_time: Duration::from_secs(2),
            ..Default::default()
        };
        let runner = match DockerRunner::new(config) {
            Ok(r) => r,
            Err(_) => return,
        };

        let result = runner.execute("sleep 30", HashMap::new()).await;
        assert!(result.is_err());
    }
}