aKode fase 2b-2: rimosso login ChatGPT e tutti i sottosistemi solo-ChatGPT

- Login: resta solo chiave API (+ header, Bedrock). Rimossi OAuth/browser,
  device code, refresh/revoke su auth.openai.com, PAT, agent/workload identity,
  runtime agent instance, backend-client e openapi-models, workspace routing.
- Rimossi: generazione immagini (ext + skill imagegen), history notes, plugin
  remoti (catalogo, sync, share), cyber access program, daybreak, cloud config
  e requirements (config_server ora fail-closed), attestation, frozen cloud
  skills, EMA MCP auth, Codex Apps/connettori, ricerca ospitata, header/zstd/
  routing/cookie del backend Codex, fallback OPENAI_API_KEY del realtime.
- Messaggi d'errore senza link a chatgpt.com.
- Chiavi di config legacy accettate e ignorate; feature rimosse = Stage::Removed.
- Test: cancellati solo quelli del codice rimosso; test di sicurezza
  (sandbox estensioni, permessi, redazione, auth storage, requirements)
  adattati. Restano solo fallimenti d'ambiente (code-mode-host/V8, ~/.agents).
- Istruzioni di sistema invariate.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
andrea.difonzo
2026-11-10 16:20:15 +01:00
parent 82347a58ed
commit 6089fbde91
1573 changed files with 10289 additions and 177679 deletions

View File

@@ -15,7 +15,6 @@ use codex_websocket_auth::WebsocketAuthArgs;
use crate::config::load_exec_server_config;
use crate::config::load_exec_server_remote_auth_provider;
use crate::config::read_chatgpt_stdin_auth;
use crate::exec_server_auth;
use crate::exec_server_credential::ExecutorCredentialPipe;
use crate::exec_server_telemetry;
@@ -99,20 +98,11 @@ pub struct ExecServerCommand {
#[arg(long = "name", value_name = "NAME", global = true)]
name: Option<String>,
/// Use Agent Identity auth from CODEX_ACCESS_TOKEN for remote registration.
#[arg(
long = "use-agent-identity-auth",
requires = "exec_server_remote",
conflicts_with = "aws_sigv4",
global = true
)]
use_agent_identity_auth: bool,
/// Read one registration bearer from stdin.
#[arg(
long,
requires = "exec_server_remote",
conflicts_with_all = ["use_agent_identity_auth", "executor_credential_stdin", "aws_sigv4"],
conflicts_with_all = ["executor_credential_stdin", "aws_sigv4"],
global = true
)]
registration_bearer_stdin: bool,
@@ -121,23 +111,11 @@ pub struct ExecServerCommand {
#[arg(
long = "executor-credential-stdin",
requires = "exec_server_remote",
conflicts_with_all = ["use_agent_identity_auth", "aws_sigv4"],
conflicts_with = "aws_sigv4",
global = true
)]
executor_credential_stdin: bool,
/// Read a ChatGPT access token from stdin for HTTPS registration.
#[arg(
long,
requires = "exec_server_remote",
conflicts_with_all = ["use_agent_identity_auth", "aws_sigv4", "executor_credential_stdin", "registration_bearer_stdin"]
)]
chatgpt_token_stdin: bool,
/// ChatGPT account ID. Must match the token's account claim when present.
#[arg(long, value_name = "ID", requires = "chatgpt_token_stdin")]
chatgpt_account_id: Option<String>,
/// Sign Direct registration and WebSocket handshake requests with AWS SigV4.
#[arg(long = "aws-sigv4", requires = "exec_server_remote", global = true)]
aws_sigv4: bool,
@@ -220,21 +198,8 @@ impl ExecServerCommand {
.registration_bearer_stdin
.then(codex_exec_server::registration_auth_from_stdin)
.transpose()?;
let stdin_auth = if self.chatgpt_token_stdin {
codex_http_client::validate_https_endpoint_url(&base_url, "ChatGPT token registry")
.map_err(anyhow::Error::msg)?;
Some(read_chatgpt_stdin_auth(self.chatgpt_account_id.take())?)
} else {
None
};
let config = load_exec_server_config(
root_config_overrides,
strict_config,
stdin_auth.as_ref(),
);
let mut parent_lifetime = if self.exit_on_stdin_close && stdin_auth.is_some() {
ParentLifetime::MonitoredStdin(exec_server_telemetry::monitor_stdin_close())
} else if self.exit_on_stdin_close {
let config = load_exec_server_config(root_config_overrides, strict_config);
let mut parent_lifetime = if self.exit_on_stdin_close {
ParentLifetime::StdinPipe
} else {
ParentLifetime::Independent
@@ -262,12 +227,6 @@ impl ExecServerCommand {
parent_lifetime = ParentLifetime::MonitoredStdin(parent_closed);
credential_provider = Some(Arc::clone(&provider));
provider
} else if let Some(auth) = stdin_auth {
anyhow::ensure!(
config.auth_config().allows_auth(&auth),
"ChatGPT authentication is not permitted for this account"
);
codex_model_provider::auth_provider_from_auth(&auth)
} else if self.aws_sigv4 {
exec_server_auth::aws_sigv4_auth_provider(
codex_aws_auth::AwsAuthConfig {
@@ -281,12 +240,7 @@ impl ExecServerCommand {
} else if let Some(provider) = registration_auth {
provider
} else {
load_exec_server_remote_auth_provider(
&config,
&base_url,
self.use_agent_identity_auth,
)
.await?
load_exec_server_remote_auth_provider(&config, &base_url).await?
};
let mut remote_config = codex_exec_server::RemoteEnvironmentConfig::new_with_transport(
base_url,
@@ -301,8 +255,6 @@ impl ExecServerCommand {
)?;
if self.executor_credential_stdin || self.registration_bearer_stdin {
remote_config = remote_config.with_executor_credential_auth()?;
} else if self.chatgpt_token_stdin {
remote_config = remote_config.with_registry_bearer_redaction();
}
if let Some(name) = self.name {
remote_config.name = name;
@@ -351,12 +303,7 @@ impl ExecServerCommand {
}
result
} else {
let config_result = load_exec_server_config(
root_config_overrides,
strict_config,
/*stdin_auth*/ None,
)
.await;
let config_result = load_exec_server_config(root_config_overrides, strict_config).await;
let config = if strict_config {
Some(config_result?)
} else {
@@ -402,10 +349,6 @@ impl ExecServerCommand {
"--registration-bearer-stdin cannot be used with --executor-credential-stdin"
);
}
anyhow::ensure!(
!self.chatgpt_token_stdin || self.command.is_none(),
"--chatgpt-token-stdin cannot be used with forward"
);
match (self.remote_transport, self.aws_sigv4) {
(ExecServerRemoteTransport::Noise, true) => {
anyhow::bail!("--aws-sigv4 requires --remote-transport direct");

View File

@@ -1,136 +1,34 @@
//! Loads executor startup configuration and remote authentication without changing CLI policy.
use anyhow::Context;
use codex_app_server::in_process::EmbeddedNetworkPolicy;
use codex_core::config::ConfigBuilder;
use codex_login::AuthManager;
use codex_login::CodexAuth;
use codex_login::read_codex_access_token_from_env;
use codex_protocol::auth::AuthMode;
use codex_utils_cli::CliConfigOverrides;
use std::io::BufRead;
use std::io::Read;
use std::sync::Arc;
use crate::exec_server_auth;
const REMOTE_AUTH_MODES: &[AuthMode] = &[
AuthMode::ApiKey,
AuthMode::Chatgpt,
AuthMode::ChatgptAuthTokens,
AuthMode::PersonalAccessToken,
];
pub(super) fn read_chatgpt_stdin_auth(account_id: Option<String>) -> anyhow::Result<CodexAuth> {
const MAX_FRAME_BYTES: u64 = 64 * 1024;
let stdin = codex_exec_server::credential_stdin()?;
#[cfg(unix)]
{
use std::os::fd::AsFd;
use std::os::unix::fs::FileTypeExt;
let kind = std::fs::File::from(stdin.as_fd().try_clone_to_owned()?)
.metadata()?
.file_type();
anyhow::ensure!(
kind.is_fifo() || kind.is_socket(),
"ChatGPT token stdin must be a pipe or socket"
);
}
#[cfg(not(unix))]
anyhow::ensure!(
!std::io::IsTerminal::is_terminal(&stdin),
"ChatGPT token stdin must be piped"
);
// The launcher keeps the pipe open for --exit-on-stdin-close after this line.
let mut line = String::new();
stdin.lock().take(MAX_FRAME_BYTES).read_line(&mut line)?;
chatgpt_auth_from_line(&line, account_id)
}
fn chatgpt_auth_from_line(line: &str, account_id: Option<String>) -> anyhow::Result<CodexAuth> {
let token = line
.strip_suffix('\n')
.context("expected a bounded, newline-terminated ChatGPT access token")?;
let token = token.strip_suffix('\r').unwrap_or(token);
anyhow::ensure!(
!token.is_empty() && token.bytes().all(|b| b.is_ascii_graphic()),
"invalid ChatGPT access token"
);
let claimed_id = codex_login::token_data::parse_chatgpt_jwt_claims(token)
.map_err(|_| anyhow::anyhow!("invalid ChatGPT access token"))?
.chatgpt_account_id;
if let (Some(id), Some(claim)) = (&account_id, &claimed_id) {
anyhow::ensure!(id == claim, "--chatgpt-account-id does not match the token");
}
// A token without an account claim cannot establish the supplied account's plan.
let plan_override = claimed_id.is_none().then_some("unknown");
let account_id = account_id
.or(claimed_id)
.context("token has no ChatGPT account ID; supply --chatgpt-account-id")?;
anyhow::ensure!(
!account_id.is_empty() && account_id.bytes().all(|b| b.is_ascii_graphic()),
"invalid ChatGPT account ID"
);
CodexAuth::from_external_chatgpt_tokens(token, &account_id, plan_override)
.map_err(|_| anyhow::anyhow!("invalid ChatGPT access token"))
}
const REMOTE_AUTH_MODES: &[AuthMode] = &[AuthMode::ApiKey];
pub(super) async fn load_exec_server_remote_auth_provider(
config: &codex_core::config::Config,
base_url: &str,
use_agent_identity_auth: bool,
) -> anyhow::Result<codex_api::SharedAuthProvider> {
if use_agent_identity_auth {
read_codex_access_token_from_env().ok_or_else(|| {
anyhow::anyhow!("CODEX_ACCESS_TOKEN is required when --use-agent-identity-auth is set")
})?;
let auth = AuthManager::shared_from_config(config, /*enable_codex_api_key_env*/ false)
.await?
.auth()
.await
.ok_or_else(|| anyhow::anyhow!("Agent Identity authentication is unavailable"))?;
if !matches!(auth, CodexAuth::AgentIdentity(_)) {
anyhow::bail!(
"CODEX_ACCESS_TOKEN did not provide permitted Agent Identity authentication"
);
}
return Ok(codex_model_provider::auth_provider_from_auth(&auth));
}
let (auth_manager, auth) = load_exec_server_remote_auth(
let (_auth_manager, auth) = load_exec_server_remote_auth(
config,
"remote exec-server registration requires ChatGPT authentication or API key authentication; run `codex login` or set CODEX_API_KEY",
"remote exec-server registration requires API key authentication; run `codex login --with-api-key` or set CODEX_API_KEY",
)
.await?;
if !is_supported_exec_server_remote_auth(&auth) {
anyhow::bail!(
"remote exec-server registration requires ChatGPT authentication or API key authentication; Agent Identity auth requires --use-agent-identity-auth"
);
}
if auth.is_api_key_auth() {
validate_api_key_remote_host(base_url)?;
}
if auth_manager.is_workload_identity_selected() {
Ok(codex_model_provider::auth_provider_from_auth_manager(
auth_manager,
&auth,
))
} else if auth.is_chatgpt_auth() {
Ok(exec_server_auth::chatgpt_auth_provider(
auth_manager,
&auth,
)?)
} else {
Ok(codex_model_provider::auth_provider_from_auth(&auth))
anyhow::bail!("remote exec-server registration requires API key authentication");
}
validate_api_key_remote_host(base_url)?;
Ok(codex_model_provider::auth_provider_from_auth(&auth))
}
pub(super) fn is_supported_exec_server_remote_auth(auth: &CodexAuth) -> bool {
auth.is_chatgpt_auth() || auth.is_api_key_auth()
auth.is_api_key_auth()
}
pub(super) fn validate_api_key_remote_host(base_url: &str) -> anyhow::Result<()> {
@@ -170,7 +68,6 @@ pub(super) fn validate_api_key_remote_host(base_url: &str) -> anyhow::Result<()>
pub(super) async fn load_exec_server_config(
root_config_overrides: &CliConfigOverrides,
strict_config: bool,
stdin_auth: Option<&CodexAuth>,
) -> anyhow::Result<codex_core::config::Config> {
let cli_kv_overrides = root_config_overrides
.parse_overrides()
@@ -179,9 +76,6 @@ pub(super) async fn load_exec_server_config(
.cli_overrides(cli_kv_overrides)
.strict_config(strict_config);
let mut config = builder.build().await?;
if stdin_auth.is_some() {
config.config_layer_stack.ensure_config_server_applied()?;
}
EmbeddedNetworkPolicy::default().activate(&mut config);
Ok(config)
}

View File

@@ -59,23 +59,3 @@ fn exec_server_remote_api_key_auth_rejects_suffix_spoof() {
"remote exec-server API-key authentication is restricted to HTTPS openai.com and openai.org hosts and subdomains or loopback hosts"
);
}
#[test]
fn stdin_chatgpt_account_must_match_the_token_claim() -> anyhow::Result<()> {
let claims =
serde_json::json!({"https://api.openai.com/auth": {"chatgpt_account_id": "alderaan"}});
let token = jsonwebtoken::encode(
&jsonwebtoken::Header::default(),
&claims,
&jsonwebtoken::EncodingKey::from_secret(b"test"),
)?;
let line = format!("{token}\n");
for account_id in [None, Some("alderaan".to_string())] {
let auth = chatgpt_auth_from_line(&line, account_id)?;
assert_eq!(auth.get_account_id().as_deref(), Some("alderaan"));
}
assert!(chatgpt_auth_from_line(&line, Some("endor".to_string())).is_err());
assert!(chatgpt_auth_from_line("e30.e30.c2ln\n", /*account_id*/ None).is_err());
assert!(chatgpt_auth_from_line(&token, /*account_id*/ None).is_err());
Ok(())
}

View File

@@ -35,7 +35,6 @@ fn exec_server_defaults_preserve_local_and_noise_modes() {
"env-1",
"--remote-transport",
"noise",
"--use-agent-identity-auth",
],
] {
let command = exec_server_from_args(&args);
@@ -48,21 +47,6 @@ fn exec_server_defaults_preserve_local_and_noise_modes() {
}
}
#[test]
fn remote_forwarder_rejects_chatgpt_token_stdin() {
let mut command = exec_server_from_args(&[
"forward",
"--connect",
"ws://localhost:8765",
"--remote",
"https://registry.example.com",
"--environment-id",
"env-1",
]);
command.chatgpt_token_stdin = true;
assert!(command.validate_remote_transport().is_err());
}
#[test]
fn exec_server_parses_direct_aws_options() {
for (options, expected) in [
@@ -97,7 +81,7 @@ fn exec_server_parses_direct_aws_options() {
);
assert_eq!(command.environment_id.as_deref(), Some("env-1"));
assert_eq!(command.remote_transport, ExecServerRemoteTransport::Direct);
assert!(command.aws_sigv4);
assert!(command.aws_sigv4);
assert_eq!(
(
command.aws_profile.as_deref(),
@@ -152,31 +136,10 @@ fn exec_server_rejects_missing_or_conflicting_remote_options() {
vec!["--aws-service", "execute-api"],
ErrorKind::MissingRequiredArgument,
),
(
vec![
"--remote-transport",
"direct",
"--aws-sigv4",
"--use-agent-identity-auth",
],
ErrorKind::ArgumentConflict,
),
(
vec!["--executor-credential-stdin", "--use-agent-identity-auth"],
ErrorKind::ArgumentConflict,
),
(
vec!["--executor-credential-stdin", "--registration-bearer-stdin"],
ErrorKind::ArgumentConflict,
),
(
vec!["--chatgpt-token-stdin", "--executor-credential-stdin"],
ErrorKind::ArgumentConflict,
),
(
vec!["--chatgpt-token-stdin", "--registration-bearer-stdin"],
ErrorKind::ArgumentConflict,
),
(
vec![
"--executor-credential-stdin",
@@ -318,7 +281,6 @@ fn exec_server_forward_parses_shared_remote_options() {
"--name",
"forwarded",
"--strict-config",
"--use-agent-identity-auth",
])
.expect("parse forward");
assert_matches!(cli.command, ExecServerCommand {
@@ -327,7 +289,6 @@ fn exec_server_forward_parses_shared_remote_options() {
environment_id: Some(environment_id),
name: Some(name),
strict_config: true,
use_agent_identity_auth: true,
..
} if connect == "ws://127.0.0.1:8765" && remote == "https://example.openai.com" && environment_id == "env-1" && name == "forwarded");
}

View File

@@ -1,17 +1,8 @@
//! Request-time authentication for remote executors, including AWS signing.
//!
//! ChatGPT credentials may rotate while an executor is idle. Reload them for
//! registry requests while retaining the identity that registered the executor.
//! Failed credential reads block headers until revalidation, without counting as logout.
//! Quarantine begins with the OAuth exchange, after credential reads and refresh-guard waits.
//! Request-time authentication for remote executors using AWS SigV4 signing.
use std::sync::Arc;
use std::sync::atomic::AtomicBool;
use std::sync::atomic::Ordering;
use std::time::Duration;
use codex_api::AuthError;
use codex_api::AuthHeadersFuture;
use codex_api::AuthProvider;
use codex_api::SharedAuthProvider;
use codex_aws_auth::AwsAuthConfig;
@@ -21,209 +12,7 @@ use codex_aws_auth::AwsRequestToSign;
use codex_http_client::Request;
use codex_http_client::RequestBody;
use codex_http_client::RequestCompression;
use codex_login::AuthManager;
use codex_login::CodexAuth;
use http::HeaderMap;
use tokio::sync::Semaphore;
/// Follows stored credential rotation without allowing an executor's owner to change.
pub(super) fn chatgpt_auth_provider(
auth_manager: Arc<AuthManager>,
expected_auth: &CodexAuth,
) -> Result<SharedAuthProvider, AuthError> {
auth_manager
.restrict_auth_modes(vec![expected_auth.auth_mode()])
.map_err(|_| AuthError::Build("executor credential policy is unavailable".to_string()))?;
let account_id = expected_auth.get_account_id();
let user_id = expected_auth.get_chatgpt_user_id();
let identity_complete = account_id.as_ref().is_some_and(|id| !id.trim().is_empty())
&& user_id.as_ref().is_some_and(|id| !id.trim().is_empty());
let unidentified_headers = if identity_complete {
None
} else {
Some(codex_model_provider::auth_provider_from_auth(expected_auth).to_auth_headers()?)
};
Ok(Arc::new(ReloadingChatgptAuthProvider {
managed: codex_model_provider::auth_provider_from_auth_manager(
Arc::clone(&auth_manager),
expected_auth,
),
auth_manager,
account_id,
user_id,
workspace_account: expected_auth.is_workspace_account(),
fedramp_account: expected_auth.is_fedramp_account(),
unidentified_headers,
resolution_lock: Arc::new(Semaphore::new(/*permits*/ 1)),
headers_valid: Arc::new(AtomicBool::new(true)),
}))
}
#[derive(Clone)]
struct ReloadingChatgptAuthProvider {
managed: SharedAuthProvider,
auth_manager: Arc<AuthManager>,
account_id: Option<String>,
user_id: Option<String>,
workspace_account: bool,
fedramp_account: bool,
unidentified_headers: Option<HeaderMap>,
resolution_lock: Arc<Semaphore>,
headers_valid: Arc<AtomicBool>,
}
impl ReloadingChatgptAuthProvider {
fn checked_headers(&self, auth: Option<CodexAuth>) -> Result<HeaderMap, AuthError> {
let auth = auth
.filter(|auth| {
auth.is_chatgpt_auth()
&& auth.get_account_id() == self.account_id
&& auth.get_chatgpt_user_id() == self.user_id
&& auth.is_workspace_account() == self.workspace_account
&& auth.is_fedramp_account() == self.fedramp_account
})
.ok_or_else(|| AuthError::Build("executor authentication changed".to_string()))?;
let token = auth.get_token().map_err(|_| {
AuthError::Build("executor credential has no usable bearer".to_string())
})?;
if token.trim().is_empty() {
return Err(AuthError::Build(
"executor credential has an empty bearer".to_string(),
));
}
let uncertain = self
.auth_manager
.is_chatgpt_refresh_unsettled(&auth)
.map_err(|_| {
AuthError::Transient("executor authentication is unavailable".to_string())
})?;
if uncertain {
return Err(AuthError::Transient(
"executor refresh outcome is unknown; replace or renew stored credentials"
.to_string(),
));
}
let headers = codex_model_provider::auth_provider_from_auth(&auth).to_auth_headers()?;
if self
.unidentified_headers
.as_ref()
.is_some_and(|expected| expected != &headers)
{
return Err(AuthError::Build(
"executor credential rotation requires a complete account and user identity"
.to_string(),
));
}
Ok(headers)
}
}
impl AuthProvider for ReloadingChatgptAuthProvider {
fn add_auth_headers(&self, headers: &mut HeaderMap) -> Result<(), AuthError> {
if !self.headers_valid.load(Ordering::Acquire) {
return Err(AuthError::Transient(
"executor credentials have not been successfully revalidated".to_string(),
));
}
headers.extend(self.checked_headers(self.auth_manager.auth_cached())?);
Ok(())
}
fn apply_auth_headers(&self, mut headers: HeaderMap) -> AuthHeadersFuture<'_> {
Box::pin(async move {
headers.extend(self.resolve(/*rejected_headers*/ None).await?);
Ok(headers)
})
}
fn refresh_after_unauthorized(
&self,
rejected_headers: HeaderMap,
) -> codex_api::AuthRefreshFuture<'_> {
Box::pin(async move {
self.resolve(Some(rejected_headers.clone()))
.await
.is_ok_and(|headers| {
headers
.iter()
.any(|(name, value)| rejected_headers.get(name) != Some(value))
})
})
}
}
impl ReloadingChatgptAuthProvider {
async fn resolve(&self, rejected_headers: Option<HeaderMap>) -> Result<HeaderMap, AuthError> {
let guard = Arc::clone(&self.resolution_lock)
.acquire_owned()
.await
.map_err(|_| {
AuthError::Transient("executor authentication is unavailable".to_string())
})?;
let provider = self.clone();
// A failed read leaves AuthManager's last known owner intact. Keep those
// cached credentials unavailable to synchronous callers until revalidated.
provider.headers_valid.store(false, Ordering::Release);
// Outlive a cancelled handshake, but never hold the permit indefinitely.
// A timed-out OAuth exchange may already have rotated the token: quarantine
// that generation until another login/refresh supplies different credentials.
tokio::spawn(async move {
let _guard = guard;
let result = tokio::time::timeout(Duration::from_secs(30), async {
provider
.auth_manager
.reload_with_cached_auth()
.await
.map_err(|_| {
AuthError::Transient(
"executor credential storage or hydration is unavailable".to_string(),
)
})?;
let current = provider.checked_headers(provider.auth_manager.auth_cached())?;
if provider.unidentified_headers.is_some() {
return Ok(current);
}
if let Some(rejected) = rejected_headers {
if !current
.iter()
.any(|(name, value)| rejected.get(name) != Some(value))
{
provider.managed.refresh_after_unauthorized(rejected).await;
}
} else {
provider.auth_manager.auth().await;
}
// auth() deliberately falls back to a snapshot on refresh errors.
// Re-read the selected source before dispatch: logout, replacement,
// or failed hydration must never authorize that fallback snapshot.
provider
.auth_manager
.reload_with_cached_auth()
.await
.map_err(|_| {
AuthError::Transient(
"executor credential storage or hydration is unavailable".to_string(),
)
})?;
provider.checked_headers(provider.auth_manager.auth_cached())
})
.await;
let result = match result {
Ok(result) => result,
Err(_) => Err(AuthError::Transient(
"executor credential resolution timed out; retry after repairing credentials"
.to_string(),
)),
};
provider
.headers_valid
.store(result.is_ok(), Ordering::Release);
result
})
.await
.map_err(|_| AuthError::Transient("executor authentication task failed".to_string()))?
}
}
/// Creates a SigV4 provider, preferring an explicit profile over the default credential chain.
pub(super) async fn aws_sigv4_auth_provider(

View File

@@ -1,565 +1,14 @@
#![cfg(test)]
use std::path::Path;
use std::time::Duration;
use codex_aws_auth::AwsAccessKeys;
use codex_login::AuthCredentialsStoreMode;
use codex_login::AuthKeyringBackendKind;
use http::HeaderValue;
use http::Method;
use pretty_assertions::assert_eq;
use tempfile::TempDir;
use wiremock::Mock;
use wiremock::MockServer;
use wiremock::ResponseTemplate;
use wiremock::matchers::header;
use wiremock::matchers::method;
use wiremock::matchers::path;
use super::*;
fn write_chatgpt_auth(codex_home: &Path, token: &str, account: &str) {
write_chatgpt_auth_with_claims(
codex_home,
token,
Some(account),
serde_json::json!({"chatgpt_user_id": "user", "chatgpt_plan_type": "pro"}),
);
}
fn write_chatgpt_auth_with_claims(
codex_home: &Path,
token: &str,
account: Option<&str>,
claims: serde_json::Value,
) {
let id_token = jsonwebtoken::encode(
&jsonwebtoken::Header::default(),
&serde_json::json!({"https://api.openai.com/auth": claims}),
&jsonwebtoken::EncodingKey::from_secret(b"executor-auth-test"),
)
.unwrap();
std::fs::write(
codex_home.join("auth.json"),
serde_json::to_vec(&serde_json::json!({
"auth_mode": "chatgpt",
"tokens": {
"id_token": id_token,
"access_token": token,
"refresh_token": "test-refresh-token",
"account_id": account,
},
"last_refresh": chrono::Utc::now(),
}))
.unwrap(),
)
.unwrap();
}
async fn stored_chatgpt_provider(codex_home: &Path) -> SharedAuthProvider {
let auth_manager = Arc::new(
AuthManager::new(
codex_home.to_path_buf(),
/*enable_codex_api_key_env*/ false,
AuthCredentialsStoreMode::File,
/*forced_chatgpt_workspace_id*/ None,
/*chatgpt_base_url*/ None,
AuthKeyringBackendKind::default(),
codex_login::test_support::transport_default_auth_route_config(),
)
.await,
);
let initial_auth = auth_manager.auth_cached().expect("load initial auth");
chatgpt_auth_provider(auth_manager, &initial_auth).unwrap()
}
#[tokio::test]
async fn chatgpt_executor_follows_disk_rotation_without_restart() {
let home = TempDir::new().unwrap();
write_chatgpt_auth(home.path(), "initial-token", "account");
let provider = stored_chatgpt_provider(home.path()).await;
for token in ["initial-token", "rotated-token", "rotated-again"] {
write_chatgpt_auth(home.path(), token, "account");
let headers = provider.apply_auth_headers(HeaderMap::new()).await.unwrap();
let mut expected = HeaderMap::new();
expected.insert(
http::header::AUTHORIZATION,
HeaderValue::from_str(&format!("Bearer {token}")).unwrap(),
);
expected.insert("ChatGPT-Account-ID", HeaderValue::from_static("account"));
assert_eq!(headers, expected);
}
}
#[tokio::test]
async fn chatgpt_executor_rejects_account_switch_and_recovers_original_identity() {
let home = TempDir::new().unwrap();
write_chatgpt_auth(home.path(), "initial-token", "account");
let provider = stored_chatgpt_provider(home.path()).await;
let rejected = provider.to_auth_headers().unwrap();
write_chatgpt_auth(home.path(), "other-token", "other-account");
assert!(!provider.refresh_after_unauthorized(rejected.clone()).await);
assert!(provider.apply_auth_headers(HeaderMap::new()).await.is_err());
assert!(provider.to_auth_headers().is_err());
write_chatgpt_auth(home.path(), "restored-token", "account");
assert!(provider.refresh_after_unauthorized(rejected).await);
assert_eq!(
provider.apply_auth_headers(HeaderMap::new()).await.unwrap()[http::header::AUTHORIZATION],
"Bearer restored-token"
);
}
#[tokio::test]
async fn chatgpt_executor_does_not_reuse_credentials_after_logout() {
let home = TempDir::new().unwrap();
write_chatgpt_auth(home.path(), "initial-token", "account");
let provider = stored_chatgpt_provider(home.path()).await;
std::fs::remove_file(home.path().join("auth.json")).unwrap();
assert!(provider.apply_auth_headers(HeaderMap::new()).await.is_err());
assert!(provider.to_auth_headers().is_err());
}
#[tokio::test]
async fn executor_rejects_incomplete_or_empty_bearer() {
for missing_refresh in [true, false] {
let home = TempDir::new().unwrap();
write_chatgpt_auth(home.path(), "initial", "account");
let provider = stored_chatgpt_provider(home.path()).await;
let path = home.path().join("auth.json");
let mut doc: serde_json::Value =
serde_json::from_slice(&std::fs::read(&path).unwrap()).unwrap();
if missing_refresh {
doc.as_object_mut().unwrap().remove("last_refresh");
} else {
doc["tokens"]["access_token"] = serde_json::json!("");
}
std::fs::write(path, serde_json::to_vec(&doc).unwrap()).unwrap();
assert!(matches!(
provider.apply_auth_headers(HeaderMap::new()).await,
Err(AuthError::Build(_))
));
}
}
#[tokio::test]
async fn chatgpt_executor_rejects_rotation_with_incomplete_identity() {
for (account, user) in [
(Some("account"), None),
(None, Some("user")),
(Some("account"), Some("")),
(Some(" "), Some("user")),
] {
let home = TempDir::new().unwrap();
let claims = serde_json::json!({"chatgpt_user_id": user});
write_chatgpt_auth_with_claims(home.path(), "initial", account, claims.clone());
let provider = stored_chatgpt_provider(home.path()).await;
assert!(provider.apply_auth_headers(HeaderMap::new()).await.is_ok());
assert!(
!provider
.refresh_after_unauthorized(provider.to_auth_headers().unwrap())
.await
);
write_chatgpt_auth_with_claims(home.path(), "replacement", account, claims);
assert!(provider.apply_auth_headers(HeaderMap::new()).await.is_err());
assert!(provider.to_auth_headers().is_err());
}
}
#[tokio::test]
async fn chatgpt_executor_rejects_changed_user_and_workspace_policy() {
for claims in [
serde_json::json!({"chatgpt_user_id": "other-user", "chatgpt_plan_type": "pro"}),
serde_json::json!({"chatgpt_user_id": "user", "chatgpt_plan_type": "enterprise"}),
serde_json::json!({"chatgpt_user_id": "user", "chatgpt_plan_type": "pro", "chatgpt_account_is_fedramp": true}),
] {
let home = TempDir::new().unwrap();
write_chatgpt_auth(home.path(), "initial", "account");
let provider = stored_chatgpt_provider(home.path()).await;
write_chatgpt_auth_with_claims(home.path(), "replacement", Some("account"), claims);
assert!(provider.apply_auth_headers(HeaderMap::new()).await.is_err());
assert!(provider.to_auth_headers().is_err());
}
}
#[tokio::test]
async fn chatgpt_executor_rejects_same_owner_agent_identity() {
let home = TempDir::new().unwrap();
write_chatgpt_auth_with_claims(
home.path(),
"initial",
Some("test-account-id"),
serde_json::json!({"chatgpt_user_id": "test-user-id", "chatgpt_plan_type": "pro"}),
);
let provider = stored_chatgpt_provider(home.path()).await;
let agent_manager = codex_login::test_support::auth_manager_with_agent_identity()
.await
.unwrap();
let Some(CodexAuth::AgentIdentity(agent)) = agent_manager.auth_cached() else {
panic!("expected agent identity fixture");
};
std::fs::write(
home.path().join("auth.json"),
serde_json::to_vec(&serde_json::json!({
"auth_mode": codex_protocol::auth::AuthMode::AgentIdentity,
"agent_identity": agent.record(),
}))
.unwrap(),
)
.unwrap();
// Ensure the fixture is a valid, loadable credential for the same owner.
assert!(matches!(
CodexAuth::from_auth_storage(
home.path(),
AuthCredentialsStoreMode::File,
/*chatgpt_base_url*/ None,
AuthKeyringBackendKind::default(),
&codex_login::test_support::transport_default_auth_route_config(),
)
.await
.unwrap(),
Some(CodexAuth::AgentIdentity(_))
));
assert!(provider.apply_auth_headers(HeaderMap::new()).await.is_err());
assert!(provider.to_auth_headers().is_err());
}
#[tokio::test]
async fn executor_concurrent_rotation_checks_identity_before_refresh() {
const CHILD_MODE: &str = "CODEX_EXECUTOR_AUTH_TEST_MODE";
let Ok(mode) = std::env::var(CHILD_MODE) else {
// Run in subprocesses so endpoint overrides cannot affect parallel tests.
let server = MockServer::start().await;
for token in [
"at-initial",
"at-rotated",
"at-retry-original",
"at-retry-new",
] {
Mock::given(method("GET"))
.and(path("/v1/user-auth-credential/whoami"))
.and(header("authorization", format!("Bearer {token}")))
.respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
"chatgpt_user_id": "user",
"chatgpt_account_id": "account",
"chatgpt_plan_type": "pro",
"chatgpt_account_is_fedramp": false,
})))
.with_priority(2)
.up_to_n_times(1)
.expect(1)
.mount(&server)
.await;
}
Mock::given(method("GET"))
.and(path("/v1/user-auth-credential/whoami"))
.and(header("authorization", "Bearer at-retry-new"))
.respond_with(ResponseTemplate::new(503))
.with_priority(1)
.up_to_n_times(1)
.expect(1)
.mount(&server)
.await;
Mock::given(method("POST"))
.and(path("/oauth/token"))
.respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
"access_token": "refreshed",
"refresh_token": "refreshed-refresh-token",
})))
.with_priority(1)
.up_to_n_times(1)
.expect(1)
.mount(&server)
.await;
Mock::given(wiremock::matchers::any())
.respond_with(ResponseTemplate::new(503))
.with_priority(10)
.expect(0)
.mount(&server)
.await;
for mode in ["pat", "chatgpt", "agent", "kind-switch", "pat-outage"] {
let mut child = tokio::process::Command::new(std::env::current_exe().unwrap());
child
.args([
"--exact",
"exec_server_auth::tests::executor_concurrent_rotation_checks_identity_before_refresh",
"--nocapture",
])
.env(CHILD_MODE, mode)
.env_remove("CODEX_ACCESS_TOKEN")
.env("NO_PROXY", "127.0.0.1,localhost")
.env("no_proxy", "127.0.0.1,localhost")
.env("CODEX_AUTHAPI_BASE_URL", server.uri())
.env("CODEX_AGENT_IDENTITY_AUTHAPI_BASE_URL", server.uri())
.env("CODEX_REFRESH_TOKEN_URL_OVERRIDE", format!("{}/oauth/token", server.uri()))
.kill_on_drop(true);
let output = tokio::time::timeout(Duration::from_secs(30), child.output())
.await
.unwrap()
.unwrap();
assert!(output.status.success(), "{output:?}");
assert!(String::from_utf8_lossy(&output.stdout).contains("1 passed"));
}
server.verify().await;
return;
};
let home = TempDir::new().unwrap();
let auth_path = home.path().join("auth.json");
if mode == "pat" || mode == "pat-outage" {
let token = if mode == "pat" {
"at-initial"
} else {
"at-retry-original"
};
std::fs::write(
&auth_path,
serde_json::to_vec(&serde_json::json!({"personal_access_token":token})).unwrap(),
)
.unwrap();
} else {
write_chatgpt_auth(home.path(), "initial", "account");
}
let provider = stored_chatgpt_provider(home.path()).await;
if mode == "pat-outage" {
std::fs::write(&auth_path, r#"{"personal_access_token":"at-retry-new"}"#).unwrap();
assert!(matches!(
provider.apply_auth_headers(HeaderMap::new()).await,
Err(AuthError::Transient(_))
));
assert!(provider.to_auth_headers().is_err());
let headers = provider.apply_auth_headers(HeaderMap::new()).await.unwrap();
assert_eq!(headers[http::header::AUTHORIZATION], "Bearer at-retry-new");
assert_eq!(provider.to_auth_headers().unwrap(), headers);
return;
}
if mode == "agent" || mode == "kind-switch" {
if mode == "agent" {
let manager = codex_login::test_support::auth_manager_with_agent_identity()
.await
.unwrap();
let Some(CodexAuth::AgentIdentity(agent)) = manager.auth_cached() else {
panic!("agent fixture");
};
let mut record = agent.record().clone();
record.task_id = None;
std::fs::write(&auth_path, serde_json::to_vec(&serde_json::json!({"auth_mode":codex_protocol::auth::AuthMode::AgentIdentity, "agent_identity":record})).unwrap()).unwrap();
} else {
std::fs::write(&auth_path, r#"{"personal_access_token":"at-initial"}"#).unwrap();
}
assert!(matches!(
provider.apply_auth_headers(HeaderMap::new()).await,
Err(AuthError::Build(_))
));
return;
}
let expected_bearer = if mode == "pat" {
std::fs::write(&auth_path, r#"{"personal_access_token":"at-rotated"}"#).unwrap();
"Bearer at-rotated"
} else {
let expired = jsonwebtoken::encode(
&jsonwebtoken::Header::default(),
&serde_json::json!({"exp": chrono::Utc::now().timestamp() - 60}),
&jsonwebtoken::EncodingKey::from_secret(b"executor-auth-test"),
)
.unwrap();
for (account, user) in [
("other-account", "user"),
("account", "other-user"),
("account", "user"),
] {
write_chatgpt_auth_with_claims(
home.path(),
&expired,
Some(account),
serde_json::json!({"chatgpt_user_id": user, "chatgpt_plan_type": "pro"}),
);
let original = std::fs::read(&auth_path).unwrap();
if account != "account" || user != "user" {
assert!(provider.apply_auth_headers(HeaderMap::new()).await.is_err());
assert_eq!(std::fs::read(&auth_path).unwrap(), original);
}
}
"Bearer refreshed"
};
let barrier = Arc::new(tokio::sync::Barrier::new(32));
let results = futures::future::join_all((0..32).map(|_| {
let provider = Arc::clone(&provider);
let barrier = Arc::clone(&barrier);
async move {
barrier.wait().await;
provider.apply_auth_headers(HeaderMap::new()).await
}
}))
.await;
for result in results {
assert_eq!(
result.unwrap()[http::header::AUTHORIZATION],
expected_bearer
);
}
}
#[tokio::test]
async fn executor_refresh_survives_cancelled_caller() {
use std::process::Stdio;
use tokio::io::AsyncBufReadExt;
use tokio::io::AsyncReadExt;
use tokio::io::AsyncWriteExt;
const CHILD_MODE: &str = "CODEX_EXECUTOR_CANCEL_TEST_MODE";
let Ok(mode) = std::env::var(CHILD_MODE) else {
// Isolate endpoint overrides; release the response only after the caller
// is cancelled, with no sleep or race against the HTTP response.
for mode in ["proactive", "recovery", "logout", "replacement", "timeout"] {
// Allow process startup and teardown around the 30-second refresh deadline.
tokio::time::timeout(Duration::from_secs(90), async {
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
let endpoint = format!("http://{}/token", listener.local_addr().unwrap());
let mut child = tokio::process::Command::new(std::env::current_exe().unwrap())
.args(["--exact", "exec_server_auth::tests::executor_refresh_survives_cancelled_caller", "--nocapture"])
.env(CHILD_MODE, mode)
.env_remove("CODEX_ACCESS_TOKEN")
.env("CODEX_REFRESH_TOKEN_URL_OVERRIDE", endpoint)
.env("NO_PROXY", "127.0.0.1,localhost")
.env("no_proxy", "127.0.0.1,localhost")
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.kill_on_drop(true)
.spawn().unwrap();
let (mut stream, _) = listener.accept().await.unwrap();
let mut request = tokio::io::BufReader::new(&mut stream);
let mut content_length = 0;
loop {
let mut line = String::new();
assert!(request.read_line(&mut line).await.unwrap() > 0);
if line == "\r\n" { break; }
if let Some(length) = line.to_ascii_lowercase().strip_prefix("content-length:") {
content_length = length.trim().parse().unwrap();
}
}
request.read_exact(&mut vec![0; content_length]).await.unwrap();
drop(request);
child.stdin.take().unwrap().write_all(b"cancel\n").await.unwrap();
let mut stdout = tokio::io::BufReader::new(child.stdout.take().unwrap());
loop {
let mut line = String::new();
assert!(stdout.read_line(&mut line).await.unwrap() > 0);
if line.contains("CALLER_CANCELLED") { break; }
}
let body = r#"{"access_token":"rotated-access","refresh_token":"rotated-refresh"}"#;
if mode != "timeout" {
stream.write_all(format!(
"HTTP/1.1 200 OK\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{body}", body.len()
).as_bytes()).await.unwrap();
}
// Keep the unanswered timeout connection open until the child finishes.
let mut remaining = String::new();
stdout.read_to_string(&mut remaining).await.unwrap();
let output = child.wait_with_output().await.unwrap();
assert!(output.status.success(), "{remaining}\n{output:?}");
assert!(remaining.contains("1 passed"), "{remaining}");
}).await.expect("cancelled refresh must finish persisting");
}
return;
};
let home = TempDir::new().unwrap();
let token = if mode != "recovery" {
jsonwebtoken::encode(
&jsonwebtoken::Header::default(),
&serde_json::json!({"exp": chrono::Utc::now().timestamp() - 60}),
&jsonwebtoken::EncodingKey::from_secret(b"executor-auth-test"),
)
.unwrap()
} else {
"initial-access".to_string()
};
write_chatgpt_auth(home.path(), &token, "account");
let provider = stored_chatgpt_provider(home.path()).await;
let caller = Arc::clone(&provider);
let rejected = provider.to_auth_headers().unwrap();
let refresh_mode = mode.clone();
let task = tokio::spawn(async move {
if refresh_mode != "recovery" {
caller.apply_auth_headers(HeaderMap::new()).await.unwrap();
} else {
assert!(caller.refresh_after_unauthorized(rejected).await);
}
});
let mut instruction = String::new();
tokio::io::BufReader::new(tokio::io::stdin())
.read_line(&mut instruction)
.await
.unwrap();
assert_eq!(instruction, "cancel\n");
task.abort();
assert!(task.await.unwrap_err().is_cancelled());
if mode == "logout" {
std::fs::remove_file(home.path().join("auth.json")).unwrap();
} else if mode == "replacement" {
write_chatgpt_auth(home.path(), "fresh-external-access", "account");
let path = home.path().join("auth.json");
let mut stored: serde_json::Value =
serde_json::from_slice(&std::fs::read(&path).unwrap()).unwrap();
stored["tokens"]["refresh_token"] = serde_json::json!("externally-rotated-refresh");
std::fs::write(path, serde_json::to_vec(&stored).unwrap()).unwrap();
}
println!("CALLER_CANCELLED");
if mode == "timeout" {
tokio::time::pause();
tokio::time::advance(Duration::from_secs(31)).await;
tokio::time::resume();
assert!(matches!(
provider.apply_auth_headers(HeaderMap::new()).await,
Err(AuthError::Transient(_))
));
// Repair is usable without releasing the old response or restarting the executor.
write_chatgpt_auth(home.path(), "fresh-external-access", "account");
}
if mode == "logout" {
assert!(matches!(
provider.apply_auth_headers(HeaderMap::new()).await,
Err(AuthError::Build(_))
));
assert!(!home.path().join("auth.json").exists());
return;
}
let repaired = mode == "replacement" || mode == "timeout";
// This waits on the same permit, proving the detached operation kept it
// until both persistence and cache update were complete.
let headers = provider.apply_auth_headers(HeaderMap::new()).await.unwrap();
assert_eq!(
headers[http::header::AUTHORIZATION],
if repaired {
"Bearer fresh-external-access"
} else {
"Bearer rotated-access"
}
);
let stored: serde_json::Value =
serde_json::from_slice(&std::fs::read(home.path().join("auth.json")).unwrap()).unwrap();
assert_eq!(
stored["tokens"]["access_token"],
if repaired {
"fresh-external-access"
} else {
"rotated-access"
}
);
assert_eq!(
stored["tokens"]["refresh_token"],
match mode.as_str() {
"replacement" => "externally-rotated-refresh",
"timeout" => "test-refresh-token",
_ => "rotated-refresh",
}
);
}
async fn test_provider() -> AwsSigV4AuthProvider {
let context = AwsAuthContext::load_with_access_keys(
AwsAuthConfig {

View File

@@ -89,10 +89,6 @@ Options:
--name <NAME>
Human-readable environment name
--use-agent-identity-auth
Use Agent Identity auth from CODEX_ACCESS_TOKEN for remote
registration
--registration-bearer-stdin
Read one registration bearer from stdin
@@ -100,12 +96,6 @@ Options:
Read rotating executor credentials from stdin; exit when the pipe
closes
--chatgpt-token-stdin
Read a ChatGPT access token from stdin for HTTPS registration
--chatgpt-account-id <ID>
ChatGPT account ID. Must match the token's account claim when present
--aws-sigv4
Sign Direct registration and WebSocket handshake requests with AWS
SigV4