fix(openai): preserve account and Guardian header precedence

Co-authored-by: Codex <noreply@openai.com>
This commit is contained in:
Zyy
2026-11-20 21:02:57 +08:00
co-authored by Codex
parent 3709ed2484
commit 369f17d654
9 changed files with 205 additions and 54 deletions
@@ -2,11 +2,14 @@
use super::{MiddlewareEntry, PluginMiddlewarePlan};
use futures::future::BoxFuture;
use gateway_core::engine::extensions::ExtensionCallScope;
use gateway_core::engine::middleware::{
MiddlewareContext, MiddlewareError, MiddlewareMount, MiddlewareNext, MiddlewarePlan,
MiddlewareRequest, MiddlewareResponse,
};
use gateway_core::middleware::{Middleware, Next};
use gateway_core::policy::ClientApiKeyId;
use gateway_core::routing::{AccountGroupId, ProviderKind};
use std::sync::Arc;
// 协议适配只提供选择条件与单层调用;顺序、上下文捕获和续体拼接共用这一个入口
@@ -61,23 +64,22 @@ impl MiddlewarePlan for PluginMiddlewarePlan {
.flatten()
.all(|entry| {
let matches = |model| {
entry.scope.matches_processing(
entry.matches_processing(
route.extensions,
route.client_api_key,
route.account_groups,
Some(provider),
Some(model),
)
};
route.extensions.contains(&entry.instance_id)
|| !matches(route.model)
|| matches(target.as_str())
!matches(route.model) || matches(target.as_str())
})
}
fn has_outbound(&self, context: &MiddlewareContext) -> bool {
self.middleware
.get(&MiddlewareMount::Outbound)
.is_some_and(|entries| entries.iter().any(|entry| outbound_matches(entry, context)))
.is_some_and(|entries| entries.iter().any(|entry| matches_context(entry, context)))
}
fn handle_outbound(
@@ -92,7 +94,7 @@ impl MiddlewarePlan for PluginMiddlewarePlan {
self.chain(
MiddlewareMount::Outbound,
context,
outbound_matches,
matches_context,
|entry, context, request, next| {
Box::pin(super::outbound::invoke(entry, context, request, next))
},
@@ -183,15 +185,7 @@ impl MiddlewarePlan for PluginMiddlewarePlan {
self.chain(
context.mount(),
context,
|entry, context| {
!context.extension_scope().contains(&entry.instance_id)
&& entry.scope.matches_processing(
context.client_key_id(),
context.account_group_ids(),
context.provider(),
context.model(),
)
},
matches_context,
|entry, context, request, next| {
Box::pin(super::request::invoke(entry, context, request, next))
},
@@ -201,13 +195,26 @@ impl MiddlewarePlan for PluginMiddlewarePlan {
}
}
// 存在性检查与实际组合使用同一选择规则,未命中时不进入原生报文准备
fn outbound_matches(entry: &MiddlewareEntry, context: &MiddlewareContext) -> bool {
!context.extension_scope().contains(&entry.instance_id)
&& entry.scope.matches_processing(
context.client_key_id(),
context.account_group_ids(),
context.provider(),
context.model(),
)
impl MiddlewareEntry {
fn matches_processing(
&self,
extensions: &ExtensionCallScope,
key: &ClientApiKeyId,
groups: &[AccountGroupId],
provider: Option<&ProviderKind>,
model: Option<&str>,
) -> bool {
!extensions.contains(&self.instance_id)
&& self.scope.matches_processing(key, groups, provider, model)
}
}
fn matches_context(entry: &MiddlewareEntry, context: &MiddlewareContext) -> bool {
entry.matches_processing(
context.extension_scope(),
context.client_key_id(),
context.account_group_ids(),
context.provider(),
context.model(),
)
}
@@ -19,7 +19,7 @@ use super::{
},
diagnostics::CodexUpstreamDiagnostics,
endpoints::endpoint_url,
headers::{insert_optional_header, is_managed_identity_header},
headers::{insert_optional_header, is_managed_model_header},
response_meta,
};
@@ -40,7 +40,7 @@ impl CodexBackendClient {
)?;
for header in extra_protocol_headers {
let name = HeaderName::from_bytes(header.name().as_bytes())?;
if is_managed_identity_header(name.as_str())
if is_managed_model_header(&name, self.fedramp_routing)
|| is_transport_managed_request_header(name.as_str())
|| is_non_codex_request_header(name.as_str())
// Live 不继承 Responses 的路由或连接续接状态
@@ -266,11 +266,16 @@ impl CodexBackendClient {
}
}
// OAuth 账号声明 FedRAMP 时,由选中凭据独占这个头;普通账号允许客户端多值透传
pub(super) fn is_managed_model_header(name: &HeaderName, fedramp_routing: bool) -> bool {
is_managed_identity_header(name.as_str()) || (fedramp_routing && name == "x-openai-fedramp")
}
pub(super) fn append_passthrough_headers(headers: &mut HeaderMap, passthrough: &HeaderMap) {
let fedramp_routing = headers.contains_key("x-openai-fedramp");
for name in passthrough.keys() {
// 身份与传输字段只由画像/正文生成;其余协议头保留原始多值字节
if (name == "x-openai-fedramp" && headers.contains_key(name))
|| is_managed_identity_header(name.as_str())
if is_managed_model_header(name, fedramp_routing)
|| matches!(
name.as_str(),
"openai-beta"
@@ -13,6 +13,21 @@ use reqwest::header::HeaderMap;
use serde::Serialize;
use serde_json::{Map, Value};
// 同一来源的多个头照常透传,只有值一致才能用来决定本地传输策略
fn explicit_guardian_role<'a>(
first: &[u8],
mut remaining: impl Iterator<Item = &'a [u8]>,
) -> Option<CodexGuardianRole> {
if remaining.any(|value| value != first) {
return None;
}
match first {
b"reviewer" => Some(CodexGuardianRole::Reviewer),
b"classifier" => Some(CodexGuardianRole::Classifier),
_ => None,
}
}
/// 官方 Codex 客户端据此触发完整历史重放的稳定错误码
pub(crate) const PREVIOUS_RESPONSE_NOT_FOUND_CODE: &str = "previous_response_not_found";
/// Responses WebSocket 用于回传同一 turn 不透明状态的官方 client metadata 键
@@ -812,22 +827,24 @@ impl CodexResponsesRequest {
middleware_headers: &[MiddlewareHeader],
) -> Option<CodexGuardianRole> {
const HEADER: &str = "x-codex-guardian";
let header = middleware_headers
let mut overrides = middleware_headers
.iter()
.find(|header| header.name().eq_ignore_ascii_case(HEADER))
.map(|header| header.value().as_ref())
.or_else(|| self.passthrough_headers.get(HEADER).map(|v| v.as_bytes()));
match header {
Some(b"reviewer") => Some(CodexGuardianRole::Reviewer),
Some(b"classifier") => Some(CodexGuardianRole::Classifier),
Some(_) => None,
None if self.guardian_role == Some(CodexGuardianRole::Reviewer)
&& self.model() != CODEX_AUTO_REVIEW_MODEL =>
{
None
}
None => self.guardian_role,
.filter(|header| header.name().eq_ignore_ascii_case(HEADER))
.map(|header| header.value().as_ref());
if let Some(first) = overrides.next() {
return explicit_guardian_role(first, overrides);
}
let mut client = self
.passthrough_headers
.get_all(HEADER)
.iter()
.map(|v| v.as_bytes());
if let Some(first) = client.next() {
return explicit_guardian_role(first, client);
}
self.guardian_role.filter(|role| {
*role != CodexGuardianRole::Reviewer || self.model() == CODEX_AUTO_REVIEW_MODEL
})
}
/// 设置 / 合并 client metadata
@@ -1,6 +1,7 @@
//! 验证 HTTP 与 WebSocket 的账号头部隔离及中间件覆盖
use super::*;
use crate::support::fedramp_secret;
const SELECTED_ACCOUNT: &str = "acct_scope_same";
const CLIENT_ACCOUNT_HEADERS: &[(&str, &[&str])] = &[
@@ -190,14 +191,6 @@ fn assert_isolated_request(
assert_eq!(body["input"], isolation_history());
}
fn fedramp_secret(access_token: &str) -> provider_openai::credential::CodexOAuthSecret {
let mut credential = secret(access_token);
let claims = base64::engine::general_purpose::URL_SAFE_NO_PAD
.encode(br#"{"https://api.openai.com/auth":{"chatgpt_account_is_fedramp":true}}"#);
credential.id_token = Some(format!("header.{claims}.signature").into());
credential
}
#[tokio::test]
async fn rotated_fedramp_claim_opens_a_new_socket_for_the_same_account_and_session() {
let store = Arc::new(MemoryAccountStore::default());
@@ -244,6 +244,96 @@ async fn guardian_samplers_keep_the_http_transport_chosen_by_the_client() {
);
}
#[tokio::test]
async fn explicit_guardian_headers_only_select_a_unique_role() {
let store = Arc::new(MemoryAccountStore::default());
create_account(&store, "acct_provider_contract").await;
let server = MockServer::start().await;
Mock::given(method("POST"))
.and(path("/codex/responses"))
.respond_with(
ResponseTemplate::new(200).set_body_raw(CAPTURE_COMPLETED_SSE, "text/event-stream"),
)
.mount(&server)
.await;
let provider = provider_with_base_url(&store, server.uri());
let conflicting = ["reviewer", "classifier"];
let reversed = ["classifier", "reviewer"];
let cases: &[(&[&str], &[&str], bool)] = &[
(&conflicting, &[], true),
(&reversed, &[], true),
(&["classifier", "classifier"], &[], false),
(&conflicting, &["classifier"], false),
(&["classifier"], &reversed, true),
];
for &(client, middleware, ambiguous) in cases {
let headers: Vec<_> = client
.iter()
.map(|v| json!(["x-codex-guardian", STANDARD.encode(v)]))
.collect();
let attempt = if middleware.is_empty() {
context("req_guardian_headers", CancellationToken::new())
} else {
context_with_middleware(
"req_guardian_headers",
Arc::new(RecordingMiddleware {
observed: Default::default(),
replacement: ("input".into(), json!([])),
request_headers: middleware
.iter()
.map(|v| {
MiddlewareHeader::new(
"x-codex-guardian",
Bytes::copy_from_slice(v.as_bytes()),
)
})
.collect(),
}),
FastMode::Default,
)
};
let values = if middleware.is_empty() {
client
} else {
middleware
};
let body = json!({"model": LUNA, "input": [], "service_tier":"default"});
let payload = ProtocolPayload::json_object("openai", body.as_object().unwrap().clone())
.unwrap()
.with_context(Map::from_iter([
("use_websocket".into(), json!(false)),
("opaque_request_headers".into(), json!(headers)),
]));
let op = Operation::Generate(GenerateRequest::from_protocol_payload(payload));
let mut stream = provider
.clone()
.execute(
planned_request_with_presentation("openai", op, LUNA, None),
attempt,
)
.await
.unwrap();
while let Some(event) = stream.next().await {
event.unwrap();
}
let sent = server.received_requests().await.unwrap().pop().unwrap();
assert_eq!(
sent.headers
.get_all("x-codex-guardian")
.iter()
.map(|v| v.to_str().unwrap())
.collect::<Vec<_>>(),
values
);
assert_eq!(sent.headers.contains_key("content-encoding"), ambiguous);
assert_eq!(sent.headers.contains_key("x-codex-routing-hint"), ambiguous);
assert_eq!(
captured_request_body(&sent).get("service_tier").is_some(),
ambiguous
);
}
}
#[tokio::test]
async fn guardian_model_freezes_the_selected_model_across_settings_changes_and_continuations() {
let store = Arc::new(MemoryAccountStore::default());
@@ -515,12 +515,29 @@ async fn live_should_scope_correlation_to_account_and_preserve_business_headers(
.mount(&upstream)
.await;
// 独立账号安装身份分别发送相同客户端标识,同账号重复请求应稳定、不同账号域应隔离
for _ in 0..2 {
for fedramp in [false, true] {
let store = Arc::new(MemoryAccountStore::default());
create_account(&store, ACCOUNT).await;
if fedramp {
store
.seed_oauth_credential(provider_openai::credential::ImportCodexOAuthCredential {
account_id: ACCOUNT.to_owned(),
name: ACCOUNT.to_owned(),
secret: crate::support::fedramp_secret(&format!("at-{ACCOUNT}")),
verified_account: crate::support::profile(&format!("chatgpt-{ACCOUNT}")),
next_refresh_at: Some(chrono::Utc::now() + chrono::Duration::minutes(30)),
enabled: true,
})
.await;
} else {
create_account(&store, ACCOUNT).await;
}
let provider = crate::admin::initialized_test_provider(store, upstream.uri()).await;
for _ in 0..2 {
let mut headers = live_protocol_headers();
headers.push(gateway_core::operation::ProviderHttpHeader::new(
"x-openai-fedramp",
Bytes::from_static(b"false"),
));
headers.push(gateway_core::operation::ProviderHttpHeader::new(
"x-test-opaque",
Bytes::from_static(b"\x80request"),
@@ -570,7 +587,19 @@ async fn live_should_scope_correlation_to_account_and_preserve_business_headers(
}
}
let requests = upstream.received_requests().await.unwrap();
for request in &requests {
for (index, request) in requests.iter().enumerate() {
let values: Vec<_> = request
.headers
.get_all("x-openai-fedramp")
.iter()
.map(|value| value.to_str().unwrap())
.collect();
let expected: &[&str] = if index < 2 {
&["true", "false"]
} else {
&["true"]
};
assert_eq!(values, expected);
assert_live_correlation(&request.headers);
assert_eq!(
request.headers["authorization"],
@@ -1500,6 +1500,15 @@ pub(crate) fn runtime_policy() -> Arc<dyn ProviderRuntimePolicyPort> {
Arc::new(StaticRuntimePolicy)
}
pub(crate) fn fedramp_secret(access_token: &str) -> CodexOAuthSecret {
use base64::Engine as _;
let mut credential = secret(access_token);
let claims = base64::engine::general_purpose::URL_SAFE_NO_PAD
.encode(br#"{"https://api.openai.com/auth":{"chatgpt_account_is_fedramp":true}}"#);
credential.id_token = Some(format!("header.{claims}.signature").into());
credential
}
pub(crate) fn secret(access_token: &str) -> CodexOAuthSecret {
CodexOAuthSecret {
access_token: SecretString::from(access_token.to_owned()),
+2 -1
View File
@@ -598,7 +598,8 @@ Guardian 身份识别同步审核和审核预热,优先选择允许 `codex-aut
发往 Codex OAuth 的同步 `codex-auto-review` 审核带 `x-codex-guardian: reviewer`,
后台分类带 `x-codex-guardian: classifier`。这两类请求都省略顶层 `service_tier` 和
`x-codex-routing-hint`,分组强制加速同样适用。后台会话整理保留会话层级并省略上述两个请求头。
后台分类和会话整理的 HTTP 请求使用未压缩 JSON。普通 OAuth 生成和同步审核的 HTTP 正文使用 zstd
后台分类和会话整理的 HTTP 请求使用未压缩 JSON。普通 OAuth 生成和同步审核的 HTTP 正文使用 zstd。
中间件设置的角色头优先于客户端。同一来源的角色头取值冲突时保留原始多值,网关按普通请求选择传输和压缩方式
同步审核和后台分类的 `client_metadata.parent_response_id` 只有在网关记录能确认同一 Client Key、
同一 OAuth 账号的父响应时才发送,HTTP 和 WS 收到的父响应都可验证。预热请求可以没有父响应。