Files
Aether/crates/aether-provider-transport/src/headers.rs
fawney19 6144473ebe feat: 新增 frontdoor 执行回环守卫与多项可观测性增强
- 新增 frontdoor_loop_guard 模块,检测并拒绝 execution runtime 回环到本地网关的请求(HTTP 508)
- candidate loop 引入 span tracking、执行尝试日志与流式看门狗超时
- 本地故障转移策略支持从 report_context 加载,新增 append_local_failover_policy_to_value
- runtime tracing 美化:移除 identity 前缀,按 span 深度树形缩进,target 固定宽度展示
- Codex OpenAI CLI 补齐 chatgpt-account-id/x-client-request-id/session_id/conversation_id 请求头
- OpenAI CLI same/cross-format 聚合规则放宽以支持 openai:compact 客户端格式,并过滤 error-like 响应体
- auth/proxy/finalize 日志补充 user_id/api_key_id/api_key_name/balance_remaining 等字段
- 启动日志拆分为 starting/ready/config 三段,新增 resolve_bind_http_base_url
- access_log middleware 将生成的 trace_id 回注到下游请求头
- Cargo.toml 启用 serde_json preserve_order 特性
2026-04-11 01:50:24 +08:00

137 lines
4.3 KiB
Rust

pub fn should_skip_request_header(name: &str) -> bool {
let normalized = name.to_ascii_lowercase();
matches!(
normalized.as_str(),
"connection"
| "keep-alive"
| "proxy-authenticate"
| "proxy-authorization"
| "proxy-connection"
| "te"
| "trailer"
| "transfer-encoding"
| "upgrade"
| "x-aether-execution-path"
| "x-aether-dependency-reason"
| "x-aether-execution-loop-guard"
| "x-aether-control-execute-fallback"
| "x-aether-rate-limit-preflight"
)
}
pub fn should_skip_upstream_passthrough_header(name: &str) -> bool {
let lower = name.to_ascii_lowercase();
// Anthropic SDK (stainless) client metadata and Anthropic-specific headers
// (anthropic-version / anthropic-beta / anthropic-dangerous-direct-browser-access / ...).
// These are only meaningful when the upstream is Anthropic. The Claude Code
// adapter re-reads what it needs directly from the original HeaderMap and
// injects its own values *after* this filter runs, so stripping both prefixes
// here prevents leakage to any other upstream (OpenAI/Gemini/Codex/...).
if lower.starts_with("x-stainless-") || lower.starts_with("anthropic-") {
return true;
}
matches!(
lower.as_str(),
"authorization"
| "x-api-key"
| "x-goog-api-key"
| "host"
| "content-length"
| "transfer-encoding"
| "connection"
| "accept-encoding"
| "content-encoding"
| "x-real-ip"
| "x-real-proto"
| "x-forwarded-for"
| "x-forwarded-proto"
| "x-forwarded-scheme"
| "x-forwarded-host"
| "x-forwarded-port"
// Claude CLI client identifier; re-injected by the Claude Code adapter
// when the upstream is Anthropic, filtered for everybody else.
| "x-app"
) || should_skip_request_header(name)
}
pub(crate) fn should_skip_upstream_complete_passthrough_header(name: &str) -> bool {
let lower = name.to_ascii_lowercase();
matches!(
lower.as_str(),
"authorization"
| "x-api-key"
| "x-goog-api-key"
| "host"
| "content-length"
| "transfer-encoding"
| "connection"
| "accept-encoding"
| "content-encoding"
| "x-real-ip"
| "x-real-proto"
| "x-forwarded-for"
| "x-forwarded-proto"
| "x-forwarded-scheme"
| "x-forwarded-host"
| "x-forwarded-port"
) || should_skip_request_header(name)
}
#[cfg(test)]
mod tests {
use super::should_skip_upstream_passthrough_header;
#[test]
fn strips_all_stainless_headers() {
let stainless = [
"x-stainless-arch",
"x-stainless-lang",
"x-stainless-os",
"x-stainless-package-version",
"x-stainless-retry-count",
"x-stainless-runtime",
"x-stainless-runtime-version",
"x-stainless-timeout",
"x-stainless-helper-method",
"X-Stainless-Arch",
"X-STAINLESS-FUTURE-HEADER",
];
for h in stainless {
assert!(
should_skip_upstream_passthrough_header(h),
"should skip {h}"
);
}
}
#[test]
fn strips_anthropic_and_claude_cli_identity_headers() {
let anthropic = [
"anthropic-version",
"anthropic-beta",
"anthropic-dangerous-direct-browser-access",
"Anthropic-Version",
"ANTHROPIC-FUTURE-HEADER",
"x-app",
"X-App",
];
for h in anthropic {
assert!(
should_skip_upstream_passthrough_header(h),
"should skip {h}"
);
}
}
#[test]
fn allows_normal_headers_through() {
let allowed = ["user-agent", "accept", "content-type", "x-custom-header"];
for h in allowed {
assert!(
!should_skip_upstream_passthrough_header(h),
"should allow {h}"
);
}
}
}