Skip to main content

harn_hostlib/sandbox/
local.rs

1//! Local enforcement backend.
2//!
3//! Runs each command through `harn-vm`'s process sandbox, so the
4//! kernel-level confinement (Landlock/seccomp on Linux, `sandbox-exec`
5//! on macOS, Job Objects on Windows, `pledge`/`unveil` on OpenBSD) is
6//! reused rather than reimplemented. Filesystem scope comes from the
7//! session's mounts; network egress is limited to deny-all or
8//! allow-all, since per-host egress filtering for a local process is a
9//! remote-backend capability (see [`SandboxCapabilities::network_policy`]).
10
11use std::collections::{BTreeMap, HashMap};
12use std::path::{Path, PathBuf};
13use std::sync::{Arc, Mutex};
14
15use async_trait::async_trait;
16use harn_vm::orchestration::{
17    pop_execution_policy, push_execution_policy, CapabilityPolicy, SandboxProfile,
18};
19use harn_vm::{compile_source, stdlib::register_vm_stdlib, Vm, VmValue};
20use tempfile::TempDir;
21
22use super::{
23    duration_secs, harn_string, normalized_mount_target, sh_quote, ExecRequest, ExecResult,
24    FilesystemAccess, FilesystemMount, NetworkPolicy, ResolvedMount, ResourceLimits,
25    SandboxBackend, SandboxCapabilities, SandboxError, SandboxResult, SandboxSession,
26    SandboxSessionId, SandboxSnapshot, SandboxSpec, SandboxState, MEMORY_MOUNT, OUTPUTS_MOUNT,
27};
28
29/// Configuration for a [`LocalSandbox`].
30#[derive(Clone, Debug)]
31pub struct LocalSandboxConfig {
32    /// Directory under which session roots are created. When `None`,
33    /// sessions are rooted under the current working directory.
34    pub root_dir: Option<PathBuf>,
35    /// The `harn-vm` sandbox profile applied to every command in this
36    /// backend.
37    pub sandbox_profile: SandboxProfile,
38}
39
40impl Default for LocalSandboxConfig {
41    fn default() -> Self {
42        Self {
43            root_dir: None,
44            sandbox_profile: SandboxProfile::OsHardened,
45        }
46    }
47}
48
49/// Local [`SandboxBackend`] that confines commands with `harn-vm`'s
50/// process sandbox.
51#[derive(Clone, Debug)]
52pub struct LocalSandbox {
53    config: LocalSandboxConfig,
54    sessions: Arc<Mutex<HashMap<SandboxSessionId, Arc<LocalSession>>>>,
55}
56
57impl LocalSandbox {
58    /// Construct a backend with the given configuration.
59    pub fn new(config: LocalSandboxConfig) -> Self {
60        Self {
61            config,
62            sessions: Arc::new(Mutex::new(HashMap::new())),
63        }
64    }
65
66    fn session(&self, session_id: &SandboxSessionId) -> SandboxResult<Arc<LocalSession>> {
67        self.sessions
68            .lock()
69            .map_err(|_| SandboxError::Lifecycle("local session lock poisoned".to_string()))?
70            .get(session_id)
71            .cloned()
72            .ok_or_else(|| SandboxError::SessionNotFound(session_id.to_string()))
73    }
74}
75
76impl Default for LocalSandbox {
77    fn default() -> Self {
78        Self::new(LocalSandboxConfig::default())
79    }
80}
81
82#[async_trait]
83impl SandboxBackend for LocalSandbox {
84    fn name(&self) -> &'static str {
85        "local"
86    }
87
88    fn capabilities(&self) -> SandboxCapabilities {
89        SandboxCapabilities {
90            local_process_sandbox: true,
91            network_policy: false,
92            snapshot: true,
93            resume: true,
94            suspend_on_idle: false,
95        }
96    }
97
98    async fn provision(&self, mut spec: SandboxSpec) -> SandboxResult<SandboxSession> {
99        let id = spec.session_id.take().unwrap_or_else(|| {
100            SandboxSessionId(format!("local-{}", uuid::Uuid::now_v7().simple()))
101        });
102        let tempdir = match &self.config.root_dir {
103            Some(root) => tempfile::Builder::new()
104                .prefix("harn-sandbox-")
105                .tempdir_in(root)?,
106            None => tempfile::Builder::new()
107                .prefix("harn-sandbox-")
108                .tempdir_in(std::env::current_dir()?)?,
109        };
110
111        let root = tempdir.path().to_path_buf();
112        let memory = root.join("mnt/memory");
113        let outputs = root.join("mnt/session/outputs");
114        std::fs::create_dir_all(&memory)?;
115        std::fs::create_dir_all(&outputs)?;
116
117        let mut mounts = vec![
118            ResolvedMount {
119                target: MEMORY_MOUNT.to_string(),
120                access: FilesystemAccess::ReadWrite,
121                host_path: Some(memory),
122            },
123            ResolvedMount {
124                target: OUTPUTS_MOUNT.to_string(),
125                access: FilesystemAccess::ReadWrite,
126                host_path: Some(outputs),
127            },
128        ];
129        for mount in spec.mounts {
130            mounts.push(resolve_local_mount(&root, mount)?);
131        }
132
133        let session = Arc::new(LocalSession {
134            id: id.clone(),
135            tempdir,
136            mounts: Mutex::new(mounts),
137            network_policy: Mutex::new(spec.network_policy),
138            limits: spec.limits,
139            state: Mutex::new(SandboxState::Running),
140            sandbox_profile: self.config.sandbox_profile,
141        });
142
143        self.sessions
144            .lock()
145            .map_err(|_| SandboxError::Lifecycle("local session lock poisoned".to_string()))?
146            .insert(id, session.clone());
147
148        session.to_public()
149    }
150
151    async fn attach_filesystem(
152        &self,
153        session_id: &SandboxSessionId,
154        mount: FilesystemMount,
155    ) -> SandboxResult<SandboxSession> {
156        let session = self.session(session_id)?;
157        let resolved = resolve_local_mount(session.tempdir.path(), mount)?;
158        session
159            .mounts
160            .lock()
161            .map_err(|_| SandboxError::Lifecycle("local mount lock poisoned".to_string()))?
162            .push(resolved);
163        session.to_public()
164    }
165
166    async fn apply_network_policy(
167        &self,
168        session_id: &SandboxSessionId,
169        policy: NetworkPolicy,
170    ) -> SandboxResult<SandboxSession> {
171        if let NetworkPolicy::Limited { allowed_hosts } = &policy {
172            if !allowed_hosts.is_empty() {
173                return Err(SandboxError::Unsupported {
174                    backend: "local",
175                    operation: "limited network allow-lists",
176                });
177            }
178        }
179        let session = self.session(session_id)?;
180        *session
181            .network_policy
182            .lock()
183            .map_err(|_| SandboxError::Lifecycle("local network lock poisoned".to_string()))? =
184            policy;
185        session.to_public()
186    }
187
188    async fn exec(
189        &self,
190        session_id: &SandboxSessionId,
191        request: ExecRequest,
192    ) -> SandboxResult<ExecResult> {
193        let session = self.session(session_id)?;
194        session.exec(request).await
195    }
196
197    async fn snapshot(&self, session_id: &SandboxSessionId) -> SandboxResult<SandboxSnapshot> {
198        let session = self.session(session_id)?;
199        Ok(SandboxSnapshot {
200            session_id: session.id.clone(),
201            backend: "local".to_string(),
202            snapshot_id: format!("local:{}", session.id),
203            metadata: BTreeMap::from([(
204                "root".to_string(),
205                session.tempdir.path().display().to_string(),
206            )]),
207        })
208    }
209
210    async fn resume(&self, session_id: &SandboxSessionId) -> SandboxResult<SandboxSession> {
211        let session = self.session(session_id)?;
212        *session
213            .state
214            .lock()
215            .map_err(|_| SandboxError::Lifecycle("local state lock poisoned".to_string()))? =
216            SandboxState::Running;
217        session.to_public()
218    }
219
220    async fn terminate(&self, session_id: &SandboxSessionId) -> SandboxResult<()> {
221        let session = self
222            .sessions
223            .lock()
224            .map_err(|_| SandboxError::Lifecycle("local session lock poisoned".to_string()))?
225            .remove(session_id)
226            .ok_or_else(|| SandboxError::SessionNotFound(session_id.to_string()))?;
227        *session
228            .state
229            .lock()
230            .map_err(|_| SandboxError::Lifecycle("local state lock poisoned".to_string()))? =
231            SandboxState::Terminated;
232        Ok(())
233    }
234}
235
236#[derive(Debug)]
237struct LocalSession {
238    id: SandboxSessionId,
239    tempdir: TempDir,
240    mounts: Mutex<Vec<ResolvedMount>>,
241    network_policy: Mutex<NetworkPolicy>,
242    limits: ResourceLimits,
243    state: Mutex<SandboxState>,
244    sandbox_profile: SandboxProfile,
245}
246
247impl LocalSession {
248    fn to_public(&self) -> SandboxResult<SandboxSession> {
249        let mounts = self
250            .mounts
251            .lock()
252            .map_err(|_| SandboxError::Lifecycle("local mount lock poisoned".to_string()))?
253            .clone();
254        let state = self
255            .state
256            .lock()
257            .map_err(|_| SandboxError::Lifecycle("local state lock poisoned".to_string()))?
258            .clone();
259        Ok(SandboxSession {
260            id: self.id.clone(),
261            backend: "local".to_string(),
262            state,
263            mounts,
264            metadata: BTreeMap::from([(
265                "root".to_string(),
266                self.tempdir.path().display().to_string(),
267            )]),
268        })
269    }
270
271    async fn exec(self: Arc<Self>, request: ExecRequest) -> SandboxResult<ExecResult> {
272        if request.command.trim().is_empty() {
273            return Err(SandboxError::InvalidRequest(
274                "exec command cannot be empty".to_string(),
275            ));
276        }
277        let timeout = request.timeout.or(self.limits.wall_time);
278        let source = self.harn_exec_source(&request)?;
279        let policy = self.execution_policy()?;
280
281        let task = tokio::task::spawn_blocking(move || run_harn_shell(source, policy));
282        match timeout {
283            Some(timeout) => tokio::time::timeout(timeout, task)
284                .await
285                .map_err(|_| SandboxError::Exec("local exec timed out".to_string()))??,
286            None => task.await?,
287        }
288    }
289
290    fn harn_exec_source(&self, request: &ExecRequest) -> SandboxResult<String> {
291        let cwd = self.resolve_cwd(request.cwd.as_deref())?;
292        let mut shell = String::new();
293        for (key, value) in mount_env(&self.mounts()?) {
294            shell.push_str("export ");
295            shell.push_str(&key);
296            shell.push('=');
297            shell.push_str(&sh_quote(&value));
298            shell.push_str("; ");
299        }
300        for (key, value) in &request.env {
301            validate_env_key(key)?;
302            shell.push_str("export ");
303            shell.push_str(key);
304            shell.push('=');
305            shell.push_str(&sh_quote(value));
306            shell.push_str("; ");
307        }
308        if let Some(stdin) = &request.stdin {
309            shell.push_str("printf %s ");
310            shell.push_str(&sh_quote(stdin));
311            shell.push_str(" | ");
312        }
313        if let Some(timeout) = request.timeout.or(self.limits.wall_time) {
314            shell.push_str("timeout ");
315            shell.push_str(&duration_secs(timeout).to_string());
316            shell.push(' ');
317        }
318        shell.push_str(&sh_quote(&request.command));
319        for arg in &request.args {
320            shell.push(' ');
321            shell.push_str(&sh_quote(arg));
322        }
323        Ok(format!(
324            "pipeline local_sandbox_exec(task) {{ return shell_at({}, {}) }}",
325            harn_string(&cwd.display().to_string()),
326            harn_string(&shell),
327        ))
328    }
329
330    fn execution_policy(&self) -> SandboxResult<CapabilityPolicy> {
331        // The session root is always writable; declared mounts split by
332        // their access so a `ReadOnly` mount lowers to a read-only root
333        // the VM and OS sandbox both refuse to write.
334        let mut roots = vec![self.tempdir.path().display().to_string()];
335        let mut read_only_roots = Vec::new();
336        for mount in self.mounts()? {
337            if let Some(path) = mount.host_path {
338                match mount.access {
339                    FilesystemAccess::ReadWrite => roots.push(path.display().to_string()),
340                    FilesystemAccess::ReadOnly => read_only_roots.push(path.display().to_string()),
341                }
342            }
343        }
344        let mut capabilities = BTreeMap::new();
345        capabilities.insert("process".to_string(), vec!["exec".to_string()]);
346        capabilities.insert(
347            "workspace".to_string(),
348            vec![
349                "read_text".to_string(),
350                "list".to_string(),
351                "exists".to_string(),
352                "write_text".to_string(),
353                "delete".to_string(),
354            ],
355        );
356
357        Ok(CapabilityPolicy {
358            capabilities,
359            workspace_roots: roots,
360            read_only_roots,
361            side_effect_level: Some("process_exec".to_string()),
362            sandbox_profile: self.sandbox_profile,
363            ..Default::default()
364        })
365    }
366
367    fn resolve_cwd(&self, cwd: Option<&str>) -> SandboxResult<PathBuf> {
368        let Some(cwd) = cwd else {
369            return Ok(self.tempdir.path().to_path_buf());
370        };
371        if cwd.trim().is_empty() {
372            return Ok(self.tempdir.path().to_path_buf());
373        }
374        if let Some(path) = self.resolve_mount_path(cwd)? {
375            return Ok(path);
376        }
377        let path = PathBuf::from(cwd);
378        if path.is_absolute() {
379            return Ok(path);
380        }
381        Ok(self.tempdir.path().join(path))
382    }
383
384    fn resolve_mount_path(&self, path: &str) -> SandboxResult<Option<PathBuf>> {
385        if !path.trim_start().starts_with('/') {
386            return Ok(None);
387        }
388        let normalized = normalized_mount_target(path)?;
389        for mount in self.mounts()?.into_iter().rev() {
390            if normalized == mount.target || normalized.starts_with(&(mount.target.clone() + "/")) {
391                let Some(host_path) = mount.host_path else {
392                    continue;
393                };
394                let suffix = normalized
395                    .trim_start_matches(&mount.target)
396                    .trim_start_matches('/');
397                return Ok(Some(host_path.join(suffix)));
398            }
399        }
400        Ok(None)
401    }
402
403    fn mounts(&self) -> SandboxResult<Vec<ResolvedMount>> {
404        Ok(self
405            .mounts
406            .lock()
407            .map_err(|_| SandboxError::Lifecycle("local mount lock poisoned".to_string()))?
408            .clone())
409    }
410}
411
412fn resolve_local_mount(root: &Path, mount: FilesystemMount) -> SandboxResult<ResolvedMount> {
413    let target = normalized_mount_target(&mount.target)?;
414    let source = if mount.source.as_os_str().is_empty() {
415        let relative = target.trim_start_matches('/');
416        root.join(relative)
417    } else if mount.source.is_absolute() {
418        mount.source
419    } else {
420        root.join(mount.source)
421    };
422    std::fs::create_dir_all(&source)?;
423    Ok(ResolvedMount {
424        target,
425        access: mount.access,
426        host_path: Some(source),
427    })
428}
429
430fn mount_env(mounts: &[ResolvedMount]) -> BTreeMap<String, String> {
431    let mut env = BTreeMap::new();
432    for mount in mounts {
433        let Some(path) = &mount.host_path else {
434            continue;
435        };
436        if mount.target == MEMORY_MOUNT {
437            env.insert("HARN_MEMORY_DIR".to_string(), path.display().to_string());
438        }
439        if mount.target == OUTPUTS_MOUNT {
440            env.insert("HARN_OUTPUTS_DIR".to_string(), path.display().to_string());
441        }
442    }
443    env
444}
445
446fn validate_env_key(key: &str) -> SandboxResult<()> {
447    if key.is_empty()
448        || key
449            .chars()
450            .any(|ch| !(ch == '_' || ch.is_ascii_alphanumeric()))
451        || key.as_bytes()[0].is_ascii_digit()
452    {
453        return Err(SandboxError::InvalidRequest(format!(
454            "invalid environment key `{key}`"
455        )));
456    }
457    Ok(())
458}
459
460fn run_harn_shell(source: String, policy: CapabilityPolicy) -> SandboxResult<ExecResult> {
461    let chunk = compile_source(&source).map_err(SandboxError::Exec)?;
462    let rt = tokio::runtime::Builder::new_current_thread()
463        .enable_all()
464        .build()
465        .map_err(SandboxError::Io)?;
466
467    rt.block_on(async {
468        let local = tokio::task::LocalSet::new();
469        local
470            .run_until(async move {
471                let _guard = ExecutionPolicyGuard::push(policy);
472                let mut vm = Vm::new();
473                register_vm_stdlib(&mut vm);
474                let value = vm.execute(&chunk).await.map_err(|error| {
475                    SandboxError::Exec(format!("harn-vm process sandbox rejected exec: {error}"))
476                })?;
477                exec_result_from_value(value)
478            })
479            .await
480    })
481}
482
483struct ExecutionPolicyGuard;
484
485impl ExecutionPolicyGuard {
486    fn push(policy: CapabilityPolicy) -> Self {
487        push_execution_policy(policy);
488        Self
489    }
490}
491
492impl Drop for ExecutionPolicyGuard {
493    fn drop(&mut self) {
494        pop_execution_policy();
495    }
496}
497
498fn exec_result_from_value(value: VmValue) -> SandboxResult<ExecResult> {
499    let VmValue::Dict(map) = value else {
500        return Err(SandboxError::Exec(format!(
501            "expected exec result dict from harn-vm, got {}",
502            value.display()
503        )));
504    };
505    let stdout = dict_string(&map, "stdout")?;
506    let stderr = dict_string(&map, "stderr")?;
507    let exit_code = dict_int(&map, "status")?;
508    Ok(ExecResult {
509        stdout,
510        stderr,
511        exit_code,
512        timed_out: false,
513    })
514}
515
516fn dict_string(map: &BTreeMap<String, VmValue>, key: &str) -> SandboxResult<String> {
517    match map.get(key) {
518        Some(VmValue::String(value)) => Ok(value.to_string()),
519        Some(other) => Err(SandboxError::Exec(format!(
520            "expected `{key}` string, got {}",
521            other.display()
522        ))),
523        None => Err(SandboxError::Exec(format!(
524            "missing `{key}` in exec result"
525        ))),
526    }
527}
528
529fn dict_int(map: &BTreeMap<String, VmValue>, key: &str) -> SandboxResult<i32> {
530    match map.get(key) {
531        Some(VmValue::Int(value)) => Ok(*value as i32),
532        Some(other) => Err(SandboxError::Exec(format!(
533            "expected `{key}` int, got {}",
534            other.display()
535        ))),
536        None => Err(SandboxError::Exec(format!(
537            "missing `{key}` in exec result"
538        ))),
539    }
540}
541
542#[cfg(test)]
543mod tests {
544    use super::*;
545
546    // Exercises a real `sh -c` invocation with POSIX env expansion and
547    // `printf`, so it only runs where a POSIX shell exists.
548    #[cfg(unix)]
549    #[tokio::test]
550    async fn local_backend_execs_inside_session_outputs() {
551        let backend = LocalSandbox::default();
552        let session = backend.provision(SandboxSpec::default()).await.unwrap();
553
554        let result = backend
555            .exec(
556                &session.id,
557                ExecRequest {
558                    command: "sh".to_string(),
559                    args: vec![
560                        "-c".to_string(),
561                        "printf ok > \"$HARN_OUTPUTS_DIR/result.txt\" && cat \"$HARN_OUTPUTS_DIR/result.txt\""
562                            .to_string(),
563                    ],
564                    ..Default::default()
565                },
566            )
567            .await
568            .unwrap();
569
570        assert_eq!(result.exit_code, 0, "{result:?}");
571        assert_eq!(result.stdout, "ok");
572    }
573
574    #[tokio::test]
575    async fn local_backend_rejects_limited_network_policy() {
576        let backend = LocalSandbox::default();
577        let session = backend.provision(SandboxSpec::default()).await.unwrap();
578        let deny_all = backend
579            .apply_network_policy(
580                &session.id,
581                NetworkPolicy::Limited {
582                    allowed_hosts: Vec::new(),
583                },
584            )
585            .await
586            .expect("deny-all egress policy is enforceable locally");
587        assert_eq!(deny_all.id, session.id);
588
589        let err = backend
590            .apply_network_policy(
591                &session.id,
592                NetworkPolicy::Limited {
593                    allowed_hosts: vec!["example.com".to_string()],
594                },
595            )
596            .await
597            .unwrap_err();
598
599        assert!(matches!(err, SandboxError::Unsupported { .. }));
600    }
601
602    #[tokio::test]
603    async fn local_backend_defaults_to_os_hardened_sandbox_profile() {
604        let backend = LocalSandbox::default();
605        let session = backend.provision(SandboxSpec::default()).await.unwrap();
606        let local = backend.session(&session.id).unwrap();
607
608        let policy = local.execution_policy().unwrap();
609
610        assert_eq!(policy.sandbox_profile, SandboxProfile::OsHardened);
611    }
612
613    #[tokio::test]
614    async fn local_backend_threads_configured_sandbox_profile_into_policy() {
615        let backend = LocalSandbox::new(LocalSandboxConfig {
616            root_dir: None,
617            sandbox_profile: SandboxProfile::Unrestricted,
618        });
619        let session = backend.provision(SandboxSpec::default()).await.unwrap();
620        let local = backend.session(&session.id).unwrap();
621
622        let policy = local.execution_policy().unwrap();
623
624        assert_eq!(policy.sandbox_profile, SandboxProfile::Unrestricted);
625    }
626
627    #[tokio::test]
628    async fn read_only_mounts_lower_to_read_only_roots() {
629        let backend = LocalSandbox::default();
630        let session = backend
631            .provision(SandboxSpec {
632                mounts: vec![FilesystemMount {
633                    source: PathBuf::new(),
634                    target: "/mnt/reference".to_string(),
635                    access: FilesystemAccess::ReadOnly,
636                }],
637                ..Default::default()
638            })
639            .await
640            .unwrap();
641        let local = backend.session(&session.id).unwrap();
642
643        let policy = local.execution_policy().unwrap();
644
645        // The canonical memory/outputs mounts plus the session root stay
646        // writable; only the declared read-only mount lands in read_only_roots.
647        assert!(
648            policy
649                .read_only_roots
650                .iter()
651                .any(|root| root.ends_with("reference")),
652            "read-only mount should lower to read_only_roots, got {:?}",
653            policy.read_only_roots
654        );
655        assert!(
656            !policy
657                .workspace_roots
658                .iter()
659                .any(|root| root.ends_with("reference")),
660            "read-only mount must not appear among writable workspace_roots, got {:?}",
661            policy.workspace_roots
662        );
663    }
664
665    #[test]
666    fn mount_env_uses_canonical_mount_names() {
667        let mounts = vec![ResolvedMount {
668            target: OUTPUTS_MOUNT.to_string(),
669            access: FilesystemAccess::ReadWrite,
670            host_path: Some(PathBuf::from("/tmp/out")),
671        }];
672        assert_eq!(
673            mount_env(&mounts).get("HARN_OUTPUTS_DIR"),
674            Some(&"/tmp/out".to_string())
675        );
676    }
677}