From f4643312020e4690168d6e28aa94980e7b138ae9 Mon Sep 17 00:00:00 2001 From: Jozef Koval Date: Fri, 27 Nov 2026 20:40:24 +0100 Subject: [PATCH] fix(security): bound CashuTokenDecoder cache by token length and byte cost (#1907) --- bitchat/Services/CashuTokenDecoder.swift | 32 ++++++++++--------- .../Services/PanicWipeCoverageTests.swift | 30 +++++++++++++++++ 2 files changed, 47 insertions(+), 15 deletions(-) diff --git a/bitchat/Services/CashuTokenDecoder.swift b/bitchat/Services/CashuTokenDecoder.swift index 0f528551..2059c12b 100644 --- a/bitchat/Services/CashuTokenDecoder.swift +++ b/bitchat/Services/CashuTokenDecoder.swift @@ -89,26 +89,21 @@ enum CashuTokenDecoder { private let cache: NSCache = { let c = NSCache() 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 } diff --git a/bitchatTests/Services/PanicWipeCoverageTests.swift b/bitchatTests/Services/PanicWipeCoverageTests.swift index 89e618c4..c93c38e5 100644 --- a/bitchatTests/Services/PanicWipeCoverageTests.swift +++ b/bitchatTests/Services/PanicWipeCoverageTests.swift @@ -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)) + } }