Skip to content

Compress tool outputs, logs, files, and RAG chunks before they reach the LLM. 20% fewer tokens for coding agents, 60-95% fewer tokens for JSON, same answers. Library, proxy, MCP server.

fix(core): case-insensitive Bearer scheme, clamp SQLite CCR TTLs, redact the Redis URL in logs (#4561)

HetCreep9e8cc64b48eb
diff --git a/crates/headroom-core/src/auth_mode.rs b/crates/headroom-core/src/auth_mode.rs
index 0c61087b5..47043f6c1 100644
--- a/crates/headroom-core/src/auth_mode.rs
+++ b/crates/headroom-core/src/auth_mode.rs
@@ -161,7 +161,13 @@ pub fn classify(headers: &HeaderMap) -> AuthMode {
None => "",
};
- if let Some(token) = auth.strip_prefix("Bearer ") {
+ // RFC 9110 §11.1: auth scheme names are case-insensitive; tolerate
+ // extra whitespace between scheme and token.
+ let bearer = auth
+ .get(..7)
+ .filter(|scheme| scheme.eq_ignore_ascii_case("Bearer "))
+ .map(|_| auth[7..].trim_start());
+ if let Some(token) = bearer {
// Order matters: the OAuth shape `sk-ant-oat*` shares a
// prefix with `sk-ant-api*` only at `sk-ant-`, so we check
// the OAuth shape FIRST. Real OAuth access tokens are
@@ -236,6 +242,24 @@ mod inline_tests {
assert_eq!(classify(&headers), AuthMode::Payg);
}
+ fn classify_auth(value: &str) -> AuthMode {
+ let mut headers = HeaderMap::new();
+ headers.insert("authorization", HeaderValue::from_str(value).unwrap());
+ classify(&headers)
+ }
+
+ #[test]
+ fn bearer_scheme_is_case_insensitive_and_token_is_trimmed() {
+ // Controls: well-formed headers keep their classification.
+ assert_eq!(classify_auth("Bearer sk-ant-api03-fake"), AuthMode::Payg);
+ assert_eq!(classify_auth("Bearer sk-ant-oat01-fake"), AuthMode::OAuth);
+ // Lowercase scheme is still Bearer -> Payg, not the non-Bearer OAuth branch.
+ assert_eq!(classify_auth("bearer sk-ant-api03-fake"), AuthMode::Payg);
+ assert_eq!(classify_auth("BEARER sk-ant-api03-fake"), AuthMode::Payg);
+ // Extra space before the token must not hide the oat prefix.
+ assert_eq!(classify_auth("Bearer sk-ant-oat01-fake"), AuthMode::OAuth);
+ }
+
#[test]
fn unparseable_auth_falls_back_to_default() {
// Non-UTF-8 Authorization header — the warn! fires but we
diff --git a/crates/headroom-core/src/ccr/backends/mod.rs b/crates/headroom-core/src/ccr/backends/mod.rs
index 3d86d933a..cf564e8db 100644
--- a/crates/headroom-core/src/ccr/backends/mod.rs
+++ b/crates/headroom-core/src/ccr/backends/mod.rs
@@ -137,7 +137,7 @@ pub fn from_config(config: &CcrBackendConfig) -> Result<Box<dyn CcrStore>, CcrBa
tracing::info!(
target = "ccr.backend",
backend = "redis",
- url = %url,
+ url = %redact_redis_url(url),
ttl_seconds = *ttl_seconds,
"ccr_backend_initialized"
);
@@ -150,3 +150,34 @@ pub fn from_config(config: &CcrBackendConfig) -> Result<Box<dyn CcrStore>, CcrBa
}),
}
}
+
+/// Redis URL with userinfo, path and query dropped (`scheme://host[:port]`),
+/// safe to log. Mirrors the Python client, which logs only the host.
+#[cfg_attr(not(feature = "redis"), allow(dead_code))]
+fn redact_redis_url(url: &str) -> String {
+ let Some((scheme, rest)) = url.split_once("://") else {
+ return "<unparseable redis url>".to_string();
+ };
+ let authority = &rest[..rest.find(['/', '?', '#']).unwrap_or(rest.len())];
+ let host = authority.rsplit('@').next().unwrap_or(authority);
+ format!("{scheme}://{host}")
+}
+
+#[cfg(test)]
+mod tests {
+ use super::redact_redis_url;
+
+ #[test]
+ fn redis_url_credentials_are_not_logged() {
+ let out = redact_redis_url("redis://:secret@host:6379/0");
+ assert!(!out.contains("secret"), "{out}");
+ assert_eq!(out, "redis://host:6379");
+ let out = redact_redis_url("rediss://user:p@[email protected]?password=secret");
+ assert_eq!(out, "rediss://cache.example.com");
+ assert_eq!(
+ redact_redis_url("redis://localhost:6379"),
+ "redis://localhost:6379"
+ );
+ assert!(!redact_redis_url("secret-without-scheme").contains("secret"));
+ }
+}
diff --git a/crates/headroom-core/src/ccr/backends/redis.rs b/crates/headroom-core/src/ccr/backends/redis.rs
index a05c8b45a..d834c0747 100644
--- a/crates/headroom-core/src/ccr/backends/redis.rs
+++ b/crates/headroom-core/src/ccr/backends/redis.rs
@@ -29,7 +29,7 @@
use redis::Commands;
-use crate::ccr::{max_lifetime_for, CcrStore};
+use crate::ccr::{max_lifetime_for, secs_i64, CcrStore};
/// Key prefix applied to every CCR entry. Configurable per-deployment
/// so multiple proxies sharing one Redis don't collide.
@@ -190,7 +190,7 @@ impl CcrStore for RedisCcrStore {
let _: redis::RedisResult<()> = conn.del(&key);
return None;
}
- let rearm: redis::RedisResult<()> = conn.expire(&key, new_ttl as i64);
+ let rearm: redis::RedisResult<()> = conn.expire(&key, secs_i64(new_ttl));
if let Err(err) = rearm {
tracing::warn!(
target = "ccr.redis",
diff --git a/crates/headroom-core/src/ccr/backends/sqlite.rs b/crates/headroom-core/src/ccr/backends/sqlite.rs
index 987fcbddb..f0c7f3d5f 100644
--- a/crates/headroom-core/src/ccr/backends/sqlite.rs
+++ b/crates/headroom-core/src/ccr/backends/sqlite.rs
@@ -48,7 +48,7 @@ use std::time::{SystemTime, UNIX_EPOCH};
use rusqlite::{params, Connection, OptionalExtension};
-use crate::ccr::{max_lifetime_for, CcrStore};
+use crate::ccr::{max_lifetime_for, secs_i64, CcrStore};
/// SQLite-backed CCR store.
pub struct SqliteCcrStore {
@@ -160,7 +160,7 @@ impl SqliteCcrStore {
"DELETE FROM ccr_entries
WHERE last_accessed + ttl_seconds < ?1
OR created_at + ?2 < ?1",
- params![now as i64, self.max_lifetime_seconds as i64],
+ params![secs_i64(now), secs_i64(self.max_lifetime_seconds)],
)?;
Ok(purged)
}
@@ -194,7 +194,7 @@ impl SqliteCcrStore {
WHERE hash = ?1
AND last_accessed + ttl_seconds >= ?2
AND created_at + ?3 >= ?2",
- params![hash, now as i64, self.max_lifetime_seconds as i64],
+ params![hash, secs_i64(now), secs_i64(self.max_lifetime_seconds)],
|r| r.get::<_, Vec<u8>>(0),
)
.optional()
@@ -213,7 +213,7 @@ impl SqliteCcrStore {
// row's idle clock. Still under the same mutex as the lookup.
if let Err(err) = conn.execute(
"UPDATE ccr_entries SET last_accessed = ?2 WHERE hash = ?1",
- params![hash, now as i64],
+ params![hash, secs_i64(now)],
) {
tracing::warn!(
target = "ccr.sqlite",
@@ -244,8 +244,8 @@ impl CcrStore for SqliteCcrStore {
params![
hash,
payload.as_bytes(),
- now as i64,
- self.default_ttl_seconds as i64,
+ secs_i64(now),
+ secs_i64(self.default_ttl_seconds),
],
);
// Loud-failure rule: surface as a structured warning. Caller
@@ -329,4 +329,13 @@ mod tests {
assert_eq!(store.get_at(&hash, 111), None);
assert_eq!(store.len(), 0, "expired row must be purged");
}
+
+ #[test]
+ fn huge_ttl_means_keep_not_expire_immediately() {
+ let dir = tempfile::tempdir().expect("tempdir");
+ let store = SqliteCcrStore::open(dir.path().join("ccr.sqlite"), u64::MAX)
+ .expect("open sqlite store");
+ store.put("k", "payload");
+ assert_eq!(store.get("k").as_deref(), Some("payload"));
+ }
}
diff --git a/crates/headroom-core/src/ccr/mod.rs b/crates/headroom-core/src/ccr/mod.rs
index cf076f848..901af113a 100644
--- a/crates/headroom-core/src/ccr/mod.rs
+++ b/crates/headroom-core/src/ccr/mod.rs
@@ -79,6 +79,12 @@ pub fn max_lifetime_for(idle_ttl: Duration) -> Duration {
idle_ttl.saturating_mul(DEFAULT_MAX_LIFETIME_MULTIPLIER)
}
+/// `u64` seconds -> `i64` for SQLite/Redis. Saturates: `u64::MAX` ("never
+/// expire") must not wrap to -1 and expire at once.
+pub(crate) fn secs_i64(secs: u64) -> i64 {
+ i64::try_from(secs).unwrap_or(i64::MAX)
+}
+
/// Compute the canonical CCR key for `payload`. BLAKE3 → first 24 hex
/// chars (96 bits — collision-resistant for the bounded LRU population
/// the proxy will hold). Centralized here so every call site (live-zone
diff --git a/headroom/proxy/auth_policy.py b/headroom/proxy/auth_policy.py
index fc7f43941..cd481fd05 100644
--- a/headroom/proxy/auth_policy.py
+++ b/headroom/proxy/auth_policy.py
@@ -87,7 +87,9 @@ def classify_auth_signals(signals: AuthSignals) -> AuthMode:
# Only the scheme is case-folded; the credential itself stays case-sensitive.
scheme, sep, credentials = auth.partition(" ")
if sep and scheme.lower() == "bearer":
- token = credentials
+ # RFC 9110 §11.4 allows one or more spaces between the scheme and the
+ # credentials; trim them so `Bearer sk-ant-oat...` still matches.
+ token = credentials.lstrip(" ")
if token.startswith("sk-ant-oat"):
return AuthMode.OAUTH
if token.startswith("sk-ant-api") or token.startswith("sk-"):
diff --git a/tests/test_auth_policy.py b/tests/test_auth_policy.py
index 1066d3f93..35a43ab5d 100644
--- a/tests/test_auth_policy.py
+++ b/tests/test_auth_policy.py
@@ -90,6 +90,16 @@ def test_bearer_case_insensitive_preserves_oauth_and_jwt_shapes() -> None:
assert classify_auth_signals(AuthSignals(authorization=f"bearer {jwt}")) is AuthMode.OAUTH
+def test_extra_spaces_between_scheme_and_token_are_ignored() -> None:
+ # Same answer as the Rust classifier (headroom_core::auth_mode::classify).
+ assert classify_auth_signals(AuthSignals(authorization="Bearer sk-ant-oat01-abc")) is (
+ AuthMode.OAUTH
+ )
+ assert classify_auth_signals(AuthSignals(authorization="bearer sk-ant-api03-abc")) is (
+ AuthMode.PAYG
+ )
+
+
def test_credential_case_is_not_folded() -> None:
# Only the scheme is case-folded; the token keeps its case, so a PAYG
# `sk-...` prefix still matches exactly (it is lowercase by construction).