Skip to content

Commit a8b0373

Browse files
committed
Make Windows executable resolution deterministic
1 parent bb05c1f commit a8b0373

18 files changed

Lines changed: 969 additions & 80 deletions

File tree

‎codex-rs/core/tests/suite/windows_sandbox.rs‎

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -209,9 +209,18 @@ async fn windows_elevated_enforces_deny_read_and_protects_setup_marker() -> anyh
209209
let exact_secret = cwd.join("exact-secret.txt");
210210
let public = cwd.join("public.txt");
211211
let setup_marker = codex_home.path().join(".sandbox").join("setup_marker.json");
212+
let batch = cwd.join("sandbox-check.CMD");
212213
std::fs::write(&glob_secret, "glob secret\n")?;
213214
std::fs::write(&exact_secret, "exact secret\n")?;
214215
std::fs::write(&public, "public ok\n")?;
216+
std::fs::write(
217+
&batch,
218+
format!(
219+
"@echo off\r\n(type secret.env 1>NUL 2>NUL && echo GLOB-READ || echo GLOB-DENIED) & (type exact-secret.txt 1>NUL 2>NUL && echo EXACT-READ || echo EXACT-DENIED) & (type \"{}\" 1>NUL 2>NUL && echo MARKER-READ-ALLOWED || echo MARKER-READ-DENIED) & (echo tampered > \"{}\" 2>NUL && echo MARKER-WRITE-ALLOWED || echo MARKER-WRITE-DENIED) & type public.txt\r\n",
220+
setup_marker.display(),
221+
setup_marker.display()
222+
),
223+
)?;
215224

216225
let file_system_sandbox_policy = FileSystemSandboxPolicy::restricted(vec![
217226
FileSystemSandboxEntry {
@@ -243,25 +252,17 @@ async fn windows_elevated_enforces_deny_read_and_protects_setup_marker() -> anyh
243252
);
244253

245254
let ExecToolCallOutput {
246-
exit_code,
247-
stdout,
248-
..
255+
exit_code, stdout, ..
249256
} = process_exec_tool_call(
250257
ExecParams {
251-
command: vec![
252-
"cmd.exe".to_string(),
253-
"/D".to_string(),
254-
"/C".to_string(),
255-
format!(
256-
"(type secret.env 1>NUL 2>NUL && echo GLOB-READ || echo GLOB-DENIED) & (type exact-secret.txt 1>NUL 2>NUL && echo EXACT-READ || echo EXACT-DENIED) & (type \"{}\" 1>NUL 2>NUL && echo MARKER-READ-ALLOWED || echo MARKER-READ-DENIED) & (echo tampered > \"{}\" 2>NUL && echo MARKER-WRITE-ALLOWED || echo MARKER-WRITE-DENIED) & type public.txt",
257-
setup_marker.display(),
258-
setup_marker.display()
259-
),
260-
],
258+
command: vec!["sandbox-check".to_string()],
261259
cwd: cwd.clone(),
262260
expiration: 10_000.into(),
263261
capture_policy: ExecCapturePolicy::ShellTool,
264-
env: HashMap::new(),
262+
env: HashMap::from([
263+
("Path".to_string(), cwd.display().to_string()),
264+
("PathExt".to_string(), ".CMD".to_string()),
265+
]),
265266
network: None,
266267
network_environment_id: None,
267268
sandbox_permissions: SandboxPermissions::UseDefault,

‎codex-rs/protocol/src/shell_environment.rs‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ where
9393

9494
// Step 4 - Apply user-provided overrides.
9595
for (key, val) in &policy.r#set {
96+
#[cfg(target_os = "windows")]
97+
env_map.retain(|existing, _| !existing.eq_ignore_ascii_case(key));
9698
env_map.insert(key.clone(), val.clone());
9799
}
98100

@@ -209,6 +211,26 @@ mod windows_tests {
209211

210212
assert_eq!(result, expected);
211213
}
214+
215+
#[test]
216+
fn explicit_env_overrides_replace_inherited_keys_case_insensitively() {
217+
for (inherited_key, override_key) in [("PATH", "Path"), ("Path", "PATH")] {
218+
let vars = make_vars(&[(inherited_key, "C:\\host")]);
219+
let mut policy = ShellEnvironmentPolicy {
220+
inherit: ShellEnvironmentPolicyInherit::All,
221+
ignore_default_excludes: true,
222+
..Default::default()
223+
};
224+
policy
225+
.r#set
226+
.insert(override_key.to_string(), "C:\\request".to_string());
227+
228+
let result = populate_env(vars, &policy, /*thread_id*/ None);
229+
let expected = HashMap::from([(override_key.to_string(), "C:\\request".to_string())]);
230+
231+
assert_eq!(result, expected);
232+
}
233+
}
212234
}
213235

214236
#[cfg(all(test, not(target_os = "windows")))]

‎codex-rs/windows-sandbox-rs/Cargo.toml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ features = [
6767
"Win32_System_Threading",
6868
"Win32_System_JobObjects",
6969
"Win32_System_SystemServices",
70+
"Win32_System_SystemInformation",
7071
"Win32_System_Environment",
7172
"Win32_System_Pipes",
7273
"Win32_System_WindowsProgramming",

‎codex-rs/windows-sandbox-rs/src/bin/command_runner/win.rs‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ use codex_windows_sandbox::hide_current_user_profile_dir;
3939
use codex_windows_sandbox::log_note;
4040
use codex_windows_sandbox::read_frame;
4141
use codex_windows_sandbox::read_handle_loop;
42-
use codex_windows_sandbox::spawn_process_with_pipes;
42+
use codex_windows_sandbox::spawn_process_with_pipes_with_launch;
4343
use codex_windows_sandbox::to_wide;
4444
use codex_windows_sandbox::token_mode_for_permission_profile;
4545
use codex_windows_sandbox::write_frame;
@@ -300,9 +300,9 @@ fn spawn_ipc_process(req: &SpawnRequest) -> Result<IpcSpawnedProcess> {
300300
let mut hpc_handle: Option<HANDLE> = None;
301301
let mut pipe_handles = None;
302302
let (pi, stdout_handle, stderr_handle, stdin_handle) = if req.tty {
303-
let (pi, mut conpty) = codex_windows_sandbox::spawn_conpty_process_as_user(
303+
let (pi, mut conpty) = codex_windows_sandbox::spawn_conpty_process_as_user_with_launch(
304304
h_token.raw(),
305-
&req.command,
305+
&req.launch,
306306
&effective_cwd,
307307
&req.env,
308308
req.use_private_desktop,
@@ -332,9 +332,9 @@ fn spawn_ipc_process(req: &SpawnRequest) -> Result<IpcSpawnedProcess> {
332332
} else {
333333
StdinMode::Closed
334334
};
335-
let spawned_pipes: PipeSpawnHandles = spawn_process_with_pipes(
335+
let spawned_pipes: PipeSpawnHandles = spawn_process_with_pipes_with_launch(
336336
h_token.raw(),
337-
&req.command,
337+
&req.launch,
338338
&effective_cwd,
339339
&req.env,
340340
stdin_mode,

0 commit comments

Comments
 (0)