Apply code-review fixes: identity/pagination, load races, TLS pin, leaks

Addresses a multi-agent code review (26 confirmed findings + a URLSession
leak) across correctness, concurrency, security, and cleanup:

- MongoDocument.id: deterministic fallback (was UUID() on every access) to stop
  SwiftUI List/ForEach identity churn; getAll pagination drives its cursor off
  the real _id via documentId, so a missing _id stops paging instead of looping
  on a fabricated cursor.
- View models: per-load generation guard (+ captured-tab guard on ONU detail)
  so a cancelled or overlapping load can't commit stale results or land data on
  the wrong tab.
- JSONValue.intValue: guard Int(d) against NaN/inf/out-of-range (crash).
- ServerTrustEvaluator: hash the real SubjectPublicKeyInfo (prepend the
  algorithm-specific ASN.1 SPKI header) so a standard openssl-captured
  public-key pin actually matches; unknown key types fail closed.
- AppEnvironment.configure: clear stale cookies + Keychain creds on host change.
- FirmwareFile.isCompatible: empty metadata no longer passes as compatible-for-all.
- APIClient: invalidate URLSession on deinit (per-configure/probe leak).
- Dashboard best-effort sections keep last-good on transient failure; unified
  optical thresholds; LoginView prefill-once; populatedBuckets unique ForEach id;
  Format rounding rollover; dead-code/docstring/dedup cleanups.

Verification pending on the Mac (no Swift toolchain on the Linux dev box).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Jon Vanvik 2026-05-31 16:36:49 +02:00
parent 8de962eeb8
commit 203465913c
21 changed files with 303 additions and 103 deletions

View file

@ -5,21 +5,29 @@ enum Format {
/// Bit-rate, e.g. 224229 "224 kbps", 7216819 "7.2 Mbps".
static func bps(_ value: Double?) -> String {
guard let value, value >= 0 else { return "" }
let units = ["bps", "kbps", "Mbps", "Gbps", "Tbps"]
var x = value, i = 0
while x >= 1000 && i < units.count - 1 { x /= 1000; i += 1 }
let digits = (i == 0 || x >= 100) ? "%.0f" : "%.1f"
return "\(String(format: digits, x)) \(units[i])"
return scaled(value, divisor: 1000, units: ["bps", "kbps", "Mbps", "Gbps", "Tbps"])
}
/// Byte count, e.g. 7401438 "7.1 MB".
static func bytes(_ value: Double?) -> String {
guard let value, value >= 0 else { return "" }
let units = ["B", "KB", "MB", "GB", "TB", "PB"]
return scaled(value, divisor: 1024, units: ["B", "KB", "MB", "GB", "TB", "PB"])
}
private static func scaled(_ value: Double, divisor: Double, units: [String]) -> String {
var x = value, i = 0
while x >= 1024 && i < units.count - 1 { x /= 1024; i += 1 }
let digits = (i == 0 || x >= 100) ? "%.0f" : "%.1f"
return "\(String(format: digits, x)) \(units[i])"
while x >= divisor && i < units.count - 1 { x /= divisor; i += 1 }
// Round at display precision FIRST, then carry to the next unit if rounding
// reached the ceiling (999.95 kbps "1000" should read "1.0 Mbps").
var digits = (i == 0 || x >= 100) ? 0 : 1
var rounded = (x * pow(10, Double(digits))).rounded() / pow(10, Double(digits))
if rounded >= divisor && i < units.count - 1 {
i += 1
rounded /= divisor
digits = rounded >= 100 ? 0 : 1
rounded = (rounded * pow(10, Double(digits))).rounded() / pow(10, Double(digits))
}
return "\(String(format: "%.\(digits)f", rounded)) \(units[i])"
}
/// Integer percent, e.g. 13 "13%".