Files
bitchat/localPackages/BitLogger/Sources/String+Sanitization.swift
T
jackandjack 46c1b6caa0 Architecture, @Observable migration, hot-path performance, and live-device transport hardening (2.0.0 build 4) (#1969)
* Bump targets to iOS 17/macOS 14 and optimize PeerList/Nostr hot paths

* Migrate all 11 AppEnvironmentModels feature models to SwiftUI @Observable

* Inject AppRuntimeDependencies and decompose ChatViewModel into domain extensions

* Decompose Nostr protocol/relay policies and chat coordinators

* Harden strict concurrency and dead-code hygiene across core packages and app

* Clear remaining iOS 17 deprecation warnings in BitchatApp and VoiceRecorder

* Fix BLE redundant-link thrash, Tor -1011 bans, EOSE promotion, and duplicate startup subs

* Cancel stale foreground Tor reconnect task across background cycles

* Bump 2.0.0 build number to 4

* Address SwiftLint, Periphery, and Codex review findings

* Preserve @Observable dependencies for group details, geohash counts, and live voice availability

* Refresh inbox rows when geohash participant display names update

---------

Co-authored-by: jack <jackjackbits@users.noreply.github.com>
2026-11-28 20:28:35 -10:00

79 lines
2.4 KiB
Swift

//
// String+Sanitization.swift
// BitLogger
//
// This is free and unencumbered software released into the public domain.
// For more information, see <https://unlicense.org>
//
import Foundation
extension String {
/// Sanitize strings to remove potentially sensitive data
func sanitized() -> String {
let key = self as NSString
// Check cache first (NSCache is thread-safe)
if let cached = Self.cache.object(forKey: key) {
return cached as String
}
var sanitized = self
// Remove full fingerprints (keep first 8 chars for debugging)
let fingerprintPattern = #/[a-fA-F0-9]{64}/#
sanitized = sanitized.replacing(fingerprintPattern) { match in
let fingerprint = String(match.output)
return String(fingerprint.prefix(8)) + "..."
}
// Remove base64 encoded data that might be keys
let base64Pattern = #/[A-Za-z0-9+/]{40,}={0,2}/#
sanitized = sanitized.replacing(base64Pattern) { _ in
"<base64-data>"
}
// Remove potential passwords (assuming they're in quotes or after "password:")
let passwordPattern = #/password["\s:=]+["']?[^"'\s]+["']?/#
sanitized = sanitized.replacing(passwordPattern) { _ in
"password: <redacted>"
}
// Truncate peer IDs to first 8 characters
let peerIDPattern = #/peerID: ([a-zA-Z0-9]{8})[a-zA-Z0-9]+/#
sanitized = sanitized.replacing(peerIDPattern) { match in
"peerID: \(match.1)..."
}
// Cache the result (NSCache is thread-safe)
Self.cache.setObject(sanitized as NSString, forKey: key)
return sanitized
}
}
// MARK: - Cache Helpers
private final class SanitizedStringCache: @unchecked Sendable {
// NSCache is documented by Foundation as thread-safe.
private let cache: NSCache<NSString, NSString>
init(countLimit: Int) {
let cache = NSCache<NSString, NSString>()
cache.countLimit = countLimit
self.cache = cache
}
func object(forKey key: NSString) -> NSString? {
cache.object(forKey: key)
}
func setObject(_ obj: NSString, forKey key: NSString) {
cache.setObject(obj, forKey: key)
}
}
private extension String {
static let cache = SanitizedStringCache(countLimit: 100) // Keep last 100 sanitized strings
}