Repository Public
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project : QYVORA7// File : security/policies/semgrep/qyvora-general.swift8// Module : Security9// 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 os4344// Test snippets of qyvora-general.yaml (`semgrep --test security/policies/semgrep`).45// `ruleid:` lines must be reported, `ok:` lines must not. This file is never compiled.4647func logging(seed: [UInt8], mnemonic: String, balance: Int, logger: Logger) {48 // ruleid: qyvora-log-secret49 print("seed = \(seed)")50 // ruleid: qyvora-log-secret51 NSLog("%@", mnemonic)52 // ruleid: qyvora-log-secret53 debugPrint(userPassword)54 // ruleid: qyvora-log-secret55 print("TronGrid key \(apiKey)")56 // ruleid: qyvora-log-secret57 NSLog("%@", node.chainCode.description)58 // ok: qyvora-log-secret59 print("balance = \(balance)")60 // ok: qyvora-log-secret61 print("self-test passed")6263 // ruleid: qyvora-logger-secret64 logger.debug("restored \(mnemonic)")65 // ruleid: qyvora-logger-secret66 log.error("bad passphrase \(passphrase, privacy: .public)")67 // ok: qyvora-logger-secret68 logger.info("synchronised \(balance) outputs")69}7071func defaultsUse(defaults: UserDefaults, pin: String, endpoints: Data) {72 // ruleid: qyvora-userdefaults-sensitive, qyvora-userdefaults-use73 UserDefaults.standard.set(mnemonic, forKey: "backup")74 // ruleid: qyvora-userdefaults-sensitive, qyvora-userdefaults-use75 defaults.set(value, forKey: "walletSeed")76 // ok: qyvora-userdefaults-sensitive77 store.set(endpoints, forKey: "endpointConfiguration.v1")7879 // ruleid: qyvora-userdefaults-use80 defaults.set(endpoints, forKey: "endpointConfiguration.v1")81 // ok: qyvora-userdefaults-use82 cache.set(endpoints, forKey: "x")83 // ok: qyvora-userdefaults-use84 let stored = defaults.data(forKey: "endpointConfiguration.v1")85}8687func pasteboard(address: String) {88 // ruleid: qyvora-pasteboard-outside-helper89 UIPasteboard.general.string = address90 // ruleid: qyvora-pasteboard-outside-helper91 let pasted = UIPasteboard.general.string92 // ruleid: qyvora-pasteboard-outside-helper93 NSPasteboard.general.clearContents()94 // ok: qyvora-pasteboard-outside-helper95 QClipboard.copy(address, sensitive: false)96}9798func randomness() {99 // ruleid: qyvora-insecure-randomness100 let a = arc4random()101 // ruleid: qyvora-insecure-randomness102 let b = arc4random_uniform(10)103 // ruleid: qyvora-insecure-randomness104 var g = SystemRandomNumberGenerator()105 // ruleid: qyvora-insecure-randomness106 let c = drand48()107 // ruleid: qyvora-insecure-randomness108 let d = random()109 // ruleid: qyvora-insecure-randomness110 let e = Int.random(in: 0..<10)111 // ruleid: qyvora-insecure-randomness112 let f = UInt64.random(in: 0...9, using: &g)113 // ruleid: qyvora-insecure-randomness114 let h = words.randomElement()115 // ruleid: qyvora-insecure-randomness116 let i = words.shuffled()117 // ruleid: qyvora-insecure-randomness118 let j = Bool.random()119 // ruleid: qyvora-insecure-randomness120 _ = SecRandomCopyBytes(kSecRandomDefault, 32, &buffer)121 // ok: qyvora-insecure-randomness122 let k = SystemRandom.bytes(32)123 // ok: qyvora-insecure-randomness124 let l = operand(values)125 // ok: qyvora-insecure-randomness126 let m = randomised.count127}128129func cleartext() {130 // ruleid: qyvora-http-literal131 let u = URL(string: "http://example.org/api")132 // ruleid: qyvora-http-literal133 let isLocal = text.hasPrefix("http://")134 // ok: qyvora-http-literal135 let v = URL(string: "https://example.org/api")136}137138final class TrustAll: NSObject, URLSessionDelegate {139 // ruleid: qyvora-tls-validation-disabled140 func urlSession(_ s: URLSession, didReceive challenge: URLAuthenticationChallenge) async -> (URLSession.AuthChallengeDisposition, URLCredential?) {141 // ruleid: qyvora-tls-validation-disabled142 let trust = challenge.protectionSpace.serverTrust!143 // ruleid: qyvora-tls-validation-disabled144 return (.useCredential, URLCredential(trust: trust))145 }146}147148final class NoRedirect: NSObject, URLSessionTaskDelegate {149 // ok: qyvora-tls-validation-disabled150 func urlSession(_ session: URLSession, task: URLSessionTask, willPerformHTTPRedirection response: HTTPURLResponse,151 newRequest request: URLRequest) async -> URLRequest? { nil }152}153154func keychain() {155 var query: [String: Any] = [:]156 // ruleid: qyvora-keychain-weak-accessibility157 query[kSecAttrAccessible as String] = kSecAttrAccessibleAfterFirstUnlock158 // ruleid: qyvora-keychain-weak-accessibility159 query[kSecAttrAccessible as String] = kSecAttrAccessibleAlways160 // ruleid: qyvora-keychain-weak-accessibility161 query[kSecAttrAccessible as String] = kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly162 // ruleid: qyvora-keychain-weak-accessibility163 query[kSecAttrAccessible as String] = kSecAttrAccessibleWhenUnlocked164 // ok: qyvora-keychain-weak-accessibility165 query[kSecAttrAccessible as String] = kSecAttrAccessibleWhenPasscodeSetThisDeviceOnly166 // ok: qyvora-keychain-weak-accessibility167 query[kSecAttrAccessible as String] = kSecAttrAccessibleWhenUnlockedThisDeviceOnly168169 // ruleid: qyvora-keychain-synchronizable170 query[kSecAttrSynchronizable as String] = true171 // ok: qyvora-keychain-synchronizable172 query[kSecAttrSynchronizable as String] = false173}174175func files(data: Data, url: URL) throws {176 // ruleid: qyvora-file-write-without-protection177 try data.write(to: url)178 // ruleid: qyvora-file-write-without-protection179 try text.write(to: url, atomically: true, encoding: .utf8)180 // ruleid: qyvora-file-write-without-protection181 FileManager.default.createFile(atPath: url.path, contents: data)182 // ok: qyvora-file-write-without-protection183 value.write(to: &text)184 // ok: qyvora-file-write-without-protection185 try store.write(data, to: "vault.qyv")186}187188// ruleid: qyvora-debug-else-branch189#if DEBUG190let debugOnly = true191#endif192193// ok: qyvora-debug-else-branch194#if os(iOS)195let ios = true196#endif197198func runtimeSwitch() {199 // ruleid: qyvora-debug-flag-runtime200 if ProcessInfo.processInfo.arguments.contains("-skipLock") {}201 // ruleid: qyvora-debug-flag-runtime202 let e = ProcessInfo.processInfo.environment["QY_DEBUG"]203 // ok: qyvora-debug-flag-runtime204 let n = ProcessInfo.processInfo.activeProcessorCount205}206207func tronKey(request: inout URLRequest, apiKey: TronAPIKey?) {208 // ruleid: qyvora-tron-api-key-header209 request.setValue("x", forHTTPHeaderField: "TRON-PRO-API-KEY")210 // ruleid: qyvora-tron-api-key-value-use211 let text = "key=\(apiKey?.value ?? "")"212 // ruleid: qyvora-tron-api-key-plain-storage, qyvora-userdefaults-use213 UserDefaults.standard.set(text, forKey: "tronGridKey")214 // ok: qyvora-tron-api-key-value-use215 let present = apiKey != nil216}217218struct LeakyProfile {219 // ruleid: qyvora-tron-api-key-plain-storage220 var tronAPIKey: String221}222