mirror of
https://github.com/pchuan98/codex.git
synced 2026-07-01 00:31:56 +08:00
fix: cancel Windows sandbox on network denial (#19880)
## Why When Guardian or the sandbox network proxy detects and denies a network attempt, core cancels the associated execution through `ExecExpiration`. The Windows sandbox capture path was only forwarding the timeout component of that expiration state. As a result, a sandboxed Windows command whose network attempt had already been denied could keep running until its timeout elapsed rather than terminating promptly in response to the denial. This change closes that cancellation-propagation gap for Windows sandbox execution. ## What changed - Added `WindowsSandboxCancellationToken` as the cancellation hook exposed to Windows capture backends. - Extracted the cancellation token from `ExecExpiration` in core and passed it to both the direct and elevated Windows sandbox capture paths alongside the existing timeout. - Updated direct capture to poll for either process exit, timeout, or cancellation and to terminate cancelled processes without reporting them as timed out. - Updated elevated capture to watch for cancellation and send the existing `Terminate` IPC frame to the elevated runner. The watcher parks for 50 ms between checks to bound response latency without a tight busy wait. - Added Windows regression coverage for a long-running PowerShell command: cancellation ends capture before its timeout and does not set `timed_out`. - Added a visible skip diagnostic when that PowerShell-dependent regression test cannot execute, and consolidated the duplicated expiration-policy branch identified in review. ## Security This improves enforcement after a denied network attempt has been attributed to a Windows sandboxed execution: the command no longer remains alive simply because Windows capture lost the cancellation signal. This PR does not claim to make Windows offline mode an airtight no-network or no-exfiltration boundary. It does not introduce AppContainer or change how network denial is detected; it makes an already-detected denial promptly stop the affected sandboxed command. ## Validation ### Commands run - `just fmt` - `cargo test -p codex-windows-sandbox` - `cargo test -p codex-core network_denial` - `cargo clippy -p codex-core -p codex-windows-sandbox --tests --no-deps -- -D warnings` - `just argument-comment-lint -p codex-windows-sandbox -p codex-core` The new capture regression is `cfg(target_os = "windows")`, so Windows CI is the execution coverage for that test path. The local macOS test runs validate the host-runnable crate and core network-denial behavior. --------- Co-authored-by: Codex <noreply@openai.com>
This commit is contained in:
@@ -225,6 +225,17 @@ impl ExecExpiration {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg_attr(not(target_os = "windows"), allow(dead_code))]
|
||||
pub(crate) fn cancellation_token(&self) -> Option<CancellationToken> {
|
||||
match self {
|
||||
ExecExpiration::Timeout(_) | ExecExpiration::DefaultTimeout => None,
|
||||
ExecExpiration::Cancellation(cancellation)
|
||||
| ExecExpiration::TimeoutOrCancellation { cancellation, .. } => {
|
||||
Some(cancellation.clone())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn with_cancellation(self, cancellation: CancellationToken) -> Self {
|
||||
match self {
|
||||
ExecExpiration::Timeout(timeout) => ExecExpiration::TimeoutOrCancellation {
|
||||
@@ -592,12 +603,16 @@ async fn exec_windows_sandbox(
|
||||
network.apply_to_env(&mut env);
|
||||
}
|
||||
|
||||
// TODO(iceweasel-oai): run_windows_sandbox_capture should support all
|
||||
// variants of ExecExpiration, not just timeout.
|
||||
let timeout_ms = if capture_policy.uses_expiration() {
|
||||
expiration.timeout_ms()
|
||||
// Windows sandbox capture still receives timeout and cancellation separately.
|
||||
let (cancellation, timeout_ms) = if capture_policy.uses_expiration() {
|
||||
let cancellation = expiration.cancellation_token().map(|token| {
|
||||
codex_windows_sandbox::WindowsSandboxCancellationToken::new(move || {
|
||||
token.is_cancelled()
|
||||
})
|
||||
});
|
||||
(cancellation, expiration.timeout_ms())
|
||||
} else {
|
||||
None
|
||||
(None, None)
|
||||
};
|
||||
|
||||
let sandbox_cwd = windows_sandbox_policy_cwd.clone();
|
||||
@@ -634,6 +649,7 @@ async fn exec_windows_sandbox(
|
||||
cwd: &cwd,
|
||||
env_map: env,
|
||||
timeout_ms,
|
||||
cancellation,
|
||||
use_private_desktop: windows_sandbox_private_desktop,
|
||||
proxy_enforced,
|
||||
read_roots_override: elevated_read_roots_override.as_deref(),
|
||||
@@ -653,6 +669,7 @@ async fn exec_windows_sandbox(
|
||||
&cwd,
|
||||
env,
|
||||
timeout_ms,
|
||||
cancellation,
|
||||
&additional_deny_read_paths,
|
||||
&additional_deny_write_paths,
|
||||
windows_sandbox_private_desktop,
|
||||
|
||||
Reference in New Issue
Block a user