mirror of
https://github.com/permissionlesstech/bitchat.git
synced 2026-11-22 08:17:28 +00:00
Merge 40ad6aa7434e18bf3e5f49225dc65263ec375aa5 into 04995f68e470a18d1fc99f86fa326c2a87bab4f2
This commit is contained in:
commit
e4704fe3f3
@ -14,6 +14,7 @@
|
||||
// For more information, see <https://unlicense.org>
|
||||
//
|
||||
|
||||
import CoreFoundation
|
||||
import Foundation
|
||||
|
||||
enum CashuTokenDecoder {
|
||||
@ -138,9 +139,13 @@ enum CashuTokenDecoder {
|
||||
mintHost = sanitizedHost(from: mint)
|
||||
}
|
||||
for proof in (entry["proofs"] as? [[String: Any]]) ?? [] {
|
||||
guard let number = proof["amount"] as? NSNumber else { continue }
|
||||
guard let number = proof["amount"] as? NSNumber,
|
||||
CFGetTypeID(number) != CFBooleanGetTypeID() else { continue }
|
||||
// NSNumber.int64Value truncates fractions and wraps oversized values.
|
||||
// Compare the converted integer with the original number before counting it.
|
||||
let value = number.int64Value
|
||||
guard value > 0, value <= maxAmount else { continue }
|
||||
guard value > 0, value <= maxAmount,
|
||||
number.compare(NSNumber(value: value)) == .orderedSame else { continue }
|
||||
total += value
|
||||
guard total <= maxAmount else { return nil }
|
||||
sawAmount = true
|
||||
@ -161,7 +166,7 @@ enum CashuTokenDecoder {
|
||||
/// { "m": mint, "u": unit, "d": memo, "t": [ { "i": bytes, "p": [ { "a": amount, … } ] } ] }
|
||||
private static func decodeV4(_ payload: Data) -> TokenInfo? {
|
||||
var reader = CBORReader(data: payload)
|
||||
guard case .map(let pairs)? = reader.parseValue(depth: 0) else { return nil }
|
||||
guard case .map(let pairs)? = reader.parseComplete() else { return nil }
|
||||
var mintHost: String?
|
||||
var unit: String?
|
||||
var memo: String?
|
||||
@ -254,6 +259,11 @@ private struct CBORReader {
|
||||
bytes = [UInt8](data)
|
||||
}
|
||||
|
||||
mutating func parseComplete() -> Value? {
|
||||
guard let value = parseValue(depth: 0), index == bytes.count else { return nil }
|
||||
return value
|
||||
}
|
||||
|
||||
mutating func parseValue(depth: Int) -> Value? {
|
||||
guard depth < Self.maxDepth, itemBudget > 0 else { return nil }
|
||||
itemBudget -= 1
|
||||
@ -267,7 +277,7 @@ private struct CBORReader {
|
||||
return readBytes(count: argument) != nil ? .opaque : nil
|
||||
case 3: // text string
|
||||
guard let raw = readBytes(count: argument) else { return nil }
|
||||
return String(bytes: raw, encoding: .utf8).map(Value.text) ?? .opaque
|
||||
return String(bytes: raw, encoding: .utf8).map(Value.text)
|
||||
case 4: // array
|
||||
guard argument <= Self.maxContainerCount else { return nil }
|
||||
var items: [Value] = []
|
||||
@ -280,10 +290,12 @@ private struct CBORReader {
|
||||
case 5: // map
|
||||
guard argument <= Self.maxContainerCount else { return nil }
|
||||
var pairs: [(Value, Value)] = []
|
||||
var textKeys = Set<String>()
|
||||
pairs.reserveCapacity(Int(min(argument, 64)))
|
||||
for _ in 0..<argument {
|
||||
guard let key = parseValue(depth: depth + 1),
|
||||
let value = parseValue(depth: depth + 1) else { return nil }
|
||||
if case .text(let name) = key, !textKeys.insert(name).inserted { return nil }
|
||||
pairs.append((key, value))
|
||||
}
|
||||
return .map(pairs)
|
||||
|
||||
@ -320,6 +320,59 @@ struct CashuTokenDecoderTests {
|
||||
#expect(info?.amount == nil)
|
||||
}
|
||||
|
||||
// MARK: - Ambiguous Numeric and CBOR Input
|
||||
|
||||
@Test func v3DoesNotCoerceBooleansOrFractionsToMoney() {
|
||||
for amount in ["true", "1.5", "2100000000000000.5", "18446744073709551617"] {
|
||||
let json = "{\"token\":[{\"proofs\":[{\"amount\":\(amount)}]}]}"
|
||||
let token = "cashuA" + base64URL(Data(json.utf8))
|
||||
#expect(CashuTokenDecoder.decode(token)?.amount == nil)
|
||||
#expect(CashuTokenDecoder.decode(token, strict: true) == nil)
|
||||
}
|
||||
}
|
||||
|
||||
@Test func v4RejectsTrailingBytes() {
|
||||
let token = makeV4Token()
|
||||
let encoded = String(token.dropFirst(6))
|
||||
let padded = encoded + String(repeating: "=", count: (4 - encoded.count % 4) % 4)
|
||||
let payload = Data(base64Encoded: padded.replacingOccurrences(of: "-", with: "+")
|
||||
.replacingOccurrences(of: "_", with: "/"))!
|
||||
for suffix in [[UInt8(0)], CBOREncode.map([])] {
|
||||
let malformed = "cashuB" + base64URL(payload + Data(suffix))
|
||||
#expect(CashuTokenDecoder.decode(malformed, strict: true) == nil)
|
||||
#expect(CashuTokenDecoder.decode(malformed)?.amount == nil)
|
||||
}
|
||||
}
|
||||
|
||||
@Test func v4RejectsDuplicateKeysAtEveryDepth() {
|
||||
let proof = CBOREncode.map([("a", CBOREncode.uint(1)), ("a", CBOREncode.uint(2))])
|
||||
let group = CBOREncode.map([("p", CBOREncode.array([proof]))])
|
||||
let duplicateProof = CBOREncode.map([("t", CBOREncode.array([group]))])
|
||||
let validProof = CBOREncode.map([("a", CBOREncode.uint(1))])
|
||||
let validGroup = CBOREncode.map([("p", CBOREncode.array([validProof]))])
|
||||
let duplicateRoot = CBOREncode.map([
|
||||
("t", CBOREncode.array([validGroup])), ("t", CBOREncode.array([]))
|
||||
])
|
||||
let duplicateGroup = CBOREncode.map([("t", CBOREncode.array([
|
||||
CBOREncode.map([("p", CBOREncode.array([validProof])), ("p", CBOREncode.array([]))])
|
||||
]))])
|
||||
for payload in [duplicateProof, duplicateRoot, duplicateGroup] {
|
||||
let token = "cashuB" + base64URL(Data(payload))
|
||||
#expect(CashuTokenDecoder.decode(token, strict: true) == nil)
|
||||
#expect(CashuTokenDecoder.decode(token)?.amount == nil)
|
||||
}
|
||||
}
|
||||
|
||||
@Test func v4RejectsMalformedUTF8EvenInUnknownFields() {
|
||||
let proof = CBOREncode.map([("a", CBOREncode.uint(1))])
|
||||
let group = CBOREncode.map([("p", CBOREncode.array([proof]))])
|
||||
let payload = CBOREncode.map([
|
||||
("t", CBOREncode.array([group])), ("x", [0x61, 0xFF])
|
||||
])
|
||||
let token = "cashuB" + base64URL(Data(payload))
|
||||
#expect(CashuTokenDecoder.decode(token, strict: true) == nil)
|
||||
}
|
||||
|
||||
// MARK: - Detection Ranges (message scanning)
|
||||
|
||||
@Test func detectionFindsWholeMessageToken() {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user