Extend runtime auth to account, plugin, and file requests (#59564)

## What changed

- Use the shared runtime and effective configuration for account API clients and remote plugin requests, including standalone plugin commands.
- Support dual-header and assertion-only authentication for remote plugins and MCP file uploads when runtime agent instances are enabled.
- Keep plugin credentials and caches scoped to the thread's account when resuming with different credentials. Bind request pools and multi-step plugin storage operations to the selected account so account replacement revokes stale operations.
- Protect runtime credentials on cross-origin redirects while preserving configured routing policies and legacy authentication for custom backends.
- Apply server-provided `upload_headers` to file storage uploads.

## Testing

Add coverage for shared runtime registration, both authentication modes, configuration reloads, resumed-thread account selection, account replacement during plugin uploads and downloads, and credential stripping on redirects. Extend file upload tests to verify runtime assertions and storage-only upload headers.

GitOrigin-RevId: 68aef81051a6f9bf919fffda52f372b108fce805
This commit is contained in:
cooper-oai
2026-11-05 03:49:13 +00:00
committed by copyberry
parent c90354172c
commit 5c0b1ac71f
55 changed files with 2145 additions and 252 deletions
@@ -17,6 +17,7 @@ use codex_model_provider::is_supported_amazon_bedrock_region;
use codex_otel::auth_storage::AuthStorageOriginator;
use codex_rmcp_client::EnterpriseOAuthCredentialGuard;
mod backend_client;
mod bedrock_gov_cloud;
mod bedrock_setup;
mod enterprise_login;
@@ -290,6 +291,7 @@ impl AccountRequestProcessor {
.await
{
Ok(config) => {
let runtime_auth = thread_manager.runtime_auth_for_config(&config);
Self::spawn_effective_plugins_changed_task(
Arc::clone(thread_manager),
config_manager.clone(),
@@ -311,6 +313,10 @@ impl AccountRequestProcessor {
&plugins_config,
Some(Arc::clone(&on_effective_plugins_changed)),
);
if let Err(err) = runtime_auth {
warn!("failed to initialize runtime auth for remote plugin refresh: {err}");
return;
}
thread_manager
.plugins_manager()
.maybe_start_remote_plugin_caches_refresh(
@@ -1186,11 +1192,7 @@ impl AccountRequestProcessor {
));
}
let client = BackendClient::from_auth(
self.config.chatgpt_base_url.clone(),
&auth,
http_client_factory,
);
let client = self.backend_client(&auth, http_client_factory).await?;
let usage_request = async {
if params.supports_luna_reserve
@@ -1303,11 +1305,7 @@ impl AccountRequestProcessor {
));
}
let client = BackendClient::from_auth(
self.config.chatgpt_base_url.clone(),
&auth,
http_client_factory,
);
let client = self.backend_client(&auth, http_client_factory).await?;
if let Some(thread_id) = thread_id {
let thread_id = thread_id.to_string();
let usage = tokio::time::timeout(
@@ -1391,11 +1389,7 @@ impl AccountRequestProcessor {
));
}
let client = BackendClient::from_auth(
self.config.chatgpt_base_url.clone(),
&auth,
http_client_factory,
);
let client = self.backend_client(&auth, http_client_factory).await?;
let messages = tokio::time::timeout(
ACCOUNT_WORKSPACE_MESSAGES_FETCH_TIMEOUT,
client.list_workspace_messages(),
@@ -1485,11 +1479,7 @@ impl AccountRequestProcessor {
));
}
let client = BackendClient::from_auth(
self.config.chatgpt_base_url.clone(),
&auth,
http_client_factory,
);
let client = self.backend_client(&auth, http_client_factory).await?;
match client
.send_add_credits_nudge_email(Self::backend_credit_type(params.credit_type))
@@ -0,0 +1,31 @@
//! Build account API clients from the latest config and captured account authority.
use super::*;
use futures::future::BoxFuture;
impl AccountRequestProcessor {
// Keep config loading and runtime bootstrap out of request-handler future types.
#[inline(never)]
pub(super) fn backend_client<'a>(
&'a self,
auth: &'a CodexAuth,
http_client_factory: codex_http_client::HttpClientFactory,
) -> BoxFuture<'a, Result<BackendClient, JSONRPCErrorError>> {
Box::pin(async move {
let config = self.load_latest_config().await;
let runtime = self
.thread_manager
.runtime_auth_for_config(&config)
.map_err(|error| internal_error(error.to_string()))?;
BackendClient::from_auth_with_runtime(
config.chatgpt_base_url.clone(),
auth,
http_client_factory,
runtime.as_ref(),
/*headers*/ &Default::default(),
)
.await
.map_err(|error| internal_error(error.to_string()))
})
}
}
@@ -111,11 +111,7 @@ impl AccountRequestProcessor {
));
}
Ok(BackendClient::from_auth(
self.config.chatgpt_base_url.clone(),
&auth,
http_client_factory,
))
self.backend_client(&auth, http_client_factory).await
}
}
@@ -399,15 +399,23 @@ impl AccountRequestProcessor {
let routing = if let Some(cached) = cached {
cached
} else {
let runtime = self
.thread_manager
.runtime_auth_for_config(&config)
.map_err(|_| WorkspaceRoutingError::DiscoveryFailed)?;
let mut recovery = self.auth_manager.unauthorized_recovery();
let mut discovery_auth = auth.clone();
let mut discovery_generation = auth_state.generation;
let response = loop {
let client = BackendClient::from_auth(
let client = BackendClient::from_auth_with_runtime(
&config.chatgpt_base_url,
&discovery_auth,
config.http_client_factory(),
);
runtime.as_ref(),
/*headers*/ &Default::default(),
)
.await
.map_err(|_| WorkspaceRoutingError::DiscoveryFailed)?;
match client.get_accounts_check().await {
Ok(response) => break response,
Err(error) => {
@@ -159,8 +159,16 @@ fn load_shared_plugin_ids_by_local_path(
})
}
fn remote_plugin_service_config(config: &Config) -> RemotePluginServiceConfig {
config.plugins_config_input().remote_plugin_service_config()
fn remote_plugin_service_config(
thread_manager: &ThreadManager,
config: &Config,
) -> Result<RemotePluginServiceConfig, JSONRPCErrorError> {
let _ = thread_manager
.runtime_auth_for_config(config)
.map_err(|error| internal_error(error.to_string()))?;
Ok(thread_manager
.plugins_manager()
.remote_plugin_service_config(&config.plugins_config_input()))
}
fn share_context_for_source(
@@ -599,7 +607,8 @@ impl PluginRequestProcessor {
let include_global_remote = context.remote_plugins_enabled() && !explicit_marketplace_kinds;
let use_remote_global_catalog =
include_global_remote && auth_mode.is_some_and(DomainAuthMode::uses_codex_backend);
let remote_plugin_service_config = remote_plugin_service_config(&config);
let remote_plugin_service_config =
remote_plugin_service_config(&self.thread_manager, &config)?;
let plugin_policy = &config.plugins;
let remote_catalog_cache_mode = if force_refetch {
RemotePluginCatalogCacheMode::ForceRefetch
@@ -1056,7 +1065,8 @@ impl PluginRequestProcessor {
);
let share_context = match share_context {
Some(context) => {
let remote_plugin_service_config = remote_plugin_service_config(&config);
let remote_plugin_service_config =
remote_plugin_service_config(&self.thread_manager, &config)?;
match codex_core_plugins::remote::fetch_remote_plugin_share_context(
&remote_plugin_service_config,
auth.as_ref(),
@@ -1184,7 +1194,8 @@ impl PluginRequestProcessor {
"remote plugin read is not enabled for marketplace {remote_marketplace_name}"
)));
}
let remote_plugin_service_config = remote_plugin_service_config(&config);
let remote_plugin_service_config =
remote_plugin_service_config(&self.thread_manager, &config)?;
validate_remote_plugin_id(&plugin_name)?;
let remote_detail = codex_core_plugins::remote::fetch_remote_plugin_detail(
&remote_plugin_service_config,
@@ -1246,7 +1257,8 @@ impl PluginRequestProcessor {
}
let auth = self.auth_manager.auth().await;
let remote_plugin_service_config = remote_plugin_service_config(&config);
let remote_plugin_service_config =
remote_plugin_service_config(&self.thread_manager, &config)?;
let remote_skill_detail = codex_core_plugins::remote::fetch_remote_plugin_skill_detail(
&remote_plugin_service_config,
auth.as_ref(),
@@ -1297,7 +1309,8 @@ impl PluginRequestProcessor {
validate_client_plugin_share_targets(share_targets)?;
}
let remote_plugin_service_config = remote_plugin_service_config(&config);
let remote_plugin_service_config =
remote_plugin_service_config(&self.thread_manager, &config)?;
let access_policy = codex_core_plugins::remote::RemotePluginShareAccessPolicy {
discoverability: discoverability.map(remote_plugin_share_discoverability),
share_targets: share_targets.map(remote_plugin_share_targets),
@@ -1345,7 +1358,8 @@ impl PluginRequestProcessor {
}
validate_client_plugin_share_targets(&share_targets)?;
let remote_plugin_service_config = remote_plugin_service_config(&config);
let remote_plugin_service_config =
remote_plugin_service_config(&self.thread_manager, &config)?;
let result = codex_core_plugins::remote::update_remote_plugin_share_targets(
&remote_plugin_service_config,
auth.as_ref(),
@@ -1379,7 +1393,8 @@ impl PluginRequestProcessor {
_params: PluginShareListParams,
) -> Result<PluginShareListResponse, JSONRPCErrorError> {
let (config, auth) = self.load_plugin_share_config_and_auth().await?;
let remote_plugin_service_config = remote_plugin_service_config(&config);
let remote_plugin_service_config =
remote_plugin_service_config(&self.thread_manager, &config)?;
let plugin_policy = &config.plugins;
let data = codex_core_plugins::remote::list_remote_plugin_shares(
&remote_plugin_service_config,
@@ -1417,7 +1432,8 @@ impl PluginRequestProcessor {
return Err(invalid_request("invalid remote plugin id"));
}
let remote_plugin_service_config = remote_plugin_service_config(&config);
let remote_plugin_service_config =
remote_plugin_service_config(&self.thread_manager, &config)?;
let result = codex_core_plugins::remote::checkout_remote_plugin_share(
&remote_plugin_service_config,
auth.as_ref(),
@@ -1448,7 +1464,8 @@ impl PluginRequestProcessor {
return Err(invalid_request("invalid remote plugin id"));
}
let remote_plugin_service_config = remote_plugin_service_config(&config);
let remote_plugin_service_config =
remote_plugin_service_config(&self.thread_manager, &config)?;
codex_core_plugins::remote::delete_remote_plugin_share(
&remote_plugin_service_config,
auth.as_ref(),
@@ -1599,6 +1616,10 @@ impl PluginRequestProcessor {
install_attempt_id: Option<String>,
) -> Result<PluginInstallResponse, JSONRPCErrorError> {
let mut config = self.load_latest_config(/*fallback_cwd*/ None).await?;
let _ = self
.thread_manager
.runtime_auth_for_config(&config)
.map_err(|error| internal_error(error.to_string()))?;
let auth = match self.auth_manager.auth_with_http_client_factory().await {
Some((auth, factory)) => {
config.application_network_policy = factory.network_policy().clone();
@@ -2105,6 +2126,10 @@ impl PluginRequestProcessor {
plugin_id: String,
) -> Result<PluginUninstallResponse, JSONRPCErrorError> {
let config = self.load_latest_config(/*fallback_cwd*/ None).await?;
let _ = self
.thread_manager
.runtime_auth_for_config(&config)
.map_err(|error| internal_error(error.to_string()))?;
let auth = self.auth_manager.auth().await;
let outcome = self
.thread_manager
@@ -78,7 +78,7 @@ impl PluginRequestProcessor {
&& let Some(remote_scope) = remote_scope
{
let page = search_remote_plugins(
&remote_plugin_service_config(&config),
&remote_plugin_service_config(&self.thread_manager, &config)?,
auth.as_ref(),
RemotePluginSearchRequest {
query: search_term,
@@ -77,6 +77,9 @@ use wiremock::matchers::method;
use wiremock::matchers::path;
use wiremock::matchers::query_param;
#[path = "plugin_runtime_auth.rs"]
mod runtime_auth_tests;
// Plugin install tests wait on connector discovery after the install response path
// starts, which is noticeably slower on Windows CI.
const DEFAULT_TIMEOUT: Duration = Duration::from_secs(60);
@@ -0,0 +1,194 @@
//! Cover direct plugin mutations when runtime authentication is enabled after startup.
use super::*;
use anyhow::Context;
use base64::Engine;
use codex_app_server_protocol::PluginUninstallParams;
use codex_app_server_protocol::PluginUninstallResponse;
use core_test_support::runtime_auth::PROBE_SERVER;
use core_test_support::runtime_auth::run_harness_through_proxy;
use pretty_assertions::assert_eq;
use test_case::test_case;
#[test_case("dual_header", "install"; "install_dual_header")]
#[test_case("assertion_only", "install"; "install_assertion_only")]
#[test_case("dual_header", "uninstall"; "uninstall_dual_header")]
#[test_case("assertion_only", "uninstall"; "uninstall_assertion_only")]
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn direct_mutation_initializes_runtime_after_config_reload(
mode: &str,
operation: &str,
) -> Result<()> {
core_test_support::skip_if_no_network!(Ok(()));
let token = format!(
"e30.{}.c2ln",
base64::engine::general_purpose::URL_SAFE_NO_PAD.encode(
json!({
"exp": 4_102_444_800_u64,
"session_id": "session-a",
"iss": "https://auth.openai.com",
"https://api.openai.com/auth": {
"chatgpt_user_id": "user-123", "chatgpt_account_id": "account-123",
},
})
.to_string()
)
);
if std::env::var(PROBE_SERVER).is_ok() {
let home = TempDir::new()?;
write_chatgpt_auth(
home.path(),
ChatGptAuthFixture::new(&token)
.account_id("account-123")
.chatgpt_user_id("user-123")
.chatgpt_account_id("account-123"),
AuthCredentialsStoreMode::File,
)?;
write_remote_plugin_catalog_config(home.path(), "https://chatgpt.com/backend-api")?;
let config_path = home.path().join("config.toml");
let initial = std::fs::read_to_string(&config_path)?;
std::fs::write(
&config_path,
format!(
"{initial}\n[features.runtime_agent_instances]\nenabled = false\nmode = {mode:?}\n"
),
)?;
let mut app = TestAppServer::builder()
.with_codex_home(home.path())
.without_auto_env()
.with_env_overrides(&[
("OPENAI_API_KEY", None),
("CODEX_ACCESS_TOKEN", None),
(TEST_ALLOW_HTTP_REMOTE_PLUGIN_BUNDLE_DOWNLOADS, Some("1")),
])
.build_initialized_with_timeout(DEFAULT_TIMEOUT)
.await?;
std::fs::write(
&config_path,
format!(
"{initial}\n[features.runtime_agent_instances]\nenabled = true\nmode = {mode:?}\n"
),
)?;
let cache = home
.path()
.join("plugins/cache/openai-curated-remote/linear");
match operation {
"install" => {
let id = send_remote_plugin_install_request(&mut app, REMOTE_PLUGIN_ID).await?;
let response: PluginInstallResponse =
timeout(DEFAULT_TIMEOUT, app.read_response(id)).await??;
assert_eq!(
response,
PluginInstallResponse {
auth_policy: PluginAuthPolicy::OnUse,
apps_needing_auth: Vec::new(),
}
);
assert!(cache.join("1.2.3/.codex-plugin/plugin.json").is_file());
}
"uninstall" => {
std::fs::create_dir_all(cache.join("1.2.3/.codex-plugin"))?;
std::fs::write(
cache.join("1.2.3/.codex-plugin/plugin.json"),
r#"{"name":"linear","version":"1.2.3"}"#,
)?;
let id = app
.send_plugin_uninstall_request(PluginUninstallParams {
plugin_id: REMOTE_PLUGIN_ID.into(),
})
.await?;
let response: PluginUninstallResponse =
timeout(DEFAULT_TIMEOUT, app.read_response(id)).await??;
assert_eq!(response, PluginUninstallResponse {});
assert!(!cache.exists());
}
_ => unreachable!("test operation"),
}
return Ok(());
}
let server = MockServer::start().await;
let bundle = mount_remote_plugin_bundle(
&server,
/*status_code*/ 200,
remote_plugin_bundle_tar_gz_bytes("linear")?,
)
.await;
Mock::given(method("POST"))
.and(path("/agent/register"))
.respond_with(ResponseTemplate::new(200).set_body_json(json!({
"runtime_agent_instance_id": "runtime-a", "agent_host_id": "host-a",
"actor": {"kind": "chatgpt", "user_id": "user-123", "account_id": "account-123"},
})))
.expect(1)
.mount(&server)
.await;
Mock::given(method("GET"))
.and(path(format!("/backend-api/ps/plugins/{REMOTE_PLUGIN_ID}")))
.respond_with(ResponseTemplate::new(200).set_body_json(json!({
"id": REMOTE_PLUGIN_ID, "name": "linear", "scope": "GLOBAL",
"installation_policy": "AVAILABLE", "authentication_policy": "ON_USE", "status": "ENABLED",
"release": {
"version": "1.2.3", "bundle_download_url": bundle, "display_name": "Linear",
"description": "Track work in Linear.",
"app_ids": [], "interface": {}, "skills": [],
},
})))
.expect(1)
.mount(&server).await;
Mock::given(method("POST"))
.and(path(format!(
"/backend-api/ps/plugins/{REMOTE_PLUGIN_ID}/{operation}"
)))
.respond_with(ResponseTemplate::new(200).set_body_json(json!({
"id": REMOTE_PLUGIN_ID, "enabled": operation == "install",
})))
.expect(1)
.mount(&server)
.await;
Mock::given(method("GET"))
.and(path("/backend-api/ps/plugins/installed"))
.respond_with(ResponseTemplate::new(200).set_body_json(json!({
"plugins": [], "pagination": {"limit": 50, "next_page_token": null},
})))
.mount(&server)
.await;
run_harness_through_proxy(
&server,
&format!("suite::v2::plugin_install::runtime_auth_tests::direct_mutation_initializes_runtime_after_config_reload::{operation}_{mode}"),
/*realtime_target*/ None,
).await?;
let requests = server
.received_requests()
.await
.context("recorded requests")?;
let plugin_requests = requests
.iter()
.filter(|request| {
request
.url
.path()
.starts_with(&format!("/backend-api/ps/plugins/{REMOTE_PLUGIN_ID}"))
})
.collect::<Vec<_>>();
assert_eq!(plugin_requests.len(), 2);
for request in plugin_requests {
let proof = if mode == "dual_header" {
assert_eq!(request.headers["authorization"], format!("Bearer {token}"));
request.headers["x-openai-agent-assertion"].to_str()?
} else {
assert!(!request.headers.contains_key("x-openai-agent-assertion"));
request.headers["authorization"]
.to_str()?
.strip_prefix("AgentAssertion ")
.context("assertion auth")?
};
let claims: serde_json::Value = serde_json::from_slice(
&base64::engine::general_purpose::URL_SAFE_NO_PAD
.decode(proof.split('.').nth(2).context("assertion payload")?)?,
)?;
assert_eq!(claims["runtime_agent_instance"], "runtime-a");
}
server.verify().await;
Ok(())
}
@@ -120,7 +120,8 @@ async fn get_account_rate_limits_returns_snapshot(
let server = MockServer::start().await;
let server_url = server.uri();
write_chatgpt_base_url(codex_home.path(), &server_url)?;
let startup_server = MockServer::start().await;
write_chatgpt_base_url(codex_home.path(), &startup_server.uri())?;
let primary_reset_timestamp = chrono::DateTime::parse_from_rfc3339("2025-01-01T00:02:00Z")
.expect("parse primary reset timestamp")
@@ -246,6 +247,14 @@ async fn get_account_rate_limits_returns_snapshot(
.build_initialized_with_timeout(DEFAULT_READ_TIMEOUT)
.await?;
// Account RPCs reload the effective config, but custom endpoints keep their original auth.
std::fs::write(
codex_home.path().join("config.toml"),
format!(
"chatgpt_base_url = {server_url:?}\nruntime_auth_mode = \"assertion_only\"\n[features]\nruntime_agent_instances = true\n",
),
)?;
let request_id = mcp.send_get_account_rate_limits_request().await?;
let received: GetAccountRateLimitsResponse =
@@ -361,6 +370,9 @@ async fn get_account_rate_limits_returns_snapshot(
};
assert_eq!(received, expected);
for request in server.received_requests().await.expect("recorded requests") {
assert!(!request.headers.contains_key("x-openai-agent-assertion"));
}
Ok(())
}