Skip to content

A cross-platform desktop All-in-One assistant for Claude Code, Codex, OpenCode,DeepSeek Harness and more. Only official website: ccswitch.io

fix(proxy): preserve Codex session headers in aggregation (#10155)

NostalgiaJohn6e53984caa20
diff --git a/src-tauri/src/proxy/forwarder.rs b/src-tauri/src/proxy/forwarder.rs
index fd2f5d452..7e63cf5f1 100644
--- a/src-tauri/src/proxy/forwarder.rs
+++ b/src-tauri/src/proxy/forwarder.rs
@@ -2521,11 +2521,8 @@ impl RequestForwarder {
codex_official_auth_passthrough,
codex_impersonate_claude_code,
codex_responses_to_anthropic,
- codex_responses_to_chat,
- codex_stack_request,
codex_unmanaged_upstream: matches!(app_type, AppType::Codex)
&& !provider.is_codex_oauth(),
- effective_endpoint: &effective_endpoint,
force_identity_encoding,
is_copilot,
custom_user_agent,
diff --git a/src-tauri/src/proxy/forwarder/headers.rs b/src-tauri/src/proxy/forwarder/headers.rs
index 3dfc633bf..476a1633d 100644
--- a/src-tauri/src/proxy/forwarder/headers.rs
+++ b/src-tauri/src/proxy/forwarder/headers.rs
@@ -95,24 +95,13 @@ pub(super) fn is_codex_client_fingerprint_header(key_str: &str) -> bool {
/// Decide whether a Codex fingerprint header must be removed before the request
/// reaches an upstream.
///
-/// Stack requests to a third party still must not carry ChatGPT session or
-/// account identity. A native Responses third party, however, may use the
-/// non-identity `x-codex-beta-features` marker to recognize the Codex protocol
-/// and reject requests without it. Keep only that marker; all other Codex and
-/// OpenAI identity headers remain stripped. Converted Chat/Anthropic requests
-/// continue to receive a clean client fingerprint.
-///
-/// Any other Codex request to a third party (`codex_unmanaged_upstream`: Codex and
-/// not a CC Switch-managed ChatGPT account) drops only the ChatGPT account identity
-/// ([`is_chatgpt_identity_header`]): the client may be the built-in `openai`
-/// provider carrying the user's ChatGPT login. Session and client headers stay,
-/// so requests from a plain custom table are unchanged.
+/// Anthropic conversion removes the Codex client fingerprint. Otherwise, Routing
+/// and Stack requests to third parties drop only ChatGPT account identity
+/// ([`is_chatgpt_identity_header`]). Session and client headers stay available for
+/// cache routing and protocol negotiation in both modes.
pub(super) fn should_strip_codex_client_fingerprint_header(
key_str: &str,
- endpoint: &str,
codex_responses_to_anthropic: bool,
- codex_responses_to_chat: bool,
- codex_stack_request: bool,
codex_official_auth_passthrough: bool,
codex_unmanaged_upstream: bool,
) -> bool {
@@ -120,13 +109,6 @@ pub(super) fn should_strip_codex_client_fingerprint_header(
return is_codex_client_fingerprint_header(key_str);
}
- if codex_stack_request && !codex_official_auth_passthrough {
- let native_third_party_responses = !codex_responses_to_chat
- && crate::proxy::providers::is_codex_responses_endpoint(endpoint);
- return is_codex_client_fingerprint_header(key_str)
- && !(native_third_party_responses && key_str == "x-codex-beta-features");
- }
-
codex_unmanaged_upstream
&& !codex_official_auth_passthrough
&& is_chatgpt_identity_header(key_str)
@@ -416,11 +398,8 @@ pub(super) struct OutboundHeaderPlan<'a> {
pub(super) codex_official_auth_passthrough: bool,
pub(super) codex_impersonate_claude_code: bool,
pub(super) codex_responses_to_anthropic: bool,
- pub(super) codex_responses_to_chat: bool,
- pub(super) codex_stack_request: bool,
/// A Codex request whose upstream is not a managed ChatGPT account.
pub(super) codex_unmanaged_upstream: bool,
- pub(super) effective_endpoint: &'a str,
pub(super) force_identity_encoding: bool,
pub(super) is_copilot: bool,
pub(super) custom_user_agent: Option<http::HeaderValue>,
@@ -526,24 +505,12 @@ impl OutboundHeaderPlan<'_> {
continue;
}
- // --- Codex/OpenAI fingerprint headers — never leak to an Anthropic upstream ---
- // These are client/session identifiers from the incoming Codex request,
- // not Anthropic protocol headers. Forwarding them both leaks identity and
- // can defeat strict gateway fingerprint checks.
- // The full set lives in `is_codex_client_fingerprint_header` so it stays in one
- // place. (HeaderName is lowercased by the http crate, so a direct match is safe.)
- // Codex Stack 请求发往第三方时同样剥掉:官方做路由时 Codex 每个请求都带着 ChatGPT
- // 身份(`chatgpt-account-id` 等),它们只能发往官方上游。Stack 目标不会是官方账号,
- // 这里仍按上游判断,防止以后放宽。只限 Codex:Claude Code 自己的 `x-stainless-*`
- // 是 Anthropic SDK 的正常请求头,Stack 请求照常转发。
- // 路由那家是第三方时 Codex 也可能以内置 openai 身份、带着 ChatGPT 登录发请求
- // (`codex_direct` 的 `openai_route`),身份头同样只能发往官方或 ChatGPT 托管账号。
+ // Anthropic conversion drops the Codex fingerprint; Routing and Stack
+ // requests to other third-party upstreams drop only ChatGPT account
+ // identity, preserving session headers used for cache affinity.
if should_strip_codex_client_fingerprint_header(
key_str,
- self.effective_endpoint,
self.codex_responses_to_anthropic,
- self.codex_responses_to_chat,
- self.codex_stack_request,
self.codex_official_auth_passthrough,
self.codex_unmanaged_upstream,
) {
diff --git a/src-tauri/src/proxy/forwarder/tests.rs b/src-tauri/src/proxy/forwarder/tests.rs
index 242590e1f..e22d684e2 100644
--- a/src-tauri/src/proxy/forwarder/tests.rs
+++ b/src-tauri/src/proxy/forwarder/tests.rs
@@ -3149,11 +3149,19 @@ mod codex_stack {
("chatgpt-account-id", "acct-workspace"),
("originator", "codex_cli_rs"),
("session_id", "session-1"),
+ ("session-id", "session-1"),
+ ("thread-id", "thread-1"),
+ ("conversation_id", "conversation-1"),
+ ("x-client-request-id", "request-1"),
("x-codex-beta-features", "remote_compaction_v2"),
("x-codex-turn-state", "state"),
("openai-beta", "responses=experimental"),
("x-oai-attestation", "device-proof"),
("x-openai-fedramp", "true"),
+ ("openai-organization", "org-1"),
+ ("openai-project", "project-1"),
+ ("version", "codex-version"),
+ ("x-responsesapi-include-timing-metrics", "true"),
] {
headers.insert(
http::HeaderName::from_static(name),
@@ -3342,102 +3350,100 @@ mod codex_stack {
}
#[tokio::test]
- async fn stacked_requests_drop_chatgpt_identity_but_keep_native_responses_beta_marker() {
+ async fn routing_and_stack_preserve_codex_session_headers_for_third_parties() {
let upstream = upstream().await;
- for api_format in ["openai_responses", "openai_chat", "anthropic"] {
- let seen = send(
- &forwarder(true),
- &upstream,
- provider(&upstream, api_format),
- "/responses",
- body("listed", json!([])),
- )
- .await;
- for name in [
- "chatgpt-account-id",
- "originator",
- "session_id",
- "x-codex-turn-state",
- "openai-beta",
- "x-oai-attestation",
- "x-openai-fedramp",
- ] {
- assert!(
- !seen.headers.contains_key(name),
- "{api_format}: {name} reached a third party"
+ let incoming = chatgpt_headers();
+ for api_format in ["openai_responses", "openai_chat"] {
+ for stack in [false, true] {
+ let mut target = provider(&upstream, api_format);
+ // Unknown Chat gateways require an explicit opt-in for this body field.
+ target.meta.as_mut().unwrap().prompt_cache_routing = Some("enabled".into());
+ let mut request = body("listed", json!([]));
+ request["prompt_cache_key"] = json!("cache-session-1");
+ let seen = send(&forwarder(stack), &upstream, target, "/responses", request).await;
+ for name in [
+ "session_id",
+ "session-id",
+ "thread-id",
+ "conversation_id",
+ "x-client-request-id",
+ "originator",
+ "x-codex-turn-state",
+ "x-codex-beta-features",
+ "openai-beta",
+ ] {
+ assert_eq!(
+ seen.headers.get(name),
+ Some(&incoming[name]),
+ "{api_format}, stack={stack}: {name} must retain its client value"
+ );
+ }
+ for name in [
+ "chatgpt-account-id",
+ "openai-organization",
+ "openai-project",
+ "version",
+ "x-oai-attestation",
+ "x-openai-fedramp",
+ "x-responsesapi-include-timing-metrics",
+ ] {
+ assert!(
+ !seen.headers.contains_key(name),
+ "{api_format}, stack={stack}: {name} reached a third party"
+ );
+ }
+ assert_eq!(
+ seen.headers["authorization"], "Bearer sk-third-party",
+ "{api_format}, stack={stack}: use the target provider's credential"
);
+ assert_eq!(seen.body["prompt_cache_key"], "cache-session-1");
}
- assert_eq!(
- seen.headers.contains_key("x-codex-beta-features"),
- api_format == "openai_responses",
- "{api_format}: protocol marker handling"
- );
- let auth = seen
- .headers
- .get("authorization")
- .or_else(|| seen.headers.get("x-api-key"))
- .and_then(|value| value.to_str().ok())
- .unwrap_or_default()
- .to_string();
- assert!(auth.contains("sk-third-party"), "{api_format}: {auth}");
- assert!(!auth.contains("chatgpt"), "{api_format}: {auth}");
}
+ }
- // Routing requests to a third party keep the session and client headers
- // (cache keys, protocol marker) but never the ChatGPT account identity:
- // the client may be the built-in `openai` provider carrying the login.
- for api_format in ["openai_responses", "openai_chat"] {
+ #[tokio::test]
+ async fn routing_and_stack_still_strip_codex_fingerprint_for_anthropic() {
+ let upstream = upstream().await;
+ for stack in [false, true] {
let seen = send(
- &forwarder(false),
+ &forwarder(stack),
&upstream,
- provider(&upstream, api_format),
+ provider(&upstream, "anthropic"),
"/responses",
body("listed", json!([])),
)
.await;
- for name in ["session_id", "originator", "x-codex-turn-state"] {
- assert!(seen.headers.contains_key(name), "{api_format}: {name}");
- }
for name in [
+ "session_id",
+ "session-id",
+ "thread-id",
+ "conversation_id",
+ "x-client-request-id",
+ "originator",
+ "x-codex-turn-state",
+ "x-codex-beta-features",
+ "openai-beta",
"chatgpt-account-id",
+ "openai-organization",
+ "openai-project",
+ "version",
"x-oai-attestation",
"x-openai-fedramp",
+ "x-responsesapi-include-timing-metrics",
] {
assert!(
!seen.headers.contains_key(name),
- "{api_format}: {name} reached a third party"
+ "stack={stack}: {name} reached an Anthropic upstream"
);
}
- let auth = seen
- .headers
- .get("authorization")
- .and_then(|value| value.to_str().ok())
- .unwrap_or_default();
- assert!(auth.contains("sk-third-party"), "{api_format}: {auth}");
+ assert_eq!(seen.headers["authorization"], "Bearer sk-third-party");
}
- let seen = send(
- &forwarder(false),
- &upstream,
- provider(&upstream, "openai_responses"),
- "/responses",
- body("listed", json!([])),
- )
- .await;
- assert!(seen.headers.contains_key("x-codex-beta-features"));
}
#[test]
fn chatgpt_identity_reaches_only_official_or_managed_account_upstreams() {
let strip = |key: &str, passthrough: bool, unmanaged: bool| {
- should_strip_codex_client_fingerprint_header(
- key,
- "/responses",
- false,
- false,
- false,
- passthrough,
- unmanaged,
- )
+ should_strip_codex_client_fingerprint_header(key, false, passthrough, unmanaged)
};
for key in ["chatgpt-account-id", "x-oai-attestation", "version"] {
assert!(strip(key, false, true), "{key} to a third party");