1use 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#[derive(Clone, Debug)]
31pub struct LocalSandboxConfig {
32 pub root_dir: Option<PathBuf>,
35 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#[derive(Clone, Debug)]
52pub struct LocalSandbox {
53 config: LocalSandboxConfig,
54 sessions: Arc<Mutex<HashMap<SandboxSessionId, Arc<LocalSession>>>>,
55}
56
57impl LocalSandbox {
58 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 let mut roots = vec![self.tempdir.path().display().to_string()];
332 for mount in self.mounts()? {
333 if let Some(path) = mount.host_path {
334 roots.push(path.display().to_string());
335 }
336 }
337 let mut capabilities = BTreeMap::new();
338 capabilities.insert("process".to_string(), vec!["exec".to_string()]);
339 capabilities.insert(
340 "workspace".to_string(),
341 vec![
342 "read_text".to_string(),
343 "list".to_string(),
344 "exists".to_string(),
345 "write_text".to_string(),
346 "delete".to_string(),
347 ],
348 );
349
350 Ok(CapabilityPolicy {
351 capabilities,
352 workspace_roots: roots,
353 side_effect_level: Some("process_exec".to_string()),
354 sandbox_profile: self.sandbox_profile,
355 ..Default::default()
356 })
357 }
358
359 fn resolve_cwd(&self, cwd: Option<&str>) -> SandboxResult<PathBuf> {
360 let Some(cwd) = cwd else {
361 return Ok(self.tempdir.path().to_path_buf());
362 };
363 if cwd.trim().is_empty() {
364 return Ok(self.tempdir.path().to_path_buf());
365 }
366 if let Some(path) = self.resolve_mount_path(cwd)? {
367 return Ok(path);
368 }
369 let path = PathBuf::from(cwd);
370 if path.is_absolute() {
371 return Ok(path);
372 }
373 Ok(self.tempdir.path().join(path))
374 }
375
376 fn resolve_mount_path(&self, path: &str) -> SandboxResult<Option<PathBuf>> {
377 if !path.trim_start().starts_with('/') {
378 return Ok(None);
379 }
380 let normalized = normalized_mount_target(path)?;
381 for mount in self.mounts()?.into_iter().rev() {
382 if normalized == mount.target || normalized.starts_with(&(mount.target.clone() + "/")) {
383 let Some(host_path) = mount.host_path else {
384 continue;
385 };
386 let suffix = normalized
387 .trim_start_matches(&mount.target)
388 .trim_start_matches('/');
389 return Ok(Some(host_path.join(suffix)));
390 }
391 }
392 Ok(None)
393 }
394
395 fn mounts(&self) -> SandboxResult<Vec<ResolvedMount>> {
396 Ok(self
397 .mounts
398 .lock()
399 .map_err(|_| SandboxError::Lifecycle("local mount lock poisoned".to_string()))?
400 .clone())
401 }
402}
403
404fn resolve_local_mount(root: &Path, mount: FilesystemMount) -> SandboxResult<ResolvedMount> {
405 let target = normalized_mount_target(&mount.target)?;
406 let source = if mount.source.as_os_str().is_empty() {
407 let relative = target.trim_start_matches('/');
408 root.join(relative)
409 } else if mount.source.is_absolute() {
410 mount.source
411 } else {
412 root.join(mount.source)
413 };
414 std::fs::create_dir_all(&source)?;
415 Ok(ResolvedMount {
416 target,
417 access: mount.access,
418 host_path: Some(source),
419 })
420}
421
422fn mount_env(mounts: &[ResolvedMount]) -> BTreeMap<String, String> {
423 let mut env = BTreeMap::new();
424 for mount in mounts {
425 let Some(path) = &mount.host_path else {
426 continue;
427 };
428 if mount.target == MEMORY_MOUNT {
429 env.insert("HARN_MEMORY_DIR".to_string(), path.display().to_string());
430 }
431 if mount.target == OUTPUTS_MOUNT {
432 env.insert("HARN_OUTPUTS_DIR".to_string(), path.display().to_string());
433 }
434 }
435 env
436}
437
438fn validate_env_key(key: &str) -> SandboxResult<()> {
439 if key.is_empty()
440 || key
441 .chars()
442 .any(|ch| !(ch == '_' || ch.is_ascii_alphanumeric()))
443 || key.as_bytes()[0].is_ascii_digit()
444 {
445 return Err(SandboxError::InvalidRequest(format!(
446 "invalid environment key `{key}`"
447 )));
448 }
449 Ok(())
450}
451
452fn run_harn_shell(source: String, policy: CapabilityPolicy) -> SandboxResult<ExecResult> {
453 let chunk = compile_source(&source).map_err(SandboxError::Exec)?;
454 let rt = tokio::runtime::Builder::new_current_thread()
455 .enable_all()
456 .build()
457 .map_err(SandboxError::Io)?;
458
459 rt.block_on(async {
460 let local = tokio::task::LocalSet::new();
461 local
462 .run_until(async move {
463 let _guard = ExecutionPolicyGuard::push(policy);
464 let mut vm = Vm::new();
465 register_vm_stdlib(&mut vm);
466 let value = vm.execute(&chunk).await.map_err(|error| {
467 SandboxError::Exec(format!("harn-vm process sandbox rejected exec: {error}"))
468 })?;
469 exec_result_from_value(value)
470 })
471 .await
472 })
473}
474
475struct ExecutionPolicyGuard;
476
477impl ExecutionPolicyGuard {
478 fn push(policy: CapabilityPolicy) -> Self {
479 push_execution_policy(policy);
480 Self
481 }
482}
483
484impl Drop for ExecutionPolicyGuard {
485 fn drop(&mut self) {
486 pop_execution_policy();
487 }
488}
489
490fn exec_result_from_value(value: VmValue) -> SandboxResult<ExecResult> {
491 let VmValue::Dict(map) = value else {
492 return Err(SandboxError::Exec(format!(
493 "expected exec result dict from harn-vm, got {}",
494 value.display()
495 )));
496 };
497 let stdout = dict_string(&map, "stdout")?;
498 let stderr = dict_string(&map, "stderr")?;
499 let exit_code = dict_int(&map, "status")?;
500 Ok(ExecResult {
501 stdout,
502 stderr,
503 exit_code,
504 timed_out: false,
505 })
506}
507
508fn dict_string(map: &BTreeMap<String, VmValue>, key: &str) -> SandboxResult<String> {
509 match map.get(key) {
510 Some(VmValue::String(value)) => Ok(value.to_string()),
511 Some(other) => Err(SandboxError::Exec(format!(
512 "expected `{key}` string, got {}",
513 other.display()
514 ))),
515 None => Err(SandboxError::Exec(format!(
516 "missing `{key}` in exec result"
517 ))),
518 }
519}
520
521fn dict_int(map: &BTreeMap<String, VmValue>, key: &str) -> SandboxResult<i32> {
522 match map.get(key) {
523 Some(VmValue::Int(value)) => Ok(*value as i32),
524 Some(other) => Err(SandboxError::Exec(format!(
525 "expected `{key}` int, got {}",
526 other.display()
527 ))),
528 None => Err(SandboxError::Exec(format!(
529 "missing `{key}` in exec result"
530 ))),
531 }
532}
533
534#[cfg(test)]
535mod tests {
536 use super::*;
537
538 #[cfg(unix)]
541 #[tokio::test]
542 async fn local_backend_execs_inside_session_outputs() {
543 let backend = LocalSandbox::default();
544 let session = backend.provision(SandboxSpec::default()).await.unwrap();
545
546 let result = backend
547 .exec(
548 &session.id,
549 ExecRequest {
550 command: "sh".to_string(),
551 args: vec![
552 "-c".to_string(),
553 "printf ok > \"$HARN_OUTPUTS_DIR/result.txt\" && cat \"$HARN_OUTPUTS_DIR/result.txt\""
554 .to_string(),
555 ],
556 ..Default::default()
557 },
558 )
559 .await
560 .unwrap();
561
562 assert_eq!(result.exit_code, 0, "{result:?}");
563 assert_eq!(result.stdout, "ok");
564 }
565
566 #[tokio::test]
567 async fn local_backend_rejects_limited_network_policy() {
568 let backend = LocalSandbox::default();
569 let session = backend.provision(SandboxSpec::default()).await.unwrap();
570 let deny_all = backend
571 .apply_network_policy(
572 &session.id,
573 NetworkPolicy::Limited {
574 allowed_hosts: Vec::new(),
575 },
576 )
577 .await
578 .expect("deny-all egress policy is enforceable locally");
579 assert_eq!(deny_all.id, session.id);
580
581 let err = backend
582 .apply_network_policy(
583 &session.id,
584 NetworkPolicy::Limited {
585 allowed_hosts: vec!["example.com".to_string()],
586 },
587 )
588 .await
589 .unwrap_err();
590
591 assert!(matches!(err, SandboxError::Unsupported { .. }));
592 }
593
594 #[tokio::test]
595 async fn local_backend_defaults_to_os_hardened_sandbox_profile() {
596 let backend = LocalSandbox::default();
597 let session = backend.provision(SandboxSpec::default()).await.unwrap();
598 let local = backend.session(&session.id).unwrap();
599
600 let policy = local.execution_policy().unwrap();
601
602 assert_eq!(policy.sandbox_profile, SandboxProfile::OsHardened);
603 }
604
605 #[tokio::test]
606 async fn local_backend_threads_configured_sandbox_profile_into_policy() {
607 let backend = LocalSandbox::new(LocalSandboxConfig {
608 root_dir: None,
609 sandbox_profile: SandboxProfile::Unrestricted,
610 });
611 let session = backend.provision(SandboxSpec::default()).await.unwrap();
612 let local = backend.session(&session.id).unwrap();
613
614 let policy = local.execution_policy().unwrap();
615
616 assert_eq!(policy.sandbox_profile, SandboxProfile::Unrestricted);
617 }
618
619 #[test]
620 fn mount_env_uses_canonical_mount_names() {
621 let mounts = vec![ResolvedMount {
622 target: OUTPUTS_MOUNT.to_string(),
623 access: FilesystemAccess::ReadWrite,
624 host_path: Some(PathBuf::from("/tmp/out")),
625 }];
626 assert_eq!(
627 mount_env(&mounts).get("HARN_OUTPUTS_DIR"),
628 Some(&"/tmp/out".to_string())
629 );
630 }
631}