mirror of
https://github.com/yattee/yattee.git
synced 2026-10-24 21:52:51 +00:00
Yattee 2 keys credentials by instance UUID with no notion of which Apple account they belong to, and reads them back with SynchronizableAny. Apple documents that Keychain data may stay on device when a user signs out and chooses to keep it, so whoever signs in next would be served the previous user's credential - guaranteed for migrated accounts, since local instances survive the switch and the migrated account UUID is deterministically the instance UUID. Credentials are now keyed "<identityScope>|<accountID>", where the scope is the CloudKit user record (identical across a user's devices, so iCloud Keychain sync still works). Usernames get the same scope and live beside the credential, keeping login identifiers out of synced metadata. The legacy unscoped item is never adopted automatically. A strict kSecAttrSynchronizable == true query was the candidate discriminator for "this secret belongs to the current Apple account", but measured on iPadOS 27.0 a retained item keeps the attribute and still matches that query with no Apple account signed in at all - the flag records what was once requested, not live iCloud Keychain membership. Reads therefore never fall back to the legacy key; adoption is an explicit user action that copies the item, verifies the scoped read-back, then records a marker bound to the identity and a salted fingerprint of the secret. The legacy item is retained, because deleting a synchronizable item propagates and would log out devices still on the previous release mid-rollout. Deleting it later needs all three of: a marker on this device, matching identity, and the item still holding the adopted secret. The instance-keyed path is untouched, so no shipping call site changes behavior. BasicAuthCredentialsManager stays instance-keyed - proxy auth belongs to the instance, not to a user account. While the scope is unresolved (first launch under a new Apple account, CloudKit unreachable) reads report "can't tell yet" via isScopeResolved rather than "signed out", and writes are refused rather than guessing a scope. Login state is cached per scope so one identity's cache can never answer for another. Also adds the debug-only harness the measurement was taken with, under Advanced > Developer > Experiments.
288 lines
10 KiB
Swift
288 lines
10 KiB
Swift
//
|
|
// KeychainProvenanceExperimentView.swift
|
|
// Yattee
|
|
//
|
|
// Debug-only driver for the Keychain provenance experiment (accounts revamp, D3).
|
|
//
|
|
|
|
#if DEBUG
|
|
import SwiftUI
|
|
|
|
#if canImport(UIKit)
|
|
import UIKit
|
|
#endif
|
|
|
|
/// Runs the experiment described in ``KeychainProvenanceProbe``.
|
|
///
|
|
/// The protocol spans an Apple-account sign-out and sign-in, so the screen is
|
|
/// built around capturing readings rather than showing live state: each
|
|
/// "Capture report" writes a persisted snapshot that survives the switch, and
|
|
/// the whole set is copied out at the end.
|
|
struct KeychainProvenanceExperimentView: View {
|
|
@State private var label = ""
|
|
@State private var userRecordID: String?
|
|
@State private var resolvingUserRecordID = true
|
|
@State private var reports: [KeychainProvenanceReport] = KeychainProvenanceReportStore.load()
|
|
@State private var latestStatus: String?
|
|
@State private var showingDeleteConfirmation = false
|
|
|
|
var body: some View {
|
|
SettingsFormContainer {
|
|
protocolSection
|
|
identitySection
|
|
runSection
|
|
reportsSection
|
|
}
|
|
.navigationTitle(Text(verbatim: "Keychain Provenance"))
|
|
#if os(iOS)
|
|
.navigationBarTitleDisplayMode(.inline)
|
|
#endif
|
|
.task {
|
|
userRecordID = await CloudKitIdentity.userRecordID()
|
|
resolvingUserRecordID = false
|
|
}
|
|
.confirmationDialog(
|
|
Text(verbatim: "Delete probe items?"),
|
|
isPresented: $showingDeleteConfirmation,
|
|
titleVisibility: .visible
|
|
) {
|
|
Button(role: .destructive) {
|
|
latestStatus = "Delete: " + KeychainProvenanceProbe.describe(KeychainProvenanceProbe.deleteAll())
|
|
} label: {
|
|
Text(verbatim: "Delete probe items")
|
|
}
|
|
Button(String(localized: "common.cancel"), role: .cancel) {}
|
|
} message: {
|
|
Text(verbatim: "Deletes both probe items on this device. The synchronizable one also disappears from every device signed in to the same Apple account.")
|
|
}
|
|
}
|
|
|
|
// MARK: - Sections
|
|
|
|
@ViewBuilder
|
|
private var protocolSection: some View {
|
|
SettingsFormSection(
|
|
"Protocol",
|
|
footer: "Two Apple accounts and a real device are required — iCloud Keychain does not behave faithfully in the Simulator. Do not press Write at steps 3 and 4: writing deletes the retained item the experiment is measuring."
|
|
) {
|
|
step(1, "Signed in as account A: Write both probes.")
|
|
step(2, "Wait ~1 minute for iCloud Keychain to sync, then Capture (label: \"A, before sign-out\").")
|
|
step(3, "Sign out of the Apple account in system Settings, choosing to KEEP Keychain data on device. Relaunch Yattee and Capture (label: \"signed out, kept\").")
|
|
step(4, "Sign in with account B. Relaunch Yattee and Capture (label: \"B, after switch\"). This is the decisive reading.")
|
|
step(5, "Write both probes again under B, then Capture (label: \"B, fresh write\").")
|
|
step(6, "Copy all reports and send them back.")
|
|
}
|
|
}
|
|
|
|
@ViewBuilder
|
|
private var identitySection: some View {
|
|
SettingsFormSection(
|
|
"Identity",
|
|
footer: "The fingerprint is an opaque hash of ubiquityIdentityToken. It must differ between accounts A and B — if it does not, the sign-out did not take effect."
|
|
) {
|
|
row("CloudKit entitlement", CloudKitAvailability.isAvailable ? "available" : "missing")
|
|
row("Apple account", CloudKitIdentity.ubiquityFingerprint.map { "fp=\($0)" } ?? "signed out")
|
|
row("User record", resolvingUserRecordID ? "resolving…" : (userRecordID ?? "unresolved"))
|
|
}
|
|
}
|
|
|
|
@ViewBuilder
|
|
private var runSection: some View {
|
|
SettingsFormSection(
|
|
"Run",
|
|
footer: "The label is stored inside the probe payload, so a report taken after an account switch still shows which account wrote the item it found."
|
|
) {
|
|
TextField(text: $label) {
|
|
Text(verbatim: "Label (e.g. \"A, before sign-out\")")
|
|
}
|
|
#if os(iOS)
|
|
.textInputAutocapitalization(.never)
|
|
.autocorrectionDisabled()
|
|
#endif
|
|
|
|
actionRow("Write both probes", systemImage: "square.and.pencil") {
|
|
writeProbes()
|
|
}
|
|
|
|
actionRow("Capture report", systemImage: "camera.viewfinder") {
|
|
capture()
|
|
}
|
|
|
|
if let latestStatus {
|
|
Text(verbatim: latestStatus)
|
|
.font(.footnote)
|
|
.foregroundStyle(.secondary)
|
|
}
|
|
}
|
|
}
|
|
|
|
@ViewBuilder
|
|
private var reportsSection: some View {
|
|
SettingsFormSection("Reports (\(reports.count))") {
|
|
if reports.isEmpty {
|
|
Text(verbatim: "No reports captured yet.")
|
|
.foregroundStyle(.secondary)
|
|
} else {
|
|
ForEach(reports) { report in
|
|
VStack(alignment: .leading, spacing: 4) {
|
|
Text(verbatim: report.label.isEmpty ? "<unlabelled>" : report.label)
|
|
.font(.headline)
|
|
Text(verbatim: report.capturedAt.formatted(date: .abbreviated, time: .standard))
|
|
.font(.caption)
|
|
.foregroundStyle(.secondary)
|
|
Text(verbatim: report.text)
|
|
.font(.system(.caption2, design: .monospaced))
|
|
.selectableText()
|
|
}
|
|
.frame(maxWidth: .infinity, alignment: .leading)
|
|
}
|
|
|
|
#if !os(tvOS)
|
|
actionRow("Copy all reports", systemImage: "doc.on.doc") {
|
|
copyToClipboard(allReportsText)
|
|
latestStatus = "Copied \(reports.count) report(s) to the clipboard."
|
|
}
|
|
#endif
|
|
|
|
actionRow("Clear reports", systemImage: "trash", role: .destructive) {
|
|
KeychainProvenanceReportStore.clear()
|
|
reports = []
|
|
latestStatus = "Cleared captured reports (probe items untouched)."
|
|
}
|
|
}
|
|
|
|
actionRow("Delete probe items", systemImage: "key.slash", role: .destructive) {
|
|
showingDeleteConfirmation = true
|
|
}
|
|
}
|
|
}
|
|
|
|
// MARK: - Actions
|
|
|
|
private func writeProbes() {
|
|
let payload = payloadText()
|
|
let synchronizable = KeychainProvenanceProbe.write(
|
|
account: KeychainProvenanceProbe.synchronizableAccount,
|
|
synchronizable: true,
|
|
payload: payload
|
|
)
|
|
let local = KeychainProvenanceProbe.write(
|
|
account: KeychainProvenanceProbe.localAccount,
|
|
synchronizable: false,
|
|
payload: payload
|
|
)
|
|
|
|
latestStatus = """
|
|
Wrote probes — synchronizable: \(KeychainProvenanceProbe.describe(synchronizable)), \
|
|
local: \(KeychainProvenanceProbe.describe(local))
|
|
"""
|
|
}
|
|
|
|
private func capture() {
|
|
let text = KeychainProvenanceReportBuilder.build(label: label, userRecordID: userRecordID)
|
|
reports = KeychainProvenanceReportStore.append(
|
|
KeychainProvenanceReport(label: label, text: text)
|
|
)
|
|
|
|
// Also logged so the report survives even if the app's storage is
|
|
// reset, and so it can be exported from the log viewer on tvOS.
|
|
LoggingService.shared.info(
|
|
"Keychain provenance report captured",
|
|
category: .keychain,
|
|
details: text
|
|
)
|
|
|
|
latestStatus = "Captured report \(reports.count)."
|
|
}
|
|
|
|
private func payloadText() -> String {
|
|
let fingerprint = CloudKitIdentity.ubiquityFingerprint ?? "none"
|
|
let written = Date().formatted(.iso8601)
|
|
return "fp=\(fingerprint)|written=\(written)|label=\(label.isEmpty ? "<unlabelled>" : label)"
|
|
}
|
|
|
|
private var allReportsText: String {
|
|
reports
|
|
.map { "\($0.text)\n" }
|
|
.joined(separator: "\n\n========================================\n\n")
|
|
}
|
|
|
|
#if !os(tvOS)
|
|
private func copyToClipboard(_ text: String) {
|
|
#if os(macOS)
|
|
NSPasteboard.general.clearContents()
|
|
NSPasteboard.general.setString(text, forType: .string)
|
|
#else
|
|
UIPasteboard.general.string = text
|
|
#endif
|
|
}
|
|
#endif
|
|
|
|
// MARK: - Row builders
|
|
|
|
@ViewBuilder
|
|
private func step(_ number: Int, _ text: String) -> some View {
|
|
HStack(alignment: .firstTextBaseline, spacing: 8) {
|
|
Text(verbatim: "\(number).")
|
|
.font(.footnote.monospacedDigit())
|
|
.foregroundStyle(.secondary)
|
|
Text(verbatim: text)
|
|
.font(.footnote)
|
|
}
|
|
.frame(maxWidth: .infinity, alignment: .leading)
|
|
}
|
|
|
|
@ViewBuilder
|
|
private func row(_ title: String, _ value: String) -> some View {
|
|
HStack {
|
|
Text(verbatim: title)
|
|
Spacer()
|
|
Text(verbatim: value)
|
|
.font(.system(.footnote, design: .monospaced))
|
|
.foregroundStyle(.secondary)
|
|
.selectableText()
|
|
}
|
|
}
|
|
|
|
@ViewBuilder
|
|
private func actionRow(
|
|
_ title: String,
|
|
systemImage: String,
|
|
role: ButtonRole? = nil,
|
|
action: @escaping () -> Void
|
|
) -> some View {
|
|
HStack {
|
|
Button(role: role, action: action) {
|
|
Label {
|
|
Text(verbatim: title)
|
|
} icon: {
|
|
Image(systemName: systemImage)
|
|
}
|
|
}
|
|
Spacer()
|
|
}
|
|
}
|
|
}
|
|
|
|
private extension View {
|
|
/// tvOS has no text selection — the report is read back from the log
|
|
/// viewer there instead.
|
|
@ViewBuilder
|
|
func selectableText() -> some View {
|
|
#if os(tvOS)
|
|
self
|
|
#else
|
|
textSelection(.enabled)
|
|
#endif
|
|
}
|
|
}
|
|
|
|
// MARK: - Preview
|
|
|
|
#Preview {
|
|
NavigationStack {
|
|
KeychainProvenanceExperimentView()
|
|
}
|
|
}
|
|
#endif
|