feat: allow limited git writes in workspace sandbox

Co-authored-by: Codex noreply@openai.com
This commit is contained in:
viyatb-oai
2026-04-24 09:34:27 -07:00
committed by David Wiesen
co-authored by Codex noreply@openai.com
parent a9c111da54
commit d2c33a0d88
58 changed files with 622 additions and 33 deletions
+6
View File
@@ -89,6 +89,12 @@ codex --sandbox danger-full-access
The same setting can be persisted in `~/.codex/config.toml` via the top-level `sandbox_mode = "MODE"` key, e.g. `sandbox_mode = "workspace-write"`.
In `workspace-write`, Codex also includes `~/.codex/memories` in its writable roots so memory maintenance does not require an extra approval.
Set the following to allow Git metadata writes under writable roots while keeping Git config and hooks read-only:
```toml
[sandbox_workspace_write]
allow_limited_git_writes = true
```
## Code Organization
@@ -3039,6 +3039,10 @@
},
{
"properties": {
"allowLimitedGitWrites": {
"default": false,
"type": "boolean"
},
"excludeSlashTmp": {
"default": false,
"type": "boolean"
@@ -13465,6 +13465,10 @@
},
{
"properties": {
"allowLimitedGitWrites": {
"default": false,
"type": "boolean"
},
"excludeSlashTmp": {
"default": false,
"type": "boolean"
@@ -13512,6 +13516,10 @@
},
"SandboxWorkspaceWrite": {
"properties": {
"allow_limited_git_writes": {
"default": false,
"type": "boolean"
},
"exclude_slash_tmp": {
"default": false,
"type": "boolean"
@@ -10180,6 +10180,10 @@
},
{
"properties": {
"allowLimitedGitWrites": {
"default": false,
"type": "boolean"
},
"excludeSlashTmp": {
"default": false,
"type": "boolean"
@@ -10227,6 +10231,10 @@
},
"SandboxWorkspaceWrite": {
"properties": {
"allow_limited_git_writes": {
"default": false,
"type": "boolean"
},
"exclude_slash_tmp": {
"default": false,
"type": "boolean"
@@ -424,6 +424,10 @@
},
{
"properties": {
"allowLimitedGitWrites": {
"default": false,
"type": "boolean"
},
"excludeSlashTmp": {
"default": false,
"type": "boolean"
@@ -732,6 +732,10 @@
},
"SandboxWorkspaceWrite": {
"properties": {
"allow_limited_git_writes": {
"default": false,
"type": "boolean"
},
"exclude_slash_tmp": {
"default": false,
"type": "boolean"
@@ -1090,6 +1090,10 @@
},
{
"properties": {
"allowLimitedGitWrites": {
"default": false,
"type": "boolean"
},
"excludeSlashTmp": {
"default": false,
"type": "boolean"
@@ -1090,6 +1090,10 @@
},
{
"properties": {
"allowLimitedGitWrites": {
"default": false,
"type": "boolean"
},
"excludeSlashTmp": {
"default": false,
"type": "boolean"
@@ -1090,6 +1090,10 @@
},
{
"properties": {
"allowLimitedGitWrites": {
"default": false,
"type": "boolean"
},
"excludeSlashTmp": {
"default": false,
"type": "boolean"
@@ -544,6 +544,10 @@
},
{
"properties": {
"allowLimitedGitWrites": {
"default": false,
"type": "boolean"
},
"excludeSlashTmp": {
"default": false,
"type": "boolean"
@@ -5,4 +5,4 @@ import type { AbsolutePathBuf } from "../AbsolutePathBuf";
import type { NetworkAccess } from "./NetworkAccess";
import type { ReadOnlyAccess } from "./ReadOnlyAccess";
export type SandboxPolicy = { "type": "dangerFullAccess" } | { "type": "readOnly", access: ReadOnlyAccess, networkAccess: boolean, } | { "type": "externalSandbox", networkAccess: NetworkAccess, } | { "type": "workspaceWrite", writableRoots: Array<AbsolutePathBuf>, readOnlyAccess: ReadOnlyAccess, networkAccess: boolean, excludeTmpdirEnvVar: boolean, excludeSlashTmp: boolean, };
export type SandboxPolicy = { "type": "dangerFullAccess" } | { "type": "readOnly", access: ReadOnlyAccess, networkAccess: boolean, } | { "type": "externalSandbox", networkAccess: NetworkAccess, } | { "type": "workspaceWrite", writableRoots: Array<AbsolutePathBuf>, readOnlyAccess: ReadOnlyAccess, networkAccess: boolean, allowLimitedGitWrites: boolean, excludeTmpdirEnvVar: boolean, excludeSlashTmp: boolean, };
@@ -2,4 +2,4 @@
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.
export type SandboxWorkspaceWrite = { writable_roots: Array<string>, network_access: boolean, exclude_tmpdir_env_var: boolean, exclude_slash_tmp: boolean, };
export type SandboxWorkspaceWrite = { writable_roots: Array<string>, network_access: boolean, allow_limited_git_writes: boolean, exclude_tmpdir_env_var: boolean, exclude_slash_tmp: boolean, };
@@ -234,6 +234,7 @@ pub struct SandboxSettings {
#[serde(default)]
pub writable_roots: Vec<AbsolutePathBuf>,
pub network_access: Option<bool>,
pub allow_limited_git_writes: Option<bool>,
pub exclude_tmpdir_env_var: Option<bool>,
pub exclude_slash_tmp: Option<bool>,
}
@@ -646,6 +646,8 @@ pub struct SandboxWorkspaceWrite {
#[serde(default)]
pub network_access: bool,
#[serde(default)]
pub allow_limited_git_writes: bool,
#[serde(default)]
pub exclude_tmpdir_env_var: bool,
#[serde(default)]
pub exclude_slash_tmp: bool,
@@ -1744,6 +1746,8 @@ pub enum SandboxPolicy {
#[serde(default)]
network_access: bool,
#[serde(default)]
allow_limited_git_writes: bool,
#[serde(default)]
exclude_tmpdir_env_var: bool,
#[serde(default)]
exclude_slash_tmp: bool,
@@ -1775,12 +1779,14 @@ impl SandboxPolicy {
writable_roots,
read_only_access,
network_access,
allow_limited_git_writes,
exclude_tmpdir_env_var,
exclude_slash_tmp,
} => codex_protocol::protocol::SandboxPolicy::WorkspaceWrite {
writable_roots: writable_roots.clone(),
read_only_access: read_only_access.to_core(),
network_access: *network_access,
allow_limited_git_writes: *allow_limited_git_writes,
exclude_tmpdir_env_var: *exclude_tmpdir_env_var,
exclude_slash_tmp: *exclude_slash_tmp,
},
@@ -1813,12 +1819,14 @@ impl From<codex_protocol::protocol::SandboxPolicy> for SandboxPolicy {
writable_roots,
read_only_access,
network_access,
allow_limited_git_writes,
exclude_tmpdir_env_var,
exclude_slash_tmp,
} => SandboxPolicy::WorkspaceWrite {
writable_roots,
read_only_access: ReadOnlyAccess::from(read_only_access),
network_access,
allow_limited_git_writes,
exclude_tmpdir_env_var,
exclude_slash_tmp,
},
@@ -9387,6 +9395,7 @@ mod tests {
readable_roots: vec![readable_root.clone()],
},
network_access: true,
allow_limited_git_writes: true,
exclude_tmpdir_env_var: false,
exclude_slash_tmp: false,
};
@@ -9401,6 +9410,7 @@ mod tests {
readable_roots: vec![readable_root],
},
network_access: true,
allow_limited_git_writes: true,
exclude_tmpdir_env_var: false,
exclude_slash_tmp: false,
}
@@ -9441,6 +9451,7 @@ mod tests {
writable_roots: vec![],
read_only_access: ReadOnlyAccess::FullAccess,
network_access: false,
allow_limited_git_writes: false,
exclude_tmpdir_env_var: false,
exclude_slash_tmp: false,
}
@@ -1896,6 +1896,7 @@ async fn turn_start_updates_sandbox_and_cwd_between_turns_v2() -> Result<()> {
writable_roots: vec![first_cwd.try_into()?],
read_only_access: codex_app_server_protocol::ReadOnlyAccess::FullAccess,
network_access: false,
allow_limited_git_writes: false,
exclude_tmpdir_env_var: false,
exclude_slash_tmp: false,
}),
@@ -538,6 +538,7 @@ async fn turn_start_shell_zsh_fork_subcommand_decline_marks_parent_declined_v2()
writable_roots: vec![workspace.clone().try_into()?],
read_only_access: codex_app_server_protocol::ReadOnlyAccess::FullAccess,
network_access: false,
allow_limited_git_writes: false,
exclude_tmpdir_env_var: false,
exclude_slash_tmp: false,
}),
@@ -1963,6 +1963,7 @@ allowed_approvals_reviewers = ["user"]
writable_roots: vec![AbsolutePathBuf::from_absolute_path(root)?],
read_only_access: Default::default(),
network_access: false,
allow_limited_git_writes: false,
exclude_tmpdir_env_var: false,
exclude_slash_tmp: false,
})
+2
View File
@@ -661,12 +661,14 @@ impl ConfigToml {
Some(SandboxWorkspaceWrite {
writable_roots,
network_access,
allow_limited_git_writes,
exclude_tmpdir_env_var,
exclude_slash_tmp,
}) => SandboxPolicy::WorkspaceWrite {
writable_roots: writable_roots.clone(),
read_only_access: ReadOnlyAccess::FullAccess,
network_access: *network_access,
allow_limited_git_writes: *allow_limited_git_writes,
exclude_tmpdir_env_var: *exclude_tmpdir_env_var,
exclude_slash_tmp: *exclude_slash_tmp,
},
+2
View File
@@ -31,6 +31,8 @@ pub struct PermissionProfileToml {
#[derive(Serialize, Deserialize, Debug, Clone, Default, PartialEq, Eq, JsonSchema)]
pub struct FilesystemPermissionsToml {
#[serde(default)]
pub allow_limited_git_writes: bool,
/// Optional maximum depth for expanding unreadable glob patterns on
/// platforms that snapshot glob matches before sandbox startup.
#[schemars(range(min = 1))]
+3
View File
@@ -681,6 +681,8 @@ pub struct SandboxWorkspaceWrite {
#[serde(default)]
pub network_access: bool,
#[serde(default)]
pub allow_limited_git_writes: bool,
#[serde(default)]
pub exclude_tmpdir_env_var: bool,
#[serde(default)]
pub exclude_slash_tmp: bool,
@@ -691,6 +693,7 @@ impl From<SandboxWorkspaceWrite> for codex_app_server_protocol::SandboxSettings
Self {
writable_roots: sandbox_workspace_write.writable_roots,
network_access: Some(sandbox_workspace_write.network_access),
allow_limited_git_writes: Some(sandbox_workspace_write.allow_limited_git_writes),
exclude_tmpdir_env_var: Some(sandbox_workspace_write.exclude_tmpdir_env_var),
exclude_slash_tmp: Some(sandbox_workspace_write.exclude_slash_tmp),
}
+4 -1
View File
@@ -12,7 +12,10 @@ Expects `/usr/bin/sandbox-exec` to be present.
When using the workspace-write sandbox policy, the Seatbelt profile allows
writes under the configured writable roots while keeping `.git` (directory or
pointer file), the resolved `gitdir:` target, and `.codex` read-only.
pointer file), the resolved `gitdir:` target, and `.codex` read-only by
default. When `allow_limited_git_writes` is enabled, Git metadata writes are allowed,
but `.git/config`, `.git/hooks`, and resolved worktree gitdir config/hooks
remain read-only.
Network access and filesystem read/write roots are controlled by
`SandboxPolicy`. Seatbelt consumes the resolved policy and enforces it.
+5 -1
View File
@@ -746,6 +746,10 @@
},
"FilesystemPermissionsToml": {
"properties": {
"allow_limited_git_writes": {
"default": false,
"type": "boolean"
},
"glob_scan_max_depth": {
"description": "Optional maximum depth for expanding unreadable glob patterns on platforms that snapshot glob matches before sandbox startup.",
"format": "uint",
@@ -3167,4 +3171,4 @@
},
"title": "ConfigToml",
"type": "object"
}
}
+36
View File
@@ -573,6 +573,8 @@ allow_upstream_proxy = false
"workspace".to_string(),
PermissionProfileToml {
filesystem: Some(FilesystemPermissionsToml {
allow_limited_git_writes: false,
glob_scan_max_depth: None,
entries: BTreeMap::from([
(
@@ -628,6 +630,8 @@ async fn permissions_profiles_network_populates_runtime_network_proxy_spec() ->
"workspace".to_string(),
PermissionProfileToml {
filesystem: Some(FilesystemPermissionsToml {
allow_limited_git_writes: false,
glob_scan_max_depth: None,
entries: BTreeMap::from([(
":minimal".to_string(),
@@ -678,6 +682,8 @@ async fn permissions_profiles_network_disabled_by_default_does_not_start_proxy()
"workspace".to_string(),
PermissionProfileToml {
filesystem: Some(FilesystemPermissionsToml {
allow_limited_git_writes: false,
glob_scan_max_depth: None,
entries: BTreeMap::from([(
":minimal".to_string(),
@@ -724,6 +730,8 @@ async fn default_permissions_profile_populates_runtime_sandbox_policy() -> std::
"workspace".to_string(),
PermissionProfileToml {
filesystem: Some(FilesystemPermissionsToml {
allow_limited_git_writes: false,
glob_scan_max_depth: None,
entries: BTreeMap::from([
(
@@ -795,6 +803,8 @@ async fn default_permissions_profile_populates_runtime_sandbox_policy() -> std::
readable_roots: vec![cwd.path().join("docs").abs(),],
},
network_access: false,
allow_limited_git_writes: false,
exclude_tmpdir_env_var: true,
exclude_slash_tmp: true,
}
@@ -821,6 +831,7 @@ async fn permission_profile_override_populates_runtime_permissions() -> std::io:
},
access: FileSystemAccessMode::Write,
}],
allow_limited_git_writes: false,
glob_scan_max_depth: None,
}),
};
@@ -859,6 +870,7 @@ async fn permission_profile_override_preserves_configured_network_proxy() -> std
},
access: FileSystemAccessMode::Write,
}],
allow_limited_git_writes: false,
glob_scan_max_depth: None,
}),
};
@@ -871,6 +883,8 @@ async fn permission_profile_override_preserves_configured_network_proxy() -> std
"workspace".to_string(),
PermissionProfileToml {
filesystem: Some(FilesystemPermissionsToml {
allow_limited_git_writes: false,
glob_scan_max_depth: None,
entries: BTreeMap::from([(
":minimal".to_string(),
@@ -929,6 +943,8 @@ async fn project_root_glob_none_compiles_to_filesystem_pattern_entry() -> std::i
"workspace".to_string(),
PermissionProfileToml {
filesystem: Some(FilesystemPermissionsToml {
allow_limited_git_writes: false,
glob_scan_max_depth: Some(2),
entries: BTreeMap::from([(
":project_roots".to_string(),
@@ -1004,6 +1020,8 @@ async fn permissions_profiles_require_default_permissions() -> std::io::Result<(
"workspace".to_string(),
PermissionProfileToml {
filesystem: Some(FilesystemPermissionsToml {
allow_limited_git_writes: false,
glob_scan_max_depth: None,
entries: BTreeMap::from([(
":minimal".to_string(),
@@ -1048,6 +1066,8 @@ async fn permissions_profiles_reject_writes_outside_workspace_root() -> std::io:
"workspace".to_string(),
PermissionProfileToml {
filesystem: Some(FilesystemPermissionsToml {
allow_limited_git_writes: false,
glob_scan_max_depth: None,
entries: BTreeMap::from([(
external_write_path.to_string(),
@@ -1092,6 +1112,8 @@ async fn permissions_profiles_reject_nested_entries_for_non_project_roots() -> s
"workspace".to_string(),
PermissionProfileToml {
filesystem: Some(FilesystemPermissionsToml {
allow_limited_git_writes: false,
glob_scan_max_depth: None,
entries: BTreeMap::from([(
":minimal".to_string(),
@@ -1152,6 +1174,8 @@ async fn load_workspace_permission_profile(
async fn permissions_profiles_allow_unknown_special_paths() -> std::io::Result<()> {
let config = load_workspace_permission_profile(PermissionProfileToml {
filesystem: Some(FilesystemPermissionsToml {
allow_limited_git_writes: false,
glob_scan_max_depth: None,
entries: BTreeMap::from([(
":future_special_path".to_string(),
@@ -1199,6 +1223,8 @@ async fn permissions_profiles_allow_unknown_special_paths_with_nested_entries()
-> std::io::Result<()> {
let config = load_workspace_permission_profile(PermissionProfileToml {
filesystem: Some(FilesystemPermissionsToml {
allow_limited_git_writes: false,
glob_scan_max_depth: None,
entries: BTreeMap::from([(
":future_special_path".to_string(),
@@ -1267,6 +1293,8 @@ async fn permissions_profiles_allow_missing_filesystem_with_warning() -> std::io
async fn permissions_profiles_allow_empty_filesystem_with_warning() -> std::io::Result<()> {
let config = load_workspace_permission_profile(PermissionProfileToml {
filesystem: Some(FilesystemPermissionsToml {
allow_limited_git_writes: false,
glob_scan_max_depth: None,
entries: BTreeMap::new(),
}),
@@ -1302,6 +1330,8 @@ async fn permissions_profiles_reject_project_root_parent_traversal() -> std::io:
"workspace".to_string(),
PermissionProfileToml {
filesystem: Some(FilesystemPermissionsToml {
allow_limited_git_writes: false,
glob_scan_max_depth: None,
entries: BTreeMap::from([(
":project_roots".to_string(),
@@ -1348,6 +1378,8 @@ async fn permissions_profiles_allow_network_enablement() -> std::io::Result<()>
"workspace".to_string(),
PermissionProfileToml {
filesystem: Some(FilesystemPermissionsToml {
allow_limited_git_writes: false,
glob_scan_max_depth: None,
entries: BTreeMap::from([(
":minimal".to_string(),
@@ -1514,6 +1546,8 @@ trust_level = "trusted"
writable_roots: vec![writable_root.clone()],
read_only_access: ReadOnlyAccess::FullAccess,
network_access: false,
allow_limited_git_writes: false,
exclude_tmpdir_env_var: true,
exclude_slash_tmp: true,
}
@@ -1555,6 +1589,8 @@ exclude_slash_tmp = true
writable_roots: vec![writable_root],
read_only_access: ReadOnlyAccess::FullAccess,
network_access: false,
allow_limited_git_writes: false,
exclude_tmpdir_env_var: true,
exclude_slash_tmp: true,
}
+4
View File
@@ -99,6 +99,10 @@ pub(crate) fn compile_permission_profile(
let network_sandbox_policy = compile_network_sandbox_policy(profile.network.as_ref());
let mut file_system_sandbox_policy = FileSystemSandboxPolicy::restricted(entries);
file_system_sandbox_policy.allow_limited_git_writes = profile
.filesystem
.as_ref()
.is_some_and(|filesystem| filesystem.allow_limited_git_writes);
file_system_sandbox_policy.glob_scan_max_depth = glob_scan_max_depth;
Ok((file_system_sandbox_policy, network_sandbox_policy))
}
@@ -55,6 +55,8 @@ async fn restricted_read_implicitly_allows_helper_executables() -> std::io::Resu
"workspace".to_string(),
PermissionProfileToml {
filesystem: Some(FilesystemPermissionsToml {
allow_limited_git_writes: false,
glob_scan_max_depth: None,
entries: BTreeMap::new(),
}),
@@ -227,6 +229,8 @@ fn network_toml_overlays_unix_socket_permissions_by_path() {
#[test]
fn read_write_glob_warnings_skip_supported_deny_read_globs_and_trailing_subpaths() {
let filesystem = FilesystemPermissionsToml {
allow_limited_git_writes: false,
glob_scan_max_depth: None,
entries: BTreeMap::from([
(
@@ -261,6 +265,8 @@ fn read_write_glob_warnings_skip_supported_deny_read_globs_and_trailing_subpaths
#[test]
fn unreadable_globstar_warning_is_suppressed_when_scan_depth_is_configured() {
let filesystem = FilesystemPermissionsToml {
allow_limited_git_writes: false,
glob_scan_max_depth: None,
entries: BTreeMap::from([(
":project_roots".to_string(),
@@ -277,6 +283,8 @@ fn unreadable_globstar_warning_is_suppressed_when_scan_depth_is_configured() {
);
let configured_filesystem = FilesystemPermissionsToml {
allow_limited_git_writes: false,
glob_scan_max_depth: Some(2),
..filesystem
};
@@ -309,6 +317,8 @@ fn read_write_trailing_glob_suffix_compiles_as_subpath() -> std::io::Result<()>
"workspace".to_string(),
PermissionProfileToml {
filesystem: Some(FilesystemPermissionsToml {
allow_limited_git_writes: false,
glob_scan_max_depth: None,
entries: BTreeMap::from([(
":project_roots".to_string(),
+1
View File
@@ -593,6 +593,7 @@ allowed_sandbox_modes = ["read-only"]
writable_roots: Vec::new(),
read_only_access: Default::default(),
network_access: false,
allow_limited_git_writes: false,
exclude_tmpdir_env_var: false,
exclude_slash_tmp: false,
})
+16
View File
@@ -472,6 +472,8 @@ fn windows_restricted_token_allows_legacy_workspace_write_policies() {
writable_roots: vec![],
read_only_access: codex_protocol::protocol::ReadOnlyAccess::FullAccess,
network_access: false,
allow_limited_git_writes: false,
exclude_tmpdir_env_var: true,
exclude_slash_tmp: true,
};
@@ -530,6 +532,8 @@ fn windows_restricted_token_rejects_split_only_filesystem_policies() {
writable_roots: vec![],
read_only_access: codex_protocol::protocol::ReadOnlyAccess::FullAccess,
network_access: false,
allow_limited_git_writes: false,
exclude_tmpdir_env_var: true,
exclude_slash_tmp: true,
};
@@ -574,6 +578,8 @@ fn windows_restricted_token_rejects_root_write_read_only_carveouts() {
writable_roots: vec![],
read_only_access: codex_protocol::protocol::ReadOnlyAccess::FullAccess,
network_access: false,
allow_limited_git_writes: false,
exclude_tmpdir_env_var: true,
exclude_slash_tmp: true,
};
@@ -621,6 +627,8 @@ fn windows_restricted_token_supports_full_read_split_write_read_carveouts() {
writable_roots: vec![],
read_only_access: codex_protocol::protocol::ReadOnlyAccess::FullAccess,
network_access: false,
allow_limited_git_writes: false,
exclude_tmpdir_env_var: true,
exclude_slash_tmp: true,
};
@@ -711,6 +719,8 @@ fn windows_elevated_supports_split_write_read_carveouts() {
writable_roots: vec![],
read_only_access: codex_protocol::protocol::ReadOnlyAccess::FullAccess,
network_access: false,
allow_limited_git_writes: false,
exclude_tmpdir_env_var: true,
exclude_slash_tmp: true,
};
@@ -765,6 +775,8 @@ fn windows_elevated_rejects_unreadable_split_carveouts() {
writable_roots: vec![],
read_only_access: codex_protocol::protocol::ReadOnlyAccess::FullAccess,
network_access: false,
allow_limited_git_writes: false,
exclude_tmpdir_env_var: true,
exclude_slash_tmp: true,
};
@@ -813,6 +825,8 @@ fn windows_elevated_rejects_unreadable_globs() {
writable_roots: vec![],
read_only_access: codex_protocol::protocol::ReadOnlyAccess::FullAccess,
network_access: false,
allow_limited_git_writes: false,
exclude_tmpdir_env_var: true,
exclude_slash_tmp: true,
};
@@ -863,6 +877,8 @@ fn windows_elevated_rejects_reopened_writable_descendants() {
writable_roots: vec![],
read_only_access: codex_protocol::protocol::ReadOnlyAccess::FullAccess,
network_access: false,
allow_limited_git_writes: false,
exclude_tmpdir_env_var: true,
exclude_slash_tmp: true,
};
+1
View File
@@ -317,6 +317,7 @@ mod agent {
writable_roots,
read_only_access: Default::default(),
network_access: false,
allow_limited_git_writes: false,
exclude_tmpdir_env_var: true,
exclude_slash_tmp: true,
};
+5
View File
@@ -30,6 +30,7 @@ fn test_writable_roots_constraint() {
writable_roots: vec![],
read_only_access: Default::default(),
network_access: false,
allow_limited_git_writes: false,
exclude_tmpdir_env_var: true,
exclude_slash_tmp: true,
};
@@ -52,6 +53,7 @@ fn test_writable_roots_constraint() {
writable_roots: vec![parent],
read_only_access: Default::default(),
network_access: false,
allow_limited_git_writes: false,
exclude_tmpdir_env_var: true,
exclude_slash_tmp: true,
};
@@ -100,6 +102,7 @@ fn granular_with_all_flags_true_matches_on_request_for_out_of_root_patch() {
writable_roots: vec![],
read_only_access: Default::default(),
network_access: false,
allow_limited_git_writes: false,
exclude_tmpdir_env_var: true,
exclude_slash_tmp: true,
};
@@ -145,6 +148,7 @@ fn granular_sandbox_approval_false_rejects_out_of_root_patch() {
writable_roots: vec![],
read_only_access: Default::default(),
network_access: false,
allow_limited_git_writes: false,
exclude_tmpdir_env_var: true,
exclude_slash_tmp: true,
};
@@ -296,6 +300,7 @@ fn missing_project_dot_codex_config_requires_approval() {
writable_roots: vec![],
read_only_access: Default::default(),
network_access: false,
allow_limited_git_writes: false,
exclude_tmpdir_env_var: true,
exclude_slash_tmp: true,
};
+2
View File
@@ -2839,6 +2839,7 @@ async fn session_configuration_apply_preserves_split_file_system_policy_on_cwd_o
readable_roots: vec![docs_dir.clone()],
},
network_access: false,
allow_limited_git_writes: false,
exclude_tmpdir_env_var: true,
exclude_slash_tmp: true,
});
@@ -3020,6 +3021,7 @@ async fn session_configuration_apply_rederives_legacy_file_system_policy_on_cwd_
readable_roots: vec![docs_dir],
},
network_access: false,
allow_limited_git_writes: false,
exclude_tmpdir_env_var: true,
exclude_slash_tmp: true,
});
@@ -241,6 +241,7 @@ fn write_permissions_for_paths_skip_dirs_already_writable_under_workspace_root()
writable_roots: vec![],
read_only_access: Default::default(),
network_access: false,
allow_limited_git_writes: false,
exclude_tmpdir_env_var: true,
exclude_slash_tmp: false,
});
@@ -264,6 +265,7 @@ fn write_permissions_for_paths_keep_dirs_outside_workspace_root() {
writable_roots: vec![],
read_only_access: Default::default(),
network_access: false,
allow_limited_git_writes: false,
exclude_tmpdir_env_var: true,
exclude_slash_tmp: true,
});
@@ -270,6 +270,7 @@ fn shell_request_escalation_execution_is_explicit() {
writable_roots: vec![AbsolutePathBuf::from_absolute_path("/tmp/original/output").unwrap()],
read_only_access: ReadOnlyAccess::FullAccess,
network_access: false,
allow_limited_git_writes: false,
exclude_tmpdir_env_var: false,
exclude_slash_tmp: false,
};
+1
View File
@@ -45,6 +45,7 @@ pub fn restrictive_workspace_write_policy() -> SandboxPolicy {
writable_roots: Vec::new(),
read_only_access: Default::default(),
network_access: false,
allow_limited_git_writes: false,
exclude_tmpdir_env_var: true,
exclude_slash_tmp: true,
}
@@ -645,6 +645,7 @@ async fn apply_patch_cli_rejects_path_traversal_outside_workspace(
writable_roots: vec![],
read_only_access: Default::default(),
network_access: false,
allow_limited_git_writes: false,
exclude_tmpdir_env_var: true,
exclude_slash_tmp: true,
};
@@ -701,6 +702,7 @@ async fn apply_patch_cli_rejects_move_path_traversal_outside_workspace(
writable_roots: vec![],
read_only_access: Default::default(),
network_access: false,
allow_limited_git_writes: false,
exclude_tmpdir_env_var: true,
exclude_slash_tmp: true,
};
+3
View File
@@ -770,6 +770,7 @@ fn scenarios() -> Vec<ScenarioSpec> {
writable_roots: vec![],
read_only_access: Default::default(),
network_access,
allow_limited_git_writes: false,
exclude_tmpdir_env_var: false,
exclude_slash_tmp: false,
};
@@ -1801,6 +1802,7 @@ async fn approving_apply_patch_for_session_skips_future_prompts_for_same_file()
writable_roots: vec![],
read_only_access: Default::default(),
network_access: false,
allow_limited_git_writes: false,
exclude_tmpdir_env_var: false,
exclude_slash_tmp: false,
};
@@ -2531,6 +2533,7 @@ allow_local_binding = true
writable_roots: vec![],
read_only_access: Default::default(),
network_access: true,
allow_limited_git_writes: false,
exclude_tmpdir_env_var: false,
exclude_slash_tmp: false,
};
@@ -544,6 +544,7 @@ async fn permissions_message_includes_writable_roots() -> Result<()> {
writable_roots: vec![writable_root],
read_only_access: Default::default(),
network_access: false,
allow_limited_git_writes: false,
exclude_tmpdir_env_var: false,
exclude_slash_tmp: false,
};
@@ -435,6 +435,7 @@ async fn overrides_turn_context_but_keeps_cached_prefix_and_key_constant() -> an
writable_roots: vec![writable.path().try_into().unwrap()],
read_only_access: Default::default(),
network_access: true,
allow_limited_git_writes: false,
exclude_tmpdir_env_var: true,
exclude_slash_tmp: true,
};
@@ -714,6 +715,7 @@ async fn per_turn_overrides_keep_cached_prefix_and_key_constant() -> anyhow::Res
writable_roots: vec![writable.abs()],
read_only_access: Default::default(),
network_access: true,
allow_limited_git_writes: false,
exclude_tmpdir_env_var: true,
exclude_slash_tmp: true,
};
@@ -288,6 +288,7 @@ fn workspace_write_excluding_tmp() -> SandboxPolicy {
writable_roots: vec![],
read_only_access: Default::default(),
network_access: false,
allow_limited_git_writes: false,
exclude_tmpdir_env_var: true,
exclude_slash_tmp: true,
}
@@ -75,6 +75,7 @@ fn workspace_write_excluding_tmp() -> SandboxPolicy {
writable_roots: vec![],
read_only_access: Default::default(),
network_access: false,
allow_limited_git_writes: false,
exclude_tmpdir_env_var: true,
exclude_slash_tmp: true,
}
@@ -485,6 +485,7 @@ fn summarize_sandbox_policy(sandbox_policy: &SandboxPolicy) -> String {
exclude_tmpdir_env_var,
exclude_slash_tmp,
read_only_access: _,
allow_limited_git_writes: _,
} => {
let mut summary = "workspace-write".to_string();
let mut writable_entries = vec!["workdir".to_string()];
+1
View File
@@ -424,6 +424,7 @@ fn session_configured_from_thread_response_uses_review_policy_from_response() {
writable_roots: vec![],
read_only_access: codex_app_server_protocol::ReadOnlyAccess::FullAccess,
network_access: false,
allow_limited_git_writes: false,
exclude_tmpdir_env_var: false,
exclude_slash_tmp: false,
},
+3
View File
@@ -184,6 +184,7 @@ async fn python_multiprocessing_lock_works_under_sandbox() {
writable_roots,
read_only_access: Default::default(),
network_access: false,
allow_limited_git_writes: false,
exclude_tmpdir_env_var: false,
exclude_slash_tmp: false,
};
@@ -299,6 +300,7 @@ async fn sandbox_distinguishes_command_and_policy_cwds() {
writable_roots: vec![],
read_only_access: Default::default(),
network_access: false,
allow_limited_git_writes: false,
exclude_tmpdir_env_var: true,
exclude_slash_tmp: true,
};
@@ -381,6 +383,7 @@ async fn sandbox_blocks_first_time_dot_codex_creation() {
writable_roots: vec![],
read_only_access: Default::default(),
network_access: false,
allow_limited_git_writes: false,
exclude_tmpdir_env_var: true,
exclude_slash_tmp: true,
};
+2
View File
@@ -1373,6 +1373,7 @@ mod tests {
],
read_only_access: Default::default(),
network_access: false,
allow_limited_git_writes: false,
exclude_tmpdir_env_var: true,
exclude_slash_tmp: true,
};
@@ -1404,6 +1405,7 @@ mod tests {
writable_roots: vec![AbsolutePathBuf::try_from(Path::new("/dev")).expect("/dev path")],
read_only_access: Default::default(),
network_access: false,
allow_limited_git_writes: false,
exclude_tmpdir_env_var: true,
exclude_slash_tmp: true,
};
@@ -458,6 +458,7 @@ fn resolve_sandbox_policies_accepts_semantically_equivalent_workspace_write_inpu
writable_roots: vec![workspace],
read_only_access: ReadOnlyAccess::FullAccess,
network_access: false,
allow_limited_git_writes: false,
exclude_tmpdir_env_var: false,
exclude_slash_tmp: false,
};
@@ -89,6 +89,7 @@ async fn run_cmd_result_with_writable_roots(
.collect(),
read_only_access: Default::default(),
network_access,
allow_limited_git_writes: false,
// Exclude tmp-related folders from writable roots because we need a
// folder that is writable by tests but that we intentionally disallow
// writing to in the sandbox.
@@ -563,6 +564,7 @@ async fn sandbox_blocks_explicit_split_policy_carveouts_under_bwrap() {
writable_roots: vec![AbsolutePathBuf::try_from(tmpdir.path()).expect("absolute tempdir")],
read_only_access: Default::default(),
network_access: true,
allow_limited_git_writes: false,
exclude_tmpdir_env_var: true,
exclude_slash_tmp: true,
};
@@ -636,6 +638,7 @@ async fn sandbox_reenables_writable_subpaths_under_unreadable_parents() {
writable_roots: vec![AbsolutePathBuf::try_from(tmpdir.path()).expect("absolute tempdir")],
read_only_access: Default::default(),
network_access: true,
allow_limited_git_writes: false,
exclude_tmpdir_env_var: true,
exclude_slash_tmp: true,
};
+10
View File
@@ -120,6 +120,7 @@ impl SandboxPermissions {
#[derive(Debug, Clone, Default, Eq, Hash, PartialEq, JsonSchema, TS)]
pub struct FileSystemPermissions {
pub entries: Vec<FileSystemSandboxEntry>,
pub allow_limited_git_writes: bool,
pub glob_scan_max_depth: Option<NonZeroUsize>,
}
@@ -149,6 +150,7 @@ impl FileSystemPermissions {
}
Self {
entries,
allow_limited_git_writes: false,
glob_scan_max_depth: None,
}
}
@@ -207,6 +209,8 @@ struct LegacyFileSystemPermissions {
struct CanonicalFileSystemPermissions {
#[serde(default, skip_serializing_if = "Vec::is_empty")]
entries: Vec<FileSystemSandboxEntry>,
#[serde(default, skip_serializing_if = "std::ops::Not::not")]
allow_limited_git_writes: bool,
#[serde(default, skip_serializing_if = "Option::is_none")]
glob_scan_max_depth: Option<NonZeroUsize>,
}
@@ -228,6 +232,7 @@ impl Serialize for FileSystemPermissions {
} else {
CanonicalFileSystemPermissions {
entries: self.entries.clone(),
allow_limited_git_writes: self.allow_limited_git_writes,
glob_scan_max_depth: self.glob_scan_max_depth,
}
.serialize(serializer)
@@ -243,9 +248,11 @@ impl<'de> Deserialize<'de> for FileSystemPermissions {
match FileSystemPermissionsDe::deserialize(deserializer)? {
FileSystemPermissionsDe::Canonical(CanonicalFileSystemPermissions {
entries,
allow_limited_git_writes,
glob_scan_max_depth,
}) => Ok(Self {
entries,
allow_limited_git_writes,
glob_scan_max_depth,
}),
FileSystemPermissionsDe::Legacy(LegacyFileSystemPermissions { read, write }) => {
@@ -350,6 +357,7 @@ impl From<&FileSystemSandboxPolicy> for FileSystemPermissions {
};
Self {
entries,
allow_limited_git_writes: value.allow_limited_git_writes,
glob_scan_max_depth: value.glob_scan_max_depth.and_then(NonZeroUsize::new),
}
}
@@ -358,6 +366,7 @@ impl From<&FileSystemSandboxPolicy> for FileSystemPermissions {
impl From<&FileSystemPermissions> for FileSystemSandboxPolicy {
fn from(value: &FileSystemPermissions) -> Self {
let mut policy = FileSystemSandboxPolicy::restricted(value.entries.clone());
policy.allow_limited_git_writes = value.allow_limited_git_writes;
policy.glob_scan_max_depth = value.glob_scan_max_depth.map(usize::from);
policy
}
@@ -1496,6 +1505,7 @@ mod tests {
path: FileSystemPath::Path { path },
access: FileSystemAccessMode::Read,
}],
allow_limited_git_writes: false,
glob_scan_max_depth: NonZeroUsize::new(2),
};
+45 -8
View File
@@ -139,6 +139,8 @@ pub enum FileSystemSandboxKind {
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema, TS)]
pub struct FileSystemSandboxPolicy {
pub kind: FileSystemSandboxKind,
#[serde(default, skip_serializing_if = "std::ops::Not::not")]
pub allow_limited_git_writes: bool,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[ts(optional)]
pub glob_scan_max_depth: Option<usize>,
@@ -157,6 +159,7 @@ struct FileSystemSemanticSignature {
has_full_disk_read_access: bool,
has_full_disk_write_access: bool,
include_platform_defaults: bool,
allow_limited_git_writes: bool,
readable_roots: Vec<AbsolutePathBuf>,
writable_roots: Vec<WritableRoot>,
unreadable_roots: Vec<AbsolutePathBuf>,
@@ -261,6 +264,7 @@ impl Default for FileSystemSandboxPolicy {
fn default() -> Self {
Self {
kind: FileSystemSandboxKind::Restricted,
allow_limited_git_writes: false,
glob_scan_max_depth: None,
entries: vec![FileSystemSandboxEntry {
path: FileSystemPath::Special {
@@ -276,6 +280,7 @@ impl FileSystemSandboxPolicy {
pub fn unrestricted() -> Self {
Self {
kind: FileSystemSandboxKind::Unrestricted,
allow_limited_git_writes: false,
glob_scan_max_depth: None,
entries: Vec::new(),
}
@@ -284,6 +289,7 @@ impl FileSystemSandboxPolicy {
pub fn external_sandbox() -> Self {
Self {
kind: FileSystemSandboxKind::ExternalSandbox,
allow_limited_git_writes: false,
glob_scan_max_depth: None,
entries: Vec::new(),
}
@@ -292,6 +298,7 @@ impl FileSystemSandboxPolicy {
pub fn restricted(entries: Vec<FileSystemSandboxEntry>) -> Self {
Self {
kind: FileSystemSandboxKind::Restricted,
allow_limited_git_writes: false,
glob_scan_max_depth: None,
entries,
}
@@ -326,6 +333,7 @@ impl FileSystemSandboxPolicy {
return rebuilt;
}
rebuilt.glob_scan_max_depth = existing.glob_scan_max_depth;
rebuilt.allow_limited_git_writes = existing.allow_limited_git_writes;
for deny_entry in existing
.entries
@@ -387,7 +395,12 @@ impl FileSystemSandboxPolicy {
/// into split filesystem policy.
pub fn from_legacy_sandbox_policy(sandbox_policy: &SandboxPolicy, cwd: &Path) -> Self {
let mut file_system_policy = Self::from(sandbox_policy);
if let SandboxPolicy::WorkspaceWrite { writable_roots, .. } = sandbox_policy {
if let SandboxPolicy::WorkspaceWrite {
writable_roots,
allow_limited_git_writes,
..
} = sandbox_policy
{
let legacy_writable_roots = sandbox_policy.get_writable_roots_with_cwd(cwd);
file_system_policy.entries.retain(|entry| {
if entry.access != FileSystemAccessMode::Read {
@@ -405,7 +418,9 @@ impl FileSystemSandboxPolicy {
if let Ok(cwd_root) = AbsolutePathBuf::from_absolute_path(cwd) {
for protected_path in default_read_only_subpaths_for_writable_root(
&cwd_root, /*protect_missing_dot_codex*/ true,
&cwd_root,
/*protect_missing_dot_codex*/ true,
*allow_limited_git_writes,
) {
append_default_read_only_path_if_no_explicit_rule(
&mut file_system_policy.entries,
@@ -417,6 +432,7 @@ impl FileSystemSandboxPolicy {
for protected_path in default_read_only_subpaths_for_writable_root(
writable_root,
/*protect_missing_dot_codex*/ false,
*allow_limited_git_writes,
) {
append_default_read_only_path_if_no_explicit_rule(
&mut file_system_policy.entries,
@@ -607,10 +623,14 @@ impl FileSystemSandboxPolicy {
.ok()
.is_some_and(|cwd| normalize_effective_absolute_path(cwd) == root);
let mut read_only_subpaths: Vec<AbsolutePathBuf> =
default_read_only_subpaths_for_writable_root(&root, protect_missing_dot_codex)
.into_iter()
.filter(|path| !has_explicit_resolved_path_entry(&resolved_entries, path))
.collect();
default_read_only_subpaths_for_writable_root(
&root,
protect_missing_dot_codex,
self.allow_limited_git_writes,
)
.into_iter()
.filter(|path| !has_explicit_resolved_path_entry(&resolved_entries, path))
.collect();
// Narrower explicit non-write entries carve out broader writable roots.
// More specific write entries still remain writable because they appear
// as separate WritableRoot values and are checked independently.
@@ -882,6 +902,7 @@ impl FileSystemSandboxPolicy {
),
read_only_access,
network_access: network_policy.is_enabled(),
allow_limited_git_writes: self.allow_limited_git_writes,
exclude_tmpdir_env_var: !tmpdir_writable,
exclude_slash_tmp: !slash_tmp_writable,
}
@@ -924,6 +945,7 @@ impl FileSystemSandboxPolicy {
has_full_disk_read_access: self.has_full_disk_read_access(),
has_full_disk_write_access: self.has_full_disk_write_access(),
include_platform_defaults: self.include_platform_defaults(),
allow_limited_git_writes: self.allow_limited_git_writes,
readable_roots: self.get_readable_roots_with_cwd(cwd),
writable_roots: self.get_writable_roots_with_cwd(cwd),
unreadable_roots: self.get_unreadable_roots_with_cwd(cwd),
@@ -1308,6 +1330,7 @@ fn normalize_effective_absolute_path(path: AbsolutePathBuf) -> AbsolutePathBuf {
fn default_read_only_subpaths_for_writable_root(
writable_root: &AbsolutePathBuf,
protect_missing_dot_codex: bool,
allow_limited_git_writes: bool,
) -> Vec<AbsolutePathBuf> {
let mut subpaths: Vec<AbsolutePathBuf> = Vec::new();
let top_level_git = writable_root.join(".git");
@@ -1321,9 +1344,21 @@ fn default_read_only_subpaths_for_writable_root(
&& is_git_pointer_file(&top_level_git)
&& let Some(gitdir) = resolve_gitdir_from_file(&top_level_git)
{
subpaths.push(gitdir);
if allow_limited_git_writes {
subpaths.push(gitdir.join("config"));
subpaths.push(gitdir.join("hooks"));
} else {
subpaths.push(gitdir);
}
}
if allow_limited_git_writes {
if top_level_git_is_dir {
subpaths.push(top_level_git.join("config"));
subpaths.push(top_level_git.join("hooks"));
}
} else {
subpaths.push(top_level_git);
}
subpaths.push(top_level_git);
}
let top_level_agents = writable_root.join(".agents");
@@ -1572,6 +1607,7 @@ mod tests {
readable_roots: vec![],
},
network_access: false,
allow_limited_git_writes: false,
exclude_tmpdir_env_var: true,
exclude_slash_tmp: true,
};
@@ -1599,6 +1635,7 @@ mod tests {
readable_roots: vec![],
},
network_access: false,
allow_limited_git_writes: false,
exclude_tmpdir_env_var: true,
exclude_slash_tmp: true,
};
+31 -2
View File
@@ -1150,6 +1150,11 @@ pub enum SandboxPolicy {
#[serde(default)]
network_access: bool,
/// When set to `true`, Git metadata stays writable except for
/// sensitive subpaths like config and hooks.
#[serde(default)]
allow_limited_git_writes: bool,
/// When set to `true`, will NOT include the per-user `TMPDIR`
/// environment variable among the default writable roots. Defaults to
/// `false`.
@@ -1235,6 +1240,7 @@ impl SandboxPolicy {
writable_roots: vec![],
read_only_access: ReadOnlyAccess::FullAccess,
network_access: false,
allow_limited_git_writes: false,
exclude_tmpdir_env_var: false,
exclude_slash_tmp: false,
}
@@ -1323,6 +1329,7 @@ impl SandboxPolicy {
exclude_tmpdir_env_var,
exclude_slash_tmp,
network_access: _,
allow_limited_git_writes,
} => {
// Start from explicitly configured writable roots.
let mut roots: Vec<AbsolutePathBuf> = writable_roots.clone();
@@ -1393,6 +1400,7 @@ impl SandboxPolicy {
read_only_subpaths: default_read_only_subpaths_for_writable_root(
&writable_root,
protect_missing_dot_codex,
*allow_limited_git_writes,
),
root: writable_root,
}
@@ -1406,6 +1414,7 @@ impl SandboxPolicy {
fn default_read_only_subpaths_for_writable_root(
writable_root: &AbsolutePathBuf,
protect_missing_dot_codex: bool,
allow_limited_git_writes: bool,
) -> Vec<AbsolutePathBuf> {
let mut subpaths: Vec<AbsolutePathBuf> = Vec::new();
let top_level_git = writable_root.join(".git");
@@ -1419,9 +1428,21 @@ fn default_read_only_subpaths_for_writable_root(
&& is_git_pointer_file(&top_level_git)
&& let Some(gitdir) = resolve_gitdir_from_file(&top_level_git)
{
subpaths.push(gitdir);
if allow_limited_git_writes {
subpaths.push(gitdir.join("config"));
subpaths.push(gitdir.join("hooks"));
} else {
subpaths.push(gitdir);
}
}
if allow_limited_git_writes {
if top_level_git_is_dir {
subpaths.push(top_level_git.join("config"));
subpaths.push(top_level_git.join("hooks"));
}
} else {
subpaths.push(top_level_git);
}
subpaths.push(top_level_git);
}
let top_level_agents = writable_root.join(".agents");
@@ -4410,6 +4431,8 @@ mod tests {
readable_roots: vec![],
},
network_access: false,
allow_limited_git_writes: false,
exclude_tmpdir_env_var: true,
exclude_slash_tmp: false,
};
@@ -4636,6 +4659,8 @@ mod tests {
readable_roots: vec![docs],
},
network_access: false,
allow_limited_git_writes: false,
exclude_tmpdir_env_var: true,
exclude_slash_tmp: true,
};
@@ -4708,6 +4733,8 @@ mod tests {
writable_roots: vec![],
read_only_access: ReadOnlyAccess::FullAccess,
network_access: false,
allow_limited_git_writes: false,
exclude_tmpdir_env_var: true,
exclude_slash_tmp: true,
},
@@ -4728,6 +4755,8 @@ mod tests {
readable_roots: vec![nested_readable_root],
},
network_access: false,
allow_limited_git_writes: false,
exclude_tmpdir_env_var: true,
exclude_slash_tmp: true,
},
+124
View File
@@ -0,0 +1,124 @@
use std::collections::HashSet;
use std::ffi::OsStr;
use codex_utils_absolute_path::AbsolutePathBuf;
use tracing::error;
pub(crate) fn default_read_only_subpaths_for_writable_root(
writable_root: &AbsolutePathBuf,
protect_missing_dot_codex: bool,
allow_limited_git_writes: bool,
) -> Vec<AbsolutePathBuf> {
let mut subpaths: Vec<AbsolutePathBuf> = Vec::new();
let top_level_git = writable_root.join(".git");
// This applies to typical repos (directory .git), worktrees/submodules
// (file .git with gitdir pointer), and bare repos when the gitdir is the
// writable root itself.
let top_level_git_is_file = top_level_git.as_path().is_file();
let top_level_git_is_dir = top_level_git.as_path().is_dir();
if top_level_git_is_dir || top_level_git_is_file {
if top_level_git_is_file
&& is_git_pointer_file(&top_level_git)
&& let Some(gitdir) = resolve_gitdir_from_file(&top_level_git)
{
if allow_limited_git_writes {
subpaths.extend(protected_git_subpaths_when_writable(&gitdir));
} else {
subpaths.push(gitdir);
}
}
if allow_limited_git_writes {
if top_level_git_is_dir {
subpaths.extend(protected_git_subpaths_when_writable(&top_level_git));
}
} else {
subpaths.push(top_level_git);
}
}
let top_level_agents = writable_root.join(".agents");
if top_level_agents.as_path().is_dir() {
subpaths.push(top_level_agents);
}
// Keep top-level project metadata under .codex read-only to the agent by
// default. For the workspace root itself, protect it even before the
// directory exists so first-time creation still goes through the
// protected-path approval flow.
let top_level_codex = writable_root.join(".codex");
if protect_missing_dot_codex || top_level_codex.as_path().is_dir() {
subpaths.push(top_level_codex);
}
let mut deduped = Vec::with_capacity(subpaths.len());
let mut seen = HashSet::new();
for path in subpaths {
if seen.insert(path.to_path_buf()) {
deduped.push(path);
}
}
deduped
}
fn protected_git_subpaths_when_writable(gitdir: &AbsolutePathBuf) -> Vec<AbsolutePathBuf> {
["config", "hooks"]
.into_iter()
.map(|subpath| gitdir.join(subpath))
.collect()
}
fn is_git_pointer_file(path: &AbsolutePathBuf) -> bool {
path.as_path().is_file() && path.as_path().file_name() == Some(OsStr::new(".git"))
}
fn resolve_gitdir_from_file(dot_git: &AbsolutePathBuf) -> Option<AbsolutePathBuf> {
let contents = match std::fs::read_to_string(dot_git.as_path()) {
Ok(contents) => contents,
Err(err) => {
error!(
"Failed to read {path} for gitdir pointer: {err}",
path = dot_git.as_path().display()
);
return None;
}
};
let trimmed = contents.trim();
let (_, gitdir_raw) = match trimmed.split_once(':') {
Some(parts) => parts,
None => {
error!(
"Expected {path} to contain a gitdir pointer, but it did not match `gitdir: <path>`.",
path = dot_git.as_path().display()
);
return None;
}
};
let gitdir_raw = gitdir_raw.trim();
if gitdir_raw.is_empty() {
error!(
"Expected {path} to contain a gitdir pointer, but it was empty.",
path = dot_git.as_path().display()
);
return None;
}
let base = match dot_git.as_path().parent() {
Some(base) => base,
None => {
error!(
"Unable to resolve parent directory for {path}.",
path = dot_git.as_path().display()
);
return None;
}
};
let gitdir_path = AbsolutePathBuf::resolve_path_against_base(gitdir_raw, base);
if !gitdir_path.as_path().exists() {
error!(
"Resolved gitdir path {path} does not exist.",
path = gitdir_path.as_path().display()
);
return None;
}
Some(gitdir_path)
}
@@ -608,6 +608,7 @@ fn sandbox_policy_with_additional_permissions(
writable_roots,
read_only_access,
network_access,
allow_limited_git_writes,
exclude_tmpdir_env_var,
exclude_slash_tmp,
} => {
@@ -620,6 +621,7 @@ fn sandbox_policy_with_additional_permissions(
extra_reads,
),
network_access: merge_network_access(*network_access, additional_permissions),
allow_limited_git_writes: *allow_limited_git_writes,
exclude_tmpdir_env_var: *exclude_tmpdir_env_var,
exclude_slash_tmp: *exclude_slash_tmp,
}
@@ -644,6 +646,7 @@ fn sandbox_policy_with_additional_permissions(
extra_reads,
),
network_access: merge_network_access(*network_access, additional_permissions),
allow_limited_git_writes: false,
exclude_tmpdir_env_var: false,
exclude_slash_tmp: false,
}
+67
View File
@@ -365,6 +365,7 @@ fn seatbelt_legacy_workspace_write_nested_readable_root_stays_writable() {
readable_roots: vec![docs.clone()],
},
network_access: false,
allow_limited_git_writes: false,
exclude_tmpdir_env_var: true,
exclude_slash_tmp: true,
},
@@ -429,6 +430,7 @@ fn dynamic_network_policy_preserves_restricted_policy_when_proxy_config_without_
writable_roots: vec![],
read_only_access: Default::default(),
network_access: true,
allow_limited_git_writes: false,
exclude_tmpdir_env_var: false,
exclude_slash_tmp: false,
},
@@ -495,6 +497,7 @@ fn dynamic_network_policy_preserves_restricted_policy_for_managed_network_withou
writable_roots: vec![],
read_only_access: Default::default(),
network_access: true,
allow_limited_git_writes: false,
exclude_tmpdir_env_var: false,
exclude_slash_tmp: false,
},
@@ -786,6 +789,7 @@ fn create_seatbelt_args_full_network_with_proxy_is_still_proxy_only() {
writable_roots: vec![],
read_only_access: Default::default(),
network_access: true,
allow_limited_git_writes: false,
exclude_tmpdir_env_var: false,
exclude_slash_tmp: false,
},
@@ -837,6 +841,7 @@ fn create_seatbelt_args_with_read_only_git_and_codex_subpaths() {
.collect(),
read_only_access: Default::default(),
network_access: false,
allow_limited_git_writes: false,
exclude_tmpdir_env_var: true,
exclude_slash_tmp: true,
};
@@ -1037,6 +1042,65 @@ fn create_seatbelt_args_with_read_only_git_and_codex_subpaths() {
);
}
#[test]
fn create_seatbelt_args_with_allow_limited_git_writes_carves_out_config_and_hooks() {
let tmp = TempDir::new().expect("tempdir");
let PopulatedTmp {
vulnerable_root,
dot_git_canonical,
empty_root,
..
} = populate_tmpdir(tmp.path());
let cwd = tmp.path().join("cwd");
fs::create_dir_all(&cwd).expect("create cwd");
let policy = SandboxPolicy::WorkspaceWrite {
writable_roots: vec![vulnerable_root, empty_root]
.into_iter()
.map(|p| p.try_into().unwrap())
.collect(),
read_only_access: Default::default(),
network_access: false,
allow_limited_git_writes: true,
exclude_tmpdir_env_var: true,
exclude_slash_tmp: true,
};
let args = create_seatbelt_command_args(
vec!["true".to_string()],
&policy,
&cwd,
/*enforce_managed_network*/ false,
/*network*/ None,
);
let writable_definitions: Vec<String> = args
.iter()
.filter(|arg| arg.starts_with("-DWRITABLE_ROOT_"))
.cloned()
.collect();
let dot_git_definition = format!("={}", dot_git_canonical.to_string_lossy());
assert!(
!writable_definitions
.iter()
.any(|arg| arg.ends_with(&dot_git_definition)),
"allow_limited_git_writes should not carve out the entire .git directory in {writable_definitions:#?}"
);
for protected_path in [
dot_git_canonical.join("config"),
dot_git_canonical.join("hooks"),
] {
let protected_definition = format!("={}", protected_path.to_string_lossy());
assert!(
writable_definitions
.iter()
.any(|arg| arg.ends_with(&protected_definition)),
"expected protected Git path {protected_path:?} in {writable_definitions:#?}"
);
}
}
#[test]
fn create_seatbelt_args_block_first_time_dot_codex_creation_with_exact_and_descendant_carveouts() {
let tmp = TempDir::new().expect("tempdir");
@@ -1056,6 +1120,7 @@ fn create_seatbelt_args_block_first_time_dot_codex_creation_with_exact_and_desce
writable_roots: vec![repo_root.as_path().try_into().expect("absolute repo root")],
read_only_access: Default::default(),
network_access: false,
allow_limited_git_writes: false,
exclude_tmpdir_env_var: true,
exclude_slash_tmp: true,
};
@@ -1112,6 +1177,7 @@ fn create_seatbelt_args_with_read_only_git_pointer_file() {
writable_roots: vec![worktree_root.try_into().expect("worktree_root is absolute")],
read_only_access: Default::default(),
network_access: false,
allow_limited_git_writes: false,
exclude_tmpdir_env_var: true,
exclude_slash_tmp: true,
};
@@ -1208,6 +1274,7 @@ fn create_seatbelt_args_for_cwd_as_git_repo() {
writable_roots: vec![],
read_only_access: Default::default(),
network_access: false,
allow_limited_git_writes: false,
exclude_tmpdir_env_var: false,
exclude_slash_tmp: false,
};
@@ -56,6 +56,7 @@ async fn preset_matching_accepts_workspace_write_with_extra_roots() {
writable_roots: vec![extra_root],
read_only_access: Default::default(),
network_access: false,
allow_limited_git_writes: false,
exclude_tmpdir_env_var: false,
exclude_slash_tmp: false,
};
@@ -539,6 +540,7 @@ async fn permissions_selection_marks_auto_review_current_with_custom_workspace_w
writable_roots: vec![extra_root],
read_only_access: ReadOnlyAccess::FullAccess,
network_access: false,
allow_limited_git_writes: false,
exclude_tmpdir_env_var: false,
exclude_slash_tmp: false,
},
+2
View File
@@ -104,6 +104,7 @@ async fn status_snapshot_includes_reasoning_details() {
writable_roots: Vec::new(),
read_only_access: Default::default(),
network_access: false,
allow_limited_git_writes: false,
exclude_tmpdir_env_var: false,
exclude_slash_tmp: false,
})
@@ -190,6 +191,7 @@ async fn status_permissions_non_default_workspace_write_is_custom() {
writable_roots: Vec::new(),
read_only_access: Default::default(),
network_access: true,
allow_limited_git_writes: false,
exclude_tmpdir_env_var: false,
exclude_slash_tmp: false,
})
@@ -24,6 +24,7 @@ pub fn summarize_sandbox_policy(sandbox_policy: &SandboxPolicy) -> String {
exclude_tmpdir_env_var,
exclude_slash_tmp,
read_only_access: _,
allow_limited_git_writes: _,
} => {
let mut summary = "workspace-write".to_string();
@@ -89,6 +90,7 @@ mod tests {
writable_roots: vec![writable_root.clone()],
read_only_access: Default::default(),
network_access: true,
allow_limited_git_writes: false,
exclude_tmpdir_env_var: true,
exclude_slash_tmp: true,
});
+150 -19
View File
@@ -2,6 +2,7 @@ use crate::policy::SandboxPolicy;
use dunce::canonicalize;
use std::collections::HashMap;
use std::collections::HashSet;
use std::fs;
use std::path::Path;
use std::path::PathBuf;
@@ -37,28 +38,36 @@ pub fn compute_allow_paths(
..
}
);
let allow_limited_git_writes = matches!(
policy,
SandboxPolicy::WorkspaceWrite {
allow_limited_git_writes: true,
..
}
);
if matches!(policy, SandboxPolicy::WorkspaceWrite { .. }) {
let add_writable_root =
|root: PathBuf,
policy_cwd: &Path,
add_allow: &mut dyn FnMut(PathBuf),
add_deny: &mut dyn FnMut(PathBuf)| {
let candidate = if root.is_absolute() {
root
} else {
policy_cwd.join(root)
};
let canonical = canonicalize(&candidate).unwrap_or(candidate);
add_allow(canonical.clone());
for protected_subdir in [".git", ".codex", ".agents"] {
let protected_entry = canonical.join(protected_subdir);
if protected_entry.exists() {
add_deny(protected_entry);
}
}
let add_writable_root = |root: PathBuf,
policy_cwd: &Path,
add_allow: &mut dyn FnMut(PathBuf),
add_deny: &mut dyn FnMut(PathBuf)| {
let candidate = if root.is_absolute() {
root
} else {
policy_cwd.join(root)
};
let canonical = canonicalize(&candidate).unwrap_or(candidate);
add_allow(canonical.clone());
add_git_deny_paths(&canonical, allow_limited_git_writes, add_deny);
for protected_subdir in [".codex", ".agents"] {
let protected_entry = canonical.join(protected_subdir);
if protected_entry.exists() {
add_deny(protected_entry);
}
}
};
add_writable_root(
command_cwd.to_path_buf(),
@@ -92,6 +101,52 @@ pub fn compute_allow_paths(
AllowDenyPaths { allow, deny }
}
fn add_git_deny_paths(
canonical_writable_root: &Path,
allow_limited_git_writes: bool,
add_deny: &mut dyn FnMut(PathBuf),
) {
let dot_git = canonical_writable_root.join(".git");
if allow_limited_git_writes {
if dot_git.is_dir() {
add_deny(dot_git.join("config"));
add_deny(dot_git.join("hooks"));
} else if dot_git.is_file()
&& let Some(gitdir) = resolve_gitdir_from_file(&dot_git)
{
add_deny(gitdir.join("config"));
add_deny(gitdir.join("hooks"));
}
} else if dot_git.exists() {
add_deny(dot_git.clone());
if dot_git.is_file()
&& let Some(gitdir) = resolve_gitdir_from_file(&dot_git)
{
add_deny(gitdir);
}
}
}
fn resolve_gitdir_from_file(dot_git: &Path) -> Option<PathBuf> {
let contents = fs::read_to_string(dot_git).ok()?;
let (_, gitdir_raw) = contents.trim().split_once(':')?;
let gitdir_raw = gitdir_raw.trim();
if gitdir_raw.is_empty() {
return None;
}
let gitdir = PathBuf::from(gitdir_raw);
let gitdir = if gitdir.is_absolute() {
gitdir
} else {
dot_git.parent()?.join(gitdir)
};
if gitdir.exists() {
Some(canonicalize(&gitdir).unwrap_or(gitdir))
} else {
None
}
}
#[cfg(test)]
mod tests {
use super::*;
@@ -112,6 +167,7 @@ mod tests {
writable_roots: vec![AbsolutePathBuf::try_from(extra_root.as_path()).unwrap()],
read_only_access: Default::default(),
network_access: false,
allow_limited_git_writes: false,
exclude_tmpdir_env_var: false,
exclude_slash_tmp: false,
};
@@ -139,6 +195,7 @@ mod tests {
writable_roots: vec![],
read_only_access: Default::default(),
network_access: false,
allow_limited_git_writes: false,
exclude_tmpdir_env_var: true,
exclude_slash_tmp: false,
};
@@ -167,6 +224,7 @@ mod tests {
writable_roots: vec![],
read_only_access: Default::default(),
network_access: false,
allow_limited_git_writes: false,
exclude_tmpdir_env_var: true,
exclude_slash_tmp: false,
};
@@ -195,6 +253,7 @@ mod tests {
writable_roots: vec![],
read_only_access: Default::default(),
network_access: false,
allow_limited_git_writes: false,
exclude_tmpdir_env_var: true,
exclude_slash_tmp: false,
};
@@ -211,6 +270,76 @@ mod tests {
assert_eq!(expected_deny, paths.deny);
}
#[test]
fn allows_git_metadata_except_config_and_hooks_when_requested() {
let tmp = TempDir::new().expect("tempdir");
let command_cwd = tmp.path().join("workspace");
let git_dir = command_cwd.join(".git");
let git_config = git_dir.join("config");
let git_hooks = git_dir.join("hooks");
fs::create_dir_all(&git_hooks).expect("create git hooks");
fs::write(&git_config, "[core]\n").expect("write git config");
let policy = SandboxPolicy::WorkspaceWrite {
writable_roots: vec![],
read_only_access: Default::default(),
network_access: false,
allow_limited_git_writes: true,
exclude_tmpdir_env_var: true,
exclude_slash_tmp: false,
};
let paths = compute_allow_paths(&policy, &command_cwd, &command_cwd, &HashMap::new());
let expected_allow: HashSet<PathBuf> = [dunce::canonicalize(&command_cwd).unwrap()]
.into_iter()
.collect();
let expected_deny: HashSet<PathBuf> = [
dunce::canonicalize(&git_config).unwrap(),
dunce::canonicalize(&git_hooks).unwrap(),
]
.into_iter()
.collect();
assert_eq!(expected_allow, paths.allow);
assert_eq!(expected_deny, paths.deny);
}
#[test]
fn allow_limited_git_writes_resolves_gitdir_pointer_file() {
let tmp = TempDir::new().expect("tempdir");
let command_cwd = tmp.path().join("workspace");
let dot_git = command_cwd.join(".git");
let gitdir = command_cwd.join("repo.git").join("worktrees").join("example");
let git_config = gitdir.join("config");
let git_hooks = gitdir.join("hooks");
fs::create_dir_all(&git_hooks).expect("create git hooks");
fs::write(&git_config, "[core]\n").expect("write git config");
fs::write(&dot_git, "gitdir: repo.git/worktrees/example").expect("write git pointer");
let policy = SandboxPolicy::WorkspaceWrite {
writable_roots: vec![],
read_only_access: Default::default(),
network_access: false,
allow_limited_git_writes: true,
exclude_tmpdir_env_var: true,
exclude_slash_tmp: false,
};
let paths = compute_allow_paths(&policy, &command_cwd, &command_cwd, &HashMap::new());
let expected_allow: HashSet<PathBuf> = [dunce::canonicalize(&command_cwd).unwrap()]
.into_iter()
.collect();
let expected_deny: HashSet<PathBuf> = [
dunce::canonicalize(&git_config).unwrap(),
dunce::canonicalize(&git_hooks).unwrap(),
]
.into_iter()
.collect();
assert_eq!(expected_allow, paths.allow);
assert_eq!(expected_deny, paths.deny);
}
#[test]
fn denies_codex_and_agents_inside_writable_root() {
let tmp = TempDir::new().expect("tempdir");
@@ -224,6 +353,7 @@ mod tests {
writable_roots: vec![],
read_only_access: Default::default(),
network_access: false,
allow_limited_git_writes: false,
exclude_tmpdir_env_var: true,
exclude_slash_tmp: false,
};
@@ -253,6 +383,7 @@ mod tests {
writable_roots: vec![],
read_only_access: Default::default(),
network_access: false,
allow_limited_git_writes: false,
exclude_tmpdir_env_var: true,
exclude_slash_tmp: false,
};
@@ -492,6 +492,7 @@ mod windows_impl {
writable_roots: Vec::new(),
read_only_access: Default::default(),
network_access,
allow_limited_git_writes: false,
exclude_tmpdir_env_var: false,
exclude_slash_tmp: false,
}
+1
View File
@@ -644,6 +644,7 @@ mod windows_impl {
writable_roots: Vec::new(),
read_only_access: Default::default(),
network_access,
allow_limited_git_writes: false,
exclude_tmpdir_env_var: false,
exclude_slash_tmp: false,
}
@@ -1374,6 +1374,7 @@ mod tests {
readable_roots: Vec::new(),
},
network_access: false,
allow_limited_git_writes: false,
exclude_tmpdir_env_var: true,
exclude_slash_tmp: true,
};