diff --git a/codex-rs/core/tests/suite/windows_sandbox.rs b/codex-rs/core/tests/suite/windows_sandbox.rs index 3525654b15..887ed5a42f 100644 --- a/codex-rs/core/tests/suite/windows_sandbox.rs +++ b/codex-rs/core/tests/suite/windows_sandbox.rs @@ -438,6 +438,78 @@ async fn windows_elevated_does_not_create_missing_workspace_metadata() -> anyhow path.display() ); } + + let firewall_output = std::process::Command::new("powershell.exe") + .args([ + "-NoProfile", + "-NonInteractive", + "-Command", + r#" +$ErrorActionPreference = 'Stop' +$sid = (Get-LocalUser -Name 'CodexSandboxOffline').SID.Value +$rules = foreach ($name in @('codex_sandbox_offline_block_inbound', 'codex_sandbox_offline_block_outbound')) { + $rule = @(Get-NetFirewallRule -PolicyStore ActiveStore -DisplayName $name) + if ($rule.Count -ne 1) { throw "Expected exactly one effective rule for $name" } + $address = $rule | Get-NetFirewallAddressFilter + $security = $rule | Get-NetFirewallSecurityFilter + $port = $rule | Get-NetFirewallPortFilter + [pscustomobject]@{ + name = $name + direction = [string]$rule[0].Direction + action = [string]$rule[0].Action + enabled = [string]$rule[0].Enabled + profile = [string]$rule[0].Profile + protocol = [string]$port.Protocol + localAddresses = @($address.LocalAddress) + remoteAddresses = @($address.RemoteAddress | Sort-Object) + localPorts = @($port.LocalPort) + remotePorts = @($port.RemotePort) + localUser = [string]$security.LocalUser + } +} +[pscustomobject]@{ offlineSid = $sid; rules = @($rules) } | ConvertTo-Json -Depth 4 -Compress +"#, + ]) + .output() + .context("read offline sandbox firewall rules")?; + assert!( + firewall_output.status.success(), + "read offline sandbox firewall rules: {}", + String::from_utf8_lossy(&firewall_output.stderr) + ); + let firewall_rules: serde_json::Value = serde_json::from_slice(&firewall_output.stdout) + .context("parse offline sandbox firewall rules")?; + let offline_sid = firewall_rules["offlineSid"] + .as_str() + .context("offline sandbox firewall rules should include their user SID")?; + let local_user = format!("O:LSD:(A;;CC;;;{offline_sid})"); + let expected_rules: Vec<_> = [ + ("codex_sandbox_offline_block_inbound", "Inbound"), + ("codex_sandbox_offline_block_outbound", "Outbound"), + ] + .into_iter() + .map(|(name, direction)| { + serde_json::json!({ + "name": name, + "direction": direction, + "action": "Block", + "enabled": "True", + "profile": "Any", + "protocol": "Any", + "localAddresses": ["Any"], + "remoteAddresses": [ + "::", + "::2-ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", + "0.0.0.0-126.255.255.255", + "128.0.0.0-255.255.255.255", + ], + "localPorts": ["Any"], + "remotePorts": ["Any"], + "localUser": local_user, + }) + }) + .collect(); + assert_eq!(firewall_rules["rules"], serde_json::json!(expected_rules)); Ok(()) } diff --git a/codex-rs/windows-sandbox-rs/src/bin/setup_main/win.rs b/codex-rs/windows-sandbox-rs/src/bin/setup_main/win.rs index f80efe4d12..93fe742006 100644 --- a/codex-rs/windows-sandbox-rs/src/bin/setup_main/win.rs +++ b/codex-rs/windows-sandbox-rs/src/bin/setup_main/win.rs @@ -750,14 +750,14 @@ fn configure_offline_sandbox_network( format!("ensure offline proxy allowlist failed: {err}"), ))); } - let firewall_result = firewall::ensure_offline_outbound_block(offline_sid_str, log); + let firewall_result = firewall::ensure_offline_network_blocks(offline_sid_str, log); if let Err(err) = firewall_result { if extract_setup_failure(&err).is_some() { return Err(err); } return Err(anyhow::Error::new(SetupFailure::new( SetupErrorCode::HelperFirewallRuleCreateOrAddFailed, - format!("ensure offline outbound block failed: {err}"), + format!("ensure offline network blocks failed: {err}"), ))); } Ok(()) diff --git a/codex-rs/windows-sandbox-rs/src/bin/setup_main/win/firewall.rs b/codex-rs/windows-sandbox-rs/src/bin/setup_main/win/firewall.rs index bf2476dd12..a2e3b27fac 100644 --- a/codex-rs/windows-sandbox-rs/src/bin/setup_main/win/firewall.rs +++ b/codex-rs/windows-sandbox-rs/src/bin/setup_main/win/firewall.rs @@ -13,7 +13,9 @@ use windows::Win32::NetworkManagement::WindowsFirewall::NET_FW_IP_PROTOCOL_UDP; use windows::Win32::NetworkManagement::WindowsFirewall::NET_FW_MODIFY_STATE; use windows::Win32::NetworkManagement::WindowsFirewall::NET_FW_MODIFY_STATE_OK; use windows::Win32::NetworkManagement::WindowsFirewall::NET_FW_PROFILE2_ALL; +use windows::Win32::NetworkManagement::WindowsFirewall::NET_FW_RULE_DIR_IN; use windows::Win32::NetworkManagement::WindowsFirewall::NET_FW_RULE_DIR_OUT; +use windows::Win32::NetworkManagement::WindowsFirewall::NET_FW_RULE_DIRECTION; use windows::Win32::NetworkManagement::WindowsFirewall::NetFwPolicy2; use windows::Win32::NetworkManagement::WindowsFirewall::NetFwRule; use windows::Win32::System::Com::CLSCTX_INPROC_SERVER; @@ -30,11 +32,14 @@ use codex_windows_sandbox::SetupFailure; // This is the stable identifier we use to find/update the rule idempotently. // It intentionally does not change between installs. const OFFLINE_BLOCK_RULE_NAME: &str = "codex_sandbox_offline_block_outbound"; +const OFFLINE_BLOCK_INBOUND_RULE_NAME: &str = "codex_sandbox_offline_block_inbound"; const OFFLINE_BLOCK_LOOPBACK_TCP_RULE_NAME: &str = "codex_sandbox_offline_block_loopback_tcp"; const OFFLINE_BLOCK_LOOPBACK_UDP_RULE_NAME: &str = "codex_sandbox_offline_block_loopback_udp"; // Friendly text shown in the firewall UI. const OFFLINE_BLOCK_RULE_FRIENDLY: &str = "Codex Sandbox Offline - Block Non-Loopback Outbound"; +const OFFLINE_BLOCK_INBOUND_RULE_FRIENDLY: &str = + "Codex Sandbox Offline - Block Non-Loopback Inbound"; const OFFLINE_BLOCK_LOOPBACK_TCP_RULE_FRIENDLY: &str = "Codex Sandbox Offline - Block Loopback TCP (Except Proxy)"; const OFFLINE_BLOCK_LOOPBACK_UDP_RULE_FRIENDLY: &str = "Codex Sandbox Offline - Block Loopback UDP"; @@ -45,6 +50,7 @@ const NON_LOOPBACK_REMOTE_ADDRESSES: &str = "0.0.0.0-126.255.255.255,128.0.0.0-2 struct BlockRuleSpec<'a> { internal_name: &'a str, friendly_desc: &'a str, + direction: NET_FW_RULE_DIRECTION, protocol: i32, local_user_spec: &'a str, offline_sid: &'a str, @@ -99,6 +105,7 @@ pub fn ensure_offline_proxy_allowlist( &BlockRuleSpec { internal_name: OFFLINE_BLOCK_LOOPBACK_UDP_RULE_NAME, friendly_desc: OFFLINE_BLOCK_LOOPBACK_UDP_RULE_FRIENDLY, + direction: NET_FW_RULE_DIR_OUT, protocol: NET_FW_IP_PROTOCOL_UDP.0, local_user_spec: &local_user_spec, offline_sid, @@ -115,6 +122,7 @@ pub fn ensure_offline_proxy_allowlist( &BlockRuleSpec { internal_name: OFFLINE_BLOCK_LOOPBACK_TCP_RULE_NAME, friendly_desc: OFFLINE_BLOCK_LOOPBACK_TCP_RULE_FRIENDLY, + direction: NET_FW_RULE_DIR_OUT, protocol: NET_FW_IP_PROTOCOL_TCP.0, local_user_spec: &local_user_spec, offline_sid, @@ -134,6 +142,7 @@ pub fn ensure_offline_proxy_allowlist( &BlockRuleSpec { internal_name: OFFLINE_BLOCK_LOOPBACK_TCP_RULE_NAME, friendly_desc: OFFLINE_BLOCK_LOOPBACK_TCP_RULE_FRIENDLY, + direction: NET_FW_RULE_DIR_OUT, protocol: NET_FW_IP_PROTOCOL_TCP.0, local_user_spec: &local_user_spec, offline_sid, @@ -153,7 +162,7 @@ pub fn ensure_offline_proxy_allowlist( result } -pub fn ensure_offline_outbound_block(offline_sid: &str, log: &mut dyn Write) -> Result<()> { +pub fn ensure_offline_network_blocks(offline_sid: &str, log: &mut dyn Write) -> Result<()> { let local_user_spec = format!("O:LSD:(A;;CC;;;{offline_sid})"); let hr = unsafe { CoInitializeEx(None, COINIT_APARTMENTTHREADED) }; @@ -187,6 +196,21 @@ pub fn ensure_offline_outbound_block(offline_sid: &str, log: &mut dyn Write) -> &BlockRuleSpec { internal_name: OFFLINE_BLOCK_RULE_NAME, friendly_desc: OFFLINE_BLOCK_RULE_FRIENDLY, + direction: NET_FW_RULE_DIR_OUT, + protocol: NET_FW_IP_PROTOCOL_ANY.0, + local_user_spec: &local_user_spec, + offline_sid, + remote_addresses: Some(NON_LOOPBACK_REMOTE_ADDRESSES), + remote_ports: None, + }, + log, + )?; + ensure_block_rule( + &rules, + &BlockRuleSpec { + internal_name: OFFLINE_BLOCK_INBOUND_RULE_NAME, + friendly_desc: OFFLINE_BLOCK_INBOUND_RULE_FRIENDLY, + direction: NET_FW_RULE_DIR_IN, protocol: NET_FW_IP_PROTOCOL_ANY.0, local_user_spec: &local_user_spec, offline_sid, @@ -335,7 +359,7 @@ fn configure_rule(rule: &INetFwRule3, spec: &BlockRuleSpec<'_>) -> Result<()> { format!("SetDescription failed: {err:?}"), )) })?; - rule.SetDirection(NET_FW_RULE_DIR_OUT).map_err(|err| { + rule.SetDirection(spec.direction).map_err(|err| { anyhow::Error::new(SetupFailure::new( SetupErrorCode::HelperFirewallRuleCreateOrAddFailed, format!("SetDirection failed: {err:?}"), @@ -519,6 +543,7 @@ mod tests { BlockRuleSpec { internal_name: OFFLINE_BLOCK_LOOPBACK_UDP_RULE_NAME, friendly_desc: OFFLINE_BLOCK_LOOPBACK_UDP_RULE_FRIENDLY, + direction: NET_FW_RULE_DIR_OUT, protocol: NET_FW_IP_PROTOCOL_UDP.0, local_user_spec, offline_sid, @@ -528,6 +553,7 @@ mod tests { BlockRuleSpec { internal_name: OFFLINE_BLOCK_LOOPBACK_TCP_RULE_NAME, friendly_desc: OFFLINE_BLOCK_LOOPBACK_TCP_RULE_FRIENDLY, + direction: NET_FW_RULE_DIR_OUT, protocol: NET_FW_IP_PROTOCOL_TCP.0, local_user_spec, offline_sid, @@ -537,6 +563,7 @@ mod tests { BlockRuleSpec { internal_name: OFFLINE_BLOCK_RULE_NAME, friendly_desc: OFFLINE_BLOCK_RULE_FRIENDLY, + direction: NET_FW_RULE_DIR_OUT, protocol: NET_FW_IP_PROTOCOL_ANY.0, local_user_spec, offline_sid, diff --git a/codex-rs/windows-sandbox-rs/src/uninstall_windows/firewall.rs b/codex-rs/windows-sandbox-rs/src/uninstall_windows/firewall.rs index 876405b58e..0e5c98d376 100644 --- a/codex-rs/windows-sandbox-rs/src/uninstall_windows/firewall.rs +++ b/codex-rs/windows-sandbox-rs/src/uninstall_windows/firewall.rs @@ -18,6 +18,7 @@ pub(super) fn cleanup_firewall_rules() -> Result<()> { let mut errors = Vec::new(); for name in [ "codex_sandbox_offline_block_outbound", + "codex_sandbox_offline_block_inbound", "codex_sandbox_offline_block_loopback_tcp", "codex_sandbox_offline_block_loopback_udp", "codex_sandbox_offline_allow_loopback_proxy",