chore: introduce SandboxPolicy::WorkspaceWrite::use_exact_writable_roots

Without this change, it is challenging to create integration tests to
verify that the folders not included in `writable_roots` in
`SandboxPolicy::WorkspaceWrite` are read-only because, by default,
`get_writable_roots_with_cwd()` includes `TMPDIR`, which is where most integrationt
tests do their work.

This introduces a `use_exact_writable_roots` option to disable the default
includes returned by `get_writable_roots_with_cwd()`.
This commit is contained in:
Michael Bolin
2025-08-01 11:27:02 -07:00
parent 5a0ad5ab8f
commit 0dac67f375
3 changed files with 20 additions and 1 deletions

View File

@@ -7,6 +7,7 @@ pub fn summarize_sandbox_policy(sandbox_policy: &SandboxPolicy) -> String {
SandboxPolicy::WorkspaceWrite {
writable_roots,
network_access,
use_exact_writable_roots,
} => {
let mut summary = "workspace-write".to_string();
if !writable_roots.is_empty() {
@@ -19,6 +20,9 @@ pub fn summarize_sandbox_policy(sandbox_policy: &SandboxPolicy) -> String {
.join(", ")
));
}
if *use_exact_writable_roots {
summary.push_str(" (exact writable roots)");
}
if *network_access {
summary.push_str(" (network access enabled)");
}

View File

@@ -350,6 +350,7 @@ impl ConfigToml {
Some(s) => SandboxPolicy::WorkspaceWrite {
writable_roots: s.writable_roots.clone(),
network_access: s.network_access,
use_exact_writable_roots: false,
},
None => SandboxPolicy::new_workspace_write_policy(),
},
@@ -720,6 +721,7 @@ writable_roots = [
SandboxPolicy::WorkspaceWrite {
writable_roots: vec![PathBuf::from("/tmp")],
network_access: false,
use_exact_writable_roots: false,
},
sandbox_workspace_write_cfg.derive_sandbox_policy(sandbox_mode_override)
);

View File

@@ -175,6 +175,10 @@ pub enum SandboxPolicy {
/// default.
#[serde(default)]
network_access: bool,
/// When set to `true`, will not include defaults like TMPDIR.
/// (Mainly used for testing.)
use_exact_writable_roots: bool,
},
}
@@ -199,6 +203,7 @@ impl SandboxPolicy {
SandboxPolicy::WorkspaceWrite {
writable_roots: vec![],
network_access: false,
use_exact_writable_roots: false,
}
}
@@ -230,7 +235,15 @@ impl SandboxPolicy {
match self {
SandboxPolicy::DangerFullAccess => Vec::new(),
SandboxPolicy::ReadOnly => Vec::new(),
SandboxPolicy::WorkspaceWrite { writable_roots, .. } => {
SandboxPolicy::WorkspaceWrite {
writable_roots,
use_exact_writable_roots,
..
} => {
if *use_exact_writable_roots {
return writable_roots.clone();
}
let mut roots = writable_roots.clone();
roots.push(cwd.to_path_buf());