From 13fe2bcb7a3b1d57f6e67897c633a7d80d4918e7 Mon Sep 17 00:00:00 2001 From: alexsong-oai Date: Tue, 25 Aug 2026 20:49:15 +0000 Subject: [PATCH] Redact Bedrock API keys from debug output (#40706) ## Why Derived debug formatting exposed the managed Amazon Bedrock API key. ## What changed Implement custom `Debug` formatting for `BedrockApiKeyAuth` that replaces the API key with `` while retaining the region. ## Testing Add regression coverage for direct and `CodexAuth`-wrapped debug output. GitOrigin-RevId: 68920fa00a9189c959b0025f22797b32f53d1995 --- codex-rs/login/src/auth/bedrock_api_key.rs | 12 +++++++++++- codex-rs/login/src/auth/bedrock_api_key_tests.rs | 14 ++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/codex-rs/login/src/auth/bedrock_api_key.rs b/codex-rs/login/src/auth/bedrock_api_key.rs index cae6ddf548..a101aa74fe 100644 --- a/codex-rs/login/src/auth/bedrock_api_key.rs +++ b/codex-rs/login/src/auth/bedrock_api_key.rs @@ -1,3 +1,4 @@ +use std::fmt; use std::path::Path; use codex_config::types::AuthCredentialsStoreMode; @@ -10,12 +11,21 @@ use super::storage::AuthKeyringBackendKind; use codex_protocol::auth::AuthMode; /// Managed Amazon Bedrock API key persisted in `auth.json`. -#[derive(Deserialize, Serialize, Clone, Debug, PartialEq, Eq)] +#[derive(Deserialize, Serialize, Clone, PartialEq, Eq)] pub struct BedrockApiKeyAuth { pub api_key: String, pub region: String, } +impl fmt::Debug for BedrockApiKeyAuth { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("BedrockApiKeyAuth") + .field("api_key", &"") + .field("region", &self.region) + .finish() + } +} + /// Writes an `auth.json` that contains only the Amazon Bedrock API key auth. pub fn login_with_bedrock_api_key( codex_home: &Path, diff --git a/codex-rs/login/src/auth/bedrock_api_key_tests.rs b/codex-rs/login/src/auth/bedrock_api_key_tests.rs index 70764abff8..cdb3273213 100644 --- a/codex-rs/login/src/auth/bedrock_api_key_tests.rs +++ b/codex-rs/login/src/auth/bedrock_api_key_tests.rs @@ -44,6 +44,20 @@ fn bedrock_auth() -> BedrockApiKeyAuth { } } +#[test] +fn bedrock_api_key_debug_redacts_secret() { + let auth = bedrock_auth(); + + assert_eq!( + format!("{auth:?}"), + r#"BedrockApiKeyAuth { api_key: "", region: "us-east-1" }"# + ); + assert_eq!( + format!("{:?}", CodexAuth::BedrockApiKey(auth)), + r#"BedrockApiKey(BedrockApiKeyAuth { api_key: "", region: "us-east-1" })"# + ); +} + #[tokio::test] #[serial(codex_auth_env)] async fn login_with_bedrock_api_key_replaces_openai_auth() -> anyhow::Result<()> {