use super::*;
#[cfg(feature = "sandbox")]
#[test]
fn a_parent_step_after_a_missing_directory_resolves_to_the_real_project() {
let dir = tempfile::tempdir().unwrap();
let real = std::fs::canonicalize(dir.path()).unwrap();
let cwd = dir.path().join("missing/../proj");
for name in [".agix", ".codix"] {
let layout = base::paths::Layout::new(name).unwrap();
let dirs = base::paths::SensitivePaths::new(None, None, layout).engine_dirs_in(&cwd);
assert!(dirs.contains(&real.join("proj").join(name)), "{dirs:?}");
assert!(dirs.contains(&real.join("proj/.agix")), "{dirs:?}");
}
}
#[cfg(any(target_os = "macos", target_os = "linux"))]
#[allow(clippy::unwrap_used)]
fn plant_git_dir(dir: &Path) {
std::fs::create_dir_all(dir.join("hooks")).unwrap();
std::fs::write(dir.join("HEAD"), "ref: refs/heads/main\n").unwrap();
std::fs::write(dir.join("config"), "[core]\n\tbare = false\n").unwrap();
}
#[cfg(any(target_os = "macos", target_os = "linux"))]
#[test]
#[allow(clippy::unwrap_used)]
fn a_sandboxed_command_cannot_rewrite_a_submodules_configuration_or_hooks() {
if !a_backend_runs() {
return;
}
let dir = tempfile::tempdir().unwrap();
let project = dir.path().join("project");
let state = dir.path().join("state");
let elsewhere = dir.path().join("elsewhere");
for d in [&project, &state, &elsewhere] {
std::fs::create_dir_all(d).unwrap();
}
std::fs::create_dir_all(project.join(".agix")).unwrap();
let git = project.join(".git");
plant_git_dir(&git);
let modules = git.join("modules");
for module in ["sub", "a", "a/modules/b", "libs/c"] {
plant_git_dir(&modules.join(module));
}
let policy = SandboxPolicy {
state_root: Some(state.clone()),
..Default::default()
};
let runs = |command: &str| {
run_confined(&project, &policy, &[], command)
.status
.success()
};
let fsmonitor = "printf '[core]\\n\\tfsmonitor = touch pwned\\n' >>";
runs("rm .git/modules/sub/HEAD");
let elsewhere = elsewhere.display();
let mut refused = vec![
format!("{fsmonitor} .git/modules/sub/config"),
format!("{fsmonitor} .git/modules/a/modules/b/config"),
format!("{fsmonitor} .git/modules/libs/c/config"),
"echo 'touch pwned' > .git/modules/sub/hooks/post-checkout".to_string(),
"echo 'touch pwned' > .git/modules/a/modules/b/hooks/x".to_string(),
"mv .git/modules/sub .git/modules/gone".to_string(),
"mv .git/modules/a/modules .git/modules/a/gone".to_string(),
format!("mv .git/modules/libs '{elsewhere}/libs'"),
];
let absent_files_held = cfg!(target_os = "macos");
if absent_files_held {
refused.push("echo /tmp > .git/modules/sub/commondir".to_string());
}
for command in refused {
assert!(!runs(&command), "`{command}` was allowed");
}
for module in ["sub", "a/modules/b", "libs/c"] {
let module = modules.join(module);
assert!(
module.symlink_metadata().unwrap().is_dir(),
"{} was replaced",
module.display()
);
let config = std::fs::read_to_string(module.join("config")).unwrap();
assert!(!config.contains("fsmonitor"), "{config}");
assert!(!absent_files_held || !module.join("commondir").exists());
}
assert!(!modules.join("a/modules/b/hooks/x").exists());
assert!(
runs("echo x > .git/modules/sub/index && echo x > .git/modules/a/modules/b/FETCH_HEAD"),
"a submodule's own bookkeeping must stay writable"
);
}
#[cfg(any(target_os = "macos", target_os = "linux"))]
#[test]
#[allow(clippy::unwrap_used)]
fn a_project_in_the_state_root_cannot_rewrite_the_state_roots_configuration() {
if !a_backend_runs() {
return;
}
let dir = tempfile::tempdir().unwrap();
let state = dir.path().canonicalize().unwrap().join("state");
write_file(&state.join("skills/s/SKILL.md"), "fine");
write_file(&state.join("hooks/h.sh"), "fine");
write_file(&state.join("settings.local.json"), "{}");
let policy = SandboxPolicy {
state_root: Some(state.clone()),
..Default::default()
};
let runs = |command: &str| run_confined(&state, &policy, &[], command).status.success();
assert!(
runs("echo x > notes.md"),
"the project itself stays writable"
);
for command in [
"echo x > skills/s/SKILL.md",
"mkdir -p skills/planted && echo x > skills/planted/SKILL.md",
"echo x >> hooks/h.sh",
"mkdir -p agents && echo x > agents/a.md",
"echo '{}' > settings.local.json",
] {
assert!(!runs(command), "`{command}` was allowed");
}
assert_eq!(
std::fs::read_to_string(state.join("skills/s/SKILL.md")).unwrap(),
"fine"
);
}
#[cfg(any(target_os = "macos", target_os = "linux"))]
#[test]
#[allow(clippy::unwrap_used)]
fn a_sandboxed_command_cannot_rewrite_the_hooks_path_or_included_config() {
if !a_backend_runs() {
return;
}
let dir = tempfile::tempdir().unwrap();
let root = dir.path().canonicalize().unwrap();
let project = root.join("project");
write_file(
&project.join(".git/config"),
"[core]\n\thooksPath = .githooks\n[include]\n\tpath = ../.gitconfig.local\n",
);
write_file(&project.join(".git/HEAD"), "ref: refs/heads/main\n");
write_file(&project.join(".githooks/pre-commit"), "fine");
write_file(&project.join(".gitconfig.local"), "fine");
let policy = SandboxPolicy {
state_root: Some(root.join("state")),
..Default::default()
};
let runs = |command: &str| {
run_confined(&project, &policy, &[], command)
.status
.success()
};
assert!(runs("echo x > notes.md"), "the project stays writable");
for command in [
"echo x > .githooks/pre-commit",
"echo x > .githooks/post-commit",
"echo x > .gitconfig.local",
] {
assert!(!runs(command), "`{command}` was allowed");
}
for file in [".githooks/pre-commit", ".gitconfig.local"] {
assert_eq!(std::fs::read_to_string(project.join(file)).unwrap(), "fine");
}
assert!(!project.join(".githooks/post-commit").exists());
}