use guardrail_config::schema::AuthConfig;
use hyper::HeaderMap;
pub(crate) const AUTH_EXEMPT_PATHS: &[&str] = &["/healthz", "/metrics"];
pub(crate) const AUTH_HEADER_NAME: &str = "x-guardrail-key";
pub fn is_authorized(config: &AuthConfig, path: &str, headers: &HeaderMap) -> bool {
if !config.require_key {
return true;
}
if AUTH_EXEMPT_PATHS.contains(&path) {
return true;
}
let presented_key = headers.get(AUTH_HEADER_NAME).and_then(|v| v.to_str().ok());
match presented_key {
Some(key) => matches_any_key(&config.keys, key),
None => false,
}
}
fn matches_any_key(keys: &[String], presented: &str) -> bool {
use subtle::{Choice, ConstantTimeEq};
let presented = presented.as_bytes();
let matched = keys.iter().fold(Choice::from(0), |acc, k| {
acc | k.as_bytes().ct_eq(presented)
});
matched.into()
}
#[cfg(test)]
mod tests {
use super::*;
fn headers_with_key(key: &str) -> HeaderMap {
let mut h = HeaderMap::new();
h.insert(AUTH_HEADER_NAME, key.parse().unwrap());
h
}
#[test]
fn test_disabled_auth_always_allows() {
let config = AuthConfig {
require_key: false,
keys: vec![],
};
assert!(is_authorized(
&config,
"/v1/chat/completions",
&HeaderMap::new()
));
}
#[test]
fn test_missing_key_rejected() {
let config = AuthConfig {
require_key: true,
keys: vec!["secret".into()],
};
assert!(!is_authorized(
&config,
"/v1/chat/completions",
&HeaderMap::new()
));
}
#[test]
fn test_wrong_key_rejected() {
let config = AuthConfig {
require_key: true,
keys: vec!["secret".into()],
};
let headers = headers_with_key("wrong");
assert!(!is_authorized(&config, "/v1/chat/completions", &headers));
}
#[test]
fn test_correct_key_allowed() {
let config = AuthConfig {
require_key: true,
keys: vec!["secret".into()],
};
let headers = headers_with_key("secret");
assert!(is_authorized(&config, "/v1/chat/completions", &headers));
}
#[test]
fn test_one_of_multiple_keys_allowed() {
let config = AuthConfig {
require_key: true,
keys: vec!["key-a".into(), "key-b".into()],
};
assert!(is_authorized(
&config,
"/v1/chat/completions",
&headers_with_key("key-b")
));
}
#[test]
fn test_healthz_exempt_even_without_key() {
let config = AuthConfig {
require_key: true,
keys: vec!["secret".into()],
};
assert!(is_authorized(&config, "/healthz", &HeaderMap::new()));
}
#[test]
fn test_metrics_exempt_even_without_key() {
let config = AuthConfig {
require_key: true,
keys: vec!["secret".into()],
};
assert!(is_authorized(&config, "/metrics", &HeaderMap::new()));
}
#[test]
fn test_proxy_path_not_exempt() {
let config = AuthConfig {
require_key: true,
keys: vec!["secret".into()],
};
assert!(!is_authorized(&config, "/v1/messages", &HeaderMap::new()));
}
#[test]
fn test_matches_any_key() {
let keys = vec!["key-a".to_string(), "key-b".to_string()];
assert!(matches_any_key(&keys, "key-a"));
assert!(matches_any_key(&keys, "key-b"));
assert!(!matches_any_key(&keys, "key-c"));
assert!(!matches_any_key(&keys, "key-"));
assert!(!matches_any_key(&keys, "key-a2"));
assert!(!matches_any_key(&keys, ""));
assert!(!matches_any_key(&[], "key-a"));
}
#[test]
fn test_key_comparison_does_not_use_eq() {
let src = include_str!("auth.rs").replace("\r\n", "\n");
let body = src
.split("fn matches_any_key")
.nth(1)
.and_then(|rest| rest.split("\n}\n").next())
.expect("matches_any_key body");
assert!(body.contains("ct_eq"), "key comparison must use ct_eq");
assert!(!body.contains("=="), "key comparison must not use ==");
}
#[test]
fn test_empty_keys_list_rejects_everything() {
let config = AuthConfig {
require_key: true,
keys: vec![],
};
assert!(!is_authorized(
&config,
"/v1/chat/completions",
&headers_with_key("anything")
));
}
}