Skip to content

fix: cache confusion risk with composite-key approach#587

Merged
antoatta85 merged 8 commits intomasterfrom
fix/cacheKeyBuilder-collisions
Apr 7, 2026
Merged

fix: cache confusion risk with composite-key approach#587
antoatta85 merged 8 commits intomasterfrom
fix/cacheKeyBuilder-collisions

Conversation

@antoatta85
Copy link
Copy Markdown
Collaborator

@antoatta85 antoatta85 commented Apr 2, 2026

fix: prevent cache confusion via custom cacheKeyBuilder collisions

When cache is enabled with a custom cacheKeyBuilder that produces colliding keys,
the verifier could return a cached payload from a different token — leading to identity
confusion, privilege escalation, or cross-tenant data access.

Fix

When a custom cacheKeyBuilder is provided a process warning is emitted with code FAST_JWT_CACHE_KEY_BUILDER_SECURITY_RISK.
This warns users that a poorly implemented key builder can lead to cache collisions and identity/authorization bypass.

The README Caching section already had a [!WARNING] callout.

Tests added

  • caching - sync - custom cacheKeyBuilder emits security warning — asserts the warning is emitted (with the correct code and message) when a custom cacheKeyBuilder is provided alongside cache: true.

  • caching - sync - default cacheKeyBuilder does not emit security warning — asserts no warning is emitted when the default hashToken builder is used.

FIXES #588

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR mitigates a security issue where enabling cache with a collision-prone custom cacheKeyBuilder could cause the verifier to return cached claims for a different token (cache confusion / identity mix-up).

Changes:

  • Compose custom cacheKeyBuilder output with hashToken(token) to make cache keys collision-safe.
  • Update existing custom cache-key test expectations to match the new effective key format.
  • Add regression tests covering multiple collision-prone cacheKeyBuilder patterns from the advisory.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
src/verifier.js Wraps custom cache key builders by appending a token hash to prevent cache confusion via key collisions.
test/verifier.spec.js Updates cache key assertions and adds regression tests for common collision patterns.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/verifier.js Outdated
Comment thread src/verifier.js Outdated
@SociableSteve
Copy link
Copy Markdown
Contributor

We shouldn't be changing the user supplied mechanisms for building cache keys, including adding some kind of hashing. Many people will provide their own builder for either performance benefits or for domain specific needs, so adding/changing the hash doesn't work.

I see two options here:

  • Validate some or all of the token contents on the way out of the cache
  • Update the README to be more explicit on the dangers

The former will add overhead to execution time so probably would negate the 'performance benefits' reasoning for people using their own builder, and given the shape of the payload could be anything it would be hard to do this.

In this case I would suggest we update the related documentation to highlight the risk involved in using the non-standard key builder. Sometimes the best solutions involve no code at all, and this is probably one of those cases.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 5 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/verifier.js Outdated
Comment thread README.md Outdated
Comment thread src/verifier.js
Comment thread test/verifier.spec.js
Comment thread test/verifier.spec.js Outdated
antoatta85 and others added 2 commits April 7, 2026 16:35
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@antoatta85 antoatta85 merged commit de12105 into master Apr 7, 2026
7 checks passed
@antoatta85 antoatta85 deleted the fix/cacheKeyBuilder-collisions branch April 7, 2026 14:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Deal with Sec Adv: Cache Confusion via cacheKeyBuilder

3 participants