undici vulnerable to cross-user cache disclosure via Vary added on background 304 revalidation
Description
Impact
When a shared cache (interceptors.cache({ type: 'shared' }) with MemoryCacheStore) holds a stale entry inside its stale-while-revalidate window, undici serves it and revalidates in the background. If the background revalidation receives a 304 Not Modified that adds a Vary field the stored response did not have (for example Vary: x-tenant), undici does not treat the representation as changed. It re-stores the old, unvaried body under the background caller's value of that header and marks it fresh for the 304's lifetime. Later callers with that header value are then served a body that was generated for a different request, possibly for another user or tenant, without the origin being contacted.
The synchronous revalidation path is not affected by this issue. Applications are affected only when they use a shared cache, the origin first returns a cacheable response without Vary and with stale-while-revalidate, and a later background 304 introduces Vary. The affected path is present from undici 7.17.0 through the patched versions.
Patches
Upgrade to 7.30.4 or 8.14.1. A background revalidation 304 that changes the stored response's Vary set, or whose final target is not the cached URL, now removes the stored entry instead of refreshing it.
Workarounds
Use a private cache (type: 'private'), disable stale-while-revalidate reuse for per-user resources, or make sure the origin sends the final Vary on the initial response.