mirror of
https://github.com/pchuan98/codex.git
synced 2026-07-01 00:31:56 +08:00
## Why The Windows sandbox runner still carried the old `SandboxPolicy` compatibility path even though core now computes `PermissionProfile`. That meant Windows command-runner execution could only see the legacy projection, so profile-only filesystem rules such as deny globs were not part of the runner input. ## What Changed - Removed the Windows-local `SandboxPolicy` parser/export and deleted `windows-sandbox-rs/src/policy.rs`. - Changed restricted-token capture/session setup, elevated setup, world-writable audit, read-root grant, and command-runner session APIs to accept `PermissionProfile` plus the profile cwd. - Bumped the elevated command-runner IPC protocol to version 2 because `SpawnRequest` now carries `permission_profile` / `permission_profile_cwd` instead of the legacy `policy_json_or_preset` / `sandbox_policy_cwd` fields. - Updated core exec, unified exec, debug-sandbox, TUI setup/grant flows, and app-server setup to pass the actual effective `PermissionProfile`. - Left regression coverage asserting the old IPC policy fields are absent and the runner serializes tagged `PermissionProfile` JSON. ## Verification - `cargo test -p codex-windows-sandbox` - `cargo test -p codex-core windows_sandbox` - `cargo test -p codex-app-server request_processors::windows_sandbox_processor` - `just fix -p codex-windows-sandbox -p codex-core -p codex-app-server -p codex-cli -p codex-tui` - `just fix -p codex-cli -p codex-tui` - `just fix -p codex-windows-sandbox -p codex-tui` - `rg "\\bSandboxPolicy\\b" codex-rs/windows-sandbox-rs` returned no matches. Note: `cargo test -p codex-cli` was attempted but did not reach crate tests because local disk filled while compiling dependencies (`No space left on device`). The targeted clippy pass compiled the affected CLI/TUI surfaces afterward. --- [//]: # (BEGIN SAPLING FOOTER) Stack created with [Sapling](https://sapling-scm.com). Best reviewed with [ReviewStack](https://reviewstack.dev/openai/codex/pull/23813). * #24108 * __->__ #23813
231 lines
8.5 KiB
Rust
231 lines
8.5 KiB
Rust
use codex_protocol::models::PermissionProfile;
|
|
use codex_utils_absolute_path::AbsolutePathBuf;
|
|
use std::collections::HashMap;
|
|
use std::path::Path;
|
|
use std::path::PathBuf;
|
|
|
|
pub struct ElevatedSandboxProfileCaptureRequest<'a> {
|
|
pub permission_profile: &'a PermissionProfile,
|
|
pub permission_profile_cwd: &'a Path,
|
|
pub codex_home: &'a Path,
|
|
pub command: Vec<String>,
|
|
pub cwd: &'a Path,
|
|
pub env_map: HashMap<String, String>,
|
|
pub timeout_ms: Option<u64>,
|
|
pub use_private_desktop: bool,
|
|
pub proxy_enforced: bool,
|
|
pub read_roots_override: Option<&'a [PathBuf]>,
|
|
pub read_roots_include_platform_defaults: bool,
|
|
pub write_roots_override: Option<&'a [PathBuf]>,
|
|
pub deny_read_paths_override: &'a [AbsolutePathBuf],
|
|
pub deny_write_paths_override: &'a [AbsolutePathBuf],
|
|
}
|
|
|
|
mod windows_impl {
|
|
use super::ElevatedSandboxProfileCaptureRequest;
|
|
use crate::acl::allow_null_device;
|
|
use crate::cap::load_or_create_cap_sids;
|
|
use crate::cap::workspace_write_cap_sid_for_root;
|
|
use crate::env::ensure_non_interactive_pager;
|
|
use crate::env::inherit_path_env;
|
|
use crate::env::normalize_null_device_env;
|
|
use crate::identity::require_logon_sandbox_creds;
|
|
use crate::ipc_framed::Message;
|
|
use crate::ipc_framed::OutputStream;
|
|
use crate::ipc_framed::SpawnRequest;
|
|
use crate::ipc_framed::decode_bytes;
|
|
use crate::ipc_framed::read_frame;
|
|
use crate::logging::log_failure;
|
|
use crate::logging::log_start;
|
|
use crate::logging::log_success;
|
|
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_permissions;
|
|
use crate::token::LocalSid;
|
|
use anyhow::Result;
|
|
use codex_utils_absolute_path::AbsolutePathBuf;
|
|
use std::path::Path;
|
|
|
|
pub use crate::windows_impl::CaptureResult;
|
|
|
|
/// Launches the command runner under the sandbox user and captures its output.
|
|
#[allow(clippy::too_many_arguments)]
|
|
pub fn run_windows_sandbox_capture_for_permission_profile(
|
|
request: ElevatedSandboxProfileCaptureRequest<'_>,
|
|
) -> Result<CaptureResult> {
|
|
let ElevatedSandboxProfileCaptureRequest {
|
|
permission_profile,
|
|
permission_profile_cwd,
|
|
codex_home,
|
|
command,
|
|
cwd,
|
|
mut env_map,
|
|
timeout_ms,
|
|
use_private_desktop,
|
|
proxy_enforced,
|
|
read_roots_override,
|
|
read_roots_include_platform_defaults,
|
|
write_roots_override,
|
|
deny_read_paths_override,
|
|
deny_write_paths_override,
|
|
} = request;
|
|
let permissions = ResolvedWindowsSandboxPermissions::try_from_permission_profile_for_cwd(
|
|
permission_profile,
|
|
permission_profile_cwd,
|
|
)?;
|
|
let deny_read_paths_override = deny_read_paths_override
|
|
.iter()
|
|
.map(AbsolutePathBuf::to_path_buf)
|
|
.collect::<Vec<_>>();
|
|
let deny_write_paths_override = deny_write_paths_override
|
|
.iter()
|
|
.map(AbsolutePathBuf::to_path_buf)
|
|
.collect::<Vec<_>>();
|
|
normalize_null_device_env(&mut env_map);
|
|
ensure_non_interactive_pager(&mut env_map);
|
|
inherit_path_env(&mut env_map);
|
|
inject_git_safe_directory(&mut env_map, cwd);
|
|
// Use a temp-based log dir that the sandbox user can write.
|
|
let sandbox_base = codex_home.join(".sandbox");
|
|
ensure_codex_home_exists(&sandbox_base)?;
|
|
|
|
let logs_base_dir: Option<&Path> = Some(sandbox_base.as_path());
|
|
log_start(&command, logs_base_dir);
|
|
let sandbox_creds = require_logon_sandbox_creds(
|
|
&permissions,
|
|
cwd,
|
|
&env_map,
|
|
codex_home,
|
|
read_roots_override,
|
|
read_roots_include_platform_defaults,
|
|
write_roots_override,
|
|
&deny_read_paths_override,
|
|
&deny_write_paths_override,
|
|
proxy_enforced,
|
|
)?;
|
|
// Build capability SID for ACL grants.
|
|
let caps = load_or_create_cap_sids(codex_home)?;
|
|
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 {
|
|
allow_null_device(sid_for_null.as_ptr());
|
|
}
|
|
|
|
(|| -> Result<CaptureResult> {
|
|
let spawn_request = SpawnRequest {
|
|
command: command.clone(),
|
|
cwd: cwd.to_path_buf(),
|
|
env: env_map.clone(),
|
|
permission_profile: permission_profile.clone(),
|
|
permission_profile_cwd: permission_profile_cwd.to_path_buf(),
|
|
codex_home: sandbox_base.clone(),
|
|
real_codex_home: codex_home.to_path_buf(),
|
|
cap_sids,
|
|
timeout_ms,
|
|
tty: false,
|
|
stdin_open: false,
|
|
use_private_desktop,
|
|
};
|
|
let transport = spawn_runner_transport(
|
|
codex_home,
|
|
cwd,
|
|
&sandbox_creds,
|
|
logs_base_dir,
|
|
spawn_request,
|
|
)?;
|
|
let (pipe_write, mut pipe_read) = transport.into_files();
|
|
drop(pipe_write);
|
|
|
|
let mut stdout = Vec::new();
|
|
let mut stderr = Vec::new();
|
|
let (exit_code, timed_out) = loop {
|
|
let msg = read_frame(&mut pipe_read)?
|
|
.ok_or_else(|| anyhow::anyhow!("runner pipe closed before exit"))?;
|
|
match msg.message {
|
|
Message::SpawnReady { .. } => {}
|
|
Message::Output { payload } => {
|
|
let bytes = decode_bytes(&payload.data_b64)?;
|
|
match payload.stream {
|
|
OutputStream::Stdout => stdout.extend_from_slice(&bytes),
|
|
OutputStream::Stderr => stderr.extend_from_slice(&bytes),
|
|
}
|
|
}
|
|
Message::Exit { payload } => break (payload.exit_code, payload.timed_out),
|
|
Message::Error { payload } => {
|
|
return Err(anyhow::anyhow!("runner error: {}", payload.message));
|
|
}
|
|
other => {
|
|
return Err(anyhow::anyhow!(
|
|
"unexpected runner message during capture: {other:?}"
|
|
));
|
|
}
|
|
}
|
|
};
|
|
|
|
if exit_code == 0 {
|
|
log_success(&command, logs_base_dir);
|
|
} else {
|
|
log_failure(&command, &format!("exit code {exit_code}"), logs_base_dir);
|
|
}
|
|
|
|
Ok(CaptureResult {
|
|
exit_code,
|
|
stdout,
|
|
stderr,
|
|
timed_out,
|
|
})
|
|
})()
|
|
}
|
|
}
|
|
|
|
#[cfg(target_os = "windows")]
|
|
pub use windows_impl::run_windows_sandbox_capture_for_permission_profile;
|
|
|
|
#[cfg(not(target_os = "windows"))]
|
|
mod stub {
|
|
use super::ElevatedSandboxProfileCaptureRequest;
|
|
use anyhow::Result;
|
|
use anyhow::bail;
|
|
|
|
#[derive(Debug, Default)]
|
|
pub struct CaptureResult {
|
|
pub exit_code: i32,
|
|
pub stdout: Vec<u8>,
|
|
pub stderr: Vec<u8>,
|
|
pub timed_out: bool,
|
|
}
|
|
|
|
/// Stub implementation for non-Windows targets; sandboxing only works on Windows.
|
|
#[allow(clippy::too_many_arguments)]
|
|
pub fn run_windows_sandbox_capture_for_permission_profile(
|
|
_request: ElevatedSandboxProfileCaptureRequest<'_>,
|
|
) -> Result<CaptureResult> {
|
|
bail!("Windows sandbox is only available on Windows")
|
|
}
|
|
}
|
|
|
|
#[cfg(not(target_os = "windows"))]
|
|
pub use stub::run_windows_sandbox_capture_for_permission_profile;
|