|
1 | 1 | use codex_protocol::models::PermissionProfile; |
2 | | -use codex_protocol::protocol::NetworkAccess; |
3 | | -use codex_protocol::protocol::SandboxPolicy; |
| 2 | +use codex_protocol::permissions::FileSystemSandboxPolicy; |
| 3 | +use codex_protocol::permissions::NetworkSandboxPolicy; |
4 | 4 | use std::path::Path; |
5 | 5 |
|
6 | | -pub fn summarize_sandbox_policy(sandbox_policy: &SandboxPolicy) -> String { |
7 | | - match sandbox_policy { |
8 | | - SandboxPolicy::DangerFullAccess => "danger-full-access".to_string(), |
9 | | - SandboxPolicy::ReadOnly { network_access, .. } => { |
10 | | - let mut summary = "read-only".to_string(); |
11 | | - if *network_access { |
12 | | - summary.push_str(" (network access enabled)"); |
13 | | - } |
14 | | - summary |
15 | | - } |
16 | | - SandboxPolicy::ExternalSandbox { network_access } => { |
17 | | - let mut summary = "external-sandbox".to_string(); |
18 | | - if matches!(network_access, NetworkAccess::Enabled) { |
19 | | - summary.push_str(" (network access enabled)"); |
20 | | - } |
21 | | - summary |
| 6 | +pub fn summarize_permission_profile(permission_profile: &PermissionProfile, cwd: &Path) -> String { |
| 7 | + match permission_profile { |
| 8 | + PermissionProfile::Disabled => "danger-full-access".to_string(), |
| 9 | + PermissionProfile::External { network } => { |
| 10 | + summary_with_network("external-sandbox", network.is_enabled()) |
22 | 11 | } |
23 | | - SandboxPolicy::WorkspaceWrite { |
24 | | - writable_roots, |
25 | | - network_access, |
26 | | - exclude_tmpdir_env_var, |
27 | | - exclude_slash_tmp, |
28 | | - } => { |
29 | | - let mut summary = "workspace-write".to_string(); |
| 12 | + PermissionProfile::Managed { |
| 13 | + file_system, |
| 14 | + network, |
| 15 | + } => summarize_managed_profile(&file_system.to_sandbox_policy(), *network, cwd), |
| 16 | + } |
| 17 | +} |
30 | 18 |
|
31 | | - let mut writable_entries = Vec::<String>::new(); |
32 | | - writable_entries.push("workdir".to_string()); |
33 | | - if !*exclude_slash_tmp { |
34 | | - writable_entries.push("/tmp".to_string()); |
35 | | - } |
36 | | - if !*exclude_tmpdir_env_var { |
37 | | - writable_entries.push("$TMPDIR".to_string()); |
38 | | - } |
39 | | - writable_entries.extend( |
40 | | - writable_roots |
41 | | - .iter() |
42 | | - .map(|p| p.to_string_lossy().to_string()), |
43 | | - ); |
| 19 | +fn summarize_managed_profile( |
| 20 | + file_system: &FileSystemSandboxPolicy, |
| 21 | + network: NetworkSandboxPolicy, |
| 22 | + cwd: &Path, |
| 23 | +) -> String { |
| 24 | + let network_enabled = network.is_enabled(); |
| 25 | + if file_system.has_full_disk_write_access() { |
| 26 | + if network_enabled { |
| 27 | + return "danger-full-access".to_string(); |
| 28 | + } |
| 29 | + return custom_summary(network_enabled); |
| 30 | + } |
44 | 31 |
|
45 | | - summary.push_str(&format!(" [{}]", writable_entries.join(", "))); |
46 | | - if *network_access { |
47 | | - summary.push_str(" (network access enabled)"); |
48 | | - } |
49 | | - summary |
| 32 | + let writable_roots = file_system.get_writable_roots_with_cwd(cwd); |
| 33 | + if writable_roots.is_empty() { |
| 34 | + if file_system.has_full_disk_read_access() { |
| 35 | + return summary_with_network("read-only", network_enabled); |
50 | 36 | } |
| 37 | + return custom_summary(network_enabled); |
51 | 38 | } |
| 39 | + if !file_system.can_write_path_with_cwd(cwd, cwd) { |
| 40 | + return custom_summary(network_enabled); |
| 41 | + } |
| 42 | + |
| 43 | + let writable_entries = writable_roots |
| 44 | + .iter() |
| 45 | + .map(|root| writable_root_display(root.root.as_path(), cwd)) |
| 46 | + .collect::<Vec<_>>(); |
| 47 | + summary_with_network( |
| 48 | + &format!("workspace-write [{}]", writable_entries.join(", ")), |
| 49 | + network_enabled, |
| 50 | + ) |
52 | 51 | } |
53 | 52 |
|
54 | | -pub fn summarize_permission_profile(permission_profile: &PermissionProfile, cwd: &Path) -> String { |
55 | | - match permission_profile.to_legacy_sandbox_policy(cwd) { |
56 | | - Ok(policy) => summarize_sandbox_policy(&policy), |
57 | | - Err(_) => { |
58 | | - if permission_profile.network_sandbox_policy().is_enabled() { |
59 | | - "custom permissions (network access enabled)".to_string() |
60 | | - } else { |
61 | | - "custom permissions".to_string() |
62 | | - } |
63 | | - } |
| 53 | +fn writable_root_display(root: &Path, cwd: &Path) -> String { |
| 54 | + if root == cwd { |
| 55 | + return "workdir".to_string(); |
| 56 | + } |
| 57 | + if cfg!(unix) && root == Path::new("/tmp") { |
| 58 | + return "/tmp".to_string(); |
| 59 | + } |
| 60 | + if root == std::env::temp_dir() { |
| 61 | + return "$TMPDIR".to_string(); |
| 62 | + } |
| 63 | + root.display().to_string() |
| 64 | +} |
| 65 | + |
| 66 | +fn summary_with_network(base: &str, network_enabled: bool) -> String { |
| 67 | + if network_enabled { |
| 68 | + format!("{base} (network access enabled)") |
| 69 | + } else { |
| 70 | + base.to_string() |
64 | 71 | } |
65 | 72 | } |
66 | 73 |
|
| 74 | +fn custom_summary(network_enabled: bool) -> String { |
| 75 | + summary_with_network("custom permissions", network_enabled) |
| 76 | +} |
| 77 | + |
67 | 78 | #[cfg(test)] |
68 | 79 | mod tests { |
69 | 80 | use super::*; |
| 81 | + use codex_protocol::permissions::FileSystemAccessMode; |
| 82 | + use codex_protocol::permissions::FileSystemPath; |
| 83 | + use codex_protocol::permissions::FileSystemSandboxEntry; |
| 84 | + use codex_protocol::permissions::FileSystemSpecialPath; |
70 | 85 | use codex_utils_absolute_path::AbsolutePathBuf; |
71 | 86 | use pretty_assertions::assert_eq; |
72 | 87 |
|
73 | 88 | #[test] |
74 | 89 | fn summarizes_external_sandbox_without_network_access_suffix() { |
75 | | - let summary = summarize_sandbox_policy(&SandboxPolicy::ExternalSandbox { |
76 | | - network_access: NetworkAccess::Restricted, |
77 | | - }); |
| 90 | + let summary = summarize_permission_profile( |
| 91 | + &PermissionProfile::External { |
| 92 | + network: NetworkSandboxPolicy::Restricted, |
| 93 | + }, |
| 94 | + Path::new("/repo"), |
| 95 | + ); |
78 | 96 | assert_eq!(summary, "external-sandbox"); |
79 | 97 | } |
80 | 98 |
|
81 | 99 | #[test] |
82 | 100 | fn summarizes_external_sandbox_with_enabled_network() { |
83 | | - let summary = summarize_sandbox_policy(&SandboxPolicy::ExternalSandbox { |
84 | | - network_access: NetworkAccess::Enabled, |
85 | | - }); |
| 101 | + let summary = summarize_permission_profile( |
| 102 | + &PermissionProfile::External { |
| 103 | + network: NetworkSandboxPolicy::Enabled, |
| 104 | + }, |
| 105 | + Path::new("/repo"), |
| 106 | + ); |
86 | 107 | assert_eq!(summary, "external-sandbox (network access enabled)"); |
87 | 108 | } |
88 | 109 |
|
89 | 110 | #[test] |
90 | 111 | fn summarizes_read_only_with_enabled_network() { |
91 | | - let summary = summarize_sandbox_policy(&SandboxPolicy::ReadOnly { |
92 | | - network_access: true, |
93 | | - }); |
| 112 | + let file_system = FileSystemSandboxPolicy::restricted(vec![FileSystemSandboxEntry { |
| 113 | + path: FileSystemPath::Special { |
| 114 | + value: FileSystemSpecialPath::Root, |
| 115 | + }, |
| 116 | + access: FileSystemAccessMode::Read, |
| 117 | + }]); |
| 118 | + let profile = PermissionProfile::from_runtime_permissions( |
| 119 | + &file_system, |
| 120 | + NetworkSandboxPolicy::Enabled, |
| 121 | + ); |
| 122 | + let summary = summarize_permission_profile(&profile, Path::new("/repo")); |
94 | 123 | assert_eq!(summary, "read-only (network access enabled)"); |
95 | 124 | } |
96 | 125 |
|
| 126 | + #[test] |
| 127 | + fn unrestricted_filesystem_without_network_is_custom_permissions() { |
| 128 | + let profile = PermissionProfile::from_runtime_permissions( |
| 129 | + &FileSystemSandboxPolicy::unrestricted(), |
| 130 | + NetworkSandboxPolicy::Restricted, |
| 131 | + ); |
| 132 | + let summary = summarize_permission_profile(&profile, Path::new("/repo")); |
| 133 | + assert_eq!(summary, "custom permissions"); |
| 134 | + } |
| 135 | + |
| 136 | + #[test] |
| 137 | + fn explicit_writable_root_outside_cwd_is_custom_permissions() { |
| 138 | + let writable_root = AbsolutePathBuf::try_from(if cfg!(windows) { |
| 139 | + "C:\\outside" |
| 140 | + } else { |
| 141 | + "/outside" |
| 142 | + }) |
| 143 | + .unwrap(); |
| 144 | + let file_system = FileSystemSandboxPolicy::restricted(vec![ |
| 145 | + FileSystemSandboxEntry { |
| 146 | + path: FileSystemPath::Special { |
| 147 | + value: FileSystemSpecialPath::Root, |
| 148 | + }, |
| 149 | + access: FileSystemAccessMode::Read, |
| 150 | + }, |
| 151 | + FileSystemSandboxEntry { |
| 152 | + path: FileSystemPath::Path { |
| 153 | + path: writable_root, |
| 154 | + }, |
| 155 | + access: FileSystemAccessMode::Write, |
| 156 | + }, |
| 157 | + ]); |
| 158 | + let profile = PermissionProfile::from_runtime_permissions( |
| 159 | + &file_system, |
| 160 | + NetworkSandboxPolicy::Restricted, |
| 161 | + ); |
| 162 | + let cwd = if cfg!(windows) { "C:\\repo" } else { "/repo" }; |
| 163 | + let summary = summarize_permission_profile(&profile, Path::new(cwd)); |
| 164 | + assert_eq!(summary, "custom permissions"); |
| 165 | + } |
| 166 | + |
97 | 167 | #[test] |
98 | 168 | fn workspace_write_summary_still_includes_network_access() { |
99 | 169 | let root = if cfg!(windows) { "C:\\repo" } else { "/repo" }; |
100 | 170 | let writable_root = AbsolutePathBuf::try_from(root).unwrap(); |
101 | | - let summary = summarize_sandbox_policy(&SandboxPolicy::WorkspaceWrite { |
102 | | - writable_roots: vec![writable_root.clone()], |
103 | | - network_access: true, |
104 | | - exclude_tmpdir_env_var: true, |
105 | | - exclude_slash_tmp: true, |
106 | | - }); |
| 171 | + let cwd = if cfg!(windows) { |
| 172 | + "C:\\workdir" |
| 173 | + } else { |
| 174 | + "/workdir" |
| 175 | + }; |
| 176 | + let profile = PermissionProfile::workspace_write_with( |
| 177 | + std::slice::from_ref(&writable_root), |
| 178 | + NetworkSandboxPolicy::Enabled, |
| 179 | + /*exclude_tmpdir_env_var*/ true, |
| 180 | + /*exclude_slash_tmp*/ true, |
| 181 | + ); |
| 182 | + let summary = summarize_permission_profile(&profile, Path::new(cwd)); |
107 | 183 | assert_eq!( |
108 | 184 | summary, |
109 | 185 | format!( |
|
0 commit comments