Skip to content

fix(codex): clear stale official auth API key#3904

Open
hhh2210 wants to merge 2 commits into
farion1231:mainfrom
hhh2210:fix/codex-official-clear-api-key
Open

fix(codex): clear stale official auth API key#3904
hhh2210 wants to merge 2 commits into
farion1231:mainfrom
hhh2210:fix/codex-official-clear-api-key

Conversation

@hhh2210

@hhh2210 hhh2210 commented Jun 8, 2026

Copy link
Copy Markdown

Summary / 概述

Fixes a Codex OpenAI Official provider save path where clearing OPENAI_API_KEY from the auth JSON editor did not remove the stale key from live ~/.codex/auth.json.

The official-provider branch previously skipped writing auth.json when the submitted auth had no login material. That made an intentional clear a no-op for the live auth file, so reopening the active provider read the old key back from live settings.

This change handles official providers separately:

  • If the submitted official auth has login material, keep the existing full auth write behavior.
  • If it has no login material, read the existing live auth and remove only OPENAI_API_KEY.
  • Preserve existing OAuth token material while still writing the updated config.
  • Keep third-party provider behavior unchanged.

Related Issue / 关联 Issue

Fixes #3903

Screenshots / 截图

N/A; backend persistence fix.

Checklist / 检查清单

  • pnpm typecheck passes / 通过 TypeScript 类型检查 (not run; Rust-only change)
  • pnpm format:check passes / 通过代码格式检查 (not run; Rust-only change)
  • cargo clippy passes (if Rust code changed) / 通过 Clippy 检查(如修改了 Rust 代码)
  • Updated i18n files if user-facing text changed / 如修改了用户可见文本,已更新国际化文件 (not needed; no user-facing text changed)

Validation

  • cargo fmt --check
  • cargo test official_provider_empty_auth_clears_stale_live_api_key_without_dropping_oauth
  • cargo test codex_config::tests::
  • cargo clippy --all-targets

Note: cargo clippy --all-targets exits successfully. It still reports three pre-existing field_reassign_with_default warnings in unrelated modules (claude_desktop_config.rs and services/proxy.rs).

Copilot AI review requested due to automatic review settings June 8, 2026 12:54

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

This PR refines provider-switching behavior for the “official” Codex provider to avoid unintentionally persisting a stale OPENAI_API_KEY while keeping existing OAuth tokens intact.

Changes:

  • Routes official-provider writes through a dedicated write_codex_official_live_for_provider helper.
  • Adds logic to strip a stale OPENAI_API_KEY from existing auth.json when switching to official with empty auth.
  • Introduces a serial test with a temp home-dir guard to validate the stale-key removal behavior.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +1285 to +1304
impl TestHomeGuard {
fn new() -> Self {
let dir = tempfile::tempdir().expect("create temp home");
let old_test_home = std::env::var_os("CC_SWITCH_TEST_HOME");
std::env::set_var("CC_SWITCH_TEST_HOME", dir.path());
Self {
_dir: dir,
old_test_home,
}
}
}

impl Drop for TestHomeGuard {
fn drop(&mut self) {
match &self.old_test_home {
Some(value) => std::env::set_var("CC_SWITCH_TEST_HOME", value),
None => std::env::remove_var("CC_SWITCH_TEST_HOME"),
}
}
}
Comment on lines +1065 to +1084
fn write_codex_official_live_for_provider(
auth: &Value,
config_text: Option<&str>,
) -> Result<(), AppError> {
if codex_auth_has_login_material(auth) {
return write_codex_live_atomic(auth, config_text);
}

let auth_path = get_codex_auth_path();
if auth_path.exists() {
let mut live_auth: Value = read_json_file(&auth_path)?;
if let Some(live_auth_obj) = live_auth.as_object_mut() {
if live_auth_obj.remove("OPENAI_API_KEY").is_some() {
return write_codex_live_atomic(&live_auth, config_text);
}
}
}

write_codex_live_config_atomic(config_text)
}
@hhh2210

hhh2210 commented Jun 8, 2026

Copy link
Copy Markdown
Author

cc @farion1231 because this PR fixes a small Codex official auth persistence bug and is currently blocked waiting for maintainer-side workflow/review action.

The change is intentionally narrow: when the OpenAI Official Codex provider is saved with empty auth, it removes only stale OPENAI_API_KEY from live auth.json while preserving OAuth tokens. Local Rust validation is in the PR body.

Switching to an official Codex provider with empty auth was removing the
OPENAI_API_KEY entry even when it was already null, breaking the existing
integration test that expects a null key to be preserved. Guard the removal
so it only triggers for a real (non-null) stale key.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Codex official provider does not clear stale OPENAI_API_KEY from auth.json

2 participants