use axum::http::HeaderMap;
use kz_server::security::{
DEFAULT_CREDENTIAL_LOCATION, extract_bearer_token, format_credential_value,
is_blocked_host, parse_credential_location, validate_ref,
};
#[test]
fn test_extract_bearer_token() {
let mut headers = HeaderMap::new();
headers.insert("authorization", "Bearer mytoken123".parse().unwrap());
let token = extract_bearer_token(&headers);
assert_eq!(token, Some("mytoken123".to_string()));
}
#[test]
fn test_extract_bearer_token_missing() {
let headers = HeaderMap::new();
let token = extract_bearer_token(&headers);
assert_eq!(token, None);
}
#[test]
fn test_parse_credential_location() {
let loc = parse_credential_location("header:X-API-Key:").expect("should parse");
assert_eq!(loc.header_name, "X-API-Key");
assert_eq!(loc.prefix, "");
}
#[test]
fn test_parse_credential_location_with_prefix() {
let loc = parse_credential_location("header:Authorization:Bearer").expect("should parse");
assert_eq!(loc.header_name, "Authorization");
assert_eq!(loc.prefix, "Bearer");
}
#[test]
fn test_format_credential_value_with_prefix() {
let loc = parse_credential_location("header:Authorization:Bearer").unwrap();
let value = format_credential_value(&loc, "sk-123");
assert_eq!(value, "Bearer sk-123");
}
#[test]
fn test_format_credential_value_no_prefix() {
let loc = parse_credential_location("header:X-API-Key:").unwrap();
let value = format_credential_value(&loc, "sk-123");
assert_eq!(value, "sk-123");
}
#[test]
fn test_blocked_hosts() {
let blocked = [
"localhost",
"127.0.0.1",
"0.0.0.0",
"::1",
"169.254.169.254",
"metadata.google.internal",
"something.internal",
"something.local",
"10.0.0.1",
"10.255.255.255",
"172.16.0.1",
"172.31.255.255",
"192.168.1.1",
"[::1]",
"fd00:ec2::254",
"100.100.100.200",
];
for host in &blocked {
assert!(is_blocked_host(host), "expected {host} to be blocked");
}
}
#[test]
fn test_allowed_hosts() {
let allowed = ["api.openai.com", "vault.example.com", "8.8.8.8"];
for host in &allowed {
assert!(!is_blocked_host(host), "expected {host} to be allowed");
}
}
#[test]
fn test_validate_ref_blocks_traversal() {
let traversals = ["../foo", "secret/../etc/passwd", "secret/data/.."];
for path in &traversals {
assert!(
validate_ref(path).is_err(),
"expected {path} to be blocked"
);
}
}
#[test]
fn test_validate_ref_allows_normal() {
let valid = ["secret/data/prod/pg", "secret/data/prod/pg/conn"];
for path in &valid {
assert!(validate_ref(path).is_ok(), "expected {path} to be allowed");
}
}
#[test]
fn test_validate_ref_blocks_empty() {
assert!(validate_ref("").is_err(), "expected empty ref to be blocked");
}
#[test]
fn test_default_credential_location() {
assert_eq!(DEFAULT_CREDENTIAL_LOCATION, "header:Authorization:Bearer");
}
#[test]
fn test_validate_ref_blocks_encoded() {
assert!(validate_ref("secret%2F..%2Fetc").is_err());
assert!(validate_ref("secret%252F").is_err());
}
#[test]
fn test_validate_ref_blocks_null_bytes() {
assert!(validate_ref("secret/data\0/../etc").is_err());
}
#[test]
fn test_blocked_hosts_ip_bypass_formats() {
assert!(is_blocked_host("0177.0.0.1"));
assert!(is_blocked_host("2130706433"));
assert!(is_blocked_host("0x7f000001"));
assert!(is_blocked_host("0x7f.0.0.1"));
}
#[test]
fn test_extract_bearer_token_empty_rejected() {
let mut headers = HeaderMap::new();
headers.insert("authorization", "Bearer ".parse().unwrap());
let token = extract_bearer_token(&headers);
assert_eq!(token, None, "empty bearer token should be rejected");
}