mirror of
https://github.com/headroomlabs-ai/headroom.git
synced 2026-11-22 13:29:19 +08:00
fix(core): case-insensitive Bearer scheme, clamp SQLite CCR TTLs, redact the Redis URL in logs (#4561)
## Description I fix the three small `headroom-core` defects from #4551. None of them changes a shipped path today (the only production caller of `auth_mode::classify` coerces every result to OAuth, and the SQLite and Redis CCR backends are only built from tests and benches), so this is hardening before those paths get wired in. 1. `auth_mode::classify` matched the Bearer scheme case-sensitively and did not trim. `bearer sk-ant-api03-...` was classified OAuth (should be Payg) and `Bearer sk-ant-oat01-...` (two spaces) was classified Payg (should be OAuth). The scheme is now matched with `eq_ignore_ascii_case` (RFC 9110 section 11.1) and the token is `trim_start`ed. 2. SQLite CCR: `u64 as i64` on TTLs wrapped `u64::MAX` to -1, so a put followed by a get returned `None`. A small `ccr::secs_i64` helper saturates to `i64::MAX`, used at every non-test cast in `sqlite.rs`. `redis.rs` had the same overflow at `conn.expire(&key, new_ttl as i64)` (`new_ttl` comes from `max_lifetime_for`, which saturates), so I switched it to the same helper. 3. `from_config` logged the full Redis URL at info, including `redis://:password@host`. It now logs `scheme://host[:port]` via a small `redact_redis_url` (userinfo, path and query dropped; a string with no `://` becomes a placeholder). No new dependency. Fixes #4551 ## Type of Change - [x] Bug fix (non-breaking change that fixes an issue) - [ ] New feature (non-breaking change that adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to change) - [ ] Documentation update - [ ] Performance improvement - [ ] Code refactoring (no functional changes) ## Changes Made - `crates/headroom-core/src/auth_mode.rs`: case-insensitive `Bearer ` scheme, `trim_start` on the token; test `bearer_scheme_is_case_insensitive_and_token_is_trimmed` (the four cases from the issue plus an upper-case scheme, fake tokens only). - `crates/headroom-core/src/ccr/mod.rs`: `pub(crate) fn secs_i64(u64) -> i64` (saturating). - `crates/headroom-core/src/ccr/backends/sqlite.rs`: use `secs_i64` at the TTL/lifetime/timestamp casts; test `huge_ttl_means_keep_not_expire_immediately` (temp dir via `tempfile`, `ttl = u64::MAX`). - `crates/headroom-core/src/ccr/backends/redis.rs`: `expire(&key, secs_i64(new_ttl))`. - `crates/headroom-core/src/ccr/backends/mod.rs`: `redact_redis_url`, used in the `ccr_backend_initialized` log; test `redis_url_credentials_are_not_logged`. ## Testing - [ ] Unit tests pass (`pytest`) - [ ] Linting passes (`ruff check .`) - [ ] Type checking passes (`mypy headroom`) - [x] New tests added for new functionality - [x] Manual testing performed Rust-only change, so the Python checks above are not applicable. I ran `cargo test -p headroom-core --lib`, `cargo clippy -p headroom-core --lib --tests -- -D warnings`, `cargo fmt -p headroom-core -- --check`, and `cargo check -p headroom-core --lib --features redis`. ### Test Output Before (source fixes reverted, new tests kept): ```text test auth_mode::inline_tests::bearer_scheme_is_case_insensitive_and_token_is_trimmed ... FAILED test ccr::backends::sqlite::tests::huge_ttl_means_keep_not_expire_immediately ... FAILED left: OAuth right: Payg left: None right: Some("payload") test result: FAILED. 0 passed; 2 failed; 0 ignored; 0 measured; 982 filtered out ``` After: ```text test ccr::backends::tests::redis_url_credentials_are_not_logged ... ok test auth_mode::inline_tests::bearer_scheme_is_case_insensitive_and_token_is_trimmed ... ok test ccr::backends::sqlite::tests::huge_ttl_means_keep_not_expire_immediately ... ok test result: ok. 3 passed; 0 failed; 0 ignored; 0 measured; 981 filtered out cargo test -p headroom-core --lib test result: ok. 983 passed; 0 failed; 1 ignored; 0 measured; 0 filtered out cargo clippy -p headroom-core --lib --tests -- -D warnings Finished `dev` profile [unoptimized + debuginfo] target(s) cargo fmt -p headroom-core -- --check (exit 0) cargo check -p headroom-core --lib --features redis Finished ``` ## Real Behavior Proof - Environment: Windows 11, native Windows cargo, Rust 1.95.0 (pinned toolchain), clone of `main` at `a2d4fe72` plus this change. - Exact command / steps: `cargo test -p headroom-core --lib -- bearer_scheme huge_ttl redis_url`, once with the source fixes reverted (tests kept) and once with the fixes; then the full `--lib` run, clippy and fmt as above. - Observed result: with the fixes reverted the bearer and SQLite tests fail with exactly the values in the issue (`bearer ...` gives OAuth, `ttl = u64::MAX` put-then-get gives `None`); with the fixes all pass, 983 passed in the full lib run. - Not tested: no Redis server, so the Redis `expire` change is compile-checked (`--features redis`) only and the redaction is covered by a unit test of the helper, not by capturing a real log line. These paths are not wired into production today per the issue, so there is no end-to-end behavior to observe yet. The `Bearer` tab-separator case and non-ASCII schemes were not exercised beyond the code reading. ## Runtime Rollout Safety - Rollout-managed feature(s): none - Minimum rollout channel: n/a - Stable/default behavior changed: no for shipped paths; `classify` gives a different result only for non-canonical Bearer headers (lowercase scheme, extra spaces) and well-formed headers keep their classification - Kill switch / disable path: none needed; revert the PR - Unsafe override required: no - Qualification impact: none expected - Rollback path: revert this PR ## Review Readiness - [x] I have performed a self-review - [x] This PR is ready for human review ## Checklist - [x] My code follows the project's style guidelines - [x] I have performed a self-review of my code - [x] I have commented my code, particularly in hard-to-understand areas - [ ] I have made corresponding changes to the documentation (none needed) - [x] My changes generate no new warnings - [x] I have added tests that prove my fix is effective or that my feature works - [x] New and existing unit tests pass locally with my changes - [x] I did **not** edit `CHANGELOG.md` — it is generated by release-please from my Conventional Commit PR title (a CI guard enforces this) ## Additional Notes The three fixes are independent; I can split them if you prefer one PR per item. Existing tests in `crates/headroom-core/tests/auth_mode.rs` were not changed and still pass. Produced with AI assistance (Claude Code) operated by me (HetCreep); the tests were run on my machine against a clone of the repository. 🤖 Generated with [Claude Code](https://claude.com/claude-code) <!-- devin-review-badge-begin --> --- <a href="https://app.devin.ai/review/headroomlabs-ai/headroom/pull/4561" target="_blank"><picture><source media="(prefers-color-scheme: dark)" srcset="https://static.devin.ai/assets/gh-devin-review-dark.svg?v=4"><img src="https://static.devin.ai/assets/gh-devin-review-light.svg?v=4" alt="Devin Review"></picture></a> <!-- devin-review-badge-end --> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: JD Davis <mxjerrett@gmail.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
JD Davis
parent
c1dae5adae
commit
9e8cc64b48
@@ -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
|
||||
|
||||
@@ -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@ss@cache.example.com?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"));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -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"));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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-"):
|
||||
|
||||
@@ -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).
|
||||
|
||||
Reference in New Issue
Block a user