mirror of
https://github.com/openai/codex.git
synced 2026-11-25 23:30:49 +00:00
Reject network proxy with unelevated Windows sandbox
This commit is contained in:
@@ -320,6 +320,15 @@ impl ConfigManager {
|
||||
format!("Invalid configuration: {err}"),
|
||||
)
|
||||
})?;
|
||||
codex_core::config::validate_materialized_config_from_layer_stack(
|
||||
self.codex_home().to_path_buf(),
|
||||
updated_layers.clone(),
|
||||
codex_core::config::ConfigOverrides::default(),
|
||||
)
|
||||
.await
|
||||
.map_err(|err| {
|
||||
ConfigManagerError::write(ConfigWriteErrorCode::ConfigValidationError, err.to_string())
|
||||
})?;
|
||||
|
||||
if !config_edits.is_empty() {
|
||||
ConfigEditsBuilder::for_config_path(provided_path.as_path())
|
||||
|
||||
@@ -742,6 +742,57 @@ personality = true
|
||||
);
|
||||
}
|
||||
|
||||
#[cfg(target_os = "windows")]
|
||||
#[tokio::test]
|
||||
async fn batch_write_rejects_unelevated_windows_sandbox_with_network_proxy() -> Result<()> {
|
||||
let tmp = tempdir().expect("tempdir");
|
||||
let config_path = tmp.path().join(CONFIG_TOML_FILE);
|
||||
let original_config = r#"
|
||||
default_permissions = "networked"
|
||||
|
||||
[features]
|
||||
network_proxy = true
|
||||
|
||||
[windows]
|
||||
sandbox = "elevated"
|
||||
|
||||
[permissions.networked.filesystem]
|
||||
":root" = "read"
|
||||
|
||||
[permissions.networked.network]
|
||||
enabled = true
|
||||
"#;
|
||||
std::fs::write(&config_path, original_config)?;
|
||||
|
||||
let service = ConfigManager::without_managed_config_for_tests(tmp.path().to_path_buf());
|
||||
let error = service
|
||||
.batch_write(ConfigBatchWriteParams {
|
||||
edits: vec![codex_app_server_protocol::ConfigEdit {
|
||||
key_path: "windows.sandbox".to_string(),
|
||||
value: serde_json::json!("unelevated"),
|
||||
merge_strategy: MergeStrategy::Replace,
|
||||
}],
|
||||
file_path: Some(config_path.display().to_string()),
|
||||
expected_version: None,
|
||||
reload_user_config: false,
|
||||
})
|
||||
.await
|
||||
.expect_err("unelevated Windows sandbox should be rejected with network proxy");
|
||||
|
||||
assert_eq!(
|
||||
error.write_error_code(),
|
||||
Some(ConfigWriteErrorCode::ConfigValidationError)
|
||||
);
|
||||
assert!(
|
||||
error
|
||||
.to_string()
|
||||
.contains("The network proxy requires the elevated Windows sandbox backend"),
|
||||
"{error}"
|
||||
);
|
||||
assert_eq!(std::fs::read_to_string(&config_path)?, original_config);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn read_reports_managed_overrides_user_and_session_flags() {
|
||||
let tmp = tempdir().expect("tempdir");
|
||||
|
||||
@@ -290,6 +290,10 @@ fn config_error_location(err: &std::io::Error) -> Option<(String, AppTextRange)>
|
||||
})
|
||||
}
|
||||
|
||||
fn is_unrecoverable_config_load_error(err: &std::io::Error) -> bool {
|
||||
codex_core::config::is_windows_sandbox_network_proxy_incompatible_error(err)
|
||||
}
|
||||
|
||||
fn exec_policy_warning_location(err: &ExecPolicyError) -> (Option<String>, Option<AppTextRange>) {
|
||||
match err {
|
||||
ExecPolicyError::ParsePolicy { path, source } => {
|
||||
@@ -499,7 +503,7 @@ pub async fn run_main_with_transport_options(
|
||||
{
|
||||
Ok(config) => (config, true),
|
||||
Err(err) => {
|
||||
if strict_config {
|
||||
if strict_config || is_unrecoverable_config_load_error(&err) {
|
||||
return Err(err);
|
||||
}
|
||||
|
||||
|
||||
@@ -63,6 +63,15 @@ impl WindowsSandboxRequestProcessor {
|
||||
config.config_layer_stack.requirements(),
|
||||
params.mode,
|
||||
)?;
|
||||
let target_windows_sandbox_level = match setup_mode {
|
||||
CoreWindowsSandboxSetupMode::Elevated => WindowsSandboxLevel::Elevated,
|
||||
CoreWindowsSandboxSetupMode::Unelevated => WindowsSandboxLevel::RestrictedToken,
|
||||
};
|
||||
codex_core::config::validate_windows_sandbox_network_proxy_compatibility(
|
||||
target_windows_sandbox_level,
|
||||
config.permissions.network.as_ref(),
|
||||
)
|
||||
.map_err(|err| invalid_request(err.to_string()))?;
|
||||
|
||||
self.outgoing
|
||||
.send_response(
|
||||
|
||||
@@ -31,3 +31,45 @@ foo = "bar"
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(target_os = "windows")]
|
||||
#[test]
|
||||
fn non_strict_config_rejects_unelevated_windows_sandbox_with_network_proxy() -> Result<()> {
|
||||
let codex_home = TempDir::new()?;
|
||||
std::fs::write(
|
||||
codex_home.path().join("config.toml"),
|
||||
r#"
|
||||
default_permissions = "networked"
|
||||
|
||||
[features]
|
||||
network_proxy = true
|
||||
|
||||
[windows]
|
||||
sandbox = "unelevated"
|
||||
|
||||
[permissions.networked.filesystem]
|
||||
":minimal" = "read"
|
||||
|
||||
[permissions.networked.network]
|
||||
enabled = true
|
||||
"#,
|
||||
)?;
|
||||
|
||||
let output = Command::new(codex_utils_cargo_bin::cargo_bin("codex-app-server")?)
|
||||
.env("CODEX_HOME", codex_home.path())
|
||||
.env(
|
||||
"CODEX_APP_SERVER_MANAGED_CONFIG_PATH",
|
||||
codex_home.path().join("managed_config.toml"),
|
||||
)
|
||||
.args(["--listen", "off"])
|
||||
.output()?;
|
||||
|
||||
assert!(!output.status.success());
|
||||
let stderr = String::from_utf8(output.stderr)?;
|
||||
assert!(
|
||||
stderr.contains("The network proxy requires the elevated Windows sandbox backend"),
|
||||
"expected Windows sandbox/network proxy config error in stderr, got: {stderr}"
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -1391,6 +1391,191 @@ enabled = true
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn windows_sandbox_network_proxy_compatibility_rejects_network_proxy_with_unelevated_windows() {
|
||||
let err = validate_windows_sandbox_network_proxy_compatibility_for_platform(
|
||||
/*is_windows*/ true,
|
||||
WindowsSandboxLevel::RestrictedToken,
|
||||
/*network_proxy_configured*/ true,
|
||||
)
|
||||
.expect_err("unelevated Windows sandbox should reject configured network proxy");
|
||||
|
||||
assert_eq!(err.kind(), std::io::ErrorKind::InvalidInput);
|
||||
assert_eq!(
|
||||
err.to_string(),
|
||||
WINDOWS_SANDBOX_NETWORK_PROXY_INCOMPATIBLE_ERROR
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn windows_sandbox_network_proxy_compatibility_allows_compatible_combinations() {
|
||||
for (windows_sandbox_level, network_proxy_configured) in [
|
||||
(WindowsSandboxLevel::Disabled, true),
|
||||
(WindowsSandboxLevel::Elevated, true),
|
||||
(WindowsSandboxLevel::RestrictedToken, false),
|
||||
] {
|
||||
validate_windows_sandbox_network_proxy_compatibility_for_platform(
|
||||
/*is_windows*/ true,
|
||||
windows_sandbox_level,
|
||||
network_proxy_configured,
|
||||
)
|
||||
.expect("compatible Windows sandbox and network proxy settings");
|
||||
}
|
||||
validate_windows_sandbox_network_proxy_compatibility_for_platform(
|
||||
/*is_windows*/ false,
|
||||
WindowsSandboxLevel::RestrictedToken,
|
||||
/*network_proxy_configured*/ true,
|
||||
)
|
||||
.expect("Windows sandbox compatibility should only be enforced on Windows");
|
||||
}
|
||||
|
||||
#[cfg(target_os = "windows")]
|
||||
#[tokio::test]
|
||||
async fn disabled_experimental_network_requirements_reject_unelevated_windows_sandbox()
|
||||
-> std::io::Result<()> {
|
||||
let codex_home = TempDir::new()?;
|
||||
std::fs::write(
|
||||
codex_home.path().join(CONFIG_TOML_FILE),
|
||||
r#"
|
||||
[windows]
|
||||
sandbox = "unelevated"
|
||||
"#,
|
||||
)?;
|
||||
|
||||
let err = ConfigBuilder::without_managed_config_for_tests()
|
||||
.codex_home(codex_home.path().to_path_buf())
|
||||
.fallback_cwd(Some(codex_home.path().to_path_buf()))
|
||||
.cloud_config_bundle(
|
||||
CloudConfigBundleFixture::loader_with_enterprise_requirement(
|
||||
r#"
|
||||
[experimental_network]
|
||||
enabled = false
|
||||
"#,
|
||||
),
|
||||
)
|
||||
.build()
|
||||
.await
|
||||
.expect_err("disabled experimental_network requirements still configure network proxy");
|
||||
|
||||
assert_eq!(err.kind(), std::io::ErrorKind::InvalidInput);
|
||||
assert_eq!(
|
||||
err.to_string(),
|
||||
WINDOWS_SANDBOX_NETWORK_PROXY_INCOMPATIBLE_ERROR
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(target_os = "windows")]
|
||||
#[tokio::test]
|
||||
async fn network_proxy_feature_rejects_unelevated_windows_sandbox() -> std::io::Result<()> {
|
||||
let codex_home = TempDir::new()?;
|
||||
let cwd = TempDir::new()?;
|
||||
std::fs::write(cwd.path().join(".git"), "gitdir: nowhere")?;
|
||||
|
||||
let err = Config::load_from_base_config_with_overrides(
|
||||
ConfigToml {
|
||||
features: Some(toml::from_str("network_proxy = true").expect("valid features")),
|
||||
default_permissions: Some("dev".to_string()),
|
||||
permissions: Some(PermissionsToml {
|
||||
entries: BTreeMap::from([(
|
||||
"dev".to_string(),
|
||||
PermissionProfileToml {
|
||||
description: None,
|
||||
extends: None,
|
||||
workspace_roots: None,
|
||||
filesystem: Some(FilesystemPermissionsToml {
|
||||
glob_scan_max_depth: None,
|
||||
entries: BTreeMap::from([(
|
||||
":minimal".to_string(),
|
||||
FilesystemPermissionToml::Access(FileSystemAccessMode::Read),
|
||||
)]),
|
||||
}),
|
||||
network: Some(NetworkToml {
|
||||
enabled: Some(true),
|
||||
..Default::default()
|
||||
}),
|
||||
},
|
||||
)]),
|
||||
}),
|
||||
windows: Some(WindowsToml {
|
||||
sandbox: Some(WindowsSandboxModeToml::Unelevated),
|
||||
sandbox_private_desktop: None,
|
||||
}),
|
||||
..Default::default()
|
||||
},
|
||||
ConfigOverrides {
|
||||
cwd: Some(cwd.path().to_path_buf()),
|
||||
..Default::default()
|
||||
},
|
||||
codex_home.abs(),
|
||||
)
|
||||
.await
|
||||
.expect_err("network_proxy feature should reject unelevated Windows sandbox");
|
||||
|
||||
assert_eq!(err.kind(), std::io::ErrorKind::InvalidInput);
|
||||
assert_eq!(
|
||||
err.to_string(),
|
||||
WINDOWS_SANDBOX_NETWORK_PROXY_INCOMPATIBLE_ERROR
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(target_os = "windows")]
|
||||
#[tokio::test]
|
||||
async fn active_profile_network_proxy_rejects_unelevated_windows_sandbox() -> std::io::Result<()> {
|
||||
let codex_home = TempDir::new()?;
|
||||
let cwd = TempDir::new()?;
|
||||
std::fs::write(cwd.path().join(".git"), "gitdir: nowhere")?;
|
||||
|
||||
std::fs::write(
|
||||
codex_home.path().join(CONFIG_TOML_FILE),
|
||||
r#"
|
||||
default_permissions = ":read-only"
|
||||
|
||||
[features]
|
||||
network_proxy = true
|
||||
|
||||
[windows]
|
||||
sandbox = "unelevated"
|
||||
|
||||
[permissions.dev.filesystem]
|
||||
":minimal" = "read"
|
||||
|
||||
[permissions.dev.network]
|
||||
enabled = true
|
||||
"#,
|
||||
)?;
|
||||
let config = ConfigBuilder::without_managed_config_for_tests()
|
||||
.codex_home(codex_home.path().to_path_buf())
|
||||
.harness_overrides(ConfigOverrides {
|
||||
cwd: Some(cwd.path().to_path_buf()),
|
||||
..Default::default()
|
||||
})
|
||||
.build()
|
||||
.await?;
|
||||
|
||||
assert_eq!(config.permissions.network, None);
|
||||
let err = config
|
||||
.network_proxy_spec_for_active_permission_profile(
|
||||
&ActivePermissionProfile::new("dev"),
|
||||
&PermissionProfile::Managed {
|
||||
file_system: ManagedFileSystemPermissions::Restricted {
|
||||
entries: Vec::new(),
|
||||
glob_scan_max_depth: None,
|
||||
},
|
||||
network: NetworkSandboxPolicy::Enabled,
|
||||
},
|
||||
)
|
||||
.expect_err("inactive network proxy profile should be rejected with unelevated sandbox");
|
||||
|
||||
assert_eq!(err.kind(), std::io::ErrorKind::InvalidInput);
|
||||
assert_eq!(
|
||||
err.to_string(),
|
||||
WINDOWS_SANDBOX_NETWORK_PROXY_INCOMPATIBLE_ERROR
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn network_proxy_feature_uses_profile_network_proxy_settings() -> std::io::Result<()> {
|
||||
let codex_home = TempDir::new()?;
|
||||
|
||||
@@ -587,6 +587,41 @@ fn build_network_proxy_spec(
|
||||
})
|
||||
}
|
||||
|
||||
const WINDOWS_SANDBOX_NETWORK_PROXY_INCOMPATIBLE_ERROR: &str = "The network proxy requires the elevated Windows sandbox backend, but Codex is using the unelevated backend. Set windows.sandbox = \"elevated\" or disable the network proxy. If these settings are managed by your organization, contact your administrator.";
|
||||
|
||||
pub fn validate_windows_sandbox_network_proxy_compatibility(
|
||||
windows_sandbox_level: WindowsSandboxLevel,
|
||||
network_proxy: Option<&NetworkProxySpec>,
|
||||
) -> std::io::Result<()> {
|
||||
validate_windows_sandbox_network_proxy_compatibility_for_platform(
|
||||
cfg!(target_os = "windows"),
|
||||
windows_sandbox_level,
|
||||
network_proxy.is_some(),
|
||||
)
|
||||
}
|
||||
|
||||
pub fn is_windows_sandbox_network_proxy_incompatible_error(err: &std::io::Error) -> bool {
|
||||
err.kind() == std::io::ErrorKind::InvalidInput
|
||||
&& err.to_string() == WINDOWS_SANDBOX_NETWORK_PROXY_INCOMPATIBLE_ERROR
|
||||
}
|
||||
|
||||
fn validate_windows_sandbox_network_proxy_compatibility_for_platform(
|
||||
is_windows: bool,
|
||||
windows_sandbox_level: WindowsSandboxLevel,
|
||||
network_proxy_configured: bool,
|
||||
) -> std::io::Result<()> {
|
||||
if is_windows
|
||||
&& network_proxy_configured
|
||||
&& windows_sandbox_level == WindowsSandboxLevel::RestrictedToken
|
||||
{
|
||||
return Err(std::io::Error::new(
|
||||
std::io::ErrorKind::InvalidInput,
|
||||
WINDOWS_SANDBOX_NETWORK_PROXY_INCOMPATIBLE_ERROR,
|
||||
));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Configured thread persistence backend.
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Default)]
|
||||
pub enum ThreadStoreConfig {
|
||||
@@ -1320,6 +1355,27 @@ impl ConfigBuilder {
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn validate_materialized_config_from_layer_stack(
|
||||
codex_home: PathBuf,
|
||||
config_layer_stack: ConfigLayerStack,
|
||||
overrides: ConfigOverrides,
|
||||
) -> std::io::Result<()> {
|
||||
let cfg: ConfigToml = config_layer_stack
|
||||
.effective_config()
|
||||
.try_into()
|
||||
.map_err(|err| std::io::Error::new(std::io::ErrorKind::InvalidData, err))?;
|
||||
let codex_home = AbsolutePathBuf::from_absolute_path_checked(codex_home)?;
|
||||
Config::load_config_with_layer_stack(
|
||||
LOCAL_FS.as_ref(),
|
||||
cfg,
|
||||
overrides,
|
||||
codex_home,
|
||||
config_layer_stack,
|
||||
)
|
||||
.await
|
||||
.map(|_| ())
|
||||
}
|
||||
|
||||
impl Config {
|
||||
pub(crate) fn multi_agent_version_from_features(&self) -> MultiAgentVersion {
|
||||
if self.features.enabled(Feature::MultiAgentV2) {
|
||||
@@ -3723,6 +3779,10 @@ impl Config {
|
||||
.unwrap_or_default(),
|
||||
otel,
|
||||
};
|
||||
validate_windows_sandbox_network_proxy_compatibility(
|
||||
WindowsSandboxLevel::from_config(&config),
|
||||
config.permissions.network.as_ref(),
|
||||
)?;
|
||||
Ok(config)
|
||||
})
|
||||
.await
|
||||
@@ -3839,11 +3899,16 @@ impl Config {
|
||||
NetworkProxyConfig::default()
|
||||
};
|
||||
|
||||
build_network_proxy_spec(
|
||||
let network_proxy = build_network_proxy_spec(
|
||||
configured_network_proxy_config,
|
||||
self.config_layer_stack.requirements().network.clone(),
|
||||
permission_profile,
|
||||
)
|
||||
)?;
|
||||
validate_windows_sandbox_network_proxy_compatibility(
|
||||
WindowsSandboxLevel::from_config(self),
|
||||
network_proxy.as_ref(),
|
||||
)?;
|
||||
Ok(network_proxy)
|
||||
}
|
||||
|
||||
pub fn bundled_skills_enabled(&self) -> bool {
|
||||
|
||||
Reference in New Issue
Block a user