mirror of
https://github.com/openai/codex.git
synced 2026-12-03 00:41:03 +00:00
Preserve account network policy for ChatGPT backend requests (#47703)
## Why ChatGPT backend requests could bypass the configured network policy by using a default HTTP client. Requests using captured credentials must also retain the corresponding account policy so revocation continues to block them. ## What changed - Use the configured HTTP client factory for ChatGPT GET and POST requests, enforcing managed network policy while retaining cookies. - Capture authentication and network policy together for app reads and plugin reads and installs, preserving that policy across the post-install configuration reload. - Bind GET requests to the current account policy before loading credentials. ## Testing Add regression tests for revoked account policies, blocked connector destinations, and network policy enforcement after configuration reload with a cached app client. GitOrigin-RevId: 5298842eec9664af49df70e89624653d1452fb33
This commit is contained in:
@@ -26,8 +26,14 @@ impl AppsRequestProcessor {
|
||||
.into_iter()
|
||||
.filter(|app_id| seen_app_ids.insert(app_id.clone()))
|
||||
.collect::<Vec<_>>();
|
||||
let config = self.load_apps_config(thread_id.as_deref()).await?;
|
||||
let auth = self.auth_manager.auth().await;
|
||||
let mut config = self.load_apps_config(thread_id.as_deref()).await?;
|
||||
let auth = match self.auth_manager.auth_with_http_client_factory().await {
|
||||
Some((auth, factory)) => {
|
||||
config.application_network_policy = factory.network_policy().clone();
|
||||
Some(auth)
|
||||
}
|
||||
None => None,
|
||||
};
|
||||
if !config
|
||||
.features
|
||||
.apps_enabled_for_auth(auth.as_ref().is_some_and(CodexAuth::uses_codex_backend))
|
||||
|
||||
@@ -1021,9 +1021,15 @@ impl PluginRequestProcessor {
|
||||
marketplace_path.as_path().parent().map(Path::to_path_buf)
|
||||
});
|
||||
|
||||
let config = self.load_latest_config(config_cwd).await?;
|
||||
let mut config = self.load_latest_config(config_cwd).await?;
|
||||
let auth = match self.auth_manager.auth_with_http_client_factory().await {
|
||||
Some((auth, factory)) => {
|
||||
config.application_network_policy = factory.network_policy().clone();
|
||||
Some(auth)
|
||||
}
|
||||
None => None,
|
||||
};
|
||||
let plugins_input = config.plugins_config_input();
|
||||
let auth = self.auth_manager.auth().await;
|
||||
|
||||
let plugin = match read_source {
|
||||
Ok(marketplace_path) => {
|
||||
@@ -1483,8 +1489,14 @@ impl PluginRequestProcessor {
|
||||
}
|
||||
};
|
||||
let config_cwd = marketplace_path.as_path().parent().map(Path::to_path_buf);
|
||||
let config = self.load_latest_config(config_cwd.clone()).await?;
|
||||
let auth = self.auth_manager.auth().await;
|
||||
let mut config = self.load_latest_config(config_cwd.clone()).await?;
|
||||
let auth = match self.auth_manager.auth_with_http_client_factory().await {
|
||||
Some((auth, factory)) => {
|
||||
config.application_network_policy = factory.network_policy().clone();
|
||||
Some(auth)
|
||||
}
|
||||
None => None,
|
||||
};
|
||||
|
||||
let plugins_manager = self.thread_manager.plugins_manager();
|
||||
let marketplace_display = marketplace_path.display().to_string();
|
||||
@@ -1509,7 +1521,11 @@ impl PluginRequestProcessor {
|
||||
}
|
||||
};
|
||||
let config = match self.load_latest_config(config_cwd).await {
|
||||
Ok(config) => config,
|
||||
Ok(mut reloaded_config) => {
|
||||
// Keep the policy captured with this installation's auth snapshot.
|
||||
reloaded_config.application_network_policy = config.application_network_policy;
|
||||
reloaded_config
|
||||
}
|
||||
Err(err) => {
|
||||
warn!(
|
||||
"failed to reload config after plugin install, using current config: {err:?}"
|
||||
@@ -1564,8 +1580,14 @@ impl PluginRequestProcessor {
|
||||
remote_plugin_id: String,
|
||||
install_attempt_id: Option<String>,
|
||||
) -> Result<PluginInstallResponse, JSONRPCErrorError> {
|
||||
let config = self.load_latest_config(/*fallback_cwd*/ None).await?;
|
||||
let auth = self.auth_manager.auth().await;
|
||||
let mut config = self.load_latest_config(/*fallback_cwd*/ None).await?;
|
||||
let auth = match self.auth_manager.auth_with_http_client_factory().await {
|
||||
Some((auth, factory)) => {
|
||||
config.application_network_policy = factory.network_policy().clone();
|
||||
Some(auth)
|
||||
}
|
||||
None => None,
|
||||
};
|
||||
let plugins_manager = self.thread_manager.plugins_manager();
|
||||
let installation = plugins_manager
|
||||
.install_remote_plugin(
|
||||
|
||||
@@ -26,7 +26,15 @@ use codex_app_server_protocol::LoginAccountResponse;
|
||||
use codex_app_server_protocol::RequestId;
|
||||
use codex_app_server_protocol::ThreadStartParams;
|
||||
use codex_app_server_protocol::ThreadStartResponse;
|
||||
use codex_chatgpt::connectors;
|
||||
use codex_config::LoaderOverrides;
|
||||
use codex_config::types::AuthCredentialsStoreMode;
|
||||
use codex_core::config::ConfigBuilder;
|
||||
use codex_http_client::DestinationPolicy;
|
||||
use codex_http_client::HttpError;
|
||||
use codex_http_client::NetworkPolicyController;
|
||||
use codex_http_client::NetworkPolicyDenied;
|
||||
use codex_login::AuthManager;
|
||||
use pretty_assertions::assert_eq;
|
||||
use serde_json::Value;
|
||||
use serde_json::json;
|
||||
@@ -85,6 +93,108 @@ fn app_read_deserializes_legacy_tool_summaries() -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn connector_requests_preserve_account_policy() -> Result<()> {
|
||||
let state = BatchServerState::new(json!({ "apps": [] }), "chatgpt-token", "codex");
|
||||
let (server_url, server_handle) = start_batch_server(state.clone()).await?;
|
||||
let home = TempDir::new()?;
|
||||
write_apps_config(home.path(), &server_url, /*apps_mcp_product_sku*/ None)?;
|
||||
write_auth(home.path())?;
|
||||
let mut config = ConfigBuilder::default()
|
||||
.codex_home(home.path().to_path_buf())
|
||||
.fallback_cwd(Some(home.path().to_path_buf()))
|
||||
.loader_overrides(LoaderOverrides::without_managed_config_for_tests())
|
||||
.build()
|
||||
.await?;
|
||||
let controller = NetworkPolicyController::default();
|
||||
let policy = controller.policy();
|
||||
controller.publish(policy.revision(), DestinationPolicy::Unrestricted);
|
||||
config.application_network_policy = policy.clone();
|
||||
let manager =
|
||||
AuthManager::shared_from_config(&config, /*enable_codex_api_key_env*/ false).await?;
|
||||
let (auth, factory) = manager.auth_with_http_client_factory().await.unwrap();
|
||||
config.application_network_policy = factory.network_policy().clone();
|
||||
let app_ids = vec!["alpha".to_string()];
|
||||
connectors::read_connector_metadata(&config, &auth, &app_ids, /*include_tools*/ false).await?;
|
||||
assert_eq!(state.requests().len(), 1);
|
||||
|
||||
policy.invalidate();
|
||||
controller.publish(policy.revision(), DestinationPolicy::Unrestricted);
|
||||
let error = timeout(
|
||||
DEFAULT_TIMEOUT,
|
||||
connectors::read_connector_metadata(&config, &auth, &app_ids, /*include_tools*/ false),
|
||||
)
|
||||
.await?
|
||||
.err()
|
||||
.expect("old credentials must retain their revoked policy");
|
||||
assert!(matches!(
|
||||
error.downcast_ref::<HttpError>(),
|
||||
Some(HttpError::Policy(NetworkPolicyDenied::Revoked))
|
||||
));
|
||||
assert_eq!(state.requests().len(), 1);
|
||||
|
||||
controller.publish(
|
||||
policy.revision(),
|
||||
DestinationPolicy::Restricted {
|
||||
allowed_hosts: Default::default(),
|
||||
},
|
||||
);
|
||||
let error = timeout(
|
||||
DEFAULT_TIMEOUT,
|
||||
connectors::list_all_connectors_with_options(&config, /*force_refetch*/ true, &[]),
|
||||
)
|
||||
.await?
|
||||
.unwrap_err();
|
||||
assert!(matches!(
|
||||
error.downcast_ref::<HttpError>(),
|
||||
Some(HttpError::Policy(NetworkPolicyDenied::Destination))
|
||||
));
|
||||
server_handle.abort();
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn app_read_applies_network_policy_to_cached_client_after_reload() -> Result<()> {
|
||||
let state = BatchServerState::new(json!({ "apps": [] }), "chatgpt-token", "codex");
|
||||
let (server_url, server_handle) = start_batch_server(state.clone()).await?;
|
||||
let home = TempDir::new()?;
|
||||
write_apps_config(home.path(), &server_url, /*apps_mcp_product_sku*/ None)?;
|
||||
write_auth(home.path())?;
|
||||
let mut app = TestAppServer::builder()
|
||||
.with_codex_home(home.path())
|
||||
.without_auto_env()
|
||||
.build_initialized_with_timeout(DEFAULT_TIMEOUT)
|
||||
.await?;
|
||||
read_apps(&mut app, vec!["alpha"], /*include_tools*/ false).await?;
|
||||
assert_eq!(state.requests().len(), 1);
|
||||
|
||||
std::fs::write(
|
||||
home.path().join("requirements.toml"),
|
||||
"[application.network]",
|
||||
)?;
|
||||
let reload_id = app.send_request("config/read", Some(json!({}))).await?;
|
||||
let _: Value = timeout(DEFAULT_TIMEOUT, app.read_response(reload_id)).await??;
|
||||
let request_id = app
|
||||
.send_apps_read_request(AppsReadParams {
|
||||
app_ids: vec!["alpha".to_string()],
|
||||
thread_id: None,
|
||||
include_tools: false,
|
||||
})
|
||||
.await?;
|
||||
let error = timeout(
|
||||
DEFAULT_TIMEOUT,
|
||||
app.read_stream_until_error_message(RequestId::Integer(request_id)),
|
||||
)
|
||||
.await??;
|
||||
assert_eq!(
|
||||
error.error.message,
|
||||
"failed to read app metadata: Failed to send request"
|
||||
);
|
||||
assert_eq!(state.requests().len(), 1);
|
||||
server_handle.abort();
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn app_read_deduplicates_orders_partial_misses_and_reuses_cached_metadata() -> Result<()> {
|
||||
let access_token = encode_id_token(
|
||||
|
||||
@@ -4,7 +4,6 @@ use codex_http_client::HttpClientFactory;
|
||||
use codex_login::AuthManager;
|
||||
use codex_login::CodexAuth;
|
||||
use codex_login::default_client::RESIDENCY_HEADER_NAME;
|
||||
use codex_login::default_client::create_client;
|
||||
use codex_login::default_client::create_client_with_chatgpt_cookies;
|
||||
use codex_login::default_client::default_headers;
|
||||
|
||||
@@ -24,15 +23,15 @@ struct CachedChatGptClient {
|
||||
client: HttpClient,
|
||||
}
|
||||
|
||||
static PSP_CHATGPT_CLIENT: LazyLock<Mutex<Option<CachedChatGptClient>>> =
|
||||
static CHATGPT_CLIENT: LazyLock<Mutex<Option<CachedChatGptClient>>> =
|
||||
LazyLock::new(|| Mutex::new(None));
|
||||
|
||||
/// Reuse the default client while retaining its configured ChatGPT cookies.
|
||||
fn psp_chatgpt_client(factory: HttpClientFactory) -> HttpClient {
|
||||
/// Reuse the client and cookies without changing the caller's captured account policy.
|
||||
fn chatgpt_client(factory: HttpClientFactory) -> HttpClient {
|
||||
let residency = default_headers()
|
||||
.get(RESIDENCY_HEADER_NAME)
|
||||
.map(|value| value.as_bytes().to_vec());
|
||||
let mut cached = PSP_CHATGPT_CLIENT
|
||||
let mut cached = CHATGPT_CLIENT
|
||||
.lock()
|
||||
.unwrap_or_else(std::sync::PoisonError::into_inner);
|
||||
if let Some(cached_client) = cached.as_ref()
|
||||
@@ -64,6 +63,12 @@ pub(crate) async fn chatgpt_get_request_with_timeout<T: DeserializeOwned>(
|
||||
path: String,
|
||||
timeout: Option<Duration>,
|
||||
) -> anyhow::Result<T> {
|
||||
// Bind before loading credentials: the temporary auth manager does not own account changes.
|
||||
let policy = config
|
||||
.application_network_policy
|
||||
.clone()
|
||||
.for_current_account();
|
||||
let client = chatgpt_client(config.http_client_factory().with_network_policy(policy));
|
||||
let chatgpt_base_url = &config.chatgpt_base_url;
|
||||
let auth_manager =
|
||||
AuthManager::shared_from_config(config, /*enable_codex_api_key_env*/ false).await?;
|
||||
@@ -79,19 +84,12 @@ pub(crate) async fn chatgpt_get_request_with_timeout<T: DeserializeOwned>(
|
||||
auth.get_account_id().is_some(),
|
||||
"ChatGPT account ID not available, please re-run `codex login`"
|
||||
);
|
||||
|
||||
let url = format!(
|
||||
"{}/{}",
|
||||
chatgpt_base_url.trim_end_matches('/'),
|
||||
path.trim_start_matches('/')
|
||||
);
|
||||
|
||||
let http_client_factory = config.http_client_factory();
|
||||
let client = if http_client_factory.has_chatgpt_cookies() {
|
||||
psp_chatgpt_client(http_client_factory)
|
||||
} else {
|
||||
create_client()
|
||||
};
|
||||
let mut request = client
|
||||
.get(&url)
|
||||
.headers(default_headers())
|
||||
@@ -118,8 +116,8 @@ pub(crate) async fn chatgpt_get_request_with_timeout<T: DeserializeOwned>(
|
||||
|
||||
/// Make a POST request to the ChatGPT backend API with an already-captured auth identity.
|
||||
///
|
||||
/// Callers that bind other state to the auth snapshot should pass that same snapshot here rather
|
||||
/// than reacquiring auth while the request is in flight.
|
||||
/// Callers must capture `auth` and `config.application_network_policy` together and preserve
|
||||
/// that snapshot rather than reacquiring either while the request is in flight.
|
||||
pub(crate) async fn chatgpt_post_request_with_timeout<
|
||||
TResponse: DeserializeOwned,
|
||||
TRequest: Serialize + ?Sized,
|
||||
@@ -145,12 +143,7 @@ pub(crate) async fn chatgpt_post_request_with_timeout<
|
||||
config.chatgpt_base_url.trim_end_matches('/'),
|
||||
path.trim_start_matches('/')
|
||||
);
|
||||
let http_client_factory = config.http_client_factory();
|
||||
let client = if http_client_factory.has_chatgpt_cookies() {
|
||||
psp_chatgpt_client(http_client_factory)
|
||||
} else {
|
||||
create_client()
|
||||
};
|
||||
let client = chatgpt_client(config.http_client_factory());
|
||||
let response = client
|
||||
.post(&url)
|
||||
.headers(default_headers())
|
||||
|
||||
@@ -134,6 +134,8 @@ pub struct ConnectorMetadataReadResult {
|
||||
///
|
||||
/// The store is created before awaiting the backend request, so a response that arrives after an
|
||||
/// account or backend change can only commit to the scope under which it was requested.
|
||||
/// Capture `auth` and `config.application_network_policy` together using
|
||||
/// `AuthManager::auth_with_http_client_factory` so the request retains that account's policy.
|
||||
pub async fn read_connector_metadata(
|
||||
config: &Config,
|
||||
auth: &CodexAuth,
|
||||
|
||||
@@ -218,13 +218,28 @@ pub fn create_client() -> HttpClient {
|
||||
build_default_client(default_http_client_builder())
|
||||
}
|
||||
|
||||
/// Creates the default client with configured ChatGPT cookies and no sensitive-response logging.
|
||||
/// Creates an API client retaining managed policy, ChatGPT cookies, and no request diagnostics.
|
||||
pub fn create_client_with_chatgpt_cookies(http_client_factory: &HttpClientFactory) -> HttpClient {
|
||||
build_default_client(
|
||||
default_http_client_builder()
|
||||
.with_chatgpt_cookies(http_client_factory)
|
||||
.without_request_logging(),
|
||||
)
|
||||
let builder = default_http_client_builder()
|
||||
.with_chatgpt_cookies(http_client_factory)
|
||||
.without_request_logging();
|
||||
if !http_client_factory.network_policy().is_managed() {
|
||||
return build_default_client(builder);
|
||||
}
|
||||
let mut pool = RouteAwareClientPool::with_builder(
|
||||
http_client_factory.clone(),
|
||||
ClientRouteClass::Api,
|
||||
builder,
|
||||
);
|
||||
if is_sandboxed() {
|
||||
pool = pool.with_legacy_direct_proxy_and_custom_ca_fallback();
|
||||
} else if matches!(
|
||||
http_client_factory.outbound_proxy_policy(),
|
||||
OutboundProxyPolicy::ReqwestDefault
|
||||
) {
|
||||
pool = pool.with_legacy_custom_ca_fallback();
|
||||
}
|
||||
pool.into_client()
|
||||
}
|
||||
|
||||
/// Create the default HTTP client without request URL or response-header diagnostics.
|
||||
|
||||
Reference in New Issue
Block a user