Advertisement
Intermediate ASP.NET Core, Caching

What is the risk of caching sensitive or user-specific data with an incorrectly scoped cache key, and how do you prevent cross-user data leakage?

Short answer

Caching user-specific data (like another user's personal dashboard) under a shared or predictable cache key risks serving one user's private data to a different user -- a critical security bug -- so cache keys for personalized data must always incorporate the user's identity…

Save ASP.NET Core mock interview
Advertisement
AModel answer
Caching user-specific data (like another user's personal dashboard) under a shared or predictable cache key risks serving one user's private data to a different user -- a critical security bug -- so cache keys for personalized data must always incorporate the user's identity, and Output Caching's VaryByHeader/VaryByValue options must correctly account for authentication state to avoid this exact class of vulnerability.
// DANGEROUS: same cache key regardless of which user is requesting
var cacheKey = "user-dashboard";  // BUG!

// CORRECT: key includes user identity
var cacheKey = $"user-dashboard-{userId}";

// Output Caching: vary by an auth-derived value
app.MapGet("/dashboard", GetDashboard).CacheOutput(policy => policy.SetVaryByHeader("Authorization"));
Real-world example
A critical security incident occurs when a response-caching misconfiguration caches a personalized dashboard response without varying by user identity, causing one user's private data to be served to the next user who happened to hit the same cached response.
Likely follow-ups
  • How does VaryByHeader specifically help prevent this class of bug?
  • What data should never be cached server-side regardless of key scoping?
Related topics