use std::path::{Path, PathBuf};
use super::{
normalize_for_policy, normalized_process_roots, normalized_workspace_roots, path_is_within,
sandbox_rejection,
};
use crate::orchestration::CapabilityPolicy;
use crate::value::VmError;
fn process_cwd_roots(policy: &CapabilityPolicy) -> Vec<PathBuf> {
let mut roots = normalized_workspace_roots(policy);
for root in normalized_process_roots(&policy.process_sandbox.read_roots)
.into_iter()
.chain(normalized_process_roots(
&policy.process_sandbox.write_roots,
))
{
if !roots.contains(&root) {
roots.push(root);
}
}
roots
}
pub(super) fn enforce_process_cwd_for_policy(
path: &Path,
policy: &CapabilityPolicy,
) -> Result<(), VmError> {
if !policy.sandbox_profile.enforces_path_scope() {
return Ok(());
}
let candidate = normalize_for_policy(path);
let roots = process_cwd_roots(policy);
if roots.iter().any(|root| path_is_within(&candidate, root)) {
return Ok(());
}
Err(sandbox_rejection(format!(
"sandbox violation: process cwd '{}' is outside the launchable roots [{}]",
candidate.display(),
roots
.iter()
.map(|root| root.display().to_string())
.collect::<Vec<_>>()
.join(", ")
)))
}