Apply remote platform semantics to exec safety checks (#42113)

## Why

Unified exec can run commands on an OS that differs from the Codex host. Using
host path and shell semantics for approval checks can therefore miss dangerous
Windows and PowerShell commands sent to a remote executor.

## What changed

- Thread the executor platform through command parsing, executable-name
  normalization, and dangerous-command classification. Legacy executors fall
  back to the host platform, while unknown reported platforms use Windows rules
  conservatively.
- Bound model-facing `exec_command` rejection messages and avoid echoing the
  rejected command into the error.
- Keep deterministic process IDs reserved after release so rejected test
  commands cannot reuse an earlier ID.

## Testing

- Add coverage that a long dangerous PowerShell command targeting a remote
  Windows executor is rejected with a bounded response.
- Verify deterministic process IDs advance after release.

GitOrigin-RevId: a26b4a63ea3d6df7140032b88b3e7aec0b85b948
This commit is contained in:
iceweasel-oai authored and copyberry committed 2026-09-01 18:24:56 +00:00
1 parent c7c824dce4
commit 1f4c47343a
9 files changed
+259 -113

No files matched your search

@@ -81,6 +81,7 @@ use codex_protocol::protocol::ExecCommandSource;
use codex_protocol::protocol::TerminalInteractionEvent;
use codex_protocol::shell_environment::is_non_inheritable_env_var;
use codex_sandboxing::SandboxCommand;
use codex_shell_command::is_dangerous_command::DangerousCommandPlatform;
use codex_tools::ToolName;
use codex_utils_output_truncation::approx_tokens_from_byte_count;
use codex_utils_path_uri::PathUri;
@@ -462,7 +463,11 @@ impl UnifiedExecProcessManager {
pub(crate) async fn release_process_id(&self, process_id: i32) {
let removed = {
let mut store = self.process_store.lock().await;
store.remove(process_id)
let entry = store.processes.remove(&process_id);
if !should_use_deterministic_process_ids() {
store.reserved_process_ids.remove(&process_id);
}
entry
};
if let Some(entry) = removed {
unregister_network_approval_for_entry(&entry).await;
@@ -1369,6 +1374,13 @@ impl UnifiedExecProcessManager {
.shell
.as_ref()
.unwrap_or(session_shell.as_ref());
// Legacy executors omit the platform. Unknown reported platforms use the
// more conservative Windows rules rather than silently weakening checks.
let command_platform = match request.turn_environment.executor_platform_os.as_deref() {
Some("linux" | "macos") => DangerousCommandPlatform::Posix,
Some("windows") | Some(_) => DangerousCommandPlatform::Windows,
None => DangerousCommandPlatform::host(),
};
let exec_approval_requirement = context
.session
.services
@@ -1390,6 +1402,7 @@ impl UnifiedExecProcessManager {
},
configured_shell,
&request.shell_mode,
command_platform,
)
.await;
let req = UnifiedExecToolRequest {
@@ -37,6 +37,16 @@ fn unified_exec_env_overrides_existing_values() {
assert_eq!(env.get("PATH"), Some(&"/usr/bin".to_string()));
}
#[tokio::test]
async fn deterministic_process_ids_are_not_reused_after_release() {
let manager = UnifiedExecProcessManager::default();
let first = manager.allocate_process_id().await;
manager.release_process_id(first).await;
let second = manager.allocate_process_id().await;
assert_eq!((first, second), (1000, 1001));
}
#[test]
fn env_overlay_for_exec_server_keeps_runtime_changes_only() {
let local_policy_env = HashMap::from([