From 292ce9580ca4a16dba18eda6a68f097560880b33 Mon Sep 17 00:00:00 2001 From: David Wiesen Date: Fri, 17 Apr 2026 18:21:19 -0700 Subject: [PATCH] fix(windows-sandbox): skip remote ports on protocol any --- codex-rs/windows-sandbox-rs/src/firewall.rs | 34 ++++++++++++++++----- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/codex-rs/windows-sandbox-rs/src/firewall.rs b/codex-rs/windows-sandbox-rs/src/firewall.rs index c0caa165fc..393da85025 100644 --- a/codex-rs/windows-sandbox-rs/src/firewall.rs +++ b/codex-rs/windows-sandbox-rs/src/firewall.rs @@ -317,14 +317,16 @@ fn configure_rule(rule: &INetFwRule3, spec: &BlockRuleSpec<'_>) -> Result<()> { format!("SetRemoteAddresses failed: {err:?}"), )) })?; - let remote_ports = spec.remote_ports.unwrap_or("*"); - rule.SetRemotePorts(&BSTR::from(remote_ports)) - .map_err(|err| { - anyhow::Error::new(SetupFailure::new( - SetupErrorCode::HelperFirewallRuleCreateOrAddFailed, - format!("SetRemotePorts failed: {err:?}"), - )) - })?; + if protocol_supports_remote_ports(spec.protocol) { + let remote_ports = spec.remote_ports.unwrap_or("*"); + rule.SetRemotePorts(&BSTR::from(remote_ports)) + .map_err(|err| { + anyhow::Error::new(SetupFailure::new( + SetupErrorCode::HelperFirewallRuleCreateOrAddFailed, + format!("SetRemotePorts failed: {err:?}"), + )) + })?; + } rule.SetLocalUserAuthorizedList(&BSTR::from(spec.local_user_spec)) .map_err(|err| { anyhow::Error::new(SetupFailure::new( @@ -354,6 +356,10 @@ fn configure_rule(rule: &INetFwRule3, spec: &BlockRuleSpec<'_>) -> Result<()> { Ok(()) } +fn protocol_supports_remote_ports(protocol: i32) -> bool { + protocol == NET_FW_IP_PROTOCOL_TCP.0 || protocol == NET_FW_IP_PROTOCOL_UDP.0 +} + fn blocked_loopback_tcp_remote_ports(proxy_ports: &[u16]) -> Option { let mut allowed_ports = proxy_ports .iter() @@ -400,3 +406,15 @@ fn log_line(log: &mut File, msg: &str) -> Result<()> { writeln!(log, "[{ts}] {msg}")?; Ok(()) } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn remote_ports_are_only_configured_for_tcp_and_udp_rules() { + assert!(protocol_supports_remote_ports(NET_FW_IP_PROTOCOL_TCP.0)); + assert!(protocol_supports_remote_ports(NET_FW_IP_PROTOCOL_UDP.0)); + assert!(!protocol_supports_remote_ports(NET_FW_IP_PROTOCOL_ANY.0)); + } +}