symbi-runtime 1.7.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
//! Native Process Sandbox Runner
//!
//! Executes code directly on the host system with resource limits enforced
//! via direct `rlimit` syscalls (no shell wrapping).
//!
//! **WARNING**: This provides minimal security isolation and should only be used
//! in trusted development environments. Gated behind the `native-sandbox` feature.

use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::path::PathBuf;
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;

/// Default file size limit in bytes (100 MB)
const DEFAULT_MAX_FSIZE_BYTES: u64 = 100 * 1024 * 1024;

/// Configuration for native (non-isolated) execution
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NativeConfig {
    /// Executable to run (e.g., "python3", "node", "bash")
    pub executable: String,
    /// Working directory for execution
    pub working_directory: PathBuf,
    /// Whether to enforce resource limits (Unix only)
    pub enforce_resource_limits: bool,
    /// Maximum memory in MB (if enforced)
    pub max_memory_mb: Option<u64>,
    /// Maximum CPU time in seconds (if enforced)
    pub max_cpu_seconds: Option<u64>,
    /// Maximum execution time (timeout)
    pub max_execution_time: Duration,
    /// Allowed executables (if empty, all are allowed)
    pub allowed_executables: Vec<String>,
    /// Maximum output bytes per stream before truncation (default: 10MB)
    pub max_output_bytes: usize,
}

impl Default for NativeConfig {
    fn default() -> Self {
        Self {
            executable: "bash".to_string(),
            working_directory: PathBuf::from("/tmp/symbiont-native"),
            enforce_resource_limits: true,
            max_memory_mb: Some(2048),
            max_cpu_seconds: Some(300),
            max_execution_time: Duration::from_secs(300),
            allowed_executables: vec![],
            max_output_bytes: DEFAULT_MAX_OUTPUT_BYTES,
        }
    }
}

/// Shell executable names that warrant a security warning
const SHELL_EXECUTABLES: &[&str] = &["bash", "sh", "zsh", "dash", "fish", "csh", "tcsh", "ksh"];

impl NativeConfig {
    /// Validate the configuration
    pub fn validate(&self) -> Result<(), anyhow::Error> {
        // Reject empty allowed_executables — force explicit configuration
        if self.allowed_executables.is_empty() {
            anyhow::bail!(
                "allowed_executables must not be empty — explicitly list the executables this runner may invoke"
            );
        }

        // Check if executable is allowed
        let exec_name = self
            .executable
            .split('/')
            .next_back()
            .unwrap_or(&self.executable);
        if !self
            .allowed_executables
            .iter()
            .any(|allowed| allowed == &self.executable || allowed == exec_name)
        {
            anyhow::bail!(
                "Executable '{}' not in allowed list: {:?}",
                self.executable,
                self.allowed_executables
            );
        }

        // Validate working directory
        if !self.working_directory.is_absolute() {
            anyhow::bail!(
                "Working directory must be absolute path: {}",
                self.working_directory.display()
            );
        }

        Ok(())
    }

    /// Log warnings for any shell executables in the allowed list
    pub fn warn_on_shell_executables(&self) {
        for allowed in &self.allowed_executables {
            let base_name = allowed.split('/').next_back().unwrap_or(allowed);
            if SHELL_EXECUTABLES.contains(&base_name) {
                tracing::warn!(
                    "SECURITY: shell executable '{}' is in allowed_executables — \
                     consider removing it unless explicitly required",
                    allowed
                );
            }
        }
    }
}

/// Native sandbox runner for direct host execution
#[derive(Debug)]
pub struct NativeRunner {
    config: NativeConfig,
}

impl NativeRunner {
    /// Create a new native runner with the given configuration
    ///
    /// # Security Warning
    ///
    /// Native execution provides **ZERO isolation** from the host system. Code
    /// executed with this runner has full access to:
    /// - The entire filesystem (subject to process permissions)
    /// - Network interfaces
    /// - System processes
    /// - Environment variables and secrets
    ///
    /// **USE ONLY IN TRUSTED DEVELOPMENT ENVIRONMENTS**
    ///
    /// This runner is gated behind the `native-sandbox` compile-time feature.
    /// It will refuse to run in production environments (`SYMBIONT_ENV=production`).
    pub fn new(config: NativeConfig) -> Result<Self, anyhow::Error> {
        // Hard-block in production — no runtime override
        if let Ok(env) = std::env::var("SYMBIONT_ENV") {
            if env.eq_ignore_ascii_case("production") {
                anyhow::bail!(
                    "SECURITY: Native execution is unconditionally disabled in production. \
                     Use a proper sandbox (Docker, gVisor, Firecracker, or E2B) instead."
                );
            }
        }

        // Always log warning when native execution is initialized
        tracing::warn!("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━");
        tracing::warn!("Native Sandbox: NO ISOLATION");
        tracing::warn!("Executable: {}", config.executable);
        tracing::warn!("Working dir: {}", config.working_directory.display());
        tracing::warn!("Code will run directly on host system");
        tracing::warn!("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━");

        // Validate configuration
        config.validate()?;

        // Warn about shell executables in the allow list
        config.warn_on_shell_executables();

        // Ensure working directory exists
        if !config.working_directory.exists() {
            tracing::info!(
                "Creating working directory: {}",
                config.working_directory.display()
            );
            std::fs::create_dir_all(&config.working_directory)?;
        }

        Ok(Self { config })
    }

    /// Create with default configuration
    pub fn with_defaults() -> Result<Self, anyhow::Error> {
        Self::new(NativeConfig::default())
    }

    /// Apply resource limits via direct rlimit syscalls in a pre_exec closure (Unix only).
    /// No shell wrapping — the command directly invokes the target executable.
    #[cfg(unix)]
    fn apply_resource_limits(&self, command: &mut Command) -> Result<(), anyhow::Error> {
        if !self.config.enforce_resource_limits {
            return Ok(());
        }

        let max_memory_mb = self.config.max_memory_mb;
        let max_cpu_seconds = self.config.max_cpu_seconds;

        // SAFETY: pre_exec runs between fork() and exec() in the child process.
        // We only call async-signal-safe functions (setrlimit, setpgid are async-signal-safe).
        unsafe {
            command.pre_exec(move || {
                // Put the child in its own process group so we can kill the
                // entire group on timeout (prevents leaked sub-processes).
                libc::setpgid(0, 0);

                // Set virtual memory limit (RLIMIT_AS)
                if let Some(mem_mb) = max_memory_mb {
                    let mem_bytes = mem_mb * 1024 * 1024;
                    rlimit::setrlimit(rlimit::Resource::AS, mem_bytes, mem_bytes).map_err(|e| {
                        std::io::Error::other(format!("Failed to set RLIMIT_AS: {}", e))
                    })?;
                }

                // Set CPU time limit (RLIMIT_CPU)
                if let Some(cpu_sec) = max_cpu_seconds {
                    rlimit::setrlimit(rlimit::Resource::CPU, cpu_sec, cpu_sec).map_err(|e| {
                        std::io::Error::other(format!("Failed to set RLIMIT_CPU: {}", e))
                    })?;
                }

                // Set file size limit (RLIMIT_FSIZE) — 100MB default
                rlimit::setrlimit(
                    rlimit::Resource::FSIZE,
                    DEFAULT_MAX_FSIZE_BYTES,
                    DEFAULT_MAX_FSIZE_BYTES,
                )
                .map_err(|e| std::io::Error::other(format!("Failed to set RLIMIT_FSIZE: {}", e)))?;

                Ok(())
            });
        }

        Ok(())
    }

    #[cfg(not(unix))]
    fn apply_resource_limits(&self, _command: &mut Command) -> Result<(), anyhow::Error> {
        if self.config.enforce_resource_limits {
            tracing::warn!(
                "Resource limits are not supported on this platform, ignoring enforce_resource_limits setting"
            );
        }
        Ok(())
    }

    /// Read output from a child stream with a size limit, returning the string and
    /// whether it was truncated.
    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 NativeRunner {
    async fn execute(
        &self,
        code: &str,
        env: HashMap<String, String>,
    ) -> Result<ExecutionResult, anyhow::Error> {
        tracing::warn!("EXECUTING CODE WITHOUT ISOLATION - Native execution mode is active");
        tracing::debug!(
            "Native execution: executable={}, working_dir={}",
            self.config.executable,
            self.config.working_directory.display()
        );

        let mut command = Command::new(&self.config.executable);

        // Determine how to pass code to the executable — direct argv, no shell wrapping
        match self
            .config
            .executable
            .split('/')
            .next_back()
            .unwrap_or(&self.config.executable)
        {
            "python" | "python3" => {
                command.arg("-c");
                command.arg(code);
            }
            "node" => {
                command.arg("-e");
                command.arg(code);
            }
            "bash" | "sh" => {
                command.arg("-c");
                command.arg(code);
            }
            _ => {
                // For unknown executables, try -c as default
                command.arg("-c");
                command.arg(code);
            }
        }

        // Set working directory, environment variables, and stdio BEFORE apply_resource_limits
        command.current_dir(&self.config.working_directory);
        command.envs(env);
        command.stdin(Stdio::null());
        command.stdout(Stdio::piped());
        command.stderr(Stdio::piped());

        // Apply resource limits via pre_exec rlimit syscalls — no shell wrapping
        self.apply_resource_limits(&mut command)?;

        let start = std::time::Instant::now();
        let max_output = self.config.max_output_bytes;

        // Spawn the child process
        let mut child = command.spawn().map_err(|e| {
            anyhow::anyhow!(
                "Failed to spawn process '{}': {}",
                self.config.executable,
                e
            )
        })?;

        // Take ownership of stdout/stderr handles
        let mut child_stdout = child.stdout.take();
        let mut child_stderr = child.stderr.take();

        // Execute with timeout, reading output with limits
        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();

                if stdout_truncated {
                    tracing::warn!(
                        "stdout truncated at {} bytes for native execution",
                        max_output
                    );
                }
                if stderr_truncated {
                    tracing::warn!(
                        "stderr truncated at {} bytes for native execution",
                        max_output
                    );
                }

                tracing::debug!(
                    "Native 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!("Native execution failed: {}", e);
                Err(anyhow::anyhow!("Process execution failed: {}", e))
            }
            Err(_) => {
                // Kill the entire process group on timeout, then fall back
                // to killing the immediate child.
                if let Some(id) = child.id() {
                    unsafe {
                        libc::killpg(id as i32, libc::SIGKILL);
                    }
                }
                let _ = child.kill().await;
                tracing::error!(
                    "Native execution timed out after {:?}",
                    self.config.max_execution_time
                );
                Err(anyhow::anyhow!(
                    "Execution timed out after {:?}",
                    self.config.max_execution_time
                ))
            }
        }
    }
}

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

    /// Helper: create a NativeConfig with bash allowed (most tests need this)
    fn config_with_bash() -> NativeConfig {
        NativeConfig {
            executable: "bash".to_string(),
            allowed_executables: vec!["bash".to_string()],
            ..Default::default()
        }
    }

    #[tokio::test]
    async fn test_native_runner_creation() {
        let config = config_with_bash();
        let runner = NativeRunner::new(config);
        assert!(runner.is_ok());
    }

    #[tokio::test]
    async fn test_default_config_rejected_without_executables() {
        // Default has empty allowed_executables — validate() should reject it
        let config = NativeConfig::default();
        assert!(config.validate().is_err());
    }

    #[tokio::test]
    async fn test_native_python_execution() {
        let config = NativeConfig {
            executable: "python3".to_string(),
            allowed_executables: vec!["python3".to_string()],
            ..Default::default()
        };

        let runner = match NativeRunner::new(config) {
            Ok(r) => r,
            Err(_) => {
                // Skip test if python3 not available
                return;
            }
        };

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

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

    #[tokio::test]
    async fn test_native_bash_execution() {
        let config = config_with_bash();

        let runner = NativeRunner::new(config).unwrap();

        let result = runner
            .execute("echo 'Testing native execution'", HashMap::new())
            .await
            .unwrap();

        assert!(result.success);
        assert!(result.stdout.contains("Testing native execution"));
        assert!(!result.stdout_truncated);
        assert!(!result.stderr_truncated);
    }

    #[tokio::test]
    async fn test_native_execution_with_env_vars() {
        let config = config_with_bash();

        let runner = NativeRunner::new(config).unwrap();

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

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

        assert!(result.success);
        assert!(result.stdout.contains("test_value"));
    }

    #[tokio::test]
    async fn test_native_execution_timeout() {
        let config = NativeConfig {
            executable: "bash".to_string(),
            allowed_executables: vec!["bash".to_string()],
            max_execution_time: Duration::from_secs(1),
            ..Default::default()
        };

        let runner = NativeRunner::new(config).unwrap();

        let result = runner.execute("sleep 5", HashMap::new()).await;

        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("timed out"));
    }

    #[tokio::test]
    async fn test_executable_validation() {
        let config = NativeConfig {
            executable: "malicious_exe".to_string(),
            allowed_executables: vec!["bash".to_string(), "python3".to_string()],
            ..Default::default()
        };

        let result = NativeRunner::new(config);
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_working_directory_validation() {
        let config = NativeConfig {
            working_directory: PathBuf::from("relative/path"),
            allowed_executables: vec!["bash".to_string()],
            ..Default::default()
        };

        let result = NativeRunner::new(config);
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_production_environment_blocked() {
        // Save original value
        let original = std::env::var("SYMBIONT_ENV").ok();

        std::env::set_var("SYMBIONT_ENV", "production");
        let config = config_with_bash();
        let result = NativeRunner::new(config);
        assert!(result.is_err());
        assert!(result
            .unwrap_err()
            .to_string()
            .contains("unconditionally disabled"));

        // Restore original value
        match original {
            Some(val) => std::env::set_var("SYMBIONT_ENV", val),
            None => std::env::remove_var("SYMBIONT_ENV"),
        }
    }

    #[tokio::test]
    async fn test_output_truncation() {
        let config = NativeConfig {
            executable: "bash".to_string(),
            allowed_executables: vec!["bash".to_string()],
            max_output_bytes: 50,
            ..Default::default()
        };

        let runner = NativeRunner::new(config).unwrap();

        // Generate output larger than 50 bytes
        let result = runner
            .execute(
                "for i in $(seq 1 100); do echo 'line'; done",
                HashMap::new(),
            )
            .await
            .unwrap();

        assert!(result.stdout_truncated);
        assert!(result.stdout.contains("[output truncated at 50 bytes]"));
    }
}