Files
codex/codex-rs/app-server/tests/common/local_websocket_exec_server.rs
T
viyatb-oai 6ee9bc1ba7 Select process sandboxes during launch preparation (#62183)
## Why

Workspace-relative permission rules need to resolve against the execution
host's workspace roots so a grant applies to the intended directory.
Restricted launches also need to check that the host can provide the
required sandbox.

## What changed

- Select the platform sandbox while preparing a launch, using the permission
  profile, network requirements, and Windows sandbox settings.
- Pass workspace roots into policy resolution for local commands and shell
  snapshots. Already-bound command profiles validate their concrete paths
  without resolving Windows launcher hints on hosts that do not use them.
- Share local launch preparation between tool commands and shell snapshots,
  and require a platform sandbox for exec-server launches that request one.

## Testing

Cover network-only restrictions and verify that a
`:workspace_roots` write grant allows writes to the configured subdirectory
under the command's working directory while denying writes to a sibling file
and the corresponding subdirectory under the parent. Exercise the path grant
through both app-server command execution and WebSocket exec-server execution.

GitOrigin-RevId: 492fe7ab8deddf30cfd2e2bdabf28d02d67e00c5
2026-11-18 03:59:37 +00:00

88 lines
3.0 KiB
Rust

use std::path::Path;
use std::process::Stdio;
use std::time::Duration;
use anyhow::Context;
use anyhow::Result;
use anyhow::anyhow;
use tokio::io::AsyncBufReadExt;
use tokio::io::BufReader;
use tokio::process::Child;
use tokio::process::Command;
const START_TIMEOUT: Duration = Duration::from_secs(10);
#[cfg(target_os = "linux")]
const CODEX_LINUX_SANDBOX_EXE_ENV_VAR: &str = "CODEX_TEST_LINUX_SANDBOX_EXE";
/// Runs a host-local exec-server over WebSocket for integration tests.
pub struct LocalWebsocketExecServer {
child: Child,
websocket_url: String,
}
impl LocalWebsocketExecServer {
pub async fn start(codex_home: &Path, exec_server_program: &Path) -> Result<Self> {
let mut command = Command::new(exec_server_program);
command.stdin(Stdio::null());
command.stdout(Stdio::piped());
command.stderr(Stdio::inherit());
command.current_dir(codex_home);
command.env("CODEX_HOME", codex_home);
#[cfg(target_os = "linux")]
command.env(
CODEX_LINUX_SANDBOX_EXE_ENV_VAR,
core_test_support::find_codex_linux_sandbox_exe()
.context("should find binary for delayed exec-server Linux sandbox helper")?,
);
command.kill_on_drop(true);
let child = command.spawn().context("start local exec-server fixture")?;
let mut exec_server = Self {
child,
websocket_url: String::new(),
};
let stdout = exec_server
.child
.stdout
.take()
.ok_or_else(|| anyhow!("local exec-server fixture stdout was not captured"))?;
let mut lines = BufReader::new(stdout).lines();
let deadline = tokio::time::Instant::now() + START_TIMEOUT;
exec_server.websocket_url = loop {
let remaining = deadline
.checked_duration_since(tokio::time::Instant::now())
.ok_or_else(|| anyhow!("timed out waiting for local exec-server listen URL"))?;
let line = tokio::time::timeout(remaining, lines.next_line())
.await
.map_err(|_| anyhow!("timed out waiting for local exec-server listen URL"))??
.ok_or_else(|| {
anyhow!("local exec-server exited before emitting its listen URL")
})?;
let listen_url = line.trim();
if listen_url.starts_with("ws://") {
break listen_url.to_string();
}
};
Ok(exec_server)
}
pub fn websocket_url(&self) -> &str {
&self.websocket_url
}
}
impl Drop for LocalWebsocketExecServer {
fn drop(&mut self) {
let _ = self.child.start_kill();
let start = std::time::Instant::now();
let timeout = Duration::from_secs(5);
while start.elapsed() < timeout {
match self.child.try_wait() {
Ok(Some(_)) => return,
Ok(None) => std::thread::sleep(Duration::from_millis(10)),
Err(_) => return,
}
}
}
}