mirror of
https://github.com/openai/codex.git
synced 2026-11-21 22:50:37 +00:00
fix(windows): preserve deny-read during escalation
Co-authored-by: Codex noreply@openai.com
This commit is contained in:
co-authored by
Codex noreply@openai.com
parent
891f1f4c85
commit
f56b2ae731
@@ -29,6 +29,7 @@ use crate::tools::sandboxing::ToolRuntime;
|
||||
use crate::tools::sandboxing::default_exec_approval_requirement;
|
||||
use crate::tools::sandboxing::sandbox_override_for_first_attempt;
|
||||
use crate::tools::sandboxing::unsandboxed_execution_allowed;
|
||||
use crate::tools::sandboxing::windows_escalation_profile_preserving_deny_read;
|
||||
use codex_hooks::PermissionRequestDecision;
|
||||
use codex_otel::ToolDecisionSource;
|
||||
use codex_protocol::error::CodexErr;
|
||||
@@ -152,7 +153,6 @@ impl ToolOrchestrator {
|
||||
let mut already_approved = false;
|
||||
|
||||
let file_system_sandbox_policy = turn_ctx.file_system_sandbox_policy();
|
||||
let network_sandbox_policy = turn_ctx.network_sandbox_policy();
|
||||
let requirement = tool.exec_approval_requirement(req).unwrap_or_else(|| {
|
||||
default_exec_approval_requirement(approval_policy, &file_system_sandbox_policy)
|
||||
});
|
||||
@@ -227,20 +227,32 @@ impl ToolOrchestrator {
|
||||
&file_system_sandbox_policy,
|
||||
);
|
||||
let managed_network_active = turn_ctx.network.is_some();
|
||||
let windows_escalation_profile = windows_escalation_profile_preserving_deny_read(
|
||||
cfg!(windows),
|
||||
sandbox_override,
|
||||
managed_network_active,
|
||||
&turn_ctx.permission_profile,
|
||||
);
|
||||
let attempt_permissions = windows_escalation_profile
|
||||
.as_ref()
|
||||
.unwrap_or(&turn_ctx.permission_profile);
|
||||
let attempt_network_sandbox_policy = attempt_permissions.network_sandbox_policy();
|
||||
let sandbox_preference = tool.sandbox_preference();
|
||||
let sandbox_requested = match sandbox_override {
|
||||
SandboxOverride::BypassSandboxFirstAttempt => false,
|
||||
SandboxOverride::NoOverride => self.sandbox.should_sandbox(
|
||||
&file_system_sandbox_policy,
|
||||
network_sandbox_policy,
|
||||
sandbox_preference,
|
||||
managed_network_active,
|
||||
),
|
||||
SandboxOverride::NoOverride | SandboxOverride::PreserveDenyRead => {
|
||||
self.sandbox.should_sandbox(
|
||||
&file_system_sandbox_policy,
|
||||
attempt_network_sandbox_policy,
|
||||
sandbox_preference,
|
||||
managed_network_active,
|
||||
)
|
||||
}
|
||||
};
|
||||
let initial_sandbox = if sandbox_requested {
|
||||
self.sandbox.select_initial(
|
||||
&file_system_sandbox_policy,
|
||||
network_sandbox_policy,
|
||||
attempt_network_sandbox_policy,
|
||||
sandbox_preference,
|
||||
turn_ctx.windows_sandbox_level,
|
||||
managed_network_active,
|
||||
@@ -260,7 +272,7 @@ impl ToolOrchestrator {
|
||||
let initial_attempt = SandboxAttempt {
|
||||
sandbox: initial_sandbox,
|
||||
sandbox_requested,
|
||||
permissions: &turn_ctx.permission_profile,
|
||||
permissions: attempt_permissions,
|
||||
exec_server_permissions: turn_ctx.config.permissions.permission_profile(),
|
||||
enforce_managed_network: managed_network_active,
|
||||
manager: &self.sandbox,
|
||||
@@ -419,14 +431,14 @@ impl ToolOrchestrator {
|
||||
let retry_sandbox_requested = !unsandboxed_allowed
|
||||
&& self.sandbox.should_sandbox(
|
||||
&file_system_sandbox_policy,
|
||||
network_sandbox_policy,
|
||||
attempt_network_sandbox_policy,
|
||||
sandbox_preference,
|
||||
managed_network_active,
|
||||
);
|
||||
let retry_sandbox = if retry_sandbox_requested {
|
||||
self.sandbox.select_initial(
|
||||
&file_system_sandbox_policy,
|
||||
network_sandbox_policy,
|
||||
attempt_network_sandbox_policy,
|
||||
sandbox_preference,
|
||||
turn_ctx.windows_sandbox_level,
|
||||
managed_network_active,
|
||||
@@ -442,7 +454,7 @@ impl ToolOrchestrator {
|
||||
let retry_attempt = SandboxAttempt {
|
||||
sandbox: retry_sandbox,
|
||||
sandbox_requested: retry_sandbox_requested,
|
||||
permissions: &turn_ctx.permission_profile,
|
||||
permissions: attempt_permissions,
|
||||
exec_server_permissions: turn_ctx.config.permissions.permission_profile(),
|
||||
enforce_managed_network: managed_network_active,
|
||||
manager: &self.sandbox,
|
||||
|
||||
@@ -16,8 +16,10 @@ use codex_network_proxy::NetworkProxy;
|
||||
use codex_protocol::approvals::ExecPolicyAmendment;
|
||||
use codex_protocol::approvals::NetworkApprovalContext;
|
||||
use codex_protocol::error::CodexErr;
|
||||
use codex_protocol::models::PermissionProfile;
|
||||
use codex_protocol::permissions::FileSystemSandboxKind;
|
||||
use codex_protocol::permissions::FileSystemSandboxPolicy;
|
||||
use codex_protocol::permissions::NetworkSandboxPolicy;
|
||||
use codex_protocol::protocol::AskForApproval;
|
||||
use codex_protocol::protocol::ReviewDecision;
|
||||
use codex_sandboxing::SandboxCommand;
|
||||
@@ -243,6 +245,7 @@ pub(crate) fn default_exec_approval_requirement(
|
||||
pub(crate) enum SandboxOverride {
|
||||
NoOverride,
|
||||
BypassSandboxFirstAttempt,
|
||||
PreserveDenyRead,
|
||||
}
|
||||
|
||||
pub(crate) fn sandbox_override_for_first_attempt(
|
||||
@@ -250,30 +253,55 @@ pub(crate) fn sandbox_override_for_first_attempt(
|
||||
exec_approval_requirement: &ExecApprovalRequirement,
|
||||
file_system_sandbox_policy: &FileSystemSandboxPolicy,
|
||||
) -> SandboxOverride {
|
||||
// Deny-read restrictions are part of the active permission policy. Running
|
||||
// without a filesystem sandbox would discard them, even if the command was
|
||||
// otherwise approved by rules or explicit escalation.
|
||||
if !unsandboxed_execution_allowed(file_system_sandbox_policy) {
|
||||
return SandboxOverride::NoOverride;
|
||||
}
|
||||
|
||||
// ExecPolicy `Allow` can intentionally imply full trust (Skip + bypass_sandbox=true),
|
||||
// which supersedes `with_additional_permissions` sandboxed execution hints.
|
||||
if matches!(
|
||||
let bypass_sandbox = matches!(
|
||||
exec_approval_requirement,
|
||||
ExecApprovalRequirement::Skip {
|
||||
bypass_sandbox: true,
|
||||
..
|
||||
}
|
||||
) {
|
||||
) || sandbox_permissions.requires_escalated_permissions();
|
||||
|
||||
// Deny-read restrictions are part of the active permission policy. Running
|
||||
// without a filesystem sandbox would discard them, even if the command was
|
||||
// otherwise approved by rules or explicit escalation.
|
||||
if !unsandboxed_execution_allowed(file_system_sandbox_policy) {
|
||||
return if bypass_sandbox {
|
||||
SandboxOverride::PreserveDenyRead
|
||||
} else {
|
||||
SandboxOverride::NoOverride
|
||||
};
|
||||
}
|
||||
|
||||
// ExecPolicy `Allow` can intentionally imply full trust (Skip + bypass_sandbox=true),
|
||||
// which supersedes `with_additional_permissions` sandboxed execution hints.
|
||||
if bypass_sandbox {
|
||||
return SandboxOverride::BypassSandboxFirstAttempt;
|
||||
}
|
||||
|
||||
if sandbox_permissions.requires_escalated_permissions() {
|
||||
SandboxOverride::BypassSandboxFirstAttempt
|
||||
} else {
|
||||
SandboxOverride::NoOverride
|
||||
SandboxOverride::NoOverride
|
||||
}
|
||||
|
||||
pub(crate) fn windows_escalation_profile_preserving_deny_read(
|
||||
is_windows: bool,
|
||||
sandbox_override: SandboxOverride,
|
||||
managed_network_active: bool,
|
||||
permission_profile: &PermissionProfile,
|
||||
) -> Option<PermissionProfile> {
|
||||
if !is_windows
|
||||
|| !matches!(sandbox_override, SandboxOverride::PreserveDenyRead)
|
||||
|| managed_network_active
|
||||
{
|
||||
return None;
|
||||
}
|
||||
|
||||
let file_system_policy = permission_profile.file_system_sandbox_policy();
|
||||
Some(
|
||||
PermissionProfile::from_runtime_permissions_with_enforcement(
|
||||
permission_profile.enforcement(),
|
||||
&file_system_policy,
|
||||
NetworkSandboxPolicy::Enabled,
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
/// Returns true when the active filesystem policy can be represented by
|
||||
|
||||
@@ -162,7 +162,7 @@ fn deny_read_blocks_explicit_escalation_and_policy_bypass() {
|
||||
},
|
||||
&file_system_policy,
|
||||
),
|
||||
SandboxOverride::NoOverride,
|
||||
SandboxOverride::PreserveDenyRead,
|
||||
"explicit escalation would drop deny-read filesystem policy, so keep the first attempt sandboxed",
|
||||
);
|
||||
assert!(!unsandboxed_execution_allowed(&file_system_policy));
|
||||
@@ -196,9 +196,36 @@ fn deny_read_blocks_explicit_escalation_and_policy_bypass() {
|
||||
},
|
||||
&file_system_policy,
|
||||
),
|
||||
SandboxOverride::NoOverride,
|
||||
SandboxOverride::PreserveDenyRead,
|
||||
"exec-policy allow rules would drop deny-read filesystem policy, so keep the first attempt sandboxed",
|
||||
);
|
||||
|
||||
let permission_profile = PermissionProfile::from_runtime_permissions(
|
||||
&file_system_policy,
|
||||
NetworkSandboxPolicy::Restricted,
|
||||
);
|
||||
let escalated = windows_escalation_profile_preserving_deny_read(
|
||||
/*is_windows*/ true,
|
||||
SandboxOverride::PreserveDenyRead,
|
||||
/*managed_network_active*/ false,
|
||||
&permission_profile,
|
||||
)
|
||||
.expect("Windows escalation profile");
|
||||
assert_eq!(escalated.file_system_sandbox_policy(), file_system_policy);
|
||||
assert_eq!(
|
||||
escalated.network_sandbox_policy(),
|
||||
NetworkSandboxPolicy::Enabled
|
||||
);
|
||||
assert_eq!(
|
||||
windows_escalation_profile_preserving_deny_read(
|
||||
/*is_windows*/ true,
|
||||
SandboxOverride::PreserveDenyRead,
|
||||
/*managed_network_active*/ true,
|
||||
&permission_profile,
|
||||
),
|
||||
None,
|
||||
"managed network requirements remain authoritative",
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -106,6 +106,13 @@ impl ResolvedWindowsSandboxPermissions {
|
||||
self.file_system.has_full_disk_read_access()
|
||||
}
|
||||
|
||||
pub(crate) fn has_root_read_access_for_cwd(&self, cwd: &Path) -> bool {
|
||||
let Some(root) = cwd.ancestors().last() else {
|
||||
return false;
|
||||
};
|
||||
self.file_system.can_read_path_with_cwd(root, cwd)
|
||||
}
|
||||
|
||||
pub(crate) fn include_platform_defaults(&self) -> bool {
|
||||
self.file_system.include_platform_defaults()
|
||||
}
|
||||
|
||||
@@ -417,8 +417,12 @@ fn gather_full_read_roots_for_permissions(
|
||||
.iter()
|
||||
.map(PathBuf::from),
|
||||
);
|
||||
if let Ok(up) = std::env::var("USERPROFILE") {
|
||||
roots.extend(profile_read_roots(Path::new(&up)));
|
||||
if let Some(user_profile) = env_map
|
||||
.get("USERPROFILE")
|
||||
.map(PathBuf::from)
|
||||
.or_else(|| std::env::var_os("USERPROFILE").map(PathBuf::from))
|
||||
{
|
||||
roots.extend(profile_read_roots(&user_profile));
|
||||
}
|
||||
roots.push(command_cwd.to_path_buf());
|
||||
roots.extend(
|
||||
@@ -436,7 +440,7 @@ pub(crate) fn gather_read_roots(
|
||||
env_map: &HashMap<String, String>,
|
||||
codex_home: &Path,
|
||||
) -> Vec<PathBuf> {
|
||||
if permissions.has_full_disk_read_access() {
|
||||
if permissions.has_root_read_access_for_cwd(command_cwd) {
|
||||
return gather_full_read_roots_for_permissions(
|
||||
command_cwd,
|
||||
permissions,
|
||||
@@ -1151,6 +1155,11 @@ mod tests {
|
||||
use crate::setup_error::extract_failure;
|
||||
use crate::setup_error::write_setup_error_report;
|
||||
use codex_protocol::models::PermissionProfile;
|
||||
use codex_protocol::permissions::FileSystemAccessMode;
|
||||
use codex_protocol::permissions::FileSystemPath;
|
||||
use codex_protocol::permissions::FileSystemSandboxEntry;
|
||||
use codex_protocol::permissions::FileSystemSandboxPolicy;
|
||||
use codex_protocol::permissions::FileSystemSpecialPath;
|
||||
use codex_protocol::permissions::NetworkSandboxPolicy;
|
||||
use codex_utils_absolute_path::AbsolutePathBuf;
|
||||
use pretty_assertions::assert_eq;
|
||||
@@ -1662,6 +1671,45 @@ mod tests {
|
||||
assert!(roots.contains(&expected));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn gather_read_roots_preserves_profile_access_with_deny_read() {
|
||||
let tmp = TempDir::new().expect("tempdir");
|
||||
let codex_home = tmp.path().join("codex-home");
|
||||
let user_profile = tmp.path().join("user-profile");
|
||||
let documents = user_profile.join("Documents");
|
||||
let command_cwd = documents.join("workspace");
|
||||
fs::create_dir_all(&command_cwd).expect("create workspace");
|
||||
let file_system_policy = FileSystemSandboxPolicy::restricted(vec![
|
||||
FileSystemSandboxEntry {
|
||||
path: FileSystemPath::Special {
|
||||
value: FileSystemSpecialPath::Root,
|
||||
},
|
||||
access: FileSystemAccessMode::Read,
|
||||
},
|
||||
FileSystemSandboxEntry {
|
||||
path: FileSystemPath::GlobPattern {
|
||||
pattern: "**/*.env".to_string(),
|
||||
},
|
||||
access: FileSystemAccessMode::Deny,
|
||||
},
|
||||
]);
|
||||
let permission_profile = PermissionProfile::from_runtime_permissions(
|
||||
&file_system_policy,
|
||||
NetworkSandboxPolicy::Restricted,
|
||||
);
|
||||
let workspace_roots = workspace_roots_for(command_cwd.as_path());
|
||||
let permissions = permissions_for(&permission_profile, workspace_roots.as_slice());
|
||||
let env_map = HashMap::from([(
|
||||
"USERPROFILE".to_string(),
|
||||
user_profile.display().to_string(),
|
||||
)]);
|
||||
|
||||
let roots = gather_read_roots(&command_cwd, &permissions, &env_map, &codex_home);
|
||||
let expected = dunce::canonicalize(&documents).expect("canonical documents");
|
||||
|
||||
assert!(roots.contains(&expected));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn workspace_write_roots_remain_readable() {
|
||||
let tmp = TempDir::new().expect("tempdir");
|
||||
|
||||
Reference in New Issue
Block a user