mirror of
https://github.com/openai/codex.git
synced 2026-05-16 19:51:15 +03:00
Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
49a9062253 | ||
|
|
56238d2fa2 | ||
|
|
86e11d7697 |
@@ -1,4 +1,5 @@
|
||||
use crate::policy::SandboxPolicy;
|
||||
use crate::resolved_permissions::ResolvedWindowsSandboxPermissions;
|
||||
use dunce::canonicalize;
|
||||
use std::collections::HashMap;
|
||||
use std::collections::HashSet;
|
||||
@@ -11,11 +12,21 @@ pub struct AllowDenyPaths {
|
||||
pub deny: HashSet<PathBuf>,
|
||||
}
|
||||
|
||||
pub fn compute_allow_paths(
|
||||
pub(crate) fn compute_allow_paths(
|
||||
policy: &SandboxPolicy,
|
||||
policy_cwd: &Path,
|
||||
command_cwd: &Path,
|
||||
env_map: &HashMap<String, String>,
|
||||
) -> AllowDenyPaths {
|
||||
let permissions =
|
||||
ResolvedWindowsSandboxPermissions::from_legacy_policy_for_cwd(policy, policy_cwd);
|
||||
compute_allow_paths_for_permissions(&permissions, command_cwd, env_map)
|
||||
}
|
||||
|
||||
pub(crate) fn compute_allow_paths_for_permissions(
|
||||
permissions: &ResolvedWindowsSandboxPermissions,
|
||||
command_cwd: &Path,
|
||||
env_map: &HashMap<String, String>,
|
||||
) -> AllowDenyPaths {
|
||||
let mut allow: HashSet<PathBuf> = HashSet::new();
|
||||
let mut deny: HashSet<PathBuf> = HashSet::new();
|
||||
@@ -30,65 +41,15 @@ pub fn compute_allow_paths(
|
||||
deny.insert(p);
|
||||
}
|
||||
};
|
||||
let include_tmp_env_vars = matches!(
|
||||
policy,
|
||||
SandboxPolicy::WorkspaceWrite {
|
||||
exclude_tmpdir_env_var: false,
|
||||
..
|
||||
}
|
||||
);
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
add_writable_root(
|
||||
command_cwd.to_path_buf(),
|
||||
policy_cwd,
|
||||
&mut add_allow_path,
|
||||
&mut add_deny_path,
|
||||
);
|
||||
|
||||
if let SandboxPolicy::WorkspaceWrite { writable_roots, .. } = policy {
|
||||
for root in writable_roots {
|
||||
add_writable_root(
|
||||
root.clone().into(),
|
||||
policy_cwd,
|
||||
&mut add_allow_path,
|
||||
&mut add_deny_path,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
if include_tmp_env_vars {
|
||||
for key in ["TEMP", "TMP"] {
|
||||
if let Some(v) = env_map.get(key) {
|
||||
let abs = PathBuf::from(v);
|
||||
add_allow_path(abs);
|
||||
} else if let Ok(v) = std::env::var(key) {
|
||||
let abs = PathBuf::from(v);
|
||||
add_allow_path(abs);
|
||||
}
|
||||
for writable_root in permissions.writable_roots_for_cwd(command_cwd, env_map) {
|
||||
let canonical = canonicalize(&writable_root.root).unwrap_or(writable_root.root);
|
||||
add_allow_path(canonical);
|
||||
for read_only_subpath in writable_root.read_only_subpaths {
|
||||
add_deny_path(read_only_subpath);
|
||||
}
|
||||
}
|
||||
|
||||
AllowDenyPaths { allow, deny }
|
||||
}
|
||||
|
||||
@@ -130,6 +91,35 @@ mod tests {
|
||||
assert!(paths.deny.is_empty(), "no deny paths expected");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn uses_policy_cwd_for_legacy_workspace_root() {
|
||||
let tmp = TempDir::new().expect("tempdir");
|
||||
let policy_cwd = tmp.path().join("workspace");
|
||||
let command_cwd = policy_cwd.join("subdir");
|
||||
fs::create_dir_all(&command_cwd).expect("create command cwd");
|
||||
|
||||
let policy = SandboxPolicy::WorkspaceWrite {
|
||||
writable_roots: vec![],
|
||||
network_access: false,
|
||||
exclude_tmpdir_env_var: true,
|
||||
exclude_slash_tmp: true,
|
||||
};
|
||||
|
||||
let paths = compute_allow_paths(&policy, &policy_cwd, &command_cwd, &HashMap::new());
|
||||
|
||||
assert!(
|
||||
paths
|
||||
.allow
|
||||
.contains(&dunce::canonicalize(&policy_cwd).unwrap())
|
||||
);
|
||||
assert!(
|
||||
!paths
|
||||
.allow
|
||||
.contains(&dunce::canonicalize(&command_cwd).unwrap())
|
||||
);
|
||||
assert!(paths.deny.is_empty(), "no deny paths expected");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn excludes_tmp_env_vars_when_requested() {
|
||||
let tmp = TempDir::new().expect("tempdir");
|
||||
@@ -146,6 +136,7 @@ mod tests {
|
||||
};
|
||||
let mut env_map = HashMap::new();
|
||||
env_map.insert("TEMP".into(), temp_dir.to_string_lossy().to_string());
|
||||
env_map.insert("TMP".into(), temp_dir.to_string_lossy().to_string());
|
||||
|
||||
let paths = compute_allow_paths(&policy, &command_cwd, &command_cwd, &env_map);
|
||||
|
||||
@@ -162,6 +153,59 @@ mod tests {
|
||||
assert!(paths.deny.is_empty(), "no deny paths expected");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn includes_tmp_env_vars_when_requested() {
|
||||
let tmp = TempDir::new().expect("tempdir");
|
||||
let command_cwd = tmp.path().join("workspace");
|
||||
let temp_dir = tmp.path().join("temp");
|
||||
let _ = fs::create_dir_all(&command_cwd);
|
||||
let _ = fs::create_dir_all(&temp_dir);
|
||||
|
||||
let policy = SandboxPolicy::WorkspaceWrite {
|
||||
writable_roots: vec![],
|
||||
network_access: false,
|
||||
exclude_tmpdir_env_var: false,
|
||||
exclude_slash_tmp: false,
|
||||
};
|
||||
let mut env_map = HashMap::new();
|
||||
env_map.insert("TEMP".into(), temp_dir.to_string_lossy().to_string());
|
||||
env_map.insert("TMP".into(), temp_dir.to_string_lossy().to_string());
|
||||
|
||||
let paths = compute_allow_paths(&policy, &command_cwd, &command_cwd, &env_map);
|
||||
|
||||
let expected_allow: HashSet<PathBuf> = [
|
||||
dunce::canonicalize(&command_cwd).unwrap(),
|
||||
dunce::canonicalize(&temp_dir).unwrap(),
|
||||
]
|
||||
.into_iter()
|
||||
.collect();
|
||||
|
||||
assert_eq!(expected_allow, paths.allow);
|
||||
assert!(paths.deny.is_empty(), "no deny paths expected");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ignores_unix_slash_tmp_for_windows_allow_roots() {
|
||||
let tmp = TempDir::new().expect("tempdir");
|
||||
let command_cwd = tmp.path().join("workspace");
|
||||
let _ = fs::create_dir_all(&command_cwd);
|
||||
|
||||
let policy = SandboxPolicy::WorkspaceWrite {
|
||||
writable_roots: vec![],
|
||||
network_access: false,
|
||||
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();
|
||||
|
||||
assert_eq!(expected_allow, paths.allow);
|
||||
assert!(paths.deny.is_empty(), "no deny paths expected");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn denies_git_dir_inside_writable_root() {
|
||||
let tmp = TempDir::new().expect("tempdir");
|
||||
|
||||
@@ -8,7 +8,8 @@ use crate::logging::debug_log;
|
||||
use crate::logging::log_note;
|
||||
use crate::path_normalization::canonical_path_key;
|
||||
use crate::policy::SandboxPolicy;
|
||||
use crate::setup::effective_write_roots_for_setup;
|
||||
use crate::resolved_permissions::ResolvedWindowsSandboxPermissions;
|
||||
use crate::setup::effective_write_roots_for_permissions;
|
||||
use crate::token::LocalSid;
|
||||
use crate::token::world_sid;
|
||||
use anyhow::Result;
|
||||
@@ -259,11 +260,18 @@ pub fn apply_capability_denies_for_world_writable(
|
||||
let cap_path = cap_sid_file(codex_home);
|
||||
let caps = load_or_create_cap_sids(codex_home)?;
|
||||
std::fs::write(&cap_path, serde_json::to_string(&caps)?)?;
|
||||
let (active_sids, workspace_roots): (Vec<LocalSid>, Vec<PathBuf>) = match sandbox_policy {
|
||||
SandboxPolicy::WorkspaceWrite { .. } => {
|
||||
let roots = effective_write_roots_for_setup(
|
||||
sandbox_policy,
|
||||
cwd,
|
||||
if matches!(
|
||||
sandbox_policy,
|
||||
SandboxPolicy::DangerFullAccess | SandboxPolicy::ExternalSandbox { .. }
|
||||
) {
|
||||
return Ok(());
|
||||
}
|
||||
let permissions =
|
||||
ResolvedWindowsSandboxPermissions::from_legacy_policy_for_cwd(sandbox_policy, cwd);
|
||||
let (active_sids, workspace_roots): (Vec<LocalSid>, Vec<PathBuf>) =
|
||||
if permissions.uses_write_capabilities_for_cwd(cwd, env_map) {
|
||||
let roots = effective_write_roots_for_permissions(
|
||||
&permissions,
|
||||
cwd,
|
||||
env_map,
|
||||
codex_home,
|
||||
@@ -277,14 +285,9 @@ pub fn apply_capability_denies_for_world_writable(
|
||||
})
|
||||
.collect::<Result<Vec<_>>>()?;
|
||||
(active_sids, roots)
|
||||
}
|
||||
SandboxPolicy::ReadOnly { .. } => {
|
||||
} else {
|
||||
(vec![LocalSid::from_string(&caps.readonly)?], Vec::new())
|
||||
}
|
||||
SandboxPolicy::DangerFullAccess | SandboxPolicy::ExternalSandbox { .. } => {
|
||||
return Ok(());
|
||||
}
|
||||
};
|
||||
};
|
||||
for path in flagged {
|
||||
if workspace_roots
|
||||
.iter()
|
||||
|
||||
@@ -22,11 +22,11 @@ use codex_windows_sandbox::OutputPayload;
|
||||
use codex_windows_sandbox::OutputStream;
|
||||
use codex_windows_sandbox::PipeSpawnHandles;
|
||||
use codex_windows_sandbox::ResizePayload;
|
||||
use codex_windows_sandbox::SandboxPolicy;
|
||||
use codex_windows_sandbox::SpawnReady;
|
||||
use codex_windows_sandbox::SpawnRequest;
|
||||
use codex_windows_sandbox::StderrMode;
|
||||
use codex_windows_sandbox::StdinMode;
|
||||
use codex_windows_sandbox::WindowsSandboxTokenMode;
|
||||
use codex_windows_sandbox::allow_null_device;
|
||||
use codex_windows_sandbox::create_readonly_token_with_caps_and_user_from;
|
||||
use codex_windows_sandbox::create_workspace_write_token_with_caps_and_user_from;
|
||||
@@ -35,11 +35,11 @@ use codex_windows_sandbox::encode_bytes;
|
||||
use codex_windows_sandbox::get_current_token_for_restriction;
|
||||
use codex_windows_sandbox::hide_current_user_profile_dir;
|
||||
use codex_windows_sandbox::log_note;
|
||||
use codex_windows_sandbox::parse_policy;
|
||||
use codex_windows_sandbox::read_frame;
|
||||
use codex_windows_sandbox::read_handle_loop;
|
||||
use codex_windows_sandbox::spawn_process_with_pipes;
|
||||
use codex_windows_sandbox::to_wide;
|
||||
use codex_windows_sandbox::token_mode_for_permission_profile;
|
||||
use codex_windows_sandbox::write_frame;
|
||||
use std::ffi::OsStr;
|
||||
use std::fs::File;
|
||||
@@ -235,7 +235,12 @@ fn effective_cwd(req_cwd: &Path, log_dir: Option<&Path>) -> PathBuf {
|
||||
fn spawn_ipc_process(req: &SpawnRequest) -> Result<IpcSpawnedProcess> {
|
||||
let log_dir = req.codex_home.clone();
|
||||
hide_current_user_profile_dir(req.codex_home.as_path());
|
||||
let policy = parse_policy(&req.policy_json_or_preset).context("parse policy_json_or_preset")?;
|
||||
let token_mode = token_mode_for_permission_profile(
|
||||
&req.permission_profile,
|
||||
&req.permission_profile_cwd,
|
||||
&req.env,
|
||||
)
|
||||
.context("resolve permission profile token mode")?;
|
||||
let mut cap_psids: Vec<LocalSid> = Vec::new();
|
||||
for sid in &req.cap_sids {
|
||||
cap_psids.push(
|
||||
@@ -253,16 +258,13 @@ fn spawn_ipc_process(req: &SpawnRequest) -> Result<IpcSpawnedProcess> {
|
||||
let cap_psid_ptrs: Vec<*mut _> = cap_psids.iter().map(LocalSid::as_ptr).collect();
|
||||
let base = OwnedWinHandle::new(unsafe { get_current_token_for_restriction()? });
|
||||
let h_token = OwnedWinHandle::new(unsafe {
|
||||
match &policy {
|
||||
SandboxPolicy::ReadOnly { .. } => {
|
||||
match token_mode {
|
||||
WindowsSandboxTokenMode::ReadOnlyCapability => {
|
||||
create_readonly_token_with_caps_and_user_from(base.raw(), &cap_psid_ptrs)
|
||||
}
|
||||
SandboxPolicy::WorkspaceWrite { .. } => {
|
||||
WindowsSandboxTokenMode::WriteCapabilityRoots => {
|
||||
create_workspace_write_token_with_caps_and_user_from(base.raw(), &cap_psid_ptrs)
|
||||
}
|
||||
SandboxPolicy::DangerFullAccess | SandboxPolicy::ExternalSandbox { .. } => {
|
||||
unreachable!()
|
||||
}
|
||||
}
|
||||
}?);
|
||||
unsafe {
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
use anyhow::Result;
|
||||
use base64::Engine as _;
|
||||
use base64::engine::general_purpose::STANDARD;
|
||||
use codex_protocol::models::PermissionProfile;
|
||||
use serde::Deserialize;
|
||||
use serde::Serialize;
|
||||
use std::collections::HashMap;
|
||||
@@ -55,8 +56,8 @@ pub struct SpawnRequest {
|
||||
pub command: Vec<String>,
|
||||
pub cwd: PathBuf,
|
||||
pub env: HashMap<String, String>,
|
||||
pub policy_json_or_preset: String,
|
||||
pub sandbox_policy_cwd: PathBuf,
|
||||
pub permission_profile: PermissionProfile,
|
||||
pub permission_profile_cwd: PathBuf,
|
||||
pub codex_home: PathBuf,
|
||||
pub real_codex_home: PathBuf,
|
||||
pub cap_sids: Vec<String>,
|
||||
@@ -164,6 +165,7 @@ pub fn read_frame<R: Read>(mut reader: R) -> Result<Option<FramedMessage>> {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use pretty_assertions::assert_eq;
|
||||
|
||||
#[test]
|
||||
fn framed_round_trip() {
|
||||
@@ -189,4 +191,43 @@ mod tests {
|
||||
other => panic!("unexpected message: {other:?}"),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn spawn_request_serializes_permission_profile() {
|
||||
let msg = FramedMessage {
|
||||
version: 1,
|
||||
message: Message::SpawnRequest {
|
||||
payload: Box::new(SpawnRequest {
|
||||
command: vec!["cmd.exe".to_string(), "/c".to_string(), "ver".to_string()],
|
||||
cwd: PathBuf::from(r"C:\workspace"),
|
||||
env: HashMap::new(),
|
||||
permission_profile: PermissionProfile::read_only(),
|
||||
permission_profile_cwd: PathBuf::from(r"C:\workspace"),
|
||||
codex_home: PathBuf::from(r"C:\codex"),
|
||||
real_codex_home: PathBuf::from(r"C:\Users\codex"),
|
||||
cap_sids: vec!["S-1-15-3-1024-1".to_string()],
|
||||
timeout_ms: Some(1000),
|
||||
tty: false,
|
||||
stdin_open: false,
|
||||
use_private_desktop: false,
|
||||
}),
|
||||
},
|
||||
};
|
||||
|
||||
let encoded = serde_json::to_value(&msg).expect("serialize");
|
||||
assert_eq!("spawn_request", encoded["type"]);
|
||||
assert_eq!("managed", encoded["payload"]["permission_profile"]["type"]);
|
||||
assert_eq!(None, encoded["payload"].get("policy_json_or_preset"));
|
||||
assert_eq!(None, encoded["payload"].get("sandbox_policy_cwd"));
|
||||
|
||||
let decoded: FramedMessage = serde_json::from_value(encoded).expect("deserialize");
|
||||
let Message::SpawnRequest { payload } = decoded.message else {
|
||||
panic!("unexpected message");
|
||||
};
|
||||
assert_eq!(PermissionProfile::read_only(), payload.permission_profile);
|
||||
assert_eq!(
|
||||
PathBuf::from(r"C:\workspace"),
|
||||
payload.permission_profile_cwd
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -39,12 +39,14 @@ mod windows_impl {
|
||||
use crate::logging::log_success;
|
||||
use crate::policy::SandboxPolicy;
|
||||
use crate::policy::parse_policy;
|
||||
use crate::resolved_permissions::ResolvedWindowsSandboxPermissions;
|
||||
use crate::runner_client::spawn_runner_transport;
|
||||
use crate::sandbox_utils::ensure_codex_home_exists;
|
||||
use crate::sandbox_utils::inject_git_safe_directory;
|
||||
use crate::setup::effective_write_roots_for_setup;
|
||||
use crate::setup::effective_write_roots_for_permissions;
|
||||
use crate::token::LocalSid;
|
||||
use anyhow::Result;
|
||||
use codex_protocol::models::PermissionProfile;
|
||||
use codex_utils_absolute_path::AbsolutePathBuf;
|
||||
use std::path::Path;
|
||||
|
||||
@@ -80,6 +82,10 @@ mod windows_impl {
|
||||
.map(AbsolutePathBuf::to_path_buf)
|
||||
.collect::<Vec<_>>();
|
||||
let policy = parse_policy(policy_json_or_preset)?;
|
||||
let permissions = ResolvedWindowsSandboxPermissions::from_legacy_policy_for_cwd(
|
||||
&policy,
|
||||
sandbox_policy_cwd,
|
||||
);
|
||||
normalize_null_device_env(&mut env_map);
|
||||
ensure_non_interactive_pager(&mut env_map);
|
||||
inherit_path_env(&mut env_map);
|
||||
@@ -111,32 +117,26 @@ mod windows_impl {
|
||||
anyhow::bail!("DangerFullAccess and ExternalSandbox are not supported for sandboxing")
|
||||
}
|
||||
let caps = load_or_create_cap_sids(codex_home)?;
|
||||
let (sid_for_null, cap_sids) = match &policy {
|
||||
SandboxPolicy::ReadOnly { .. } => {
|
||||
let sid = LocalSid::from_string(&caps.readonly)?;
|
||||
(sid, vec![caps.readonly])
|
||||
}
|
||||
SandboxPolicy::WorkspaceWrite { .. } => {
|
||||
let write_roots = effective_write_roots_for_setup(
|
||||
&policy,
|
||||
sandbox_policy_cwd,
|
||||
cwd,
|
||||
&env_map,
|
||||
codex_home,
|
||||
write_roots_override,
|
||||
);
|
||||
let cap_sids = write_roots
|
||||
.iter()
|
||||
.map(|root| workspace_write_cap_sid_for_root(codex_home, cwd, root))
|
||||
.collect::<Result<Vec<_>>>()?;
|
||||
if cap_sids.is_empty() {
|
||||
anyhow::bail!("workspace-write sandbox has no writable root capability SIDs");
|
||||
}
|
||||
(LocalSid::from_string(&cap_sids[0])?, cap_sids)
|
||||
}
|
||||
SandboxPolicy::DangerFullAccess | SandboxPolicy::ExternalSandbox { .. } => {
|
||||
unreachable!("DangerFullAccess handled above")
|
||||
let (sid_for_null, cap_sids) = if permissions.uses_write_capabilities_for_cwd(cwd, &env_map)
|
||||
{
|
||||
let write_roots = effective_write_roots_for_permissions(
|
||||
&permissions,
|
||||
cwd,
|
||||
&env_map,
|
||||
codex_home,
|
||||
write_roots_override,
|
||||
);
|
||||
let cap_sids = write_roots
|
||||
.iter()
|
||||
.map(|root| workspace_write_cap_sid_for_root(codex_home, cwd, root))
|
||||
.collect::<Result<Vec<_>>>()?;
|
||||
if cap_sids.is_empty() {
|
||||
anyhow::bail!("workspace-write sandbox has no writable root capability SIDs");
|
||||
}
|
||||
(LocalSid::from_string(&cap_sids[0])?, cap_sids)
|
||||
} else {
|
||||
let sid = LocalSid::from_string(&caps.readonly)?;
|
||||
(sid, vec![caps.readonly])
|
||||
};
|
||||
|
||||
unsafe {
|
||||
@@ -144,12 +144,14 @@ mod windows_impl {
|
||||
}
|
||||
|
||||
(|| -> Result<CaptureResult> {
|
||||
let permission_profile =
|
||||
PermissionProfile::from_legacy_sandbox_policy_for_cwd(&policy, sandbox_policy_cwd);
|
||||
let spawn_request = SpawnRequest {
|
||||
command: command.clone(),
|
||||
cwd: cwd.to_path_buf(),
|
||||
env: env_map.clone(),
|
||||
policy_json_or_preset: policy_json_or_preset.to_string(),
|
||||
sandbox_policy_cwd: sandbox_policy_cwd.to_path_buf(),
|
||||
permission_profile,
|
||||
permission_profile_cwd: sandbox_policy_cwd.to_path_buf(),
|
||||
codex_home: sandbox_base.clone(),
|
||||
real_codex_home: codex_home.to_path_buf(),
|
||||
cap_sids,
|
||||
|
||||
@@ -1,12 +1,13 @@
|
||||
use crate::dpapi;
|
||||
use crate::logging::debug_log;
|
||||
use crate::policy::SandboxPolicy;
|
||||
use crate::resolved_permissions::ResolvedWindowsSandboxPermissions;
|
||||
use crate::setup::SandboxNetworkIdentity;
|
||||
use crate::setup::SandboxUserRecord;
|
||||
use crate::setup::SandboxUsersFile;
|
||||
use crate::setup::SetupMarker;
|
||||
use crate::setup::gather_read_roots;
|
||||
use crate::setup::gather_write_roots;
|
||||
use crate::setup::gather_write_roots_for_permissions;
|
||||
use crate::setup::offline_proxy_settings_from_env;
|
||||
use crate::setup::run_elevated_setup;
|
||||
use crate::setup::run_setup_refresh_with_overrides;
|
||||
@@ -143,14 +144,16 @@ pub fn require_logon_sandbox_creds(
|
||||
deny_write_paths_override: &[PathBuf],
|
||||
proxy_enforced: bool,
|
||||
) -> Result<SandboxCreds> {
|
||||
let permissions =
|
||||
ResolvedWindowsSandboxPermissions::from_legacy_policy_for_cwd(policy, policy_cwd);
|
||||
let sandbox_dir = crate::setup::sandbox_dir(codex_home);
|
||||
let needed_read = read_roots_override
|
||||
.map(<[PathBuf]>::to_vec)
|
||||
.unwrap_or_else(|| gather_read_roots(command_cwd, policy, codex_home));
|
||||
let needed_write = write_roots_override
|
||||
.map(<[PathBuf]>::to_vec)
|
||||
.unwrap_or_else(|| gather_write_roots(policy, policy_cwd, command_cwd, env_map));
|
||||
let network_identity = SandboxNetworkIdentity::from_policy(policy, proxy_enforced);
|
||||
.unwrap_or_else(|| gather_write_roots_for_permissions(&permissions, command_cwd, env_map));
|
||||
let network_identity = SandboxNetworkIdentity::from_permissions(&permissions, proxy_enforced);
|
||||
let desired_offline_proxy_settings = offline_proxy_settings_from_env(env_map, network_identity);
|
||||
// NOTE: Do not add CODEX_HOME/.sandbox to `needed_write`; it must remain non-writable by the
|
||||
// restricted capability token. The setup helper's `lock_sandbox_dir` is responsible for
|
||||
|
||||
@@ -38,6 +38,8 @@ mod policy;
|
||||
#[cfg(target_os = "windows")]
|
||||
mod process;
|
||||
#[cfg(target_os = "windows")]
|
||||
mod resolved_permissions;
|
||||
#[cfg(target_os = "windows")]
|
||||
mod token;
|
||||
#[cfg(target_os = "windows")]
|
||||
mod wfp;
|
||||
@@ -195,6 +197,10 @@ pub use process::read_handle_loop;
|
||||
#[cfg(target_os = "windows")]
|
||||
pub use process::spawn_process_with_pipes;
|
||||
#[cfg(target_os = "windows")]
|
||||
pub use resolved_permissions::WindowsSandboxTokenMode;
|
||||
#[cfg(target_os = "windows")]
|
||||
pub use resolved_permissions::token_mode_for_permission_profile;
|
||||
#[cfg(target_os = "windows")]
|
||||
pub use setup::SETUP_VERSION;
|
||||
#[cfg(target_os = "windows")]
|
||||
pub use setup::SandboxSetupRequest;
|
||||
@@ -286,6 +292,7 @@ mod windows_impl {
|
||||
use super::process::create_process_as_user;
|
||||
use super::sandbox_utils::ensure_codex_home_exists;
|
||||
use super::spawn_prep::LegacyAclSids;
|
||||
use super::spawn_prep::SpawnPrepOptions;
|
||||
use super::spawn_prep::allow_null_device_for_workspace_write;
|
||||
use super::spawn_prep::apply_legacy_session_acl_rules;
|
||||
use super::spawn_prep::legacy_session_capability_roots;
|
||||
@@ -394,17 +401,20 @@ mod windows_impl {
|
||||
.collect::<Vec<_>>();
|
||||
let common = prepare_legacy_spawn_context(
|
||||
policy_json_or_preset,
|
||||
sandbox_policy_cwd,
|
||||
codex_home,
|
||||
cwd,
|
||||
&mut env_map,
|
||||
&command,
|
||||
/*inherit_path*/ false,
|
||||
/*add_git_safe_directory*/ false,
|
||||
SpawnPrepOptions {
|
||||
inherit_path: false,
|
||||
add_git_safe_directory: false,
|
||||
},
|
||||
)?;
|
||||
let policy = common.policy;
|
||||
let current_dir = common.current_dir;
|
||||
let logs_base_dir = common.logs_base_dir.as_deref();
|
||||
let is_workspace_write = common.is_workspace_write;
|
||||
let uses_write_capabilities = common.uses_write_capabilities;
|
||||
if !policy.has_full_disk_read_access() {
|
||||
anyhow::bail!(
|
||||
"Restricted read-only access requires the elevated Windows sandbox backend"
|
||||
@@ -423,8 +433,8 @@ mod windows_impl {
|
||||
codex_home,
|
||||
);
|
||||
let security = prepare_legacy_session_security(&policy, codex_home, cwd, capability_roots)?;
|
||||
allow_null_device_for_workspace_write(is_workspace_write);
|
||||
let persist_aces = is_workspace_write;
|
||||
allow_null_device_for_workspace_write(uses_write_capabilities);
|
||||
let persist_aces = uses_write_capabilities;
|
||||
let guards = apply_legacy_session_acl_rules(
|
||||
&policy,
|
||||
sandbox_policy_cwd,
|
||||
@@ -631,7 +641,8 @@ mod windows_impl {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::policy::SandboxPolicy;
|
||||
use crate::spawn_prep::should_apply_network_block;
|
||||
use crate::resolved_permissions::ResolvedWindowsSandboxPermissions;
|
||||
use std::path::Path;
|
||||
|
||||
fn workspace_policy(network_access: bool) -> SandboxPolicy {
|
||||
SandboxPolicy::WorkspaceWrite {
|
||||
@@ -642,6 +653,11 @@ mod windows_impl {
|
||||
}
|
||||
}
|
||||
|
||||
fn should_apply_network_block(policy: &SandboxPolicy) -> bool {
|
||||
ResolvedWindowsSandboxPermissions::from_legacy_policy_for_cwd(policy, Path::new("."))
|
||||
.should_apply_network_block()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn applies_network_block_when_access_is_disabled() {
|
||||
assert!(should_apply_network_block(&workspace_policy(
|
||||
|
||||
332
codex-rs/windows-sandbox-rs/src/resolved_permissions.rs
Normal file
332
codex-rs/windows-sandbox-rs/src/resolved_permissions.rs
Normal file
@@ -0,0 +1,332 @@
|
||||
use anyhow::Result;
|
||||
use codex_protocol::models::PermissionProfile;
|
||||
use codex_protocol::permissions::FileSystemPath;
|
||||
use codex_protocol::permissions::FileSystemSandboxEntry;
|
||||
use codex_protocol::permissions::FileSystemSandboxKind;
|
||||
use codex_protocol::permissions::FileSystemSandboxPolicy;
|
||||
use codex_protocol::permissions::NetworkSandboxPolicy;
|
||||
use codex_protocol::protocol::SandboxPolicy;
|
||||
use codex_utils_absolute_path::AbsolutePathBuf;
|
||||
use std::collections::HashMap;
|
||||
use std::path::Path;
|
||||
use std::path::PathBuf;
|
||||
|
||||
/// Windows-local view of the runtime permission profile.
|
||||
///
|
||||
/// Most Windows sandbox code needs resolved runtime permissions plus a few
|
||||
/// Windows-specific path conventions, not the user/config-facing
|
||||
/// `PermissionProfile` enum itself.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub(crate) struct ResolvedWindowsSandboxPermissions {
|
||||
file_system: FileSystemSandboxPolicy,
|
||||
network: NetworkSandboxPolicy,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub(crate) struct WindowsWritableRoot {
|
||||
pub(crate) root: PathBuf,
|
||||
pub(crate) read_only_subpaths: Vec<PathBuf>,
|
||||
}
|
||||
|
||||
/// Restricted-token family needed to enforce a Windows permission profile.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum WindowsSandboxTokenMode {
|
||||
ReadOnlyCapability,
|
||||
WriteCapabilityRoots,
|
||||
}
|
||||
|
||||
/// Chooses the restricted-token family needed for a managed permission profile.
|
||||
pub fn token_mode_for_permission_profile(
|
||||
permission_profile: &PermissionProfile,
|
||||
cwd: &Path,
|
||||
env_map: &HashMap<String, String>,
|
||||
) -> Result<WindowsSandboxTokenMode> {
|
||||
let permissions =
|
||||
ResolvedWindowsSandboxPermissions::try_from_permission_profile(permission_profile)?;
|
||||
if permissions.file_system.has_full_disk_write_access() {
|
||||
anyhow::bail!(
|
||||
"permission profile requests full-disk filesystem writes, which cannot be enforced by the Windows sandbox"
|
||||
);
|
||||
}
|
||||
if permissions.writable_roots_for_cwd(cwd, env_map).is_empty() {
|
||||
Ok(WindowsSandboxTokenMode::ReadOnlyCapability)
|
||||
} else {
|
||||
Ok(WindowsSandboxTokenMode::WriteCapabilityRoots)
|
||||
}
|
||||
}
|
||||
|
||||
impl ResolvedWindowsSandboxPermissions {
|
||||
pub(crate) fn from_legacy_policy_for_cwd(policy: &SandboxPolicy, cwd: &Path) -> Self {
|
||||
Self {
|
||||
file_system: FileSystemSandboxPolicy::from_legacy_sandbox_policy_for_cwd(policy, cwd)
|
||||
.materialize_project_roots_with_cwd(cwd),
|
||||
network: NetworkSandboxPolicy::from(policy),
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn try_from_permission_profile(
|
||||
permission_profile: &PermissionProfile,
|
||||
) -> Result<Self> {
|
||||
if !matches!(permission_profile, PermissionProfile::Managed { .. }) {
|
||||
anyhow::bail!(
|
||||
"only managed permission profiles can be enforced by the Windows sandbox"
|
||||
);
|
||||
}
|
||||
let (file_system, network) = permission_profile.to_runtime_permissions();
|
||||
if !matches!(file_system.kind, FileSystemSandboxKind::Restricted) {
|
||||
anyhow::bail!(
|
||||
"only restricted managed filesystem permissions can be enforced by the Windows sandbox"
|
||||
);
|
||||
}
|
||||
Ok(Self {
|
||||
file_system,
|
||||
network,
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) fn should_apply_network_block(&self) -> bool {
|
||||
!self.network.is_enabled()
|
||||
}
|
||||
|
||||
pub(crate) fn network_policy(&self) -> NetworkSandboxPolicy {
|
||||
self.network
|
||||
}
|
||||
|
||||
pub(crate) fn uses_write_capabilities_for_cwd(
|
||||
&self,
|
||||
cwd: &Path,
|
||||
env_map: &HashMap<String, String>,
|
||||
) -> bool {
|
||||
!self.writable_roots_for_cwd(cwd, env_map).is_empty()
|
||||
}
|
||||
|
||||
pub(crate) fn writable_roots_for_cwd(
|
||||
&self,
|
||||
cwd: &Path,
|
||||
env_map: &HashMap<String, String>,
|
||||
) -> Vec<WindowsWritableRoot> {
|
||||
let mut file_system = self.file_system.clone();
|
||||
file_system
|
||||
.entries
|
||||
.retain(|FileSystemSandboxEntry { path, .. }| {
|
||||
!matches!(
|
||||
path,
|
||||
FileSystemPath::Special {
|
||||
value: codex_protocol::permissions::FileSystemSpecialPath::Tmpdir
|
||||
| codex_protocol::permissions::FileSystemSpecialPath::SlashTmp,
|
||||
}
|
||||
)
|
||||
});
|
||||
|
||||
let mut roots = file_system
|
||||
.get_writable_roots_with_cwd(cwd)
|
||||
.into_iter()
|
||||
.map(|root| WindowsWritableRoot {
|
||||
root: root.root.into_path_buf(),
|
||||
read_only_subpaths: root
|
||||
.read_only_subpaths
|
||||
.into_iter()
|
||||
.map(AbsolutePathBuf::into_path_buf)
|
||||
.collect(),
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
if self.has_writable_tmpdir_entry() {
|
||||
roots.extend(windows_temp_env_roots(env_map).into_iter().map(|root| {
|
||||
WindowsWritableRoot {
|
||||
root,
|
||||
read_only_subpaths: Vec::new(),
|
||||
}
|
||||
}));
|
||||
}
|
||||
|
||||
roots
|
||||
}
|
||||
|
||||
fn has_writable_tmpdir_entry(&self) -> bool {
|
||||
self.file_system
|
||||
.entries
|
||||
.iter()
|
||||
.any(|FileSystemSandboxEntry { path, access }| {
|
||||
matches!(
|
||||
path,
|
||||
FileSystemPath::Special {
|
||||
value: codex_protocol::permissions::FileSystemSpecialPath::Tmpdir,
|
||||
}
|
||||
) && access.can_write()
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
fn windows_temp_env_roots(env_map: &HashMap<String, String>) -> Vec<PathBuf> {
|
||||
["TEMP", "TMP"]
|
||||
.into_iter()
|
||||
.filter_map(|key| {
|
||||
env_map
|
||||
.get(key)
|
||||
.map(|value| PathBuf::from(value.as_str()))
|
||||
.or_else(|| std::env::var_os(key).map(PathBuf::from))
|
||||
})
|
||||
.filter(|path| path.is_absolute())
|
||||
.collect()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use codex_protocol::models::ManagedFileSystemPermissions;
|
||||
use codex_protocol::permissions::FileSystemAccessMode;
|
||||
use codex_protocol::permissions::FileSystemSandboxEntry;
|
||||
use codex_protocol::permissions::FileSystemSpecialPath;
|
||||
use pretty_assertions::assert_eq;
|
||||
use tempfile::TempDir;
|
||||
|
||||
#[test]
|
||||
fn permission_profile_workspace_write_uses_windows_temp_env_vars() {
|
||||
let tmp = TempDir::new().expect("tempdir");
|
||||
let cwd = tmp.path().join("workspace");
|
||||
let temp_dir = tmp.path().join("temp");
|
||||
std::fs::create_dir_all(&cwd).expect("create cwd");
|
||||
std::fs::create_dir_all(&temp_dir).expect("create temp dir");
|
||||
|
||||
let mut env_map = HashMap::new();
|
||||
env_map.insert("TEMP".to_string(), temp_dir.to_string_lossy().to_string());
|
||||
env_map.insert("TMP".to_string(), temp_dir.to_string_lossy().to_string());
|
||||
|
||||
let permissions = ResolvedWindowsSandboxPermissions::try_from_permission_profile(
|
||||
&PermissionProfile::workspace_write(),
|
||||
)
|
||||
.expect("managed permission profile");
|
||||
let roots = permissions
|
||||
.writable_roots_for_cwd(&cwd, &env_map)
|
||||
.into_iter()
|
||||
.map(|root| root.root)
|
||||
.collect::<std::collections::HashSet<_>>();
|
||||
|
||||
let expected_roots = [
|
||||
temp_dir,
|
||||
dunce::canonicalize(&cwd).expect("canonicalize cwd"),
|
||||
]
|
||||
.into_iter()
|
||||
.collect::<std::collections::HashSet<_>>();
|
||||
|
||||
assert_eq!(expected_roots, roots);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn legacy_workspace_root_stays_bound_to_policy_cwd() {
|
||||
let tmp = TempDir::new().expect("tempdir");
|
||||
let policy_cwd = tmp.path().join("workspace");
|
||||
let command_cwd = policy_cwd.join("subdir");
|
||||
std::fs::create_dir_all(&command_cwd).expect("create command cwd");
|
||||
|
||||
let policy = SandboxPolicy::WorkspaceWrite {
|
||||
writable_roots: Vec::new(),
|
||||
network_access: false,
|
||||
exclude_tmpdir_env_var: true,
|
||||
exclude_slash_tmp: true,
|
||||
};
|
||||
let permissions =
|
||||
ResolvedWindowsSandboxPermissions::from_legacy_policy_for_cwd(&policy, &policy_cwd);
|
||||
|
||||
let roots = permissions
|
||||
.writable_roots_for_cwd(&command_cwd, &HashMap::new())
|
||||
.into_iter()
|
||||
.map(|root| root.root)
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
assert_eq!(
|
||||
roots,
|
||||
vec![dunce::canonicalize(&policy_cwd).expect("canonical policy cwd")]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn token_mode_for_profile_without_writable_roots_uses_readonly_capability() {
|
||||
let tmp = TempDir::new().expect("tempdir");
|
||||
let cwd = tmp.path().join("workspace");
|
||||
std::fs::create_dir_all(&cwd).expect("create cwd");
|
||||
|
||||
let token_mode = token_mode_for_permission_profile(
|
||||
&PermissionProfile::read_only(),
|
||||
&cwd,
|
||||
&HashMap::new(),
|
||||
)
|
||||
.expect("token mode");
|
||||
|
||||
assert_eq!(WindowsSandboxTokenMode::ReadOnlyCapability, token_mode);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn token_mode_for_profile_with_writable_roots_uses_write_capabilities() {
|
||||
let tmp = TempDir::new().expect("tempdir");
|
||||
let cwd = tmp.path().join("workspace");
|
||||
std::fs::create_dir_all(&cwd).expect("create cwd");
|
||||
|
||||
let token_mode = token_mode_for_permission_profile(
|
||||
&PermissionProfile::workspace_write(),
|
||||
&cwd,
|
||||
&HashMap::new(),
|
||||
)
|
||||
.expect("token mode");
|
||||
|
||||
assert_eq!(WindowsSandboxTokenMode::WriteCapabilityRoots, token_mode);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn permission_profile_rejects_disabled_profiles() {
|
||||
let err = ResolvedWindowsSandboxPermissions::try_from_permission_profile(
|
||||
&PermissionProfile::Disabled,
|
||||
)
|
||||
.expect_err("disabled profile should not resolve for sandbox enforcement");
|
||||
|
||||
assert!(
|
||||
err.to_string()
|
||||
.contains("only managed permission profiles can be enforced")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn permission_profile_rejects_unrestricted_managed_filesystem() {
|
||||
let permission_profile = PermissionProfile::Managed {
|
||||
file_system: ManagedFileSystemPermissions::Unrestricted,
|
||||
network: NetworkSandboxPolicy::Restricted,
|
||||
};
|
||||
|
||||
let err =
|
||||
ResolvedWindowsSandboxPermissions::try_from_permission_profile(&permission_profile)
|
||||
.expect_err("unrestricted profile should not resolve for sandbox enforcement");
|
||||
|
||||
assert!(
|
||||
err.to_string()
|
||||
.contains("only restricted managed filesystem permissions can be enforced")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn token_mode_rejects_full_disk_write_entries() {
|
||||
let tmp = TempDir::new().expect("tempdir");
|
||||
let cwd = tmp.path().join("workspace");
|
||||
std::fs::create_dir_all(&cwd).expect("create cwd");
|
||||
let permission_profile = PermissionProfile::Managed {
|
||||
file_system: ManagedFileSystemPermissions::Restricted {
|
||||
entries: vec![FileSystemSandboxEntry {
|
||||
path: FileSystemPath::Special {
|
||||
value: FileSystemSpecialPath::Root,
|
||||
},
|
||||
access: FileSystemAccessMode::Write,
|
||||
}],
|
||||
glob_scan_max_depth: None,
|
||||
},
|
||||
network: NetworkSandboxPolicy::Restricted,
|
||||
};
|
||||
|
||||
let err = token_mode_for_permission_profile(&permission_profile, &cwd, &HashMap::new())
|
||||
.expect_err("full disk writes should not resolve to a token mode");
|
||||
|
||||
assert!(
|
||||
err.to_string()
|
||||
.contains("full-disk filesystem writes, which cannot be enforced")
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -17,6 +17,7 @@ use crate::logging::log_note;
|
||||
use crate::path_normalization::canonical_path_key;
|
||||
use crate::path_normalization::canonicalize_path;
|
||||
use crate::policy::SandboxPolicy;
|
||||
use crate::resolved_permissions::ResolvedWindowsSandboxPermissions;
|
||||
use crate::setup_error::SetupErrorCode;
|
||||
use crate::setup_error::SetupFailure;
|
||||
use crate::setup_error::clear_setup_error_report;
|
||||
@@ -173,8 +174,12 @@ fn run_setup_refresh_inner(
|
||||
let (read_roots, write_roots) = build_payload_roots(&request, &overrides);
|
||||
let deny_read_paths = build_payload_deny_read_paths(overrides.deny_read_paths);
|
||||
let deny_write_paths = build_payload_deny_write_paths(&request, overrides.deny_write_paths);
|
||||
let permissions = ResolvedWindowsSandboxPermissions::from_legacy_policy_for_cwd(
|
||||
request.policy,
|
||||
request.policy_cwd,
|
||||
);
|
||||
let network_identity =
|
||||
SandboxNetworkIdentity::from_policy(request.policy, request.proxy_enforced);
|
||||
SandboxNetworkIdentity::from_permissions(&permissions, request.proxy_enforced);
|
||||
let offline_proxy_settings = offline_proxy_settings_from_env(request.env_map, network_identity);
|
||||
let payload = ElevationPayload {
|
||||
version: SETUP_VERSION,
|
||||
@@ -389,20 +394,16 @@ pub(crate) fn gather_read_roots(
|
||||
gather_legacy_full_read_roots(command_cwd, policy, codex_home)
|
||||
}
|
||||
|
||||
pub(crate) fn gather_write_roots(
|
||||
policy: &SandboxPolicy,
|
||||
policy_cwd: &Path,
|
||||
pub(crate) fn gather_write_roots_for_permissions(
|
||||
permissions: &ResolvedWindowsSandboxPermissions,
|
||||
command_cwd: &Path,
|
||||
env_map: &HashMap<String, String>,
|
||||
) -> Vec<PathBuf> {
|
||||
let mut roots: Vec<PathBuf> = Vec::new();
|
||||
// Always include the command CWD for workspace-write.
|
||||
if matches!(policy, SandboxPolicy::WorkspaceWrite { .. }) {
|
||||
roots.push(command_cwd.to_path_buf());
|
||||
}
|
||||
let AllowDenyPaths { allow, .. } =
|
||||
compute_allow_paths(policy, policy_cwd, command_cwd, env_map);
|
||||
roots.extend(allow);
|
||||
let roots = permissions
|
||||
.writable_roots_for_cwd(command_cwd, env_map)
|
||||
.into_iter()
|
||||
.map(|root| root.root)
|
||||
.collect::<Vec<_>>();
|
||||
let mut dedup: HashSet<PathBuf> = HashSet::new();
|
||||
let mut out: Vec<PathBuf> = Vec::new();
|
||||
for r in canonical_existing(&roots) {
|
||||
@@ -420,11 +421,29 @@ pub(crate) fn effective_write_roots_for_setup(
|
||||
env_map: &HashMap<String, String>,
|
||||
codex_home: &Path,
|
||||
write_roots_override: Option<&[PathBuf]>,
|
||||
) -> Vec<PathBuf> {
|
||||
let permissions =
|
||||
ResolvedWindowsSandboxPermissions::from_legacy_policy_for_cwd(policy, policy_cwd);
|
||||
effective_write_roots_for_permissions(
|
||||
&permissions,
|
||||
command_cwd,
|
||||
env_map,
|
||||
codex_home,
|
||||
write_roots_override,
|
||||
)
|
||||
}
|
||||
|
||||
pub(crate) fn effective_write_roots_for_permissions(
|
||||
permissions: &ResolvedWindowsSandboxPermissions,
|
||||
command_cwd: &Path,
|
||||
env_map: &HashMap<String, String>,
|
||||
codex_home: &Path,
|
||||
write_roots_override: Option<&[PathBuf]>,
|
||||
) -> Vec<PathBuf> {
|
||||
let write_roots = if let Some(roots) = write_roots_override {
|
||||
canonical_existing(roots)
|
||||
} else {
|
||||
gather_write_roots(policy, policy_cwd, command_cwd, env_map)
|
||||
gather_write_roots_for_permissions(permissions, command_cwd, env_map)
|
||||
};
|
||||
let write_roots = expand_user_profile_root(write_roots);
|
||||
let write_roots = filter_user_profile_root(write_roots);
|
||||
@@ -468,8 +487,11 @@ pub(crate) enum SandboxNetworkIdentity {
|
||||
}
|
||||
|
||||
impl SandboxNetworkIdentity {
|
||||
pub(crate) fn from_policy(policy: &SandboxPolicy, proxy_enforced: bool) -> Self {
|
||||
if proxy_enforced || !policy.has_full_network_access() {
|
||||
pub(crate) fn from_permissions(
|
||||
permissions: &ResolvedWindowsSandboxPermissions,
|
||||
proxy_enforced: bool,
|
||||
) -> Self {
|
||||
if proxy_enforced || !permissions.network_policy().is_enabled() {
|
||||
Self::Offline
|
||||
} else {
|
||||
Self::Online
|
||||
@@ -749,8 +771,12 @@ pub fn run_elevated_setup(
|
||||
let (read_roots, write_roots) = build_payload_roots(&request, &overrides);
|
||||
let deny_read_paths = build_payload_deny_read_paths(overrides.deny_read_paths);
|
||||
let deny_write_paths = build_payload_deny_write_paths(&request, overrides.deny_write_paths);
|
||||
let permissions = ResolvedWindowsSandboxPermissions::from_legacy_policy_for_cwd(
|
||||
request.policy,
|
||||
request.policy_cwd,
|
||||
);
|
||||
let network_identity =
|
||||
SandboxNetworkIdentity::from_policy(request.policy, request.proxy_enforced);
|
||||
SandboxNetworkIdentity::from_permissions(&permissions, request.proxy_enforced);
|
||||
let offline_proxy_settings = offline_proxy_settings_from_env(request.env_map, network_identity);
|
||||
let payload = ElevationPayload {
|
||||
version: SETUP_VERSION,
|
||||
@@ -1478,6 +1504,37 @@ mod tests {
|
||||
assert!(!effective_write_roots.contains(&forbidden_sandbox));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn effective_write_roots_use_policy_cwd_for_legacy_workspace_root() {
|
||||
let tmp = TempDir::new().expect("tempdir");
|
||||
let codex_home = tmp.path().join("codex-home");
|
||||
let policy_cwd = tmp.path().join("workspace");
|
||||
let command_cwd = policy_cwd.join("subdir");
|
||||
fs::create_dir_all(&codex_home).expect("create codex home");
|
||||
fs::create_dir_all(&command_cwd).expect("create command cwd");
|
||||
|
||||
let policy = SandboxPolicy::WorkspaceWrite {
|
||||
writable_roots: vec![],
|
||||
network_access: false,
|
||||
exclude_tmpdir_env_var: true,
|
||||
exclude_slash_tmp: true,
|
||||
};
|
||||
|
||||
let effective_write_roots = super::effective_write_roots_for_setup(
|
||||
&policy,
|
||||
&policy_cwd,
|
||||
&command_cwd,
|
||||
&HashMap::new(),
|
||||
&codex_home,
|
||||
/*write_roots_override*/ None,
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
effective_write_roots,
|
||||
vec![dunce::canonicalize(&policy_cwd).expect("canonical policy cwd")]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn payload_deny_write_paths_merge_explicit_and_protected_children() {
|
||||
let tmp = TempDir::new().expect("tempdir");
|
||||
|
||||
@@ -3,6 +3,7 @@ use crate::acl::add_deny_write_ace;
|
||||
use crate::acl::allow_null_device;
|
||||
use crate::allow::AllowDenyPaths;
|
||||
use crate::allow::compute_allow_paths;
|
||||
use crate::allow::compute_allow_paths_for_permissions;
|
||||
use crate::cap::load_or_create_cap_sids;
|
||||
use crate::cap::workspace_write_cap_sid_for_root;
|
||||
use crate::cap::workspace_write_root_contains_path;
|
||||
@@ -20,9 +21,10 @@ use crate::logging::log_start;
|
||||
use crate::path_normalization::canonicalize_path;
|
||||
use crate::policy::SandboxPolicy;
|
||||
use crate::policy::parse_policy;
|
||||
use crate::resolved_permissions::ResolvedWindowsSandboxPermissions;
|
||||
use crate::sandbox_utils::ensure_codex_home_exists;
|
||||
use crate::sandbox_utils::inject_git_safe_directory;
|
||||
use crate::setup::effective_write_roots_for_setup;
|
||||
use crate::setup::effective_write_roots_for_permissions;
|
||||
use crate::token::LocalSid;
|
||||
use crate::token::create_readonly_token_with_cap;
|
||||
use crate::token::create_workspace_write_token_with_caps_from;
|
||||
@@ -42,10 +44,11 @@ use windows_sys::Win32::Foundation::HANDLE;
|
||||
|
||||
pub(crate) struct SpawnContext {
|
||||
pub(crate) policy: SandboxPolicy,
|
||||
pub(crate) permissions: ResolvedWindowsSandboxPermissions,
|
||||
pub(crate) current_dir: PathBuf,
|
||||
pub(crate) sandbox_base: PathBuf,
|
||||
pub(crate) logs_base_dir: Option<PathBuf>,
|
||||
pub(crate) is_workspace_write: bool,
|
||||
pub(crate) uses_write_capabilities: bool,
|
||||
}
|
||||
|
||||
pub(crate) struct ElevatedSpawnContext {
|
||||
@@ -54,6 +57,12 @@ pub(crate) struct ElevatedSpawnContext {
|
||||
pub(crate) cap_sids: Vec<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub(crate) struct SpawnPrepOptions {
|
||||
pub(crate) inherit_path: bool,
|
||||
pub(crate) add_git_safe_directory: bool,
|
||||
}
|
||||
|
||||
pub(crate) struct LegacySessionSecurity {
|
||||
pub(crate) h_token: HANDLE,
|
||||
pub(crate) readonly_sid: Option<LocalSid>,
|
||||
@@ -73,18 +82,14 @@ pub(crate) struct LegacyAclSids<'a> {
|
||||
pub(crate) write_root_sids: &'a [RootCapabilitySid],
|
||||
}
|
||||
|
||||
pub(crate) fn should_apply_network_block(policy: &SandboxPolicy) -> bool {
|
||||
!policy.has_full_network_access()
|
||||
}
|
||||
|
||||
fn prepare_spawn_context_common(
|
||||
policy_json_or_preset: &str,
|
||||
policy_cwd: &Path,
|
||||
codex_home: &Path,
|
||||
cwd: &Path,
|
||||
env_map: &mut HashMap<String, String>,
|
||||
command: &[String],
|
||||
inherit_path: bool,
|
||||
add_git_safe_directory: bool,
|
||||
options: SpawnPrepOptions,
|
||||
) -> Result<SpawnContext> {
|
||||
let policy = parse_policy(policy_json_or_preset)?;
|
||||
if matches!(
|
||||
@@ -96,10 +101,10 @@ fn prepare_spawn_context_common(
|
||||
|
||||
normalize_null_device_env(env_map);
|
||||
ensure_non_interactive_pager(env_map);
|
||||
if inherit_path {
|
||||
if options.inherit_path {
|
||||
inherit_path_env(env_map);
|
||||
}
|
||||
if add_git_safe_directory {
|
||||
if options.add_git_safe_directory {
|
||||
inject_git_safe_directory(env_map, cwd);
|
||||
}
|
||||
|
||||
@@ -109,36 +114,39 @@ fn prepare_spawn_context_common(
|
||||
let logs_base_dir = Some(sandbox_base.clone());
|
||||
log_start(command, logs_base_dir.as_deref());
|
||||
|
||||
let is_workspace_write = matches!(&policy, SandboxPolicy::WorkspaceWrite { .. });
|
||||
let permissions =
|
||||
ResolvedWindowsSandboxPermissions::from_legacy_policy_for_cwd(&policy, policy_cwd);
|
||||
let uses_write_capabilities = permissions.uses_write_capabilities_for_cwd(cwd, env_map);
|
||||
|
||||
Ok(SpawnContext {
|
||||
policy,
|
||||
permissions,
|
||||
current_dir: cwd.to_path_buf(),
|
||||
sandbox_base,
|
||||
logs_base_dir,
|
||||
is_workspace_write,
|
||||
uses_write_capabilities,
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) fn prepare_legacy_spawn_context(
|
||||
policy_json_or_preset: &str,
|
||||
policy_cwd: &Path,
|
||||
codex_home: &Path,
|
||||
cwd: &Path,
|
||||
env_map: &mut HashMap<String, String>,
|
||||
command: &[String],
|
||||
inherit_path: bool,
|
||||
add_git_safe_directory: bool,
|
||||
options: SpawnPrepOptions,
|
||||
) -> Result<SpawnContext> {
|
||||
let common = prepare_spawn_context_common(
|
||||
policy_json_or_preset,
|
||||
policy_cwd,
|
||||
codex_home,
|
||||
cwd,
|
||||
env_map,
|
||||
command,
|
||||
inherit_path,
|
||||
add_git_safe_directory,
|
||||
options,
|
||||
)?;
|
||||
if should_apply_network_block(&common.policy) {
|
||||
if common.permissions.should_apply_network_block() {
|
||||
apply_no_network_to_env(env_map)?;
|
||||
}
|
||||
Ok(common)
|
||||
@@ -195,14 +203,15 @@ pub(crate) fn legacy_session_capability_roots(
|
||||
env_map: &HashMap<String, String>,
|
||||
codex_home: &Path,
|
||||
) -> Vec<PathBuf> {
|
||||
let allow_paths = compute_allow_paths(policy, policy_cwd, current_dir, env_map)
|
||||
let permissions =
|
||||
ResolvedWindowsSandboxPermissions::from_legacy_policy_for_cwd(policy, policy_cwd);
|
||||
let allow_paths = compute_allow_paths_for_permissions(&permissions, current_dir, env_map)
|
||||
.allow
|
||||
.into_iter()
|
||||
.collect::<Vec<_>>();
|
||||
if matches!(policy, SandboxPolicy::WorkspaceWrite { .. }) {
|
||||
effective_write_roots_for_setup(
|
||||
policy,
|
||||
policy_cwd,
|
||||
if permissions.uses_write_capabilities_for_cwd(current_dir, env_map) {
|
||||
effective_write_roots_for_permissions(
|
||||
&permissions,
|
||||
current_dir,
|
||||
env_map,
|
||||
codex_home,
|
||||
@@ -378,7 +387,7 @@ pub(crate) fn apply_legacy_session_acl_rules(
|
||||
allow_null_device(readonly_sid.as_ptr());
|
||||
}
|
||||
if persist_aces
|
||||
&& matches!(policy, SandboxPolicy::WorkspaceWrite { .. })
|
||||
&& !acl_sids.write_root_sids.is_empty()
|
||||
&& let Some(workspace_sid) =
|
||||
matching_root_capability(current_dir, acl_sids.write_root_sids)
|
||||
{
|
||||
@@ -408,32 +417,30 @@ pub(crate) fn prepare_elevated_spawn_context(
|
||||
) -> Result<ElevatedSpawnContext> {
|
||||
let common = prepare_spawn_context_common(
|
||||
policy_json_or_preset,
|
||||
sandbox_policy_cwd,
|
||||
codex_home,
|
||||
cwd,
|
||||
env_map,
|
||||
command,
|
||||
/*inherit_path*/ true,
|
||||
/*add_git_safe_directory*/ true,
|
||||
SpawnPrepOptions {
|
||||
inherit_path: true,
|
||||
add_git_safe_directory: true,
|
||||
},
|
||||
)?;
|
||||
|
||||
let AllowDenyPaths { allow, deny } = compute_allow_paths(
|
||||
&common.policy,
|
||||
sandbox_policy_cwd,
|
||||
&common.current_dir,
|
||||
env_map,
|
||||
);
|
||||
let AllowDenyPaths { allow, deny } =
|
||||
compute_allow_paths_for_permissions(&common.permissions, &common.current_dir, env_map);
|
||||
let write_roots: Vec<PathBuf> = allow.into_iter().collect();
|
||||
let deny_write_paths: Vec<PathBuf> = deny.into_iter().collect();
|
||||
let computed_write_roots_override = if common.is_workspace_write {
|
||||
let computed_write_roots_override = if common.uses_write_capabilities {
|
||||
Some(write_roots.as_slice())
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let write_roots_for_setup = write_roots_override.or(computed_write_roots_override);
|
||||
let effective_write_roots = if common.is_workspace_write {
|
||||
effective_write_roots_for_setup(
|
||||
&common.policy,
|
||||
sandbox_policy_cwd,
|
||||
let effective_write_roots = if common.uses_write_capabilities {
|
||||
effective_write_roots_for_permissions(
|
||||
&common.permissions,
|
||||
&common.current_dir,
|
||||
env_map,
|
||||
codex_home,
|
||||
@@ -442,7 +449,7 @@ pub(crate) fn prepare_elevated_spawn_context(
|
||||
} else {
|
||||
Vec::new()
|
||||
};
|
||||
let setup_write_roots_override = if common.is_workspace_write {
|
||||
let setup_write_roots_override = if common.uses_write_capabilities {
|
||||
Some(effective_write_roots.as_slice())
|
||||
} else {
|
||||
write_roots_override
|
||||
@@ -465,24 +472,20 @@ pub(crate) fn prepare_elevated_spawn_context(
|
||||
/*proxy_enforced*/ false,
|
||||
)?;
|
||||
let caps = load_or_create_cap_sids(codex_home)?;
|
||||
let (psid_to_use, cap_sids) = match &common.policy {
|
||||
SandboxPolicy::ReadOnly { .. } => (
|
||||
let (psid_to_use, cap_sids) = if common.uses_write_capabilities {
|
||||
let cap_sids = root_capability_sids(codex_home, cwd, effective_write_roots)?
|
||||
.into_iter()
|
||||
.map(|root_sid| root_sid.sid_str)
|
||||
.collect::<Vec<_>>();
|
||||
if cap_sids.is_empty() {
|
||||
anyhow::bail!("workspace-write sandbox has no writable root capability SIDs");
|
||||
}
|
||||
(LocalSid::from_string(&cap_sids[0])?, cap_sids)
|
||||
} else {
|
||||
(
|
||||
LocalSid::from_string(&caps.readonly)?,
|
||||
vec![caps.readonly.clone()],
|
||||
),
|
||||
SandboxPolicy::WorkspaceWrite { .. } => {
|
||||
let cap_sids = root_capability_sids(codex_home, cwd, effective_write_roots)?
|
||||
.into_iter()
|
||||
.map(|root_sid| root_sid.sid_str)
|
||||
.collect::<Vec<_>>();
|
||||
if cap_sids.is_empty() {
|
||||
anyhow::bail!("workspace-write sandbox has no writable root capability SIDs");
|
||||
}
|
||||
(LocalSid::from_string(&cap_sids[0])?, cap_sids)
|
||||
}
|
||||
SandboxPolicy::DangerFullAccess | SandboxPolicy::ExternalSandbox { .. } => {
|
||||
unreachable!("dangerous policies rejected before elevated session prep")
|
||||
}
|
||||
)
|
||||
};
|
||||
|
||||
unsafe {
|
||||
@@ -499,19 +502,26 @@ pub(crate) fn prepare_elevated_spawn_context(
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::SandboxPolicy;
|
||||
use super::SpawnPrepOptions;
|
||||
use super::deny_root_capabilities_for_path;
|
||||
use super::legacy_session_capability_roots;
|
||||
use super::prepare_legacy_spawn_context;
|
||||
use super::prepare_spawn_context_common;
|
||||
use super::root_capability_sids;
|
||||
use super::should_apply_network_block;
|
||||
use crate::cap::load_or_create_cap_sids;
|
||||
use crate::cap::workspace_write_cap_sid_for_root;
|
||||
use crate::resolved_permissions::ResolvedWindowsSandboxPermissions;
|
||||
use codex_utils_absolute_path::AbsolutePathBuf;
|
||||
use pretty_assertions::assert_eq;
|
||||
use std::collections::HashMap;
|
||||
use std::path::Path;
|
||||
use tempfile::TempDir;
|
||||
|
||||
fn should_apply_network_block(policy: &SandboxPolicy) -> bool {
|
||||
ResolvedWindowsSandboxPermissions::from_legacy_policy_for_cwd(policy, Path::new("."))
|
||||
.should_apply_network_block()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn no_network_env_rewrite_applies_for_workspace_write() {
|
||||
assert!(should_apply_network_block(
|
||||
@@ -539,12 +549,15 @@ mod tests {
|
||||
|
||||
let _context = prepare_legacy_spawn_context(
|
||||
"workspace-write",
|
||||
cwd.path(),
|
||||
codex_home.path(),
|
||||
cwd.path(),
|
||||
&mut env_map,
|
||||
&["cmd.exe".to_string()],
|
||||
/*inherit_path*/ true,
|
||||
/*add_git_safe_directory*/ false,
|
||||
SpawnPrepOptions {
|
||||
inherit_path: true,
|
||||
add_git_safe_directory: false,
|
||||
},
|
||||
)
|
||||
.expect("legacy env prep");
|
||||
|
||||
@@ -566,12 +579,15 @@ mod tests {
|
||||
|
||||
let context = prepare_spawn_context_common(
|
||||
"workspace-write",
|
||||
cwd.path(),
|
||||
codex_home.path(),
|
||||
cwd.path(),
|
||||
&mut env_map,
|
||||
&["cmd.exe".to_string()],
|
||||
/*inherit_path*/ true,
|
||||
/*add_git_safe_directory*/ true,
|
||||
SpawnPrepOptions {
|
||||
inherit_path: true,
|
||||
add_git_safe_directory: true,
|
||||
},
|
||||
)
|
||||
.expect("preserve existing env prep");
|
||||
assert_eq!(context.policy, SandboxPolicy::new_workspace_write_policy());
|
||||
@@ -583,6 +599,36 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn legacy_session_capability_roots_use_policy_cwd_for_workspace_root() {
|
||||
let tmp = TempDir::new().expect("tempdir");
|
||||
let codex_home = tmp.path().join("codex-home");
|
||||
let policy_cwd = tmp.path().join("workspace");
|
||||
let command_cwd = policy_cwd.join("subdir");
|
||||
std::fs::create_dir_all(&codex_home).expect("create codex home");
|
||||
std::fs::create_dir_all(&command_cwd).expect("create command cwd");
|
||||
|
||||
let policy = SandboxPolicy::WorkspaceWrite {
|
||||
writable_roots: Vec::new(),
|
||||
network_access: false,
|
||||
exclude_tmpdir_env_var: true,
|
||||
exclude_slash_tmp: true,
|
||||
};
|
||||
|
||||
let roots = legacy_session_capability_roots(
|
||||
&policy,
|
||||
&policy_cwd,
|
||||
&command_cwd,
|
||||
&HashMap::new(),
|
||||
&codex_home,
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
roots,
|
||||
vec![dunce::canonicalize(&policy_cwd).expect("canonical policy cwd")]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn root_capability_sids_only_include_active_roots() {
|
||||
let temp = TempDir::new().expect("tempdir");
|
||||
|
||||
@@ -10,6 +10,7 @@ use crate::ipc_framed::SpawnRequest;
|
||||
use crate::runner_client::spawn_runner_transport;
|
||||
use crate::spawn_prep::prepare_elevated_spawn_context;
|
||||
use anyhow::Result;
|
||||
use codex_protocol::models::PermissionProfile;
|
||||
use codex_utils_absolute_path::AbsolutePathBuf;
|
||||
use codex_utils_pty::ProcessDriver;
|
||||
use codex_utils_pty::SpawnedProcess;
|
||||
@@ -60,12 +61,16 @@ pub(crate) async fn spawn_windows_sandbox_session_elevated(
|
||||
&deny_write_paths_override,
|
||||
)?;
|
||||
|
||||
let permission_profile = PermissionProfile::from_legacy_sandbox_policy_for_cwd(
|
||||
&elevated.common.policy,
|
||||
sandbox_policy_cwd,
|
||||
);
|
||||
let spawn_request = SpawnRequest {
|
||||
command: command.clone(),
|
||||
cwd: cwd.to_path_buf(),
|
||||
env: env_map.clone(),
|
||||
policy_json_or_preset: policy_json_or_preset.to_string(),
|
||||
sandbox_policy_cwd: sandbox_policy_cwd.to_path_buf(),
|
||||
permission_profile,
|
||||
permission_profile_cwd: sandbox_policy_cwd.to_path_buf(),
|
||||
codex_home: elevated.common.sandbox_base.clone(),
|
||||
real_codex_home: codex_home.to_path_buf(),
|
||||
cap_sids: elevated.cap_sids.clone(),
|
||||
|
||||
@@ -11,6 +11,7 @@ use crate::process::StdinMode;
|
||||
use crate::process::read_handle_loop;
|
||||
use crate::process::spawn_process_with_pipes;
|
||||
use crate::spawn_prep::LegacyAclSids;
|
||||
use crate::spawn_prep::SpawnPrepOptions;
|
||||
use crate::spawn_prep::allow_null_device_for_workspace_write;
|
||||
use crate::spawn_prep::apply_legacy_session_acl_rules;
|
||||
use crate::spawn_prep::legacy_session_capability_roots;
|
||||
@@ -295,12 +296,15 @@ pub(crate) async fn spawn_windows_sandbox_session_legacy(
|
||||
) -> Result<SpawnedProcess> {
|
||||
let common = prepare_legacy_spawn_context(
|
||||
policy_json_or_preset,
|
||||
sandbox_policy_cwd,
|
||||
codex_home,
|
||||
cwd,
|
||||
&mut env_map,
|
||||
&command,
|
||||
/*inherit_path*/ false,
|
||||
/*add_git_safe_directory*/ false,
|
||||
SpawnPrepOptions {
|
||||
inherit_path: false,
|
||||
add_git_safe_directory: false,
|
||||
},
|
||||
)?;
|
||||
if !common.policy.has_full_disk_read_access() {
|
||||
anyhow::bail!("Restricted read-only access requires the elevated Windows sandbox backend");
|
||||
@@ -323,9 +327,9 @@ pub(crate) async fn spawn_windows_sandbox_session_legacy(
|
||||
);
|
||||
let security =
|
||||
prepare_legacy_session_security(&common.policy, codex_home, cwd, capability_roots)?;
|
||||
allow_null_device_for_workspace_write(common.is_workspace_write);
|
||||
allow_null_device_for_workspace_write(common.uses_write_capabilities);
|
||||
|
||||
let persist_aces = common.is_workspace_write;
|
||||
let persist_aces = common.uses_write_capabilities;
|
||||
let guards = apply_legacy_session_acl_rules(
|
||||
&common.policy,
|
||||
sandbox_policy_cwd,
|
||||
|
||||
Reference in New Issue
Block a user