🤖 The AI Prompt That Checks Whether a Caching Layer Can Return Another Tenant’s Data Because the Cache Key Omits the Tenant ID
Caching code gets reviewed for hit rate and invalidation logic far more often than for whether its cache key actually isolates one tenant’s data from another’s in a multi-tenant system. A key built only from a resource ID — without the tenant, account, or organization ID folded in — can return Tenant A’s cached result to Tenant B simply because they both happened to request a resource with the same ID in their own separate data sets. This prompt isolates that one question so it never gets skipped in a general code review.
🔎 The Prompt
You are reviewing caching code in a multi-tenant application where each tenant's data must stay fully isolated from every other tenant's. For the caching code below, answer only these questions: 1. What exact fields make up the cache key for each cached value? Quote the key construction code or format string. 2. Does the cache key include the tenant, account, or organization identifier? If not, could two different tenants ever produce the same key for logically different data (e.g. both have a "resource ID 42")? 3. Is the tenant identifier used to build the key read from a trusted source (an authenticated session/claim) or from something the caller supplies directly (a query parameter, a request body field)? 4. If the cache is shared infrastructure (a shared Redis instance, a shared in-memory cache across requests), confirm there is no code path that could read a key before confirming which tenant is making the current request. --- CACHING CODE --- <paste the cache read/write code, including key construction></paste>
✅ What This Prompt Catches
- A cache key built from just a resource ID (`”product:42″`) in a system where every tenant has its own independent product catalog, so Tenant B’s request for their own product 42 can be served Tenant A’s cached result the moment both IDs happen to collide.
- A tenant ID that IS included in the key, but is read from a request header or query parameter the client controls rather than from the authenticated session — letting a malicious caller simply supply a different tenant ID to probe another tenant’s cached data directly.
- A cache populated by a background job that runs without per-tenant context at all, writing a key that no per-request tenant check was ever applied to in the first place.
🚨 Why This Is Dangerous
- This prompt only evaluates the key construction logic you paste in — it can’t verify that every single call site in a large codebase builds the key the same safe way, so grep for every place that reads from or writes to this cache before concluding the whole system is isolated.
A cache key that forgets the tenant isn’t a performance bug waiting to happen — it’s a data breach waiting for two tenants to ask for the same resource ID on the same day.
