| 1 | use super::*; |
| 2 | |
| 3 | /// #4085: an inherited read-write extension is necessary but never sufficient |
| 4 | /// for WorkspaceWrite. Every extension-backed write must also match one |
| 5 | /// approved root and retain that root's protected-subpath exclusions. |
| 6 | #[test] |
| 7 | fn file_provider_extensions_intersect_each_workspace_root_and_exclusions() { |
| 8 | assert!( |
| 9 | is_available(), |
| 10 | "UNRUN: macOS sandbox-exec is unavailable; generated policy was not parsed" |
| 11 | ); |
| 12 | |
| 13 | let fixture = tempfile::tempdir().expect("create policy fixture"); |
| 14 | let workspace = fixture.path().join("workspace"); |
| 15 | let additional_root = fixture.path().join("additional-root"); |
| 16 | std::fs::create_dir_all(workspace.join(".codewhale")).expect("create protected workspace path"); |
| 17 | std::fs::create_dir_all(additional_root.join(".deepseek")) |
| 18 | .expect("create protected additional-root path"); |
| 19 | |
| 20 | let policy = SandboxPolicy::WorkspaceWrite { |
| 21 | writable_roots: vec![additional_root], |
| 22 | network_access: false, |
| 23 | exclude_tmpdir: true, |
| 24 | exclude_slash_tmp: true, |
| 25 | }; |
| 26 | let workspace_write = generate_policy(&policy, &workspace, &[]); |
| 27 | let read_only = generate_policy(&SandboxPolicy::read_only(), &workspace, &[]); |
| 28 | |
| 29 | for policy in [&workspace_write, &read_only] { |
| 30 | assert!(policy.contains("(version 1)")); |
| 31 | assert!(policy.contains("(deny default)")); |
| 32 | assert!(policy.contains("(allow file-read*)")); |
| 33 | assert!(policy.contains(r#"(allow file-read* (extension "com.apple.app-sandbox.read"))"#)); |
| 34 | assert!( |
| 35 | policy.contains(r#"(allow file-read* (extension "com.apple.app-sandbox.read-write"))"#) |
| 36 | ); |
| 37 | } |
| 38 | assert!(!workspace_write.contains("network-outbound")); |
| 39 | |
| 40 | for root_index in 0..=1 { |
| 41 | let expected = format!( |
| 42 | r#"(require-all (extension "com.apple.app-sandbox.read-write") (subpath (param "WRITABLE_ROOT_{root_index}")) (require-not (subpath (param "WRITABLE_ROOT_{root_index}_RO_0")))"# |
| 43 | ); |
| 44 | assert!( |
| 45 | workspace_write.contains(&expected), |
| 46 | "extension write must retain root {root_index} and its exclusion:\n{workspace_write}" |
| 47 | ); |
| 48 | } |
| 49 | let read_write_extension = r#"(extension "com.apple.app-sandbox.read-write")"#; |
| 50 | assert_eq!( |
| 51 | workspace_write.matches(read_write_extension).count(), |
| 52 | 3, |
| 53 | "one read rule plus exactly one root-scoped write rule per approved root" |
| 54 | ); |
| 55 | assert!(workspace_write.contains("file-write*")); |
| 56 | assert!(!workspace_write.lines().any(|line| { |
| 57 | line.trim() == r#"(allow file-write* (extension "com.apple.app-sandbox.read-write"))"# |
| 58 | })); |
| 59 | assert!(!read_only.contains(r#"file-write* (extension "com.apple.app-sandbox.read-write")"#)); |
| 60 | assert_eq!(read_only.matches(read_write_extension).count(), 1); |
| 61 | assert!(!read_only.contains("WRITABLE_ROOT")); |
| 62 | |
| 63 | let args = create_seatbelt_args( |
| 64 | vec!["/usr/bin/true".to_string()], |
| 65 | &policy, |
| 66 | &workspace, |
| 67 | &[], |
| 68 | None, |
| 69 | ); |
| 70 | let output = Command::new(SANDBOX_EXEC_PATH) |
| 71 | .args(args) |
| 72 | .current_dir(&workspace) |
| 73 | .output() |
| 74 | .expect("parse generated policy with sandbox-exec"); |
| 75 | assert!( |
| 76 | output.status.success(), |
| 77 | "sandbox-exec rejected generated intersection policy: {}", |
| 78 | String::from_utf8_lossy(&output.stderr) |
| 79 | ); |
| 80 | } |
| 81 | |
| 82 | /// Hermetic command/policy-shape coverage for the six operations reported in |
| 83 | /// #4085. Each operation gets a fresh fixture so an early failure cannot hide |
| 84 | /// later results. This is not physical File Provider acceptance. |
| 85 | #[test] |
| 86 | fn file_provider_synthetic_operations_are_independent_under_seatbelt() { |
| 87 | assert!( |
| 88 | is_available(), |
| 89 | "UNRUN: macOS sandbox-exec is unavailable; no synthetic operation evidence collected" |
| 90 | ); |
| 91 | |
| 92 | #[derive(Clone, Copy, Debug)] |
| 93 | enum Operation { |
| 94 | Mkdir, |
| 95 | Write, |
| 96 | Read, |
| 97 | Grep, |
| 98 | DeleteFile, |
| 99 | DeleteDirectory, |
| 100 | } |
| 101 | |
| 102 | let mut failures = Vec::new(); |
| 103 | for operation in [ |
| 104 | Operation::Mkdir, |
| 105 | Operation::Write, |
| 106 | Operation::Read, |
| 107 | Operation::Grep, |
| 108 | Operation::DeleteFile, |
| 109 | Operation::DeleteDirectory, |
| 110 | ] { |
| 111 | let fixture = tempfile::tempdir().expect("create independent operation fixture"); |
| 112 | let workspace = fixture |
| 113 | .path() |
| 114 | .join("Library/CloudStorage/TestProvider/Workspace"); |
| 115 | std::fs::create_dir_all(&workspace).expect("create synthetic CloudStorage workspace"); |
| 116 | let target = workspace.join("target"); |
| 117 | let source = workspace.join("source"); |
| 118 | |
| 119 | let command = match operation { |
| 120 | Operation::Mkdir => vec![ |
| 121 | "/bin/mkdir".to_string(), |
| 122 | target.to_string_lossy().into_owned(), |
| 123 | ], |
| 124 | Operation::Write => { |
| 125 | std::fs::write(&source, b"file-provider\n").expect("seed copy source"); |
| 126 | vec![ |
| 127 | "/bin/cp".to_string(), |
| 128 | source.to_string_lossy().into_owned(), |
| 129 | target.to_string_lossy().into_owned(), |
| 130 | ] |
| 131 | } |
| 132 | Operation::Read => { |
| 133 | std::fs::write(&target, b"file-provider\n").expect("seed read target"); |
| 134 | vec![ |
| 135 | "/bin/cat".to_string(), |
| 136 | target.to_string_lossy().into_owned(), |
| 137 | ] |
| 138 | } |
| 139 | Operation::Grep => { |
| 140 | std::fs::write(&target, b"file-provider\n").expect("seed grep target"); |
| 141 | vec![ |
| 142 | "/usr/bin/grep".to_string(), |
| 143 | "-q".to_string(), |
| 144 | "file-provider".to_string(), |
| 145 | target.to_string_lossy().into_owned(), |
| 146 | ] |
| 147 | } |
| 148 | Operation::DeleteFile => { |
| 149 | std::fs::write(&target, b"file-provider\n").expect("seed deletion target"); |
| 150 | vec!["/bin/rm".to_string(), target.to_string_lossy().into_owned()] |
| 151 | } |
| 152 | Operation::DeleteDirectory => { |
| 153 | std::fs::create_dir(&target).expect("seed directory deletion target"); |
| 154 | vec![ |
| 155 | "/bin/rmdir".to_string(), |
| 156 | target.to_string_lossy().into_owned(), |
| 157 | ] |
| 158 | } |
| 159 | }; |
| 160 | |
| 161 | let args = create_seatbelt_args(command, &SandboxPolicy::default(), &workspace, &[], None); |
| 162 | let output = Command::new(SANDBOX_EXEC_PATH) |
| 163 | .args(args) |
| 164 | .current_dir(&workspace) |
| 165 | .output() |
| 166 | .expect("execute sandboxed operation"); |
| 167 | |
| 168 | let effect_matches = match operation { |
| 169 | Operation::Mkdir => target.is_dir(), |
| 170 | Operation::Write => { |
| 171 | matches!(std::fs::read(&target), Ok(bytes) if bytes == b"file-provider\n") |
| 172 | } |
| 173 | Operation::Read => output.stdout == b"file-provider\n", |
| 174 | Operation::Grep => true, |
| 175 | Operation::DeleteFile | Operation::DeleteDirectory => !target.exists(), |
| 176 | }; |
| 177 | if !output.status.success() || !effect_matches { |
| 178 | failures.push(format!( |
| 179 | "{operation:?}: status={:?}, stderr={}", |
| 180 | output.status.code(), |
| 181 | String::from_utf8_lossy(&output.stderr) |
| 182 | )); |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | assert!( |
| 187 | failures.is_empty(), |
| 188 | "independent sandbox operations failed:\n{}", |
| 189 | failures.join("\n") |
| 190 | ); |
| 191 | } |
| 192 | |
| 193 | /// S1 (#5568): the opt-in read deny-list must actually stop reads that the |
| 194 | /// full-disk-read posture would otherwise allow — live, with sandbox-exec — |
| 195 | /// and an empty list must leave the generated profile byte-identical. |
| 196 | #[test] |
| 197 | fn denied_read_subpaths_block_reads_under_every_sandboxed_posture() { |
| 198 | assert!( |
| 199 | is_available(), |
| 200 | "UNRUN: macOS sandbox-exec is unavailable; no deny-list evidence collected" |
| 201 | ); |
| 202 | |
| 203 | let secret_dir = tempfile::tempdir().expect("secret dir"); |
| 204 | let secret_file = secret_dir.path().join("id_ed25519"); |
| 205 | std::fs::write(&secret_file, "PRIVATE KEY MATERIAL").expect("write secret"); |
| 206 | let workspace = tempfile::tempdir().expect("workspace"); |
| 207 | let read_cmd = vec![ |
| 208 | "/bin/cat".to_string(), |
| 209 | secret_file.to_string_lossy().into_owned(), |
| 210 | ]; |
| 211 | |
| 212 | for policy in [SandboxPolicy::read_only(), SandboxPolicy::default()] { |
| 213 | // Without the deny-list the read succeeds (full-disk read posture). |
| 214 | let open_args = |
| 215 | create_seatbelt_args(read_cmd.clone(), &policy, workspace.path(), &[], None); |
| 216 | let open = Command::new(SANDBOX_EXEC_PATH) |
| 217 | .args(open_args) |
| 218 | .current_dir(workspace.path()) |
| 219 | .output() |
| 220 | .expect("run un-denied read"); |
| 221 | assert!( |
| 222 | open.status.success(), |
| 223 | "baseline read should pass under {policy:?}: {}", |
| 224 | String::from_utf8_lossy(&open.stderr) |
| 225 | ); |
| 226 | |
| 227 | // With the parent directory denied, the same read must fail. The |
| 228 | // rule set comes from the manager's setter, which canonicalizes: |
| 229 | // macOS tempdirs live behind the /var -> /private/var symlink, and |
| 230 | // Seatbelt matches the kernel-resolved path, so a literal-only rule |
| 231 | // silently never fires (caught live by this very test). |
| 232 | let mut manager = crate::SandboxManager::new(); |
| 233 | manager.set_denied_read_subpaths(vec![secret_dir.path().to_path_buf()]); |
| 234 | let denied_args = create_seatbelt_args( |
| 235 | read_cmd.clone(), |
| 236 | &policy, |
| 237 | workspace.path(), |
| 238 | manager.denied_read_subpaths_for_test(), |
| 239 | None, |
| 240 | ); |
| 241 | let denied = Command::new(SANDBOX_EXEC_PATH) |
| 242 | .args(denied_args) |
| 243 | .current_dir(workspace.path()) |
| 244 | .output() |
| 245 | .expect("run denied read"); |
| 246 | assert!( |
| 247 | !denied.status.success(), |
| 248 | "deny-listed read must fail under {policy:?}, stdout: {}", |
| 249 | String::from_utf8_lossy(&denied.stdout) |
| 250 | ); |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | #[test] |
| 255 | fn private_tmpdir_rule_precedes_deny_list_and_blocks_root_unlink() { |
| 256 | let workspace = tempfile::tempdir().expect("workspace tempdir"); |
| 257 | let secret_dir = workspace.path().join("secret"); |
| 258 | let scratch_dir = tempfile::tempdir().expect("scratch tempdir"); |
| 259 | let scratch = scratch_dir |
| 260 | .path() |
| 261 | .canonicalize() |
| 262 | .unwrap_or_else(|_| scratch_dir.path().to_path_buf()); |
| 263 | let args = create_seatbelt_args( |
| 264 | vec![ |
| 265 | "/bin/sh".to_string(), |
| 266 | "-c".to_string(), |
| 267 | "echo ok > \"$1/f\" && ! rmdir \"$1\"".to_string(), |
| 268 | "_".to_string(), |
| 269 | scratch.to_string_lossy().into_owned(), |
| 270 | ], |
| 271 | &SandboxPolicy::read_only(), |
| 272 | workspace.path(), |
| 273 | std::slice::from_ref(&secret_dir), |
| 274 | Some(&scratch), |
| 275 | ); |
| 276 | assert_eq!(args[0], "-p"); |
| 277 | let policy_text = &args[1]; |
| 278 | let allow_pos = policy_text |
| 279 | .find("(allow file-write* (subpath (param \"PRIVATE_TMPDIR\")))") |
| 280 | .expect("allow rule"); |
| 281 | let unlink_pos = policy_text |
| 282 | .find("(deny file-write-unlink (literal (param \"PRIVATE_TMPDIR\")))") |
| 283 | .expect("unlink deny rule"); |
| 284 | let deny_read_pos = policy_text |
| 285 | .find("(deny file-read* (subpath") |
| 286 | .expect("read deny-list"); |
| 287 | assert!(allow_pos < unlink_pos && unlink_pos < deny_read_pos); |
| 288 | |
| 289 | let output = Command::new(SANDBOX_EXEC_PATH) |
| 290 | .args(args) |
| 291 | .current_dir(workspace.path()) |
| 292 | .output() |
| 293 | .expect("run seatbelt"); |
| 294 | assert!( |
| 295 | output.status.success(), |
| 296 | "{}", |
| 297 | String::from_utf8_lossy(&output.stderr) |
| 298 | ); |
| 299 | } |
| 300 |