fix(audit-2026-06-11/revocation-lag): evict PrincipalCache on credential change

The PrincipalCache (token-hash -> resolved Principal, 60s TTL) had no
eviction hook, so deactivation / password change / API-key revocation
flipped the DB rows but the cache kept serving the stale principal for
up to the TTL window. Closes the audit's PrincipalCache revocation-lag
Medium and greens authz::deactivating_user_revokes_their_api_keys.

- PrincipalCache::evict_user(user_id) + evict_token(hash).
- One shared cache Arc threaded into AuthState / AdminsState /
  ApiKeysState (a per-state cache would let the middleware's own copy
  keep authenticating a revoked principal).
- Evict on: deactivation, password change (both evict_user), logout
  (evict_token, precise), single API-key delete (evict_user).
- H-E1 note: DB-write invalidation stays best-effort by design (a blip
  must not undo the rotation); the cache evict is what makes it take
  effect on the next request.
- Renamed bearer_and_cookie_produce_same_principal ->
  session_token_and_api_key_produce_same_principal (cookie auth was
  removed in C-1; the test never tested cookies).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
MechaCat02
2026-06-12 18:12:54 +02:00
parent 19f3647f0e
commit 31ecfae86e
6 changed files with 116 additions and 4 deletions

View File

@@ -92,6 +92,32 @@ impl PrincipalCache {
}
guard.insert(token_hash, (Instant::now(), principal));
}
/// Evict every cached principal belonging to `user_id`.
///
/// Audit 2026-06-11 (PrincipalCache revocation-lag, Medium) — the
/// revocation paths (`set_active(false)`, password change,
/// API-key delete) flip the backing DB rows, but a cached principal
/// keeps authenticating for up to [`PRINCIPAL_CACHE_TTL`] unless it
/// is evicted. Callers invoke this right after the DB mutation so a
/// just-revoked credential stops working on the next request. The
/// cache value carries the resolved [`Principal`], so we can target
/// the user without knowing which token hashes belong to them.
pub fn evict_user(&self, user_id: AdminUserId) {
let Ok(mut guard) = self.inner.lock() else {
return;
};
guard.retain(|_, (_, p)| p.user_id != user_id);
}
/// Evict a single cached entry by its token hash. Used on logout,
/// where exactly one session token is being invalidated and we
/// don't want to disturb the user's other live sessions.
pub fn evict_token(&self, token_hash: &str) {
if let Ok(mut guard) = self.inner.lock() {
guard.remove(token_hash);
}
}
}
/// Prefix on the wire that selects the API-key path. The body that
@@ -471,4 +497,46 @@ mod tests {
assert!(p.scopes.is_none());
assert!(p.app_binding.is_none());
}
fn principal_for(user_id: AdminUserId) -> Principal {
Principal {
user_id,
instance_role: InstanceRole::Owner,
scopes: None,
app_binding: None,
}
}
#[test]
fn evict_user_drops_only_that_users_entries() {
// Audit 2026-06-11 (PrincipalCache revocation-lag).
let cache = PrincipalCache::new();
let alice = AdminUserId::new();
let bob = AdminUserId::new();
cache.insert("hash-alice-1".into(), principal_for(alice));
cache.insert("hash-alice-2".into(), principal_for(alice));
cache.insert("hash-bob-1".into(), principal_for(bob));
cache.evict_user(alice);
assert!(cache.get("hash-alice-1").is_none());
assert!(cache.get("hash-alice-2").is_none());
// Bob is untouched.
assert!(cache.get("hash-bob-1").is_some());
}
#[test]
fn evict_token_drops_only_that_entry() {
let cache = PrincipalCache::new();
let alice = AdminUserId::new();
cache.insert("hash-1".into(), principal_for(alice));
cache.insert("hash-2".into(), principal_for(alice));
cache.evict_token("hash-1");
// Logout drops exactly one session; the user's other live
// session stays cached.
assert!(cache.get("hash-1").is_none());
assert!(cache.get("hash-2").is_some());
}
}