mirror of
https://github.com/fawney19/Aether.git
synced 2026-09-01 17:00:21 +08:00
refactor: 大规模模块拆分与重组,新增 aether-admin crate
- 新建独立 aether-admin crate 承载 admin 相关共享契约与纯辅助函数 - 拆分 ai_pipeline 下 kiro/private_envelope/conversion/planner 等大文件为子模块目录 - 重组 admin handlers 各业务域(billing/oauth/provider/system/users 等)为目录结构,移除 shared.rs/builders.rs 等反模式 - 移除 ai_pipeline runtime adapters 旧实现(claude/openai/gemini/kiro/vertex/antigravity 等),改由 provider transport 统一承载 - 移除 control_facade/execution_facade/auth_snapshot_facade 等冗余 facade 层 - 拆分 query/billing 与 query/monitoring 模块、state/runtime/payments 与 security 模块 - 扩展架构测试覆盖 admin_billing/admin_model/admin_users 等新模块 - 删除 docs/architecture/refactor-execution-plan.md 已完成的执行计划文档
This commit is contained in:
@@ -1,9 +1,11 @@
|
||||
mod auth;
|
||||
mod fingerprint;
|
||||
mod policy;
|
||||
mod request;
|
||||
mod url;
|
||||
|
||||
pub use auth::supports_local_claude_code_auth;
|
||||
pub use fingerprint::{generate_fingerprint, generate_random_fingerprint, sanitize_fingerprint};
|
||||
pub use policy::supports_local_claude_code_transport_with_network;
|
||||
pub use request::{build_claude_code_passthrough_headers, sanitize_claude_code_request_body};
|
||||
pub use url::build_claude_code_messages_url;
|
||||
|
||||
351
crates/aether-provider-transport/src/claude_code/fingerprint.rs
Normal file
351
crates/aether-provider-transport/src/claude_code/fingerprint.rs
Normal file
@@ -0,0 +1,351 @@
|
||||
use serde_json::{Map, Value};
|
||||
use sha2::{Digest, Sha256};
|
||||
use uuid::Uuid;
|
||||
|
||||
// Chrome impersonate profiles
|
||||
const CHROME_IMPERSONATE_PROFILES: &[&str] = &[
|
||||
"chrome110",
|
||||
"chrome116",
|
||||
"chrome119",
|
||||
"chrome120",
|
||||
"chrome123",
|
||||
"chrome124",
|
||||
"chrome131",
|
||||
"chrome133",
|
||||
];
|
||||
|
||||
const CHROME_VERSIONS: &[(&str, &str)] = &[
|
||||
("chrome110", "110.0.5481.177"),
|
||||
("chrome116", "116.0.5845.188"),
|
||||
("chrome119", "119.0.6045.214"),
|
||||
("chrome120", "120.0.6099.216"),
|
||||
("chrome123", "123.0.6312.122"),
|
||||
("chrome124", "124.0.6367.243"),
|
||||
("chrome131", "131.0.6778.265"),
|
||||
("chrome133", "133.0.6943.142"),
|
||||
];
|
||||
|
||||
// (os, arch, platform_token, platform_info)
|
||||
const PLATFORM_VARIANTS: &[(&str, &str, &str, &str)] = &[
|
||||
("Linux", "x64", "X11; Linux x86_64", "Linux x86_64"),
|
||||
("Linux", "arm64", "X11; Linux arm64", "Linux arm64"),
|
||||
(
|
||||
"Windows",
|
||||
"x64",
|
||||
"Windows NT 10.0; Win64; x64",
|
||||
"Windows x64",
|
||||
),
|
||||
(
|
||||
"MacOS",
|
||||
"x64",
|
||||
"Macintosh; Intel Mac OS X 10_15_7",
|
||||
"Darwin x64",
|
||||
),
|
||||
(
|
||||
"MacOS",
|
||||
"arm64",
|
||||
"Macintosh; ARM Mac OS X 14_0_0",
|
||||
"Darwin arm64",
|
||||
),
|
||||
];
|
||||
|
||||
const STAINLESS_PACKAGE_VERSIONS: &[&str] = &["0.68.0", "0.69.0", "0.70.0", "0.71.0"];
|
||||
const NODE_VERSIONS: &[&str] = &["v20.18.1", "v22.12.0", "v22.14.0", "v24.13.0"];
|
||||
const ELECTRON_VERSIONS: &[&str] = &["35.5.1", "36.7.1", "37.3.0", "38.7.0", "39.2.3"];
|
||||
const STAINLESS_TIMEOUTS: &[&str] = &["600", "900"];
|
||||
|
||||
/// Deterministic hash-based index picker, compatible with Python implementation.
|
||||
/// Each `slot` produces a different selection from the same seed.
|
||||
struct SeededPicker {
|
||||
seed_bytes: [u8; 32],
|
||||
}
|
||||
|
||||
impl SeededPicker {
|
||||
fn new(seed: &str) -> Self {
|
||||
let mut hasher = Sha256::new();
|
||||
hasher.update(seed.as_bytes());
|
||||
Self {
|
||||
seed_bytes: hasher.finalize().into(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Pick an index from `[0, len)` using a specific slot.
|
||||
/// Different slots produce independent-looking selections from the same seed.
|
||||
fn pick(&self, slot: u8, len: usize) -> usize {
|
||||
if len == 0 {
|
||||
return 0;
|
||||
}
|
||||
// Hash seed_bytes + slot to get a new digest, take first 8 bytes as u64
|
||||
let mut hasher = Sha256::new();
|
||||
hasher.update(self.seed_bytes);
|
||||
hasher.update([slot]);
|
||||
let hash = hasher.finalize();
|
||||
let value = u64::from_be_bytes(hash[..8].try_into().unwrap());
|
||||
(value % len as u64) as usize
|
||||
}
|
||||
}
|
||||
|
||||
fn chrome_version_for_profile(profile: &str) -> &'static str {
|
||||
for (p, v) in CHROME_VERSIONS {
|
||||
if p.eq_ignore_ascii_case(profile) {
|
||||
return v;
|
||||
}
|
||||
}
|
||||
"120.0.6099.216"
|
||||
}
|
||||
|
||||
fn build_user_agent(platform_token: &str, chrome_version: &str, electron_version: &str) -> String {
|
||||
format!(
|
||||
"Mozilla/5.0 ({platform_token}) AppleWebKit/537.36 (KHTML, like Gecko) \
|
||||
Chrome/{chrome_version} Electron/{electron_version} Safari/537.36"
|
||||
)
|
||||
}
|
||||
|
||||
fn resolve_platform_token(os: &str, arch: &str) -> &'static str {
|
||||
let os_lower = os.to_ascii_lowercase();
|
||||
let arch_lower = arch.to_ascii_lowercase();
|
||||
|
||||
if os_lower.starts_with("win") {
|
||||
return "Windows NT 10.0; Win64; x64";
|
||||
}
|
||||
if matches!(os_lower.as_str(), "darwin" | "mac" | "macos") {
|
||||
return if matches!(arch_lower.as_str(), "arm64" | "aarch64") {
|
||||
"Macintosh; ARM Mac OS X 14_0_0"
|
||||
} else {
|
||||
"Macintosh; Intel Mac OS X 10_15_7"
|
||||
};
|
||||
}
|
||||
if matches!(arch_lower.as_str(), "arm64" | "aarch64") {
|
||||
"X11; Linux arm64"
|
||||
} else {
|
||||
"X11; Linux x86_64"
|
||||
}
|
||||
}
|
||||
|
||||
/// Generate a complete fingerprint JSON from a seed (deterministic).
|
||||
/// Compatible with the Python `generate_fingerprint(seed=...)` output.
|
||||
pub fn generate_fingerprint(seed: &str) -> Value {
|
||||
let picker = SeededPicker::new(seed);
|
||||
|
||||
let impersonate =
|
||||
CHROME_IMPERSONATE_PROFILES[picker.pick(0, CHROME_IMPERSONATE_PROFILES.len())];
|
||||
let chrome_version = chrome_version_for_profile(impersonate);
|
||||
let node_version = NODE_VERSIONS[picker.pick(1, NODE_VERSIONS.len())];
|
||||
let electron_version = ELECTRON_VERSIONS[picker.pick(2, ELECTRON_VERSIONS.len())];
|
||||
let platform = PLATFORM_VARIANTS[picker.pick(3, PLATFORM_VARIANTS.len())];
|
||||
let (stainless_os, stainless_arch, platform_token, platform_info) = platform;
|
||||
let stainless_package_version =
|
||||
STAINLESS_PACKAGE_VERSIONS[picker.pick(4, STAINLESS_PACKAGE_VERSIONS.len())];
|
||||
let stainless_timeout = STAINLESS_TIMEOUTS[picker.pick(5, STAINLESS_TIMEOUTS.len())];
|
||||
|
||||
let vscode_session_id = Uuid::new_v5(
|
||||
&Uuid::NAMESPACE_URL,
|
||||
format!("aether:fingerprint:{seed}").as_bytes(),
|
||||
)
|
||||
.simple()
|
||||
.to_string();
|
||||
|
||||
let user_agent = build_user_agent(platform_token, chrome_version, electron_version);
|
||||
|
||||
serde_json::json!({
|
||||
"impersonate": impersonate,
|
||||
"stainless_package_version": stainless_package_version,
|
||||
"stainless_os": stainless_os,
|
||||
"stainless_arch": stainless_arch,
|
||||
"stainless_runtime_version": node_version,
|
||||
"stainless_timeout": stainless_timeout,
|
||||
"node_version": node_version,
|
||||
"chrome_version": chrome_version,
|
||||
"electron_version": electron_version,
|
||||
"vscode_session_id": vscode_session_id,
|
||||
"platform_info": platform_info,
|
||||
"user_agent": user_agent,
|
||||
})
|
||||
}
|
||||
|
||||
/// Generate a random (non-deterministic) fingerprint.
|
||||
pub fn generate_random_fingerprint() -> Value {
|
||||
let random_seed = Uuid::new_v4().to_string();
|
||||
generate_fingerprint(&random_seed)
|
||||
}
|
||||
|
||||
/// Sanitize an existing fingerprint JSON, filling missing fields with
|
||||
/// deterministic fallbacks derived from `key_id`.
|
||||
pub fn sanitize_fingerprint(raw: &Value, key_id: &str) -> Value {
|
||||
let generated = generate_fingerprint(key_id);
|
||||
let gen_map = generated.as_object().unwrap();
|
||||
|
||||
let raw_map = match raw.as_object() {
|
||||
Some(m) => m,
|
||||
None => return generated,
|
||||
};
|
||||
|
||||
let mut out = Map::new();
|
||||
|
||||
// Start with generated values, then overlay non-empty raw values
|
||||
for (key, gen_value) in gen_map {
|
||||
let value = raw_map
|
||||
.get(key)
|
||||
.and_then(Value::as_str)
|
||||
.map(str::trim)
|
||||
.filter(|v| !v.is_empty())
|
||||
.map(|v| Value::String(v.to_string()))
|
||||
.unwrap_or_else(|| gen_value.clone());
|
||||
out.insert(key.clone(), value);
|
||||
}
|
||||
|
||||
// Normalize impersonate to known profile
|
||||
let impersonate = out
|
||||
.get("impersonate")
|
||||
.and_then(Value::as_str)
|
||||
.unwrap_or_default()
|
||||
.to_ascii_lowercase();
|
||||
let is_known = CHROME_IMPERSONATE_PROFILES
|
||||
.iter()
|
||||
.any(|p| p.eq_ignore_ascii_case(&impersonate));
|
||||
if !is_known {
|
||||
out.insert("impersonate".to_string(), gen_map["impersonate"].clone());
|
||||
}
|
||||
|
||||
// Ensure chrome_version matches impersonate profile
|
||||
let profile = out
|
||||
.get("impersonate")
|
||||
.and_then(Value::as_str)
|
||||
.unwrap_or_default();
|
||||
let chrome_version = chrome_version_for_profile(profile);
|
||||
out.insert(
|
||||
"chrome_version".to_string(),
|
||||
Value::String(chrome_version.to_string()),
|
||||
);
|
||||
|
||||
// Rebuild user_agent if missing
|
||||
let has_ua = out
|
||||
.get("user_agent")
|
||||
.and_then(Value::as_str)
|
||||
.map(str::trim)
|
||||
.is_some_and(|v| !v.is_empty());
|
||||
if !has_ua {
|
||||
let os = out
|
||||
.get("stainless_os")
|
||||
.and_then(Value::as_str)
|
||||
.unwrap_or("Linux");
|
||||
let arch = out
|
||||
.get("stainless_arch")
|
||||
.and_then(Value::as_str)
|
||||
.unwrap_or("x64");
|
||||
let electron = out
|
||||
.get("electron_version")
|
||||
.and_then(Value::as_str)
|
||||
.unwrap_or("38.7.0");
|
||||
let platform_token = resolve_platform_token(os, arch);
|
||||
out.insert(
|
||||
"user_agent".to_string(),
|
||||
Value::String(build_user_agent(platform_token, chrome_version, electron)),
|
||||
);
|
||||
}
|
||||
|
||||
Value::Object(out)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn deterministic_generation_from_seed() {
|
||||
let fp1 = generate_fingerprint("key-abc-123");
|
||||
let fp2 = generate_fingerprint("key-abc-123");
|
||||
assert_eq!(fp1, fp2, "same seed should produce identical fingerprint");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn different_seeds_produce_different_fingerprints() {
|
||||
let fp1 = generate_fingerprint("key-1");
|
||||
let fp2 = generate_fingerprint("key-2");
|
||||
// At least one field should differ (statistically near-certain)
|
||||
assert_ne!(fp1, fp2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn generated_fingerprint_has_all_fields() {
|
||||
let fp = generate_fingerprint("test-key");
|
||||
let expected_keys = [
|
||||
"impersonate",
|
||||
"stainless_package_version",
|
||||
"stainless_os",
|
||||
"stainless_arch",
|
||||
"stainless_runtime_version",
|
||||
"stainless_timeout",
|
||||
"node_version",
|
||||
"chrome_version",
|
||||
"electron_version",
|
||||
"vscode_session_id",
|
||||
"platform_info",
|
||||
"user_agent",
|
||||
];
|
||||
let map = fp.as_object().unwrap();
|
||||
for key in expected_keys {
|
||||
assert!(map.contains_key(key), "missing field: {key}");
|
||||
let value = map[key].as_str().unwrap();
|
||||
assert!(!value.is_empty(), "empty field: {key}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sanitize_preserves_user_overrides() {
|
||||
let raw = serde_json::json!({
|
||||
"stainless_os": "MacOS",
|
||||
"stainless_arch": "arm64",
|
||||
"stainless_timeout": "900",
|
||||
"user_agent": "Custom-Agent/1.0",
|
||||
});
|
||||
let sanitized = sanitize_fingerprint(&raw, "test-key");
|
||||
let map = sanitized.as_object().unwrap();
|
||||
assert_eq!(map["stainless_os"].as_str(), Some("MacOS"));
|
||||
assert_eq!(map["stainless_arch"].as_str(), Some("arm64"));
|
||||
assert_eq!(map["stainless_timeout"].as_str(), Some("900"));
|
||||
assert_eq!(map["user_agent"].as_str(), Some("Custom-Agent/1.0"));
|
||||
// Other fields should be filled from generation
|
||||
assert!(map.contains_key("impersonate"));
|
||||
assert!(map.contains_key("stainless_package_version"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sanitize_fills_missing_fields_from_seed() {
|
||||
let raw = serde_json::json!({});
|
||||
let sanitized = sanitize_fingerprint(&raw, "test-key");
|
||||
let generated = generate_fingerprint("test-key");
|
||||
// All fields should match generated since raw is empty
|
||||
let s = sanitized.as_object().unwrap();
|
||||
let g = generated.as_object().unwrap();
|
||||
for key in g.keys() {
|
||||
assert!(s.contains_key(key), "sanitized missing key: {key}");
|
||||
assert!(
|
||||
!s[key].as_str().unwrap().is_empty(),
|
||||
"sanitized empty key: {key}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sanitize_normalizes_unknown_impersonate_profile() {
|
||||
let raw = serde_json::json!({
|
||||
"impersonate": "firefox99",
|
||||
});
|
||||
let sanitized = sanitize_fingerprint(&raw, "test-key");
|
||||
let profile = sanitized["impersonate"].as_str().unwrap();
|
||||
assert!(
|
||||
CHROME_IMPERSONATE_PROFILES
|
||||
.iter()
|
||||
.any(|p| p.eq_ignore_ascii_case(profile)),
|
||||
"should normalize to known profile, got: {profile}"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn random_fingerprint_differs_each_call() {
|
||||
let fp1 = generate_random_fingerprint();
|
||||
let fp2 = generate_random_fingerprint();
|
||||
assert_ne!(fp1, fp2);
|
||||
}
|
||||
}
|
||||
@@ -15,6 +15,17 @@ const REQUIRED_BETA_TOKENS: &[&str] = &[
|
||||
];
|
||||
const EXCLUDED_BETA_TOKENS: &[&str] = &["context-1m-2025-08-07"];
|
||||
|
||||
/// Fingerprint field -> HTTP header mapping.
|
||||
/// Every stainless / identity dimension that can vary per-key is listed here.
|
||||
const FINGERPRINT_HEADER_MAP: &[(&str, &str)] = &[
|
||||
("stainless_package_version", "x-stainless-package-version"),
|
||||
("stainless_os", "x-stainless-os"),
|
||||
("stainless_arch", "x-stainless-arch"),
|
||||
("stainless_runtime_version", "x-stainless-runtime-version"),
|
||||
("stainless_timeout", "x-stainless-timeout"),
|
||||
("user_agent", "user-agent"),
|
||||
];
|
||||
|
||||
pub fn build_claude_code_passthrough_headers(
|
||||
headers: &http::HeaderMap,
|
||||
auth_header: &str,
|
||||
@@ -31,34 +42,46 @@ pub fn build_claude_code_passthrough_headers(
|
||||
Some("application/json"),
|
||||
);
|
||||
|
||||
// -- Anthropic protocol headers --
|
||||
out.insert("accept".to_string(), DEFAULT_ACCEPT.to_string());
|
||||
out.insert(
|
||||
"anthropic-version".to_string(),
|
||||
DEFAULT_ANTHROPIC_VERSION.to_string(),
|
||||
);
|
||||
// Read incoming anthropic-beta directly from the original HeaderMap because the
|
||||
// upstream passthrough filter now strips `anthropic-*` headers to avoid leaking
|
||||
// them to non-Anthropic upstreams.
|
||||
let incoming_anthropic_beta = headers
|
||||
.get("anthropic-beta")
|
||||
.and_then(|value| value.to_str().ok());
|
||||
out.insert(
|
||||
"anthropic-beta".to_string(),
|
||||
merge_anthropic_beta_tokens(out.get("anthropic-beta").map(String::as_str)),
|
||||
merge_anthropic_beta_tokens(incoming_anthropic_beta),
|
||||
);
|
||||
out.insert(
|
||||
"anthropic-dangerous-direct-browser-access".to_string(),
|
||||
"true".to_string(),
|
||||
);
|
||||
out.insert("x-app".to_string(), "cli".to_string());
|
||||
|
||||
// -- Stainless SDK identity headers --
|
||||
// Fixed values: these don't vary per fingerprint.
|
||||
out.insert("x-stainless-lang".to_string(), "js".to_string());
|
||||
out.insert("x-stainless-runtime".to_string(), "node".to_string());
|
||||
out.insert("x-stainless-retry-count".to_string(), "0".to_string());
|
||||
|
||||
// Defaults for fingerprint-overridable fields (used when no fingerprint is present).
|
||||
out.insert(
|
||||
"x-stainless-package-version".to_string(),
|
||||
"0.70.0".to_string(),
|
||||
);
|
||||
out.insert("x-stainless-os".to_string(), "Linux".to_string());
|
||||
out.insert("x-stainless-arch".to_string(), "arm64".to_string());
|
||||
out.insert("x-stainless-runtime".to_string(), "node".to_string());
|
||||
out.insert(
|
||||
"x-stainless-runtime-version".to_string(),
|
||||
"v24.13.0".to_string(),
|
||||
);
|
||||
out.insert("x-stainless-retry-count".to_string(), "0".to_string());
|
||||
out.insert("x-stainless-timeout".to_string(), "600".to_string());
|
||||
out.insert("x-app".to_string(), "cli".to_string());
|
||||
out.insert(
|
||||
"anthropic-dangerous-direct-browser-access".to_string(),
|
||||
"true".to_string(),
|
||||
);
|
||||
|
||||
if stream {
|
||||
out.insert(
|
||||
@@ -69,33 +92,18 @@ pub fn build_claude_code_passthrough_headers(
|
||||
out.remove("x-stainless-helper-method");
|
||||
}
|
||||
|
||||
if let Some(fingerprint) = fingerprint.and_then(Value::as_object) {
|
||||
override_header_from_fingerprint(
|
||||
&mut out,
|
||||
fingerprint,
|
||||
"stainless_package_version",
|
||||
"x-stainless-package-version",
|
||||
);
|
||||
override_header_from_fingerprint(&mut out, fingerprint, "stainless_os", "x-stainless-os");
|
||||
override_header_from_fingerprint(
|
||||
&mut out,
|
||||
fingerprint,
|
||||
"stainless_arch",
|
||||
"x-stainless-arch",
|
||||
);
|
||||
override_header_from_fingerprint(
|
||||
&mut out,
|
||||
fingerprint,
|
||||
"stainless_runtime_version",
|
||||
"x-stainless-runtime-version",
|
||||
);
|
||||
override_header_from_fingerprint(
|
||||
&mut out,
|
||||
fingerprint,
|
||||
"stainless_timeout",
|
||||
"x-stainless-timeout",
|
||||
);
|
||||
override_header_from_fingerprint(&mut out, fingerprint, "user_agent", "user-agent");
|
||||
// Override from fingerprint: all mapped fields are applied uniformly.
|
||||
if let Some(fp) = fingerprint.and_then(Value::as_object) {
|
||||
for &(fp_key, header_key) in FINGERPRINT_HEADER_MAP {
|
||||
if let Some(value) = fp
|
||||
.get(fp_key)
|
||||
.and_then(Value::as_str)
|
||||
.map(str::trim)
|
||||
.filter(|v| !v.is_empty())
|
||||
{
|
||||
out.insert(header_key.to_string(), value.to_string());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
out
|
||||
@@ -210,23 +218,6 @@ fn append_beta_token(seen: &mut BTreeSet<String>, merged: &mut Vec<String>, toke
|
||||
}
|
||||
}
|
||||
|
||||
fn override_header_from_fingerprint(
|
||||
headers: &mut BTreeMap<String, String>,
|
||||
fingerprint: &Map<String, Value>,
|
||||
fingerprint_key: &str,
|
||||
header_key: &str,
|
||||
) {
|
||||
let Some(value) = fingerprint
|
||||
.get(fingerprint_key)
|
||||
.and_then(Value::as_str)
|
||||
.map(str::trim)
|
||||
.filter(|value| !value.is_empty())
|
||||
else {
|
||||
return;
|
||||
};
|
||||
headers.insert(header_key.to_string(), value.to_string());
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::{build_claude_code_passthrough_headers, sanitize_claude_code_request_body};
|
||||
|
||||
@@ -19,8 +19,18 @@ pub fn should_skip_request_header(name: &str) -> bool {
|
||||
}
|
||||
|
||||
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!(
|
||||
name.to_ascii_lowercase().as_str(),
|
||||
lower.as_str(),
|
||||
"authorization"
|
||||
| "x-api-key"
|
||||
| "x-goog-api-key"
|
||||
@@ -37,5 +47,66 @@ pub fn should_skip_upstream_passthrough_header(name: &str) -> bool {
|
||||
| "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)
|
||||
}
|
||||
|
||||
#[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}"
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user