JINNBITSResearch & insights

The Cache Key That Quietly Crosses a Tenant Boundary

Learn why a correct authorization check can still return another tenant's data, then use a focused cache exercise to expose and repair the boundary.

Read this sequence as a sixty-second security exercise. Tenant Cedar requests report 17. The service verifies Cedar's membership, loads Cedar's report, and caches the result under report:17. Tenant Harbor then requests its own report 17. Membership verification succeeds again. The cache returns Cedar's report.

No authentication step was skipped. Both users were entitled to request a report. The defect lives in the name used to remember the answer. The cache key represents fewer distinctions than the protected resource does.

The compact lesson is useful, but recognizing the pattern is only the first step. To transfer it into real code review, a developer needs to reason about identity, representation, invalidation, and the order of operations. This deeper exercise builds those connections without assuming a particular framework or caching product.

First prediction: what should the key mean?

A key is a claim that two requests can share one stored result. If the service uses only a report number, it claims that every request for that number is equivalent. In a system where report numbers are unique only within a tenant, that equivalence is false.

Adding the authenticated tenant identifier fixes the immediate collision. Conceptually, the key becomes a tuple containing the tenant and report identifiers. Use an unambiguous encoding rather than naive concatenation: the pairs ab plus c and a plus bc must not produce the same string. Separators require escaping or a structured serialization when identifiers can contain them.

The tenant value must come from an authoritative access context. Taking a tenant identifier from a query parameter and placing it in the key does not establish access to that tenant. First resolve the caller's permitted scope; then use that resolved scope consistently for the database lookup and cache identity.

Ask yourself what happens when the request names one tenant but the session belongs to another. A sound implementation rejects the mismatch or deliberately selects an authorized tenant. It does not let the cache lookup become a shortcut around the decision.

A small table exposes the collision

Imagine four requests: Cedar report 17, Harbor report 17, Cedar report 18, and Harbor report 18. A key based solely on report number creates two cache entries for four protected resources. A tenant-qualified key creates four. The extra entries are not waste; they preserve the distinctions the authorization model already requires.

The example below models 100 requests, evenly divided between two tenants, all asking for their own report 17. Cedar warms the cache first. Under the defective key, all 50 Harbor requests receive the wrong tenant's cached object. With a tenant-qualified key, none do, assuming the rest of the path behaves correctly.

Figure 1 — A deliberately constructed collision affects half the requests in this exercise. Source: worked example, not an estimate of real application defect rates.

Second prediction: is tenant plus report sufficient?

Now add two roles within Cedar. A manager can see every column, while a contractor sees a redacted view. If the cache stores the final rendered representation, the manager's response must not be reusable for the contractor merely because both belong to Cedar.

There are two broad designs. Cache a canonical object that remains inside the service, then apply authorization and redaction for every response. Or cache the final representation using a key that includes every relevant permission and representation dimension. Neither design eliminates the need to understand the data flow.

Caching canonical data simplifies reuse but increases the importance of never returning the cached object directly. A helper introduced later might bypass the redaction step. Caching final responses can reduce rendering cost, but role names alone may be insufficient if permissions differ by object or change independently of the role label.

Other representation dimensions include language, document version, selected fields, and output format. Some affect only correctness; others affect confidentiality. Treat them explicitly. A key that grows endlessly is a signal to reconsider what layer should be cached, rather than evidence that caching itself is impossible.

Permission changes create a time dimension

Suppose a user loses access at 10:00 while a response cached at 09:59 remains valid for ten minutes. If authorization is evaluated before every cache read, the new request can be denied even though the data remains cached. If the cache sits in front of the authorization decision, the old response may remain available.

This is why short cache lifetimes are not a complete fix. A one-minute leak is still a leak, and the effective lifetime may include downstream caches. The architectural question is whether each request crosses the required authorization boundary before protected bytes are released.

For permission-dependent representations, a policy version can help invalidate stale results. It must be bound to the current authoritative policy state; simply storing an old version in a session does not make it current. When policy freshness cannot be established, define a deliberate failure behavior instead of assuming that the cached view remains acceptable.

In an illustrative exercise, a permission change occurs at minute zero. A cache-only path keeps serving an obsolete representation until its ten-minute lifetime ends. A path that checks current access before release denies subsequent requests immediately, under the exercise's assumption of instantaneous policy propagation.

Figure 2 — Cache placement determines whether stale content can bypass a changed permission. Source: illustrative model with immediate policy propagation and a ten-minute cache lifetime.

Build a regression that cannot pass accidentally

Use two synthetic tenants with deliberately overlapping report identifiers and visibly different contents. A weak test uses different identifiers and never creates a collision. Another weak test clears the cache between requests, preventing the cross-request behavior that causes the defect.

The meaningful sequence warms the cache with Cedar, requests Harbor without clearing it, and verifies Harbor's exact content. Repeat in the reverse order. An implementation can appear safe in one direction if a fixture happens to create the cache entry expected by the assertion.

Then test permission variants within one tenant. Warm the privileged representation, request the restricted one, and confirm that excluded fields are absent. Check absence structurally rather than searching a rendered string: an extra field may survive in a hidden payload even if the page does not display it.

Finally, change access while the cache remains warm. The next request should follow the documented policy for revocation and freshness. This is a behavioral assertion about the security boundary, not a test that a key contains a particular substring. It will remain useful if the implementation later moves to a different caching layer.

Turn the lesson into a review habit

For a short learning card, ask the learner to predict the second response before showing the explanation. Then change one condition: globally unique report IDs, role-specific fields, or a cache placed at the edge. The goal is to make the learner identify which equivalence assumption changed.

A useful review prompt is: which requests are allowed to share these bytes? Answering it requires naming the caller scope, the object, the representation, and the validity period. If those answers differ from what the key and cache placement enforce, there is a design gap.

Do not measure mastery by whether someone remembers the phrase include the tenant ID. That phrase is a starting repair, not a universal rule. Mastery appears when the developer can explain why a cache is safe for one representation and unsafe for another, construct a collision test, and preserve the authorization boundary through a later refactor.