Files
codex/codex-rs/sandboxing/src/linux_tests.rs
T
viyatb-oai bcff988bd1 Resolve Linux sandbox mode once before launching the helper (#62215)
## Why

Managed proxy routes require bubblewrap's network namespace even when
`features.use_legacy_landlock` is enabled. WSL1 compatibility checks and
helper arguments need to agree on the effective mode.

## What changed

Resolve bubblewrap or legacy Landlock once and use that mode for both the
WSL1 check and the `codex-linux-sandbox` arguments. Use Linux sandbox
naming for the shared launcher and CLI code, and preserve the published
analytics category for a missing helper executable.

## Testing

Unit tests exercise the production argument builder, including managed
network context serialization when legacy Landlock is requested. They
also cover which sandbox configurations WSL1 accepts and rejects.

GitOrigin-RevId: 619ab9d3eef965de6bc6cb13ff8a33132018abcf
2026-11-18 07:23:25 +00:00

137 lines
3.9 KiB
Rust

#![cfg(test)]
use super::*;
use pretty_assertions::assert_eq;
#[test]
fn legacy_landlock_flag_is_included_when_requested() {
let command = vec!["/bin/true".to_string()];
let command_cwd = Path::new("/tmp/link");
let cwd = Path::new("/tmp");
let permission_profile = PermissionProfile::read_only();
let default_bwrap = create_linux_sandbox_command_args_for_permission_profile(
command.clone(),
command_cwd,
&permission_profile,
cwd,
LinuxSandboxMode::resolve(
/*use_legacy_landlock*/ false, /*managed_network*/ None,
),
);
assert_eq!(
default_bwrap.contains(&"--use-legacy-landlock".to_string()),
false
);
let legacy_landlock = create_linux_sandbox_command_args_for_permission_profile(
command,
command_cwd,
&permission_profile,
cwd,
LinuxSandboxMode::resolve(
/*use_legacy_landlock*/ true, /*managed_network*/ None,
),
);
assert_eq!(
legacy_landlock.contains(&"--use-legacy-landlock".to_string()),
true
);
}
#[test]
fn managed_context_takes_precedence_over_legacy_landlock() {
let command = vec!["/bin/true".to_string()];
let command_cwd = Path::new("/tmp/link");
let cwd = Path::new("/tmp");
let permission_profile = PermissionProfile::read_only();
let route = ManagedNetworkSandboxContext {
loopback_ports: vec![8123],
..Default::default()
};
let args = create_linux_sandbox_command_args_for_permission_profile(
command,
command_cwd,
&permission_profile,
cwd,
LinuxSandboxMode::resolve(/*use_legacy_landlock*/ true, Some(&route)),
);
let payload = args
.windows(2)
.find(|pair| pair[0] == "--managed-network")
.expect("managed network argument");
assert_eq!(
serde_json::from_str::<ManagedNetworkSandboxContext>(&payload[1]).expect("network context"),
route
);
assert_eq!(args.contains(&"--use-legacy-landlock".to_string()), false);
}
#[test]
fn permission_profile_flag_is_included() {
let command = vec!["/bin/true".to_string()];
let command_cwd = Path::new("/tmp/link");
let cwd = Path::new("/tmp");
let permission_profile = PermissionProfile::read_only();
let args = create_linux_sandbox_command_args_for_permission_profile(
command,
command_cwd,
&permission_profile,
cwd,
LinuxSandboxMode::LegacyLandlock,
);
assert_eq!(
args.windows(2)
.any(|window| { window[0] == "--permission-profile" && !window[1].is_empty() }),
true
);
assert_eq!(
args.windows(2)
.any(|window| window[0] == "--command-cwd" && window[1] == "/tmp/link"),
true
);
}
#[cfg(target_os = "linux")]
#[test]
fn wsl1_rejects_linux_bubblewrap_path() {
let read_only = PermissionProfile::read_only();
let no_proxy = LinuxSandboxMode::Bubblewrap {
managed_network: None,
};
assert!(matches!(
no_proxy.ensure_supported_on_wsl1(&read_only),
Err(SandboxTransformError::Wsl1UnsupportedForBubblewrap)
));
let route = ManagedNetworkSandboxContext::default();
for use_legacy_landlock in [false, true] {
let mode = LinuxSandboxMode::resolve(use_legacy_landlock, Some(&route));
assert!(matches!(
mode.ensure_supported_on_wsl1(&PermissionProfile::Disabled),
Err(SandboxTransformError::Wsl1UnsupportedForBubblewrap)
));
}
}
#[cfg(target_os = "linux")]
#[test]
fn wsl1_allows_non_bubblewrap_linux_paths() {
let no_proxy = LinuxSandboxMode::Bubblewrap {
managed_network: None,
};
assert!(
no_proxy
.ensure_supported_on_wsl1(&PermissionProfile::Disabled)
.is_ok()
);
assert!(
LinuxSandboxMode::LegacyLandlock
.ensure_supported_on_wsl1(&PermissionProfile::read_only())
.is_ok()
);
}