mirror of
https://github.com/openai/codex.git
synced 2026-11-17 22:10:37 +00:00
Preserve native sandbox executable paths and PTY dispatch names (#61910)
## Why Sandbox helpers with non-UTF-8 paths can lose their exact executable path when converted into command requests. PTY launches with a custom `argv[0]` can also try to execute the dispatch name instead of the configured program. ## What changed - Carry native executable paths through sandbox requests, local execution, shell snapshots, and sandbox integrity checks. - Use native Unix PTY spawning when `argv[0]` differs from the executable, preserving the dispatch name and controlling terminal setup. - Preserve PTY executable lookup through the child environment's `PATH` and fallback to its home directory when the requested working directory is missing. ## Testing Add regression coverage for non-UTF-8 sandbox helper paths, PTY dispatch names, child `HOME`/`PATH` lookup, controlling terminals, and exit-code preservation. GitOrigin-RevId: 6a2af0486220d98195f5d67fde2597e3c4515e11
This commit is contained in:
@@ -325,29 +325,22 @@ pub(crate) async fn wait_for_helper_output(
|
||||
}
|
||||
|
||||
pub(crate) fn spawn_command(
|
||||
SandboxExecRequest {
|
||||
command: argv,
|
||||
cwd,
|
||||
mut env,
|
||||
arg0,
|
||||
..
|
||||
}: SandboxExecRequest,
|
||||
request: SandboxExecRequest,
|
||||
stdin: ChildStdin,
|
||||
) -> Result<Child, JSONRPCErrorError> {
|
||||
let Some((program, args)) = argv.split_first() else {
|
||||
let Some((program, args)) = request.program_and_args() else {
|
||||
return Err(invalid_request("fs sandbox command was empty".to_string()));
|
||||
};
|
||||
let mut command = Command::new(program);
|
||||
command.args(args);
|
||||
#[cfg(unix)]
|
||||
if let Some(arg0) = arg0 {
|
||||
if let Some(arg0) = request.arg0 {
|
||||
command.arg0(arg0);
|
||||
}
|
||||
#[cfg(not(unix))]
|
||||
let _ = arg0;
|
||||
command.args(args);
|
||||
// TODO(anp): Keep PathUri through the filesystem helper launch boundary.
|
||||
let cwd = cwd.to_abs_path().map_err(io_error)?;
|
||||
let cwd = request.cwd.to_abs_path().map_err(io_error)?;
|
||||
command.current_dir(cwd.as_path());
|
||||
let mut env = request.env;
|
||||
env.retain(|name, _| !codex_protocol::shell_environment::is_non_inheritable_env_var(name));
|
||||
command.envs(env);
|
||||
command.stdin(stdin);
|
||||
|
||||
@@ -235,6 +235,7 @@ fn powershell_command(script: &str, path: &Path) -> anyhow::Result<SandboxExecRe
|
||||
let cwd = PathUri::from_host_native_path(std::env::current_dir()?)?;
|
||||
|
||||
Ok(SandboxExecRequest {
|
||||
native_program: None,
|
||||
sandbox_override: SandboxOverride::NoOverride,
|
||||
command: vec![
|
||||
powershell.to_string_lossy().into_owned(),
|
||||
|
||||
@@ -400,9 +400,9 @@ impl LocalProcess {
|
||||
&self.inner.credential_brokers,
|
||||
)
|
||||
.await?;
|
||||
if prepared.command.is_empty() {
|
||||
return Err(invalid_params("argv must not be empty".to_string()));
|
||||
}
|
||||
let (program, args) = prepared
|
||||
.program_and_args()
|
||||
.ok_or_else(|| invalid_params("argv must not be empty".to_string()))?;
|
||||
let sandbox_type = match prepared.sandbox {
|
||||
SandboxType::None => Some(ProcessSandboxType::None),
|
||||
SandboxType::MacosSeatbelt => Some(ProcessSandboxType::MacosSeatbelt),
|
||||
@@ -433,7 +433,8 @@ impl LocalProcess {
|
||||
#[cfg(not(unix))]
|
||||
let inherited_fds = Vec::new();
|
||||
let spawned_result = codex_sandboxing::spawn_process(codex_sandboxing::SpawnRequest {
|
||||
command: &prepared.command,
|
||||
program,
|
||||
args,
|
||||
cwd: prepared.cwd.as_path(),
|
||||
env: &prepared.env,
|
||||
arg0: &prepared.arg0,
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
use std::collections::HashMap;
|
||||
use std::ffi::OsStr;
|
||||
use std::ffi::OsString;
|
||||
use std::path::Path;
|
||||
use std::sync::Arc;
|
||||
|
||||
@@ -46,6 +48,7 @@ const CREDENTIAL_MASKING_FAILURE_MODE: CredentialMaskingFailureMode =
|
||||
|
||||
pub(crate) struct PreparedExecRequest {
|
||||
pub(crate) command: Vec<String>,
|
||||
native_program: Option<OsString>,
|
||||
pub(crate) cwd: AbsolutePathBuf,
|
||||
pub(crate) env: HashMap<String, String>,
|
||||
pub(crate) arg0: Option<String>,
|
||||
@@ -65,6 +68,16 @@ struct PreparedWindowsSandboxRequest {
|
||||
}
|
||||
|
||||
impl PreparedExecRequest {
|
||||
pub(crate) fn program_and_args(&self) -> Option<(&OsStr, &[String])> {
|
||||
let (program, args) = self.command.split_first()?;
|
||||
Some((
|
||||
self.native_program
|
||||
.as_deref()
|
||||
.unwrap_or_else(|| OsStr::new(program)),
|
||||
args,
|
||||
))
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
pub(crate) fn credential_snapshot_proxy(&self, params: &ExecParams) -> Option<&NetworkProxy> {
|
||||
let launch = params.network_proxy.as_ref()?;
|
||||
@@ -151,6 +164,7 @@ pub(crate) async fn prepare_exec_request_with_telemetry(
|
||||
let Some(sandbox_context) = params.sandbox.as_ref() else {
|
||||
return Ok(PreparedExecRequest {
|
||||
command: params.argv.clone(),
|
||||
native_program: None,
|
||||
cwd: native_path(¶ms.cwd, "cwd")?,
|
||||
env,
|
||||
arg0: params.arg0.clone(),
|
||||
@@ -300,6 +314,7 @@ pub(crate) async fn prepare_exec_request_with_telemetry(
|
||||
};
|
||||
Ok(PreparedExecRequest {
|
||||
command: request.command,
|
||||
native_program: request.native_program,
|
||||
cwd: native_path(&request.cwd, "cwd")?,
|
||||
env: request.env,
|
||||
arg0: request.arg0,
|
||||
|
||||
@@ -51,6 +51,7 @@ pub(super) fn sandbox_request(
|
||||
policy: &FileSystemSandboxPolicy,
|
||||
) -> SandboxExecRequest {
|
||||
SandboxExecRequest {
|
||||
native_program: None,
|
||||
sandbox_override: SandboxOverride::NoOverride,
|
||||
command: vec![cwd.join("not-executed").to_string_lossy().into_owned()],
|
||||
cwd: PathUri::from_abs_path(cwd),
|
||||
|
||||
@@ -34,7 +34,7 @@ pub(super) fn critical_dependencies(
|
||||
.transpose()
|
||||
.map_err(io::Error::other)?;
|
||||
let mut dependencies = codex_and_launcher_dependencies(request);
|
||||
if let Some(exe) = request.command.first()
|
||||
if let Some((exe, _)) = request.program_and_args()
|
||||
&& let Some(bwrap) = codex_linux_sandbox::find_bundled_bwrap_for_exe(
|
||||
&command_cwd.as_path().join(exe),
|
||||
command_cwd.as_path(),
|
||||
|
||||
@@ -33,7 +33,7 @@ pub(super) fn codex_and_launcher_dependencies(
|
||||
request: &codex_sandboxing::SandboxExecRequest,
|
||||
) -> Vec<Dependency> {
|
||||
let mut dependencies = vec![codex_executable()];
|
||||
if let Some(exe) = request.command.first() {
|
||||
if let Some((exe, _)) = request.program_and_args() {
|
||||
dependencies.push(("sandbox_launcher", Ok(PathBuf::from(exe))));
|
||||
}
|
||||
dependencies
|
||||
|
||||
@@ -448,9 +448,7 @@ done
|
||||
format!("printf '\\0%s\\0%s\\0' '{SOURCE_MARKER}' 0\n{script}")
|
||||
};
|
||||
let shell_start = prepared.command.len() - params.argv.len();
|
||||
let mut argv = prepared.command.clone();
|
||||
argv[shell_start + 2] = script;
|
||||
let (program, args) = argv.split_first().ok_or_else(|| {
|
||||
let (program, args) = prepared.program_and_args().ok_or_else(|| {
|
||||
(
|
||||
"missing_command",
|
||||
internal_error("missing shell snapshot command".to_string()),
|
||||
@@ -459,7 +457,9 @@ done
|
||||
|
||||
let mut command = Command::new(program);
|
||||
command
|
||||
.args(args)
|
||||
.args(&args[..shell_start + 1])
|
||||
.arg(script)
|
||||
.args(&args[shell_start + 2..])
|
||||
.current_dir(prepared.cwd.as_path())
|
||||
.env_clear()
|
||||
.envs(&prepared.env)
|
||||
|
||||
Reference in New Issue
Block a user