.NET › Caching (In-Memory, Distributed & Redis)
What are the risks of caching sensitive or user-specific data, and how do you avoid cache poisoning across users?
IntermediateAnswer
Caching sensitive data (like another user's personal information) under a shared or predictable cache key risks accidentally serving one user's private data to another -- a critical security bug -- so cache keys for user-specific data must always incorporate the user's identity (or session) to prevent cross-user leakage, and truly sensitive data (passwords, tokens) generally shouldn't be cached at all, or only in short-lived, tightly-scoped caches.
// DANGEROUS: shared key regardless of user
var cacheKey = "user-profile"; // BUG: same key for every user!
// CORRECT: key includes user identity
var cacheKey = $"user-profile-{userId}";Real-world example A critical security incident occurs when a response-caching misconfiguration caches a personalized dashboard response without varying by user, causing one user's private financial data to be served to the next user who happened to hit the same cached response.