Repository Public
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project : QYVORA7// File : Tests/QyvoraWalletTests/AuditRegressionTests.swift8// Module : Tests9// Repository : https://git.spboucher.ai/qyvora10// Project Website : Not specified11// Created : 2026-12-0412// Last Modified : 2026-12-0413//14// Author : Simon-Pierre Boucher15// Position : Professor of Finance16// Institution : Université du Québec en Outaouais (UQO)17// Organization : SP Boucher18// Website : https://spboucher.ai19// Git : https://git.spboucher.ai20//21// Preferred Contact: contact@spboucher.ai22// Development : dev@spboucher.ai23// Support : support@spboucher.ai24// Security/Admin : admin@spboucher.ai25//26// Copyright (c) 202627// Simon-Pierre Boucher28// All rights reserved where applicable. This file is governed by the29// applicable project license, copyright notices and relevant law.30// Existing third-party rights and notices remain unaffected.31//32// License : None specified (no license file in this repository)33//34// Do not embed credentials, private keys, access tokens,35// certificates or confidential personal information.36//37// =============================================================================38// END OF SP BOUCHER SOURCE HEADER39// =============================================================================4041import Foundation42import Testing43import QyvoraMath44import QyvoraCore45import QyvoraBitcoin46import QyvoraEthereum47import QyvoraNetworking48@testable import QyvoraWallet4950/// Regression tests of the security audit (findings QYV-SVC-001 and QYV-SVC-003): answers of a hostile endpoint51/// that used to trap the process are now refused or read as "unknown".52struct AuditRegressionTests {53 /// QYV-SVC-003. Two addresses whose history is longer than the wallet reads (so their unspent outputs cannot be54 /// cross-checked) each list 4 500 outputs of 21 million bitcoin: 9 000 × 2.1e15 exceeds 2^64. The balance sum used55 /// to overflow and trap; the synchronization now refuses the answer.56 @Test func hostileUnspentListsCannotOverflowTheBalance() async throws {57 let h = try await Harness.make(historyPages: 1)58 defer { h.cleanup() }59 try await h.service.importWallet(words: Harness.abandon, password: Harness.password)60 let a0 = try await h.receive(0), a1 = try await h.receive(1)61 for _ in 0..<26 {62 _ = h.backend.fund(a0, 10_000)63 _ = h.backend.fund(a1, 10_000)64 }65 func list(_ tag: String) -> String {66 let items = (0..<4_500).map { i -> String in67 let txid = tag + String(repeating: "0", count: 60 - String(i).count) + String(i)68 return "{\"txid\":\"\(txid)\",\"vout\":0,\"value\":2100000000000000,\"status\":{\"confirmed\":true,\"block_height\":100,\"block_time\":1700000000}}"69 }70 return "[" + items.joined(separator: ",") + "]"71 }72 h.backend.overrides = [73 (match: "GET esplora.test/api/address/\(a0)/utxo", status: 200, body: list("aaaa")),74 (match: "GET esplora.test/api/address/\(a1)/utxo", status: 200, body: list("bbbb")),75 ]76 guard case .untrustedResponse(let text)? = await walletError({ try await h.service.syncBitcoin() }) else {77 Issue.record("expected untrustedResponse")78 return79 }80 #expect(text.contains("monetary range"))81 #expect(await h.service.latestBitcoinSnapshot() == nil)82 }8384 /// The same bound with a short list: three outputs of 21 million bitcoin on one address are already impossible.85 @Test func unspentTotalAboveTheMoneySupplyIsRefused() async throws {86 let h = try await Harness.make(historyPages: 1)87 defer { h.cleanup() }88 try await h.service.importWallet(words: Harness.abandon, password: Harness.password)89 let a0 = try await h.receive(0)90 for _ in 0..<26 { _ = h.backend.fund(a0, 10_000) }91 let items = (0..<3).map { "{\"txid\":\"\(String(repeating: "c\($0)", count: 32))\",\"vout\":0,\"value\":2100000000000000,\"status\":{\"confirmed\":true,\"block_height\":100}}" }92 h.backend.overrides = [(match: "GET esplora.test/api/address/\(a0)/utxo", status: 200, body: "[" + items.joined(separator: ",") + "]")]93 guard case .untrustedResponse? = await walletError({ try await h.service.syncBitcoin() }) else {94 Issue.record("expected untrustedResponse")95 return96 }97 }9899 /// QYV-SVC-001. Numbers of the indexer outside the 64-bit range (block number 1e300, token decimals 2^64 − 1) used100 /// to trap in `UInt64(Double)` / `Int(UInt64)`. They are now read as unknown and the synchronization completes.101 @Test func hostileIndexerNumbersDoNotCrashTheSynchronization() async throws {102 let h = try await Harness.abandonWallet()103 defer { h.cleanup() }104 let me = await h.ethereumAddress105 let other = "0x00000000000000000000000000000000000000bb"106 let hash = "0x" + String(repeating: "ab", count: 32)107 h.backend.indexerTransactions = """108 {"items":[{"hash":"\(hash)","from":{"hash":"\(other)"},"to":{"hash":"\(me)"},"value":"1000","status":"ok",109 "block_number":1e300,"timestamp":"2026-01-01T00:00:00.000000Z"}],"next_page_params":null}110 """111 h.backend.indexerTokenTransfers = """112 {"items":[{"transaction_hash":"\(hash)","from":{"hash":"\(other)"},"to":{"hash":"\(me)"},"total":{"value":"5"},113 "block_number":18446744073709551616,114 "token":{"address_hash":"\(other)","symbol":"EVIL","decimals":"18446744073709551615"}}],"next_page_params":null}115 """116 let snapshot = try await h.service.syncEthereum()117 let history = try #require(snapshot.history.value)118 #expect(history.count == 2)119 #expect(history.allSatisfy { $0.blockNumber == nil })120 for entry in history {121 if case .token(_, _, let decimals, let verified) = entry.kind {122 #expect(decimals == nil)123 #expect(!verified)124 }125 }126 }127}128