From f7f56ab1cc23ad7ad8ccc973442ced235439fd33 Mon Sep 17 00:00:00 2001 From: Sharang Parnerkar <30073382+mighty840@users.noreply.github.com> Date: Tue, 13 Oct 2026 23:30:33 +0200 Subject: [PATCH] fix(api): cap the API-key verdict cache at 10,000 entries Callers choose the keys, so cached invalid verdicts must not grow without end: at the cap expired verdicts are dropped first, and if none are, the new verdict is answered but not kept. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01EYKjxacrr6K88DruUmP48p --- compliance-agent/src/registry.rs | 47 +++++++++++++++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) diff --git a/compliance-agent/src/registry.rs b/compliance-agent/src/registry.rs index abf573a..c3112e0 100644 --- a/compliance-agent/src/registry.rs +++ b/compliance-agent/src/registry.rs @@ -25,6 +25,10 @@ use compliance_core::AgentConfig; /// How long an entitlement answer is trusted before it is re-fetched. pub const ENTITLEMENT_TTL: Duration = Duration::from_secs(60); +/// The most API-key verdicts held at once. Callers choose the keys, so an +/// unbounded cache of invalid ones would grow without end. +const API_KEY_CACHE_MAX: usize = 10_000; + /// How long an API-key verdict is reused. A revoked key therefore keeps /// working for up to this long; past it, an unreachable registry means `503`. pub const API_KEY_TTL: Duration = Duration::from_secs(60); @@ -509,9 +513,23 @@ impl RegistryClient { } } let verdict = self.fetch_api_key_verdict(key).await?; + self.cache_api_key_verdict(digest, &verdict); + Ok(verdict) + } + + /// Keep `verdict` under `digest`. At [`API_KEY_CACHE_MAX`] the expired + /// verdicts go first; if none have, this one is not kept (the request + /// still gets it). + fn cache_api_key_verdict(&self, digest: String, verdict: &ApiKeyVerify) { + if self.api_keys.len() >= API_KEY_CACHE_MAX { + self.api_keys + .retain(|_, (at, _)| at.elapsed() < API_KEY_TTL); + if self.api_keys.len() >= API_KEY_CACHE_MAX { + return; + } + } self.api_keys .insert(digest, (Instant::now(), verdict.clone())); - Ok(verdict) } async fn fetch_api_key_verdict(&self, key: &str) -> Result { @@ -735,6 +753,33 @@ mod tests { assert!(!err.contains("bp_good"), "{err}"); } + #[tokio::test] + async fn a_full_cache_drops_expired_verdicts_and_never_grows_past_its_cap() { + let (client, _, _) = verify_registry().await; + let verdict = client.verify_api_key("bp_good").await.unwrap(); + let aged = Instant::now() + .checked_sub(API_KEY_TTL + Duration::from_secs(1)) + .unwrap(); + for i in 0..API_KEY_CACHE_MAX { + client + .api_keys + .insert(format!("old-{i}"), (aged, verdict.clone())); + } + client.verify_api_key("bp_new").await.unwrap(); + assert!(client.api_keys.len() < 10, "{}", client.api_keys.len()); + assert!(client.api_keys.contains_key(&key_digest("bp_new"))); + + // Full of fresh verdicts: the new one is answered but not kept. + for i in 0..API_KEY_CACHE_MAX { + client + .api_keys + .insert(format!("new-{i}"), (Instant::now(), verdict.clone())); + } + let before = client.api_keys.len(); + assert!(client.verify_api_key("bp_newer").await.unwrap().valid); + assert_eq!(client.api_keys.len(), before); + } + #[tokio::test] async fn the_cache_holds_a_digest_never_the_key() { let (client, _, _) = verify_registry().await;