The request before yours matters: testing cache isolation in both orders
Alice's test passes. Bob's test passes. Then Alice makes her request first, and Bob receives her dashboard. If you've worked on a multi-user or multi-tenant app with a cache in front of it, this bug probably looks famil
Alice's test passes. Bob's test passes. Then Alice makes her request first, and Bob receives her dashboard.
If you've worked on a multi-user or multi-tenant app with a cache in front of it, this bug probably looks familiar. This post covers why normal tests miss it, a small experiment that catches it, and the open-source CLI I built to run that experiment: CacheWitness.
The bug fits in one line
Here's the entire difference between the broken and fixed versions of the demo server that ships with the tool:
// broken: every user shares one cache entry
const key = 'dashboard';
// fixed: the entry is scoped to the tenant
const key = `dashboard:${account.tenant}`;
The same shape turns up all over the place:
- a per-user response cached under a URL-only key,
- tenant settings cached without the tenant ID,
- a price cached without the
currencyquery parameter, - a CDN or reverse proxy that ignores a header the app relies on (
Authorization,Accept-Language, โฆ).
Why your tests stay green
Most tests run one identity per test, often against a fresh cache. Under those conditions the shared key is invisible: whoever asks first always gets their own data.
The failure needs two things your test suite rarely combines:
- a different identity primes the cache first, and
- the second identity asks for the same endpoint before the entry expires.
So you need to test the order, not just the identities.
The experiment
CacheWitness tests one question:
Does a target variant receive its normal response after a different variant primes the same endpoint?
You supply one JSON GET endpoint, two or more variants (headers and/or query params), and a synchronous reset endpoint for the test cache. Then:
1. Cold baselines. For each variant, reset โ request โ normalize JSON โ fingerprint, repeated (twice by default). If the repetitions disagree, the baseline is unstable and the affected pairs are reported as inconclusive, not passed.
2. Every ordered pair. For A and B:
reset โ A โ A again โ B
and separately B โ A. With n variants that's n ร (n โ 1) ordered checks.
3. Compare.
| Observation | Verdict |
|---|---|
| B equals cold B | pass |
| B equals cold A | contamination |
| B matches neither | drift (investigate) |
| cold A equals cold B | indistinguishable (inconclusive) |
What it looks like
$ cachewitness demo
CacheWitness โ FAIL
2 variants ยท 2 ordered pairs ยท 16 HTTP requests
0 passed ยท 2 contamination ยท 0 drift ยท 0 indistinguishable ยท 0 unstable
alice โ bob: contamination
Target received the source baseline instead of its own baseline.
Changed fields: /body/balanceCents, /body/owner, /body/tenant
bob โ alice: contamination
Target received the source baseline instead of its own baseline.
Changed fields: /body/balanceCents, /body/owner, /body/tenant
With the fixed key:
$ cachewitness demo --fixed
CacheWitness โ PASS
2 variants ยท 2 ordered pairs ยท 16 HTTP requests
2 passed ยท 0 contamination ยท 0 drift ยท 0 indistinguishable ยท 0 unstable
--html report.html writes a single offline file with the pair matrix, baseline stability, and per-check evidence. Reports keep variant names, JSON pointer paths, and fingerprints. They don't keep raw response values, credentials, or full URLs, so they're reasonable to attach to a CI run. (Field names and hashes can still be sensitive; the SECURITY.md covers this.)
Put it in CI
- name: Check response isolation
uses: godz1llla/[email protected]
with:
config: cachewitness.json
env:
CACHEWITNESS_BASE_URL: http://127.0.0.1:3000
CACHEWITNESS_ALICE_TOKEN: ${{ secrets.TEST_ALICE_TOKEN }}
CACHEWITNESS_BOB_TOKEN: ${{ secrets.TEST_BOB_TOKEN }}
CACHEWITNESS_RESET_TOKEN: ${{ secrets.TEST_RESET_TOKEN }}
Contamination, drift, or an inconclusive audit fails the job, and you get a job summary plus JSON/HTML reports to upload as artifacts. cachewitness init --preset auth|tenant|currency generates a starting config.
What it doesn't prove
I'd rather be upfront about this:
- A witness isn't a root cause. Shared session state, a mutable global, or a broken reset can produce the same output. The report tells you what changed and in which order; you still trace why.
- A pass isn't universal isolation. It covers the endpoint, variants, sequence, and compared fields you supplied. Concurrency and races aren't tested yet.
- It doesn't see inside your cache. It observes HTTP responses only.
Try it
Node.js 22+, zero runtime dependencies, MIT:
npm install --global https://github.com/godz1llla/cachewitness/releases/download/v0.2.0/godz1llla-cachewitness-0.2.0.tgz
cachewitness demo --html broken.html
cachewitness demo --fixed --html fixed.html
The first command exits with code 1 on purpose: the demo server is deliberately broken.
Repo: https://github.com/godz1llla/cachewitness
If you've hit a cache-isolation bug this sequence wouldn't catch, I'd love to hear about it in the comments or in an issue.
Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes โ full credit and traffic to the original publisher.