mirror of
https://github.com/openai/codex.git
synced 2026-11-15 21:50:46 +00:00
Apply shared network policy throughout embedded Codex startup (#47411)
## Why Application network restrictions need to cover clients created by the TUI and `codex exec` before embedded app-server startup, and remain effective when configuration is rebuilt. ## What changed - Introduce `EmbeddedNetworkPolicy` to share live policy between caller-owned transports and the embedded app-server. - Apply loaded requirements before creating telemetry, background HTTP clients, and executor connections. Limit authentication and cloud configuration bootstrap requests to exact endpoints under local policy. - Preserve the shared policy across TUI session changes, permission-profile rebuilds, and worktree cloud loaders so existing clients observe policy reloads and failures. - Document application network policy behavior and transport coverage. ## Testing Add regression coverage for policy activation, reload failure and recovery, retained policy across TUI rebuilds, rejected OAuth and cloud-policy redirects, and workspace network restrictions with `CODEX_API_KEY`. Also cover retaining the caller-selected project during embedded startup. GitOrigin-RevId: 629009f087b8cb21fc34a1ee7ea6fe95d9e0f2b5
This commit is contained in:
@@ -0,0 +1,85 @@
|
||||
//! Embedded startup must keep the project selected by the caller.
|
||||
|
||||
use super::configure;
|
||||
use crate::config_manager::ConfigManager;
|
||||
use codex_arg0::Arg0DispatchPaths;
|
||||
use codex_config::CloudConfigBundleLoader;
|
||||
use codex_config::LoaderOverrides;
|
||||
use codex_config::NoopThreadConfigLoader;
|
||||
use codex_core::config::ConfigBuilder;
|
||||
use codex_core::config::ConfigOverrides;
|
||||
use codex_core::config::set_project_trust_level;
|
||||
use codex_protocol::config_types::TrustLevel;
|
||||
use pretty_assertions::assert_eq;
|
||||
use std::sync::Arc;
|
||||
|
||||
#[tokio::test]
|
||||
async fn startup_reloads_the_callers_selected_project() -> anyhow::Result<()> {
|
||||
let temp = tempfile::tempdir()?;
|
||||
let root = temp.path().canonicalize()?;
|
||||
let home = root.join("home");
|
||||
let selected = root.join("selected");
|
||||
std::fs::create_dir(&home)?;
|
||||
std::fs::create_dir_all(selected.join(".codex"))?;
|
||||
std::fs::create_dir(selected.join(".git"))?;
|
||||
set_project_trust_level(&home, &selected, TrustLevel::Trusted)?;
|
||||
let selected_url = "https://selected.example/backend-api/";
|
||||
let user_config = home.join("config.toml");
|
||||
let trust = std::fs::read_to_string(&user_config)?;
|
||||
std::fs::write(
|
||||
user_config,
|
||||
format!("chatgpt_base_url = '{selected_url}'\n{trust}"),
|
||||
)?;
|
||||
let project_config = selected.join(".codex/config.toml");
|
||||
std::fs::write(&project_config, "model = 'selected-model'")?;
|
||||
let loader_overrides = LoaderOverrides::without_managed_config_for_tests();
|
||||
let mut config = Arc::new(
|
||||
ConfigBuilder::default()
|
||||
.codex_home(home.clone())
|
||||
.harness_overrides(ConfigOverrides {
|
||||
cwd: Some(selected.clone()),
|
||||
..Default::default()
|
||||
})
|
||||
.loader_overrides(loader_overrides.clone())
|
||||
.build()
|
||||
.await?,
|
||||
);
|
||||
assert_eq!(config.chatgpt_base_url, selected_url);
|
||||
assert_eq!(config.model.as_deref(), Some("selected-model"));
|
||||
let manager = ConfigManager::new(
|
||||
home,
|
||||
Vec::new(),
|
||||
loader_overrides,
|
||||
/*strict_config*/ true,
|
||||
CloudConfigBundleLoader::default(),
|
||||
Arg0DispatchPaths::default(),
|
||||
Arc::new(NoopThreadConfigLoader),
|
||||
);
|
||||
let _auth = configure(
|
||||
&manager,
|
||||
&mut config,
|
||||
/*enable_codex_api_key_env*/ false,
|
||||
)
|
||||
.await?;
|
||||
assert_eq!(
|
||||
config.cwd.as_path().canonicalize()?,
|
||||
selected.canonicalize()?
|
||||
);
|
||||
assert_eq!(config.chatgpt_base_url, selected_url);
|
||||
|
||||
std::fs::write(&project_config, "[malformed")?;
|
||||
let error = configure(
|
||||
&manager,
|
||||
&mut config,
|
||||
/*enable_codex_api_key_env*/ false,
|
||||
)
|
||||
.await
|
||||
.err()
|
||||
.ok_or_else(|| anyhow::anyhow!("embedded startup ignored the selected project"))?;
|
||||
assert!(
|
||||
error
|
||||
.to_string()
|
||||
.contains("Error parsing project config file")
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
Reference in New Issue
Block a user