fix: resolve $var: references in custom ai resource headers (#12764)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Ruben Fiszel
2026-11-30 13:01:24 +01:00
committed by GitHub
co-authored by Claude Opus 5.5
parent d48296808e
commit a57f2df2c8
2 changed files with 72 additions and 1 deletions
@@ -409,3 +409,69 @@ async fn test_ai_proxy_x_resource_value(db: Pool<Postgres>) -> anyhow::Result<()
Ok(())
}
/// A `$var:` reference in a resource's custom headers reaches the upstream resolved.
#[sqlx::test(migrations = "../migrations", fixtures("base"))]
async fn test_ai_proxy_resolves_header_var_refs(db: Pool<Postgres>) -> anyhow::Result<()> {
use axum::{http::HeaderMap, http::StatusCode, routing::post, Json, Router};
initialize_tracing().await;
std::env::set_var("ALLOW_PRIVATE_AI_BASE_URLS", "true");
let server = ApiServer::start(db.clone()).await?;
let port = server.addr.port();
let app = Router::new().fallback(post(|headers: HeaderMap| async move {
if headers
.get("x-subscription-key")
.and_then(|v| v.to_str().ok())
== Some("sub-key")
{
Ok(Json(json!({
"id": "chatcmpl-test",
"object": "chat.completion",
"choices": [{"message": {"role": "assistant", "content": "hello"}}]
})))
} else {
Err(StatusCode::UNAUTHORIZED)
}
}));
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await?;
let base_url = format!("http://127.0.0.1:{}/v1", listener.local_addr()?.port());
tokio::spawn(async move { axum::serve(listener, app).await.unwrap() });
sqlx::query(
"INSERT INTO variable (workspace_id, path, value, is_secret, description, extra_perms) \
VALUES ('test-workspace', 'f/ai/sub_key', 'sub-key', false, '', '{}'::jsonb)",
)
.execute(&db)
.await?;
let value = json!({
"api_key": "sk-test",
"base_url": base_url,
"headers": { "X-Subscription-Key": "$var:f/ai/sub_key" }
})
.to_string();
let encoded: String = value.bytes().map(|b| format!("%{b:02X}")).collect();
let resp = authed(
client()
.post(format!(
"http://localhost:{port}/api/w/test-workspace/ai/proxy/chat/completions"
))
.header("X-Provider", "customai")
.header("X-Resource-Value", encoded)
.json(&json!({
"model": "gpt-4",
"messages": [{"role": "user", "content": "hi"}]
})),
)
.send()
.await?;
assert_2xx(
resp.status().as_u16(),
&resp.text().await?,
"a $var: reference in a custom header must be resolved",
);
Ok(())
}
+6 -1
View File
@@ -439,6 +439,11 @@ async fn resolve_provider_credentials(
} else {
resource.platform
};
let mut custom_headers = HashMap::with_capacity(resource.headers.len());
for (name, value) in resource.headers {
let value = resolve_var(value, db, w_id, user_db.as_ref(), authed).await?;
custom_headers.insert(name, value);
}
Ok(ProviderCredentials {
provider: provider.clone(),
@@ -455,7 +460,7 @@ async fn resolve_provider_credentials(
anthropic_federation: None,
vertex_workload_identity,
platform,
custom_headers: resource.headers,
custom_headers,
})
}
AIResource::AnthropicFederated(resource) => {