From 1d804e91b75454927fb4f51615b067ba6158c37c Mon Sep 17 00:00:00 2001 From: jackz Date: Fri, 25 Sep 2026 19:01:36 +0000 Subject: [PATCH] Protect `.aws` directories under sandbox writable roots (#48176) ## Why AWS profiles can select credential helpers that the application executes. Granting write access to a containing directory should therefore keep `.aws` protected by default. ## What changed Add `.aws` to the protected metadata paths alongside `.git`, `.agents`, and `.codex`, including workspace roots and additional writable roots. Preserve explicit user rules that override the default protection. ## Testing Extend policy and sandbox tests to cover `.aws` protection. Add macOS coverage for existing and absent `.aws` directories, deletion and replacement attempts, and symlink targets. Verify that Linux sandbox writes to protected configuration files fail while sibling files remain writable. GitOrigin-RevId: 31ea287bd0dd9b13aff893783fba9ca3aed0c89d --- codex-rs/core/src/config/config_tests.rs | 6 +- ...arios__mxc__preferred_mxc_permissions.snap | 2 +- codex-rs/core/tests/suite/workspace_roots.rs | 2 +- codex-rs/linux-sandbox/src/bwrap.rs | 22 ++- .../linux-sandbox/tests/suite/landlock.rs | 77 ++++---- .../tests/suite/nested_metadata_tests.rs | 6 +- codex-rs/mxc-sandbox/src/policy_tests.rs | 17 +- .../permission_profile_intersection_tests.rs | 2 +- codex-rs/protocol/src/permissions.rs | 106 +++++++---- codex-rs/sandboxing/src/seatbelt_tests.rs | 179 ++++++++++++++---- codex-rs/windows-sandbox-rs/src/allow.rs | 5 +- 11 files changed, 281 insertions(+), 143 deletions(-) diff --git a/codex-rs/core/src/config/config_tests.rs b/codex-rs/core/src/config/config_tests.rs index fbf31a0940..dbd3e4654a 100644 --- a/codex-rs/core/src/config/config_tests.rs +++ b/codex-rs/core/src/config/config_tests.rs @@ -3730,7 +3730,7 @@ async fn implicit_builtin_workspace_profile_preserves_add_dir_metadata_carveouts let codex_home = TempDir::new()?; let cwd = TempDir::new()?; let extra_root = TempDir::new()?; - for subpath in [".git", ".agents", ".codex"] { + for subpath in [".git", ".agents", ".codex", ".aws"] { std::fs::create_dir_all(extra_root.path().join(subpath))?; } let project_key = cwd.path().to_string_lossy().to_string(); @@ -3763,7 +3763,7 @@ async fn implicit_builtin_workspace_profile_preserves_add_dir_metadata_carveouts policy.can_write_local_path_with_cwd(extra_root.as_path(), cwd.path()), "expected implicit :workspace to preserve additional writable roots, policy: {policy:?}" ); - for subpath in [".git", ".agents", ".codex"] { + for subpath in [".git", ".agents", ".codex", ".aws"] { assert!( !policy.can_write_local_path_with_cwd(&extra_root.join(subpath), cwd.path()), "expected implicit :workspace to preserve legacy metadata carveout for {subpath}, \ @@ -4953,7 +4953,7 @@ exclude_slash_tmp = true missing_path_behavior: None, }) ); - for subpath in [".git", ".agents", ".codex"] { + for subpath in [".git", ".agents", ".codex", ".aws"] { assert!( file_system_policy .entries diff --git a/codex-rs/core/tests/suite/snapshots/all__suite__scenarios__mxc__preferred_mxc_permissions.snap b/codex-rs/core/tests/suite/snapshots/all__suite__scenarios__mxc__preferred_mxc_permissions.snap index 8b231ed53c..ed3f7db382 100644 --- a/codex-rs/core/tests/suite/snapshots/all__suite__scenarios__mxc__preferred_mxc_permissions.snap +++ b/codex-rs/core/tests/suite/snapshots/all__suite__scenarios__mxc__preferred_mxc_permissions.snap @@ -30,7 +30,7 @@ Scenario: Resolved local MXC selection preserves workspace-write permissions in - [hash=c88e32494c7023a5] + [hash=e9b73aecc0d54647] 02:message/user: Inspect the sandbox. diff --git a/codex-rs/core/tests/suite/workspace_roots.rs b/codex-rs/core/tests/suite/workspace_roots.rs index 63fcfb5be3..fbe86a8fc4 100644 --- a/codex-rs/core/tests/suite/workspace_roots.rs +++ b/codex-rs/core/tests/suite/workspace_roots.rs @@ -343,7 +343,7 @@ async fn workspace_roots_allow_file_and_command_writes_in_secondary_root( #[tokio::test(flavor = "multi_thread", worker_threads = 2)] async fn workspace_roots_allow_patches_but_protect_metadata_directories() -> Result<()> { const PATCH_CONTENTS: &str = "workspace root patch access"; - const PROTECTED_METADATA_DIRECTORIES: [&str; 3] = [".git", ".agents", ".codex"]; + const PROTECTED_METADATA_DIRECTORIES: [&str; 4] = [".git", ".agents", ".codex", ".aws"]; skip_if_wine_exec!( Ok(()), diff --git a/codex-rs/linux-sandbox/src/bwrap.rs b/codex-rs/linux-sandbox/src/bwrap.rs index 40b0aa9e6f..319b6af419 100644 --- a/codex-rs/linux-sandbox/src/bwrap.rs +++ b/codex-rs/linux-sandbox/src/bwrap.rs @@ -2038,6 +2038,7 @@ mod tests { workspace.join(".git"), workspace.join(".agents"), workspace.join(".codex"), + workspace.join(".aws"), ] ); assert!( @@ -2075,6 +2076,7 @@ mod tests { dot_git.clone(), workspace.join(".agents"), workspace.join(".codex"), + workspace.join(".aws"), ] ); assert!( @@ -2116,7 +2118,12 @@ mod tests { assert_empty_directory_mounted_read_only(&args.args, &workspace.join(".codex")); assert_eq!( synthetic_mount_target_paths(&args), - vec![workspace.join(".codex"), dot_git, workspace.join(".agents")], + vec![ + workspace.join(".codex"), + dot_git, + workspace.join(".agents"), + workspace.join(".aws"), + ], ); assert!( protected_create_target_paths(&args).is_empty(), @@ -2153,7 +2160,12 @@ mod tests { assert_empty_directory_mounted_read_only(&args.args, &workspace.join(".codex")); assert_eq!( synthetic_mount_target_paths(&args), - vec![workspace.join(".codex"), dot_git, workspace.join(".agents")], + vec![ + workspace.join(".codex"), + dot_git, + workspace.join(".agents"), + workspace.join(".aws"), + ], ); assert!( protected_create_target_paths(&args).is_empty(), @@ -2313,12 +2325,14 @@ mod tests { assert_eq!( synthetic_mount_target_paths(&args), vec![ + PathBuf::from("/.aws"), PathBuf::from("/.git"), PathBuf::from("/.agents"), PathBuf::from("/.codex"), PathBuf::from("/dev/.git"), PathBuf::from("/dev/.agents"), PathBuf::from("/dev/.codex"), + PathBuf::from("/dev/.aws"), ] ); let dev_mounts = args @@ -2542,7 +2556,7 @@ mod tests { FileSystemSandboxEntry::new(docs.clone().into(), FileSystemAccessMode::Read), FileSystemSandboxEntry::new(docs_public.clone().into(), FileSystemAccessMode::Write), ]; - for name in [".git", ".agents", ".codex"] { + for name in [".git", ".agents", ".codex", ".aws"] { entries.push(FileSystemSandboxEntry::skip_missing_path( docs_public.join(name).into(), FileSystemAccessMode::Read, @@ -2572,7 +2586,7 @@ mod tests { "expected read-only parent remount before nested writable bind: {:#?}", args.args ); - for name in [".git", ".agents", ".codex"] { + for name in [".git", ".agents", ".codex", ".aws"] { let metadata_path = path_to_string(docs_public.join(name).as_path()); let mount_indices = args .args diff --git a/codex-rs/linux-sandbox/tests/suite/landlock.rs b/codex-rs/linux-sandbox/tests/suite/landlock.rs index 26edc65de3..9666af8f3a 100644 --- a/codex-rs/linux-sandbox/tests/suite/landlock.rs +++ b/codex-rs/linux-sandbox/tests/suite/landlock.rs @@ -896,55 +896,50 @@ async fn sandbox_blocks_nc() { assert_network_blocked(&["nc", "-z", "127.0.0.1", "80"]).await; } +#[test_case::test_case(".git", "config")] +#[test_case::test_case(".codex", "config.toml")] +#[test_case::test_case(".aws", "config")] #[tokio::test] -async fn sandbox_blocks_git_and_codex_writes_inside_writable_root() { +async fn sandbox_blocks_metadata_writes_inside_writable_root(name: &str, config: &str) { if should_skip_bwrap_tests().await { eprintln!("skipping bwrap test: bwrap sandbox prerequisites are unavailable"); return; } - let tmpdir = tempfile::tempdir().expect("tempdir"); - let dot_git = tmpdir.path().join(".git"); - let dot_codex = tmpdir.path().join(".codex"); - std::fs::create_dir_all(&dot_git).expect("create .git"); - std::fs::create_dir_all(&dot_codex).expect("create .codex"); - - let git_target = dot_git.join("config"); - let codex_target = dot_codex.join("config.toml"); - - let git_output = expect_denied( - run_cmd_result_with_writable_roots( - &[ - "bash", - "-lc", - &format!("echo denied > {}", git_target.to_string_lossy()), - ], - &[tmpdir.path().to_path_buf()], - LONG_TIMEOUT_MS, - /*use_legacy_landlock*/ false, - /*network_access*/ true, - ) - .await, - ".git write should be denied under bubblewrap", + let home = tempfile::tempdir().expect("tempdir"); + let metadata = home.path().join(name); + std::fs::create_dir(&metadata).expect("create protected directory"); + let target = metadata.join(config); + std::fs::write(&target, "original").expect("write protected config"); + let output = run_cmd_result_with_writable_roots( + &[ + "/bin/sh", + "-c", + r#"set -eu +writable_home="$1" +printf permitted > "$writable_home/allowed" +if (printf changed > "$2") 2>/dev/null; then exit 1; fi +printf protected"#, + "metadata-test", + home.path().to_str().expect("UTF-8 home"), + target.to_str().expect("UTF-8 config"), + ], + &[home.path().to_path_buf()], + LONG_TIMEOUT_MS, + /*use_legacy_landlock*/ false, + /*network_access*/ true, + ) + .await + .expect("sandbox should run with a separate writable home"); + assert_eq!( + (output.exit_code, output.stdout.text, output.stderr.text), + (0, "protected".to_string(), String::new()) ); - - let codex_output = expect_denied( - run_cmd_result_with_writable_roots( - &[ - "bash", - "-lc", - &format!("echo denied > {}", codex_target.to_string_lossy()), - ], - &[tmpdir.path().to_path_buf()], - LONG_TIMEOUT_MS, - /*use_legacy_landlock*/ false, - /*network_access*/ true, - ) - .await, - ".codex write should be denied under bubblewrap", + assert_eq!(std::fs::read_to_string(target).unwrap(), "original"); + assert_eq!( + std::fs::read_to_string(home.path().join("allowed")).unwrap(), + "permitted" ); - assert_ne!(git_output.exit_code, 0); - assert_ne!(codex_output.exit_code, 0); } #[tokio::test] diff --git a/codex-rs/linux-sandbox/tests/suite/nested_metadata_tests.rs b/codex-rs/linux-sandbox/tests/suite/nested_metadata_tests.rs index 6cd9392307..6d2ef25558 100644 --- a/codex-rs/linux-sandbox/tests/suite/nested_metadata_tests.rs +++ b/codex-rs/linux-sandbox/tests/suite/nested_metadata_tests.rs @@ -61,7 +61,7 @@ async fn sandbox_starts_with_nested_writable_metadata(layout: RootLayout, relati ]; // Remote clients expand generated workspace metadata rules to concrete // paths, including paths that do not yet exist on the executor. - for name in [".git", ".agents", ".codex"] { + for name in [".git", ".agents", ".codex", ".aws"] { entries.push(FileSystemSandboxEntry::skip_missing_path( visualization.join(name).into(), FileSystemAccessMode::Read, @@ -86,7 +86,7 @@ fi if (touch "$workspace/.codex/forbidden.txt") 2>/dev/null; then exit 11 fi -for name in .git .agents .codex; do +for name in .git .agents .codex .aws; do test -d "$visualization/$name" if (touch "$visualization/$name/forbidden.txt") 2>/dev/null; then exit 12 @@ -125,7 +125,7 @@ printf nested-metadata-protected ] { assert_eq!(std::fs::read_to_string(path).expect("read file"), expected); } - for name in [".git", ".agents", ".codex"] { + for name in [".git", ".agents", ".codex", ".aws"] { assert!( !visualization.join(name).exists(), "temporary {name} mountpoint should be cleaned up" diff --git a/codex-rs/mxc-sandbox/src/policy_tests.rs b/codex-rs/mxc-sandbox/src/policy_tests.rs index d9415f82d9..4ffa1e7409 100644 --- a/codex-rs/mxc-sandbox/src/policy_tests.rs +++ b/codex-rs/mxc-sandbox/src/policy_tests.rs @@ -236,10 +236,12 @@ fn native_grants_preserve_denies_and_read_only_carveouts() -> Result<()> { let request = build_request(&command(&profile, root), root, Vec::new(), &[], &[])?; let mut expected_read = [ root.join(".agents"), + root.join(".aws"), root.join(".codex"), root.join(".git"), readonly, writable_child.join(".agents"), + writable_child.join(".aws"), writable_child.join(".codex"), writable_child.join(".git"), ]; @@ -296,6 +298,7 @@ fn volume_expansion_does_not_turn_read_only_child_writable() -> Result<()> { request.policy.readonly_paths, vec![ root.join(".agents").to_str().unwrap(), + root.join(".aws").to_str().unwrap(), root.join(".codex").to_str().unwrap(), root.join(".git").to_str().unwrap(), readonly.to_str().unwrap() @@ -609,7 +612,7 @@ fn root_deny_keeps_only_narrow_explicit_grants() -> Result<()> { FileSystemAccessMode::Read => (Vec::new(), allowed), FileSystemAccessMode::Write => ( allowed, - [".agents", ".codex", ".git"] + [".agents", ".aws", ".codex", ".git"] .map(|name| child.join(name).to_str().unwrap().to_owned()) .to_vec(), ), @@ -782,11 +785,13 @@ fn symbolic_root_preserves_equal_path_precedence_and_denies() -> Result<()> { reads_in_volumes, volumes[..2] .iter() - .flat_map(|volume| [".agents", ".codex", ".git"].map(|name| volume - .join(name) - .to_str() - .unwrap() - .to_owned())) + .flat_map( + |volume| [".agents", ".aws", ".codex", ".git"].map(|name| volume + .join(name) + .to_str() + .unwrap() + .to_owned()) + ) .collect::>() ); assert_eq!( diff --git a/codex-rs/protocol/src/permission_profile_intersection_tests.rs b/codex-rs/protocol/src/permission_profile_intersection_tests.rs index f91813e306..de9351e22d 100644 --- a/codex-rs/protocol/src/permission_profile_intersection_tests.rs +++ b/codex-rs/protocol/src/permission_profile_intersection_tests.rs @@ -136,7 +136,7 @@ fn effective_workspace_intersection_preserves_network_metadata_and_temp() { ); assert_eq!(result.network_sandbox_policy(), Restricted); assert!(policy.entries.contains(&special(Tmpdir, Write))); - for name in [".git", ".agents", ".codex"] { + for name in [".git", ".agents", ".codex", ".aws"] { let protected = project.join(name); assert!(!policy.can_write_local_path_with_cwd(protected.as_path(), root.as_path())); assert!(policy.entries.contains(&skipped(protected.into(), Read))); diff --git a/codex-rs/protocol/src/permissions.rs b/codex-rs/protocol/src/permissions.rs index c8bc16afe4..140090f5ef 100644 --- a/codex-rs/protocol/src/permissions.rs +++ b/codex-rs/protocol/src/permissions.rs @@ -40,12 +40,14 @@ pub use windows_glob::windows_deny_read_glob_scan; const PROTECTED_METADATA_GIT_PATH_NAME: &str = ".git"; const PROTECTED_METADATA_AGENTS_PATH_NAME: &str = ".agents"; const PROTECTED_METADATA_CODEX_PATH_NAME: &str = ".codex"; +const PROTECTED_METADATA_AWS_PATH_NAME: &str = ".aws"; /// Top-level workspace metadata paths that stay protected under writable roots. pub const PROTECTED_METADATA_PATH_NAMES: &[&str] = &[ PROTECTED_METADATA_GIT_PATH_NAME, PROTECTED_METADATA_AGENTS_PATH_NAME, PROTECTED_METADATA_CODEX_PATH_NAME, + PROTECTED_METADATA_AWS_PATH_NAME, ]; /// Returns true when a path basename is one of the protected workspace metadata names. @@ -858,6 +860,7 @@ impl FileSystemSandboxPolicy { append_default_read_only_project_root_subpath_if_no_explicit_rule(&mut entries, ".git"); append_default_read_only_project_root_subpath_if_no_explicit_rule(&mut entries, ".agents"); append_default_read_only_project_root_subpath_if_no_explicit_rule(&mut entries, ".codex"); + append_default_read_only_project_root_subpath_if_no_explicit_rule(&mut entries, ".aws"); for writable_root in writable_roots { for protected_path in default_read_only_subpaths_for_writable_root( writable_root, @@ -2377,6 +2380,12 @@ pub(crate) fn default_read_only_subpaths_for_writable_root( subpaths.push(top_level_codex); } + // AWS profiles can select credential helpers that the application executes. + let top_level_aws = writable_root.join(PROTECTED_METADATA_AWS_PATH_NAME); + if top_level_aws.as_path().is_dir() { + subpaths.push(top_level_aws); + } + dedup_absolute_paths(subpaths, /*normalize_effective_paths*/ false) } @@ -3363,6 +3372,12 @@ mod tests { }, FileSystemAccessMode::Read, ), + FileSystemSandboxEntry::skip_missing_path( + FileSystemPath::Special { + value: FileSystemSpecialPath::project_roots(Some(".aws".into())), + }, + FileSystemAccessMode::Read, + ), ]) ); } @@ -3390,52 +3405,55 @@ mod tests { #[cfg(unix)] #[test] - fn writable_roots_skip_default_dot_codex_when_explicit_user_rule_exists() { - let cwd = TempDir::new().expect("tempdir"); - let expected_root = AbsolutePathBuf::from_absolute_path( - cwd.path().canonicalize().expect("canonicalize cwd"), - ) - .expect("absolute canonical root"); - let explicit_dot_codex = expected_root.join(".codex"); + fn writable_roots_skip_default_metadata_when_explicit_user_rule_exists() { + for name in [".codex", ".aws"] { + let cwd = TempDir::new().expect("tempdir"); + let expected_root = AbsolutePathBuf::from_absolute_path( + cwd.path().canonicalize().expect("canonicalize cwd"), + ) + .expect("absolute canonical root"); + let explicit_metadata = expected_root.join(name); + fs::create_dir(&explicit_metadata).expect("create metadata directory"); - let policy = FileSystemSandboxPolicy::restricted(vec![ - FileSystemSandboxEntry { - path: FileSystemPath::Special { - value: FileSystemSpecialPath::project_roots(/*subpath*/ None), + let policy = FileSystemSandboxPolicy::restricted(vec![ + FileSystemSandboxEntry { + path: FileSystemPath::Special { + value: FileSystemSpecialPath::project_roots(/*subpath*/ None), + }, + access: FileSystemAccessMode::Write, + missing_path_behavior: None, }, - access: FileSystemAccessMode::Write, - missing_path_behavior: None, - }, - FileSystemSandboxEntry { - path: FileSystemPath::Path { - path: explicit_dot_codex.clone().into(), + FileSystemSandboxEntry { + path: FileSystemPath::Path { + path: explicit_metadata.clone().into(), + }, + access: FileSystemAccessMode::Write, + missing_path_behavior: None, }, - access: FileSystemAccessMode::Write, - missing_path_behavior: None, - }, - ]); + ]); - let writable_roots = policy.get_writable_roots_with_cwd(cwd.path()); - let workspace_root = writable_roots - .iter() - .find(|root| root.root == expected_root) - .expect("workspace writable root"); - assert!( - !workspace_root - .protected_metadata_names - .contains(&".codex".to_string()), - "explicit .codex rule should remove the metadata-name protection" - ); - assert!( - !workspace_root - .read_only_subpaths - .contains(&explicit_dot_codex), - "explicit .codex rule should win over the default protected carveout" - ); - assert!(policy.can_write_local_path_with_cwd( - explicit_dot_codex.join("config.toml").as_path(), - cwd.path() - )); + let writable_roots = policy.get_writable_roots_with_cwd(cwd.path()); + let workspace_root = writable_roots + .iter() + .find(|root| root.root == expected_root) + .expect("workspace writable root"); + assert!( + !workspace_root + .protected_metadata_names + .contains(&name.to_string()), + "explicit {name} rule should remove the metadata-name protection" + ); + assert!( + !workspace_root + .read_only_subpaths + .contains(&explicit_metadata), + "explicit {name} rule should win over the default protected carveout" + ); + assert!(policy.can_write_local_path_with_cwd( + explicit_metadata.join("config.toml").as_path(), + cwd.path() + )); + } } #[test] @@ -3444,6 +3462,7 @@ mod tests { let dot_git_config = cwd.path().join(".git").join("config"); let dot_agents_config = cwd.path().join(".agents").join("config"); let dot_codex_config = cwd.path().join(".codex").join("config.toml"); + let dot_aws_config = cwd.path().join(".aws").join("config"); let root = AbsolutePathBuf::from_absolute_path(cwd.path()).expect("absolute cwd"); let file_system_policy = FileSystemSandboxPolicy::restricted(vec![FileSystemSandboxEntry { @@ -3455,6 +3474,7 @@ mod tests { assert!(!file_system_policy.can_write_local_path_with_cwd(&dot_git_config, cwd.path())); assert!(!file_system_policy.can_write_local_path_with_cwd(&dot_agents_config, cwd.path())); assert!(!file_system_policy.can_write_local_path_with_cwd(&dot_codex_config, cwd.path())); + assert!(!file_system_policy.can_write_local_path_with_cwd(&dot_aws_config, cwd.path())); let writable_roots = file_system_policy.get_writable_roots_with_cwd(cwd.path()); assert_eq!(writable_roots.len(), 1); @@ -3464,11 +3484,13 @@ mod tests { ".git".to_string(), ".agents".to_string(), ".codex".to_string(), + ".aws".to_string(), ] ); assert!(!writable_roots[0].is_path_writable(&dot_git_config)); assert!(!writable_roots[0].is_path_writable(&dot_agents_config)); assert!(!writable_roots[0].is_path_writable(&dot_codex_config)); + assert!(!writable_roots[0].is_path_writable(&dot_aws_config)); } #[test] diff --git a/codex-rs/sandboxing/src/seatbelt_tests.rs b/codex-rs/sandboxing/src/seatbelt_tests.rs index 374205440b..88ae9b97c3 100644 --- a/codex-rs/sandboxing/src/seatbelt_tests.rs +++ b/codex-rs/sandboxing/src/seatbelt_tests.rs @@ -1980,6 +1980,13 @@ fn create_seatbelt_args_with_read_only_git_and_codex_subpaths() { .join(".agents") .display() ), + format!( + "-DWRITABLE_ROOT_0_EXCLUDED_3={}", + cwd.canonicalize() + .expect("canonicalize cwd") + .join(".aws") + .display() + ), format!( "-DWRITABLE_ROOT_1={}", vulnerable_root_canonical.to_string_lossy() @@ -2542,53 +2549,145 @@ fn seatbelt_protects_writable_root_created_as_directory() { ); } +#[cfg(target_os = "macos")] +#[test] +fn seatbelt_protects_aws_in_extra_writable_home() { + for existing in [true, false] { + let tmp = TempDir::new().expect("tempdir"); + let home = tmp.path().join("home"); + let workspace = tmp.path().join("workspace"); + let aws = home.join(".aws"); + let config = aws.join("config"); + let probe = home.join("probe"); + fs::create_dir_all(&home).expect("create home"); + fs::create_dir_all(&workspace).expect("create workspace"); + fs::write(home.join("replacement"), "replacement").expect("write replacement"); + fs::create_dir(home.join("replacement-dir")).expect("create replacement directory"); + if existing { + fs::create_dir(&aws).expect("create .aws"); + fs::write(&config, "original").expect("write AWS config"); + } + let policy = SandboxPolicy::WorkspaceWrite { + writable_roots: vec![home.as_path().try_into().expect("absolute home")], + network_access: false, + exclude_tmpdir_env_var: true, + exclude_slash_tmp: true, + }; + let attempts: &[&str] = if existing { + &[ + r#"printf changed > "$HOME/.aws/config""#, + r#"/bin/rm "$HOME/.aws/config""#, + r#"/bin/mv "$HOME/.aws/config" "$HOME/renamed-config""#, + r#"/bin/mv "$HOME/replacement" "$HOME/.aws/config""#, + r#"/bin/mv "$HOME/.aws" "$HOME/renamed-aws""#, + ] + } else { + &[ + r#"/bin/mkdir "$HOME/.aws""#, + r#"/bin/mv "$HOME/replacement-dir" "$HOME/.aws""#, + r#"/bin/ln -s "$HOME/replacement-dir" "$HOME/.aws""#, + ] + }; + for attempt in attempts { + let args = create_seatbelt_command_args_for_legacy_policy( + vec![ + "/bin/sh".to_string(), + "-c".to_string(), + format!(r#"printf ran > "$HOME/probe" && {attempt}"#), + ], + &policy, + &workspace, + /*enforce_managed_network*/ false, + /*network*/ None, + ) + .expect("build seatbelt command"); + let output = Command::new(MACOS_PATH_TO_SEATBELT_EXECUTABLE) + .args(args) + .env("HOME", &home) + .current_dir(&workspace) + .output() + .expect("execute seatbelt command"); + assert_eq!( + fs::read_to_string(&probe).expect("read sibling probe"), + "ran" + ); + fs::remove_file(&probe).expect("reset sibling probe"); + let stderr = String::from_utf8_lossy(&output.stderr); + assert!( + !output.status.success() && stderr.contains("Operation not permitted"), + "expected Seatbelt denial for {attempt}: {stderr}" + ); + if existing { + assert_eq!( + fs::read_to_string(&config).expect("read AWS config"), + "original" + ); + } else { + assert!( + fs::symlink_metadata(&aws).is_err(), + ".aws should remain absent" + ); + } + } + } +} + #[cfg(target_os = "macos")] #[test] fn seatbelt_protects_resolved_target_of_symlinked_metadata_directory() { use std::os::unix::fs::symlink; - let tmp = TempDir::new().expect("tempdir"); - let writable_root = tmp.path().join("workspace"); - let actual_config = writable_root.join("actual-config"); - let dot_codex = writable_root.join(".codex"); - let config_toml = actual_config.join("config.toml"); - fs::create_dir_all(&actual_config).expect("create actual config directory"); - fs::write(&config_toml, "original").expect("write config"); - symlink(&actual_config, &dot_codex).expect("create .codex symlink"); - let policy = restricted_write_policy(&[writable_root.as_path()]); - let args = create_seatbelt_command_args(CreateSeatbeltCommandArgsParams { - command: vec![ - "/bin/sh".to_string(), - "-c".to_string(), - "printf escaped > \"$1\"".to_string(), - "sh".to_string(), - config_toml.display().to_string(), - ], - file_system_sandbox_policy: &policy, - network_sandbox_policy: NetworkSandboxPolicy::Restricted, - sandbox_policy_cwd: &writable_root, - enforce_managed_network: false, - managed_network: None, - environment_id: None, - network: None, - extra_allow_unix_sockets: &[], - }) - .expect("build seatbelt command"); + for (name, config_name) in [(".codex", "config.toml"), (".aws", "config")] { + let tmp = TempDir::new().expect("tempdir"); + let writable_root = tmp.path().join("workspace"); + let actual_config = writable_root.join("actual-config"); + let metadata_dir = writable_root.join(name); + let config = actual_config.join(config_name); + let probe = writable_root.join("probe"); + fs::create_dir_all(&actual_config).expect("create actual config directory"); + fs::write(&config, "original").expect("write config"); + symlink(&actual_config, &metadata_dir).expect("create metadata symlink"); + let policy = restricted_write_policy(&[writable_root.as_path()]); + let args = create_seatbelt_command_args(CreateSeatbeltCommandArgsParams { + command: vec![ + "/bin/sh".to_string(), + "-c".to_string(), + "printf ran > \"$2\" && printf escaped > \"$1\"".to_string(), + "sh".to_string(), + config.display().to_string(), + probe.display().to_string(), + ], + file_system_sandbox_policy: &policy, + network_sandbox_policy: NetworkSandboxPolicy::Restricted, + sandbox_policy_cwd: &writable_root, + enforce_managed_network: false, + managed_network: None, + environment_id: None, + network: None, + extra_allow_unix_sockets: &[], + }) + .expect("build seatbelt command"); - let output = Command::new(MACOS_PATH_TO_SEATBELT_EXECUTABLE) - .args(&args) - .current_dir(&writable_root) - .output() - .expect("execute seatbelt command"); + let output = Command::new(MACOS_PATH_TO_SEATBELT_EXECUTABLE) + .args(&args) + .current_dir(&writable_root) + .output() + .expect("execute seatbelt command"); - assert!( - !output.status.success(), - "resolved .codex target should remain read-only" - ); - assert_eq!( - fs::read_to_string(&config_toml).expect("read config"), - "original" - ); + assert_eq!( + fs::read_to_string(&probe).expect("read sibling probe"), + "ran" + ); + let stderr = String::from_utf8_lossy(&output.stderr); + assert!( + !output.status.success() && stderr.contains("Operation not permitted"), + "resolved {name} target should remain read-only: {stderr}" + ); + assert_eq!( + fs::read_to_string(&config).expect("read config"), + "original" + ); + } } #[test] diff --git a/codex-rs/windows-sandbox-rs/src/allow.rs b/codex-rs/windows-sandbox-rs/src/allow.rs index 5a10261d0c..4eb4ec2961 100644 --- a/codex-rs/windows-sandbox-rs/src/allow.rs +++ b/codex-rs/windows-sandbox-rs/src/allow.rs @@ -317,13 +317,15 @@ mod tests { } #[test] - fn denies_codex_and_agents_inside_writable_root() { + fn denies_codex_agents_and_aws_inside_writable_root() { let tmp = TempDir::new().expect("tempdir"); let command_cwd = tmp.path().join("workspace"); let codex_dir = command_cwd.join(".codex"); let agents_dir = command_cwd.join(".agents"); + let aws_dir = command_cwd.join(".aws"); let _ = fs::create_dir_all(&codex_dir); let _ = fs::create_dir_all(&agents_dir); + let _ = fs::create_dir_all(&aws_dir); let permission_profile = workspace_write_profile( &[], @@ -344,6 +346,7 @@ mod tests { let expected_deny: HashSet = [ dunce::canonicalize(&codex_dir).unwrap(), dunce::canonicalize(&agents_dir).unwrap(), + dunce::canonicalize(&aws_dir).unwrap(), ] .into_iter() .collect();