Stop --chdir from binding the working directory read-write
This commit is contained in:
+57
-8
@@ -28,19 +28,65 @@ impl Drop for CleanupPath {
|
||||
}
|
||||
}
|
||||
#[test]
|
||||
fn cwd_is_writable() {
|
||||
let _cleanup = CleanupPath::new("./sandbox_canary");
|
||||
fn chdir_alone_makes_nothing_visible() {
|
||||
let dir = TempDir::new().unwrap();
|
||||
let dir_str = dir.path().to_str().unwrap();
|
||||
|
||||
let output = Sandbox::new(&[])
|
||||
.args(["--", "bash", "-c", "touch ./sandbox_canary && echo ok"])
|
||||
let output = Sandbox::new(&["--chdir", dir_str])
|
||||
.args(["--", "true"])
|
||||
.output()
|
||||
.expect("agent-sandbox binary failed to execute");
|
||||
|
||||
let stderr = String::from_utf8_lossy(&output.stderr);
|
||||
assert!(!output.status.success(), "chdir into an unbound path ran");
|
||||
assert!(
|
||||
stderr.contains(&format!(
|
||||
"Can't chdir to {dir_str}: No such file or directory"
|
||||
)),
|
||||
"expected bwrap's chdir error, got: {stderr}"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn chdir_under_ro_is_read_only() {
|
||||
let dir = TempDir::new().unwrap();
|
||||
let dir_str = dir.path().to_str().unwrap();
|
||||
|
||||
let output = Sandbox::new(&["--ro", dir_str, "--chdir", dir_str])
|
||||
.args([
|
||||
"--",
|
||||
"bash",
|
||||
"-c",
|
||||
"touch canary 2>/dev/null || echo readonly",
|
||||
])
|
||||
.output()
|
||||
.expect("agent-sandbox binary failed to execute");
|
||||
|
||||
let stdout = String::from_utf8_lossy(&output.stdout);
|
||||
assert!(
|
||||
stdout.contains("ok"),
|
||||
"expected 'ok' in stdout, got: {stdout}"
|
||||
stdout.contains("readonly"),
|
||||
"expected 'readonly' in stdout, got: {stdout}, stderr: {}",
|
||||
String::from_utf8_lossy(&output.stderr)
|
||||
);
|
||||
assert!(!dir.path().join("canary").exists());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn chdir_under_rw_is_writable() {
|
||||
let dir = TempDir::new().unwrap();
|
||||
let dir_str = dir.path().to_str().unwrap();
|
||||
|
||||
let output = Sandbox::new(&["--rw", dir_str, "--chdir", dir_str])
|
||||
.args(["--", "touch", "canary"])
|
||||
.output()
|
||||
.expect("agent-sandbox binary failed to execute");
|
||||
|
||||
assert!(
|
||||
output.status.success(),
|
||||
"stderr: {}",
|
||||
String::from_utf8_lossy(&output.stderr)
|
||||
);
|
||||
assert!(dir.path().join("canary").exists());
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -647,7 +693,7 @@ fn persistent_tmp_resumes_with_explicit_key_matching_derived_hash() {
|
||||
let _cleanup_tmp = CleanupPath::new(format!("/tmp/agent-sandbox-{key}"));
|
||||
let _cleanup_var = CleanupPath::new(format!("/var/tmp/agent-sandbox-{key}"));
|
||||
|
||||
let writer = Sandbox::new(&["--persistent-tmp", "--chdir", chdir_str])
|
||||
let writer = Sandbox::new(&["--persistent-tmp", "--ro", chdir_str, "--chdir", chdir_str])
|
||||
.args(["--", "bash", "-c", "echo persisted > /tmp/canary"])
|
||||
.output()
|
||||
.expect("write run failed");
|
||||
@@ -660,12 +706,15 @@ fn persistent_tmp_resumes_with_explicit_key_matching_derived_hash() {
|
||||
// Second run: different chdir (would have derived a different hash) plus an
|
||||
// extra flag, but an explicit key pinning the same host directory.
|
||||
let other_chdir = TempDir::new().unwrap();
|
||||
let other_chdir_str = other_chdir.path().to_str().unwrap();
|
||||
let reader = Sandbox::new(&[
|
||||
"--persistent-tmp",
|
||||
"--persistent-key",
|
||||
&key,
|
||||
"--ro",
|
||||
other_chdir_str,
|
||||
"--chdir",
|
||||
other_chdir.path().to_str().unwrap(),
|
||||
other_chdir_str,
|
||||
"--unshare-net",
|
||||
])
|
||||
.args(["--", "cat", "/tmp/canary"])
|
||||
|
||||
Reference in New Issue
Block a user