fix(security): bound CashuTokenDecoder cache by token length and byte cost (#1907)

This commit is contained in:
Jozef Koval authored and GitHub committed 2026-11-27 09:40:24 -10:00
1 parent e64e5be1e1
commit f464331202
2 files changed
+47 -15

No files matched your search

+17 -15
View File
@@ -89,26 +89,21 @@ enum CashuTokenDecoder {
private let cache: NSCache<NSString, DecodeCacheEntry> = {
let c = NSCache<NSString, DecodeCacheEntry>()
c.countLimit = 256
c.totalCostLimit = 1_024 * 1_024
return c
}()
func currentGeneration() -> UInt64 {
func lookup(for key: String) -> (entry: DecodeCacheEntry?, generation: UInt64) {
lock.lock()
defer { lock.unlock() }
return generation
}
func entry(for key: String) -> DecodeCacheEntry? {
lock.lock()
defer { lock.unlock() }
return cache.object(forKey: key as NSString)
return (cache.object(forKey: key as NSString), generation)
}
func store(_ info: TokenInfo?, for key: String, expectedGeneration: UInt64) {
lock.lock()
defer { lock.unlock() }
guard generation == expectedGeneration else { return }
cache.setObject(DecodeCacheEntry(info), forKey: key as NSString)
cache.setObject(DecodeCacheEntry(info), forKey: key as NSString, cost: max(1, key.utf8.count))
}
func reset() {
@@ -127,15 +122,22 @@ enum CashuTokenDecoder {
permissiveCache.reset()
}
#if DEBUG
static func hasCachedDecode(for raw: String) -> Bool {
permissiveCache.lookup(for: raw).entry != nil
}
#endif
static func decode(_ raw: String, strict: Bool = false) -> TokenInfo? {
let generation = strict ? 0 : permissiveCache.currentGeneration()
if !strict, let cached = permissiveCache.entry(for: raw) {
guard !strict, raw.utf8.count <= maxTokenLength else {
return decodeUncached(raw, strict: strict)
}
let (cached, generation) = permissiveCache.lookup(for: raw)
if let cached {
return cached.info
}
let decoded = decodeUncached(raw, strict: strict)
if !strict {
permissiveCache.store(decoded, for: raw, expectedGeneration: generation)
}
let decoded = decodeUncached(raw, strict: false)
permissiveCache.store(decoded, for: raw, expectedGeneration: generation)
return decoded
}
@@ -489,4 +489,34 @@ final class PanicWipeCoverageTests: XCTestCase {
XCTAssertEqual(Set(results).count, 1)
}
func test_cashuTokenDecoder_resetForPanic_evictsCachedToken() throws {
let payload: [String: Any] = [
"token": [[
"mint": "https://mint.example.com",
"proofs": [["amount": 10, "id": "009a1f293253e41e", "secret": "s", "C": "02c"] as [String: Any]]
] as [String: Any]]
]
let data = try JSONSerialization.data(withJSONObject: payload)
let encoded = data.base64EncodedString()
.replacingOccurrences(of: "+", with: "-")
.replacingOccurrences(of: "/", with: "_")
.replacingOccurrences(of: "=", with: "")
let token = "cashuA" + encoded
let decoded = CashuTokenDecoder.decode(token)
XCTAssertEqual(decoded?.amount, 10)
XCTAssertTrue(CashuTokenDecoder.hasCachedDecode(for: token))
CashuTokenDecoder.resetForPanic()
XCTAssertFalse(CashuTokenDecoder.hasCachedDecode(for: token))
}
func test_cashuTokenDecoder_oversizedInput_isNotCached() {
let huge = "cashuA" + String(repeating: "Q", count: CashuTokenDecoder.maxTokenLength + 1)
XCTAssertNil(CashuTokenDecoder.decode(huge))
XCTAssertFalse(CashuTokenDecoder.hasCachedDecode(for: huge))
}
}