diff --git a/.claude/rules/data-sync-storage.md b/.claude/rules/data-sync-storage.md index 953cf0908a..49b33f7ff5 100644 --- a/.claude/rules/data-sync-storage.md +++ b/.claude/rules/data-sync-storage.md @@ -181,6 +181,8 @@ This path holds user data. A silent wrong answer here looks exactly like an empt | Snowflake SSO and MFA tokens, per host and user. A bare account's `NAME.USER` item stays for 0.83.1, which reads only that key | Keychain, device-local | `SnowflakeTokenCache` | | Whether `connections.json` was ever given an integrity tag, so a deleted tag reads as modified | Keychain `com.TablePro.connectionStoreStamped.`, device-local | `ConnectionStoreIntegrity` | | Whether Local only connections' secrets were copied to device-only items | UserDefaults `com.TablePro.localOnlySecretsKeptOnDevice`, device-local, never synced | `ConnectionStorage` | +| Whether plugin secure fields moved out of `connections.json` into the Keychain, set only when every field moved and the save succeeded | UserDefaults `com.TablePro.pluginSecureFieldsMovedToKeychain`, device-local, never synced | `ConnectionStorage` | +| Whether a saved-queries store replaced a damaged file and waits for a full fetch, so push treats it as unreadable | Table `sync_refill` in the saved-queries SQLite store, dropped once a full fetch applied | `SQLFavoriteStorage` | | Deletions the server reported for a sync category that is off, applied when the category is next read | UserDefaults, device-local, never synced | `SyncMetadataStorage.deferredDeletions` | | Where a connection came from (`origin`): this Mac, imported, iCloud | `connections.json`, never synced or exported | `ConnectionStorage` | | Table load timings (no identifying data, never uploaded) | JSON lines | `TableLoadHistoryStore` | diff --git a/.claude/rules/tests.md b/.claude/rules/tests.md index 749bd65552..6b366b0dee 100644 --- a/.claude/rules/tests.md +++ b/.claude/rules/tests.md @@ -73,7 +73,7 @@ UI flows: `.claude/rules/ui-tests.md`. Running tests and reading verdicts: `.cla Use one before writing a local helper. - `TableProTestSupport` (TableProCore): `waitUntil`, `AsyncGate`, `ManualClock` (production code takes `clock: any Clock = ContinuousClock()`), `BoundedCall`, `HeldOpenWriter`, `BackgroundPipeWriter`, `BackgroundPipeReader`. -- `apple/TableProTests/Helpers/`: `GlobalStateSuites`, `EphemeralDefaults`, `.appSettings { }`, `.recordingClipboard`, `CoordinatorFixture`, `OfflineRegistry`, `InMemoryColumnLayoutStore`, `InMemoryKeychain`, `PluginDriverStub`, `TestFixtures`, `AlwaysAllowGate`, `AlwaysDenyGate`. +- `apple/TableProTests/Helpers/`: `GlobalStateSuites`, `EphemeralDefaults`, `.appSettings { }`, `.recordingClipboard`, `CoordinatorFixture`, `OfflineRegistry`, `InMemoryColumnLayoutStore`, `InMemoryKeychain`, `PluginDriverStub`, `TestFixtures`, `AlwaysAllowGate`, `AlwaysDenyGate`, `MCPInMemoryApprovalStore`, `MCPFixedApprovalReader`, `ArrayRowProvider`, `ScopeRecordingKeychain`. - `TableProMCPTestSupport` (TableProMacCore): `MCPTestClock`, `MockHttpServer`, `RecordingBridgeLogger`. `TableProTLSTestFixtures` (TableProCore), also for the iOS tests. ## Known failures and flakes diff --git a/apple/Packages/TableProCore/Sources/TableProHTTP/LoopbackRedirectListener.swift b/apple/Packages/TableProCore/Sources/TableProHTTP/LoopbackRedirectListener.swift new file mode 100644 index 0000000000..fbf9c30d86 --- /dev/null +++ b/apple/Packages/TableProCore/Sources/TableProHTTP/LoopbackRedirectListener.swift @@ -0,0 +1,499 @@ +import Darwin +import Foundation +import Network +import os + +public enum LoopbackRedirectListenerError: Error, Equatable, Sendable { + case portInUse + case timedOut +} + +public enum LoopbackRedirectAnswer: Sendable { + case ignore(Data) + case settle(Data, Result) +} + +// Waits on loopback for one browser redirect (RFC 8252 section 7.3). `answer` sees each complete request head; +// a request it ignores gets its reply and changes nothing, so a page probing the port cannot end the wait. +public final class LoopbackRedirectListener: @unchecked Sendable { + public typealias Answer = @Sendable (_ requestHead: Data) -> LoopbackRedirectAnswer + + private struct Admission { + let connection: NWConnection + let order: UInt64 + var awaitingHead: Bool + } + + private struct Teardown { + let listener: NWListener? + let connections: [NWConnection] + let ipv6Hold: Int32 + let readyContinuation: CheckedContinuation? + let readyTimeout: DispatchWorkItem? + let outcomeContinuation: CheckedContinuation? + let outcomeTimeout: DispatchWorkItem? + } + + private let port: UInt16 + private let headTimeout: TimeInterval + private let answer: Answer + private let holdIPv6: @Sendable (UInt16) -> LoopbackPortHold + private let queue = DispatchQueue(label: "com.TablePro.LoopbackRedirectListener") + private let lock = NSLock() + private var listener: NWListener? + private var ipv6Hold: Int32 = -1 + private var portAttempts = 1 + private var connections: [ObjectIdentifier: Admission] = [:] + private var admissions: UInt64 = 0 + private var readyContinuation: CheckedContinuation? + private var readyTimeout: DispatchWorkItem? + private var outcomeContinuation: CheckedContinuation? + private var outcomeTimeout: DispatchWorkItem? + private var settled: Result? + + // Port 0 lets the kernel pick one. + public convenience init(port: UInt16, headTimeout: TimeInterval = 5, answer: @escaping Answer) { + self.init(port: port, headTimeout: headTimeout, holdIPv6: { LoopbackPortHold.ipv6(port: $0) }, answer: answer) + } + + init( + port: UInt16, + headTimeout: TimeInterval, + holdIPv6: @escaping @Sendable (UInt16) -> LoopbackPortHold, + answer: @escaping Answer + ) { + self.port = port + self.headTimeout = headTimeout + self.holdIPv6 = holdIPv6 + self.answer = answer + } + + @discardableResult + public func start(readyTimeout: TimeInterval = 10) async throws -> UInt16 { + let listener: NWListener + do { + listener = try makeListener() + } catch { + LoopbackRedirectLimits.logger.error("Could not create the loopback redirect listener") + throw LoopbackRedirectListenerError.portInUse + } + return try await withCheckedThrowingContinuation { (continuation: CheckedContinuation) in + let refusal = lock.withLock { () -> (any Error)? in + if let settled { + guard case .failure(let error) = settled else { return CancellationError() } + return error + } + self.listener = listener + readyContinuation = continuation + let item = DispatchWorkItem { [weak self] in + self?.finish(.failure(LoopbackRedirectListenerError.timedOut)) + } + self.readyTimeout = item + queue.asyncAfter(deadline: .now() + readyTimeout, execute: item) + return nil + } + guard let refusal else { + listener.start(queue: queue) + return + } + listener.cancel() + continuation.resume(throwing: refusal) + } + } + + public func outcome(timeout: TimeInterval, timeoutError: any Error) async throws -> Outcome { + try await withTaskCancellationHandler { + try await withCheckedThrowingContinuation { (continuation: CheckedContinuation) in + let early = lock.withLock { () -> Result? in + if let settled { return settled } + outcomeContinuation = continuation + let item = DispatchWorkItem { [weak self] in + self?.finish(.failure(timeoutError)) + } + outcomeTimeout = item + queue.asyncAfter(deadline: .now() + timeout, execute: item) + return nil + } + if let early { + continuation.resume(with: early) + } + } + } onCancel: { + finish(.failure(CancellationError())) + } + } + + public func stop() { + finish(.failure(CancellationError())) + } + + private func makeListener() throws -> NWListener { + guard let endpointPort = NWEndpoint.Port(rawValue: port) else { + throw LoopbackRedirectListenerError.portInUse + } + let parameters = NWParameters.tcp + parameters.requiredLocalEndpoint = .hostPort(host: .ipv4(.loopback), port: endpointPort) + parameters.allowLocalEndpointReuse = true + let listener = try NWListener(using: parameters) + listener.stateUpdateHandler = { [weak self, weak listener] state in + guard let listener else { return } + self?.listenerChanged(state, of: listener) + } + listener.newConnectionHandler = { [weak self] connection in + guard let self else { + connection.cancel() + return + } + self.accept(connection) + } + return listener + } + + private func listenerChanged(_ state: NWListener.State, of source: NWListener) { + // A listener given up for another kernel port reports its own cancellation; that ends nothing. + let isCurrent = lock.withLock { listener === source } + guard isCurrent else { return } + switch state { + case .ready: + let awaitingReady = lock.withLock { readyContinuation != nil } + guard awaitingReady else { return } + guard let port = source.port?.rawValue, port > 0 else { + finish(.failure(LoopbackRedirectListenerError.portInUse)) + return + } + holdIPv6Loopback(port: port) + case .failed(let error), .waiting(let error): + LoopbackRedirectLimits.logger.error( + "The loopback redirect listener failed (\(LoopbackRedirectLimits.code(of: error), privacy: .public))" + ) + finish(.failure(LoopbackRedirectListenerError.portInUse)) + default: + break + } + } + + // A redirect to localhost may come in over ::1, so no other process may take that port (RFC 8252 section 8.3). + private func holdIPv6Loopback(port bound: UInt16) { + switch holdIPv6(bound) { + case .held(let descriptor): + let kept = lock.withLock { () -> Bool in + guard settled == nil else { return false } + ipv6Hold = descriptor + return true + } + guard kept else { + close(descriptor) + return + } + resumeReady(bound) + case .unavailable: + resumeReady(bound) + case .taken(let code): + // A port the kernel picked is only a bad draw; a fixed redirect port has no alternative. + if port == 0, listenOnAnotherKernelPort() { return } + LoopbackRedirectLimits.logger.error("Another process holds the IPv6 loopback port (errno \(code, privacy: .public))") + finish(.failure(LoopbackRedirectListenerError.portInUse)) + } + } + + private func listenOnAnotherKernelPort() -> Bool { + guard let replacement = try? makeListener() else { return false } + let (swapped, previous) = lock.withLock { () -> (Bool, NWListener?) in + guard settled == nil, readyContinuation != nil, + portAttempts < LoopbackRedirectLimits.maximumKernelPortAttempts + else { return (false, nil) } + portAttempts += 1 + let previous = listener + listener = replacement + return (true, previous) + } + guard swapped else { + replacement.cancel() + return false + } + previous?.cancel() + replacement.start(queue: queue) + return true + } + + private func resumeReady(_ port: UInt16) { + let (continuation, item) = lock.withLock { () -> (CheckedContinuation?, DispatchWorkItem?) in + let pending = (readyContinuation, readyTimeout) + readyContinuation = nil + readyTimeout = nil + return pending + } + item?.cancel() + continuation?.resume(returning: port) + } + + // When every slot is taken, the oldest connection that never sent its request makes room for the new one. + private func accept(_ connection: NWConnection) { + let (admitted, evicted) = lock.withLock { () -> (Bool, NWConnection?) in + guard settled == nil else { return (false, nil) } + var evicted: NWConnection? + if connections.count >= LoopbackRedirectLimits.maximumConnections { + let idle = connections.filter { $0.value.awaitingHead } + guard let oldest = idle.min(by: { $0.value.order < $1.value.order }) else { return (false, nil) } + connections[oldest.key] = nil + evicted = oldest.value.connection + } + admissions += 1 + connections[ObjectIdentifier(connection)] = Admission(connection: connection, order: admissions, awaitingHead: true) + return (true, evicted) + } + evicted?.cancel() + guard admitted else { + connection.cancel() + return + } + connection.stateUpdateHandler = { [weak self, weak connection] state in + guard let connection else { return } + switch state { + case .failed, .cancelled: + self?.release(connection) + default: + break + } + } + connection.start(queue: queue) + receive(on: connection, head: Data()) + queue.asyncAfter(deadline: .now() + headTimeout) { [weak self, weak connection] in + guard let self, let connection, self.dropIfAwaitingHead(connection) else { return } + connection.cancel() + } + } + + private func receive(on connection: NWConnection, head: Data) { + let remaining = LoopbackRedirectLimits.maximumRequestHeadBytes - head.count + connection.receive(minimumIncompleteLength: 1, maximumLength: remaining) { [weak self] content, _, isComplete, error in + guard let self else { + connection.cancel() + return + } + var received = head + if let content { + received.append(content) + } + if error != nil, received.isEmpty { + connection.cancel() + return + } + let hasFullHead = LoopbackRedirectRequest.isComplete(received) + if error == nil, !isComplete, !hasFullHead, received.count < LoopbackRedirectLimits.maximumRequestHeadBytes { + self.receive(on: connection, head: received) + return + } + self.respond(on: connection, head: received) + } + } + + private func respond(on connection: NWConnection, head: Data) { + switch answer(head) { + case .ignore(let response): + markAnswered(connection) + send(response, on: connection) + case .settle(let response, let result): + release(connection) + send(response, on: connection) + finish(result) + } + } + + private func send(_ response: Data, on connection: NWConnection) { + connection.send(content: response, completion: .contentProcessed { _ in + connection.cancel() + }) + } + + private func markAnswered(_ connection: NWConnection) { + lock.withLock { + connections[ObjectIdentifier(connection)]?.awaitingHead = false + return + } + } + + private func dropIfAwaitingHead(_ connection: NWConnection) -> Bool { + lock.withLock { () -> Bool in + let key = ObjectIdentifier(connection) + guard connections[key]?.awaitingHead == true else { return false } + connections[key] = nil + return true + } + } + + private func release(_ connection: NWConnection) { + lock.withLock { + connections[ObjectIdentifier(connection)] = nil + } + } + + private func finish(_ result: Result) { + let teardown = lock.withLock { () -> Teardown? in + guard settled == nil else { return nil } + settled = result + let teardown = Teardown( + listener: listener, + connections: connections.values.map { $0.connection }, + ipv6Hold: ipv6Hold, + readyContinuation: readyContinuation, + readyTimeout: readyTimeout, + outcomeContinuation: outcomeContinuation, + outcomeTimeout: outcomeTimeout + ) + listener = nil + connections = [:] + ipv6Hold = -1 + readyContinuation = nil + readyTimeout = nil + outcomeContinuation = nil + outcomeTimeout = nil + return teardown + } + guard let teardown else { return } + teardown.readyTimeout?.cancel() + teardown.outcomeTimeout?.cancel() + teardown.listener?.cancel() + teardown.connections.forEach { $0.cancel() } + if teardown.ipv6Hold >= 0 { + close(teardown.ipv6Hold) + } + if let ready = teardown.readyContinuation { + switch result { + case .failure(let error): + ready.resume(throwing: error) + case .success: + ready.resume(throwing: CancellationError()) + } + } + teardown.outcomeContinuation?.resume(with: result) + } +} + +public enum LoopbackRedirectRequest { + public static func isComplete(_ head: Data) -> Bool { + head.range(of: Data("\r\n\r\n".utf8)) != nil || head.range(of: Data("\n\n".utf8)) != nil + } + + public static func text(of head: Data) -> String { + String(bytes: head, encoding: .utf8) ?? String(bytes: head, encoding: .isoLatin1) ?? "" + } + + // The target of a `GET` in origin form, the only shape a browser sends for a redirect. + public static func getTarget(_ head: String) -> String? { + let scalars = head.unicodeScalars + let lineEnd = scalars.firstIndex(of: "\n") ?? scalars.endIndex + var requestLine = String(scalars[scalars.startIndex.. String? { + let matches = items.filter { $0.name == name } + guard matches.count == 1 else { return nil } + return matches[0].value + } + + // A redirect query is form encoded, so "+" stands for a space. + public static func formDecodedItems(in components: URLComponents) -> [URLQueryItem] { + (components.percentEncodedQueryItems ?? []).map { item in + URLQueryItem(name: formDecoded(item.name), value: item.value.map(formDecoded)) + } + } + + public static func constantTimeEquals(_ lhs: String, _ rhs: String) -> Bool { + let left = Array(lhs.utf8) + let right = Array(rhs.utf8) + guard left.count == right.count else { return false } + var difference: UInt8 = 0 + for index in left.indices { + difference |= left[index] ^ right[index] + } + return difference == 0 + } + + private static func formDecoded(_ value: String) -> String { + let spaced = value.replacingOccurrences(of: "+", with: " ") + return spaced.removingPercentEncoding ?? spaced + } +} + +public enum LoopbackRedirectPage { + public static func response(status: String, title: String, message: String) -> Data { + let body = Data(""" + TablePro + +

\(title)

\(message)

+ """.utf8) + let headers = [ + "HTTP/1.1 \(status)", + "Content-Type: text/html; charset=utf-8", + "Content-Length: \(body.count)", + "Cache-Control: no-store", + "X-Content-Type-Options: nosniff", + "Referrer-Policy: no-referrer", + "Content-Security-Policy: default-src 'none'; style-src 'unsafe-inline'", + "Connection: close" + ] + return Data((headers.joined(separator: "\r\n") + "\r\n\r\n").utf8) + body + } + + public static let badRequest = response(status: "400 Bad Request", title: "Bad Request", message: "") +} + +enum LoopbackRedirectLimits { + static let maximumRequestHeadBytes = 16_384 + static let maximumConnections = 16 + static let maximumKernelPortAttempts = 5 + static let logger = Logger(subsystem: "com.TablePro", category: "LoopbackRedirectListener") + + static func code(of error: NWError) -> Int32 { + if case .posix(let code) = error { return code.rawValue } + return -1 + } +} + +enum LoopbackPortHold: Sendable { + case held(Int32) + case unavailable + case taken(Int32) + + // Bound but never listening: a browser that tries ::1 first is not answered there and falls back to 127.0.0.1. + static func ipv6(port: UInt16) -> LoopbackPortHold { + let descriptor = socket(AF_INET6, SOCK_STREAM, 0) + guard descriptor >= 0 else { + let failure = errno + return isUnavailable(failure) ? .unavailable : .taken(failure) + } + _ = fcntl(descriptor, F_SETFD, FD_CLOEXEC) + var address = sockaddr_in6() + address.sin6_len = UInt8(MemoryLayout.size) + address.sin6_family = sa_family_t(AF_INET6) + address.sin6_port = port.bigEndian + guard inet_pton(AF_INET6, "::1", &address.sin6_addr) == 1 else { + close(descriptor) + return .unavailable + } + let result = withUnsafePointer(to: &address) { pointer in + pointer.withMemoryRebound(to: sockaddr.self, capacity: 1) { generic in + bind(descriptor, generic, socklen_t(MemoryLayout.size)) + } + } + if result == 0 { + return .held(descriptor) + } + let failure = errno + close(descriptor) + return isUnavailable(failure) ? .unavailable : .taken(failure) + } + + private static func isUnavailable(_ code: Int32) -> Bool { + code == EADDRNOTAVAIL || code == EAFNOSUPPORT || code == EPROTONOSUPPORT + } +} diff --git a/apple/Packages/TableProCore/Sources/TableProSnowflakeCore/SnowflakeBrowserCallback.swift b/apple/Packages/TableProCore/Sources/TableProSnowflakeCore/SnowflakeBrowserCallback.swift new file mode 100644 index 0000000000..dd9ad0a3ad --- /dev/null +++ b/apple/Packages/TableProCore/Sources/TableProSnowflakeCore/SnowflakeBrowserCallback.swift @@ -0,0 +1,23 @@ +import Foundation +import TableProHTTP + +// The identity provider sends the SAML token to http://localhost:/?token=..., with no state to check, +// so only that exact shape ends the wait and anything else is answered and ignored. +public enum SnowflakeBrowserCallback: Sendable, Equatable { + case token(String) + case ignore + + public static func parse(requestHead: Data) -> SnowflakeBrowserCallback { + guard LoopbackRedirectRequest.isComplete(requestHead), + let target = LoopbackRedirectRequest.getTarget(LoopbackRedirectRequest.text(of: requestHead)), + let components = URLComponents(string: target), + components.host == nil, + components.path == "/", + let token = LoopbackRedirectRequest.singleValue(named: "token", in: components.queryItems ?? []), + !token.isEmpty + else { + return .ignore + } + return .token(token) + } +} diff --git a/apple/Packages/TableProCore/Sources/TableProSyncTransport/CloudKitSyncEngine.swift b/apple/Packages/TableProCore/Sources/TableProSyncTransport/CloudKitSyncEngine.swift index 78795f78de..7e5b4fd852 100644 --- a/apple/Packages/TableProCore/Sources/TableProSyncTransport/CloudKitSyncEngine.swift +++ b/apple/Packages/TableProCore/Sources/TableProSyncTransport/CloudKitSyncEngine.swift @@ -7,6 +7,7 @@ public struct PullResult: Sendable { public let deletedRecordIDs: [CKRecord.ID] public let newToken: CKServerChangeToken? // Records the zone holds that the fetch could not read, so their absence from `changedRecords` proves nothing. + // Until `UnreadRecordLedger` releases one, `newToken` stops before it, so the next pull lists it again. public let unreadRecordIDs: [CKRecord.ID] public init( @@ -34,6 +35,11 @@ public actor CloudKitSyncEngine { private static let maxRetries = 3 + // Under the 400 items per operation CloudKit's limitExceeded documentation gives as its guideline. + private static let refetchBatchSize = 200 + + private var unreadLedger = UnreadRecordLedger() + /// What a build without the iCloud entitlement reports, in CloudKit's own terms, so it is /// classified like the server's answer to the same fault. private static var unavailable: CKError { CKError(.missingEntitlement) } @@ -236,7 +242,7 @@ public actor CloudKitSyncEngine { public func pull(since token: CKServerChangeToken?) async throws -> PullResult { var changedRecords: [CKRecord] = [] var deletedRecordIDs: [CKRecord.ID] = [] - var unreadRecordIDs: [CKRecord.ID] = [] + var pages: [PullAcknowledgement.Page] = [] var cursor = token while true { @@ -246,17 +252,89 @@ public actor CloudKitSyncEngine { changedRecords.append(contentsOf: page.result.changedRecords) deletedRecordIDs.append(contentsOf: page.result.deletedRecordIDs) - unreadRecordIDs.append(contentsOf: page.result.unreadRecordIDs) + pages.append(PullAcknowledgement.Page( + startToken: cursor, + changed: Set(page.result.changedRecords.map(\.recordID)), + deleted: Set(page.result.deletedRecordIDs), + unread: page.result.unreadRecordIDs + )) cursor = page.result.newToken ?? cursor - guard page.moreComing, page.result.newToken != nil else { - return PullResult( - changedRecords: changedRecords, - deletedRecordIDs: deletedRecordIDs, - newToken: cursor, - unreadRecordIDs: unreadRecordIDs - ) + guard page.moreComing, page.result.newToken != nil else { break } + } + + let unread = Set(PullAcknowledgement.settle(pages, finalToken: cursor).unread).union(unreadLedger.released) + let refetched = await refetchByID(Array(unread)) + changedRecords.append(contentsOf: refetched.records) + unreadLedger.forget(refetched.resolved) + var settled = PullAcknowledgement.settle( + pages, finalToken: cursor, resolved: refetched.resolved.union(unreadLedger.released) + ) + let releasing = unreadLedger.countPull(holding: settled.unread) + if !releasing.isEmpty { + let names = releasing.map(\.recordName).sorted().joined(separator: ", ") + Self.logger.error( + "Released \(releasing.count, privacy: .public) records unread after \(UnreadRecordLedger.heldPulls, privacy: .public) pulls; fetching them by ID: \(names, privacy: .private)" + ) + settled = PullAcknowledgement.settle( + pages, finalToken: cursor, resolved: refetched.resolved.union(unreadLedger.released) + ) + } + if !settled.unread.isEmpty { + Self.logger.error( + "\(settled.unread.count, privacy: .public) changed records could not be read; the change token stays before them" + ) + } + return PullResult( + changedRecords: changedRecords, + deletedRecordIDs: deletedRecordIDs, + newToken: settled.token, + unreadRecordIDs: settled.unread + unreadLedger.released.sorted { $0.recordName < $1.recordName } + ) + } + + private struct Refetch { + var records: [CKRecord] = [] + // Read, or gone from the zone, so it holds the token no longer. + var resolved: Set = [] + } + + // A failed batch leaves its records unread, which holds the token rather than failing the pages already read. + private func refetchByID(_ recordIDs: [CKRecord.ID]) async -> Refetch { + var outcome = Refetch() + var start = 0 + while start < recordIDs.count { + let end = min(start + Self.refetchBatchSize, recordIDs.count) + await refetch(Array(recordIDs[start.. 1 { + let middle = batch.count / 2 + await refetch(Array(batch[.. { + let startToken: Token? + let changed: Set + let deleted: Set + let unread: [CKRecord.ID] + } + + struct Settled { + let token: Token? + let unread: [CKRecord.ID] + } + + // A record a later page changed or deleted was read at a state no older than its failure. One in `resolved` + // holds nothing either: it was read by ID, is gone, or was released by `UnreadRecordLedger`. + static func settle( + _ pages: [Page], + finalToken: Token?, + resolved: Set = [] + ) -> Settled { + var token = finalToken + var unread: [CKRecord.ID] = [] + var readLater = resolved + for page in pages.reversed() { + let stillUnread = page.unread.filter { !readLater.contains($0) } + if !stillUnread.isEmpty { + token = page.startToken + unread.insert(contentsOf: stillUnread, at: 0) + } + readLater.formUnion(page.changed) + readLater.formUnion(page.deleted) + } + var listed: Set = [] + return Settled(token: token, unread: unread.filter { listed.insert($0).inserted }) + } +} + +// A record CloudKit keeps failing to deliver holds the token for `heldPulls` pulls in a row. Then it is released: the +// token moves past it, so sync and the full-fetch deletion check go on, and each later pull fetches it by ID until it +// reads or is gone. Kept for the life of the engine. +struct UnreadRecordLedger { + static let heldPulls = 3 + + private var pullsHeld: [CKRecord.ID: Int] = [:] + private(set) var released: Set = [] + + mutating func forget(_ recordIDs: Set) { + released.subtract(recordIDs) + } + + // Counts one more pull for each record that still holds the token; a record that stopped holding starts over. + // Returns the records this pull releases. + mutating func countPull(holding recordIDs: [CKRecord.ID]) -> Set { + let holding = Set(recordIDs) + pullsHeld = pullsHeld.filter { holding.contains($0.key) } + var releasing: Set = [] + for recordID in holding { + let pulls = (pullsHeld[recordID] ?? 0) + 1 + if pulls >= Self.heldPulls { + releasing.insert(recordID) + pullsHeld[recordID] = nil + } else { + pullsHeld[recordID] = pulls + } + } + released.formUnion(releasing) + return releasing + } +} diff --git a/apple/Packages/TableProCore/Tests/TableProHTTPTests/LoopbackRedirectListenerTests.swift b/apple/Packages/TableProCore/Tests/TableProHTTPTests/LoopbackRedirectListenerTests.swift new file mode 100644 index 0000000000..b618f27224 --- /dev/null +++ b/apple/Packages/TableProCore/Tests/TableProHTTPTests/LoopbackRedirectListenerTests.swift @@ -0,0 +1,305 @@ +import Darwin +import Foundation +@testable import TableProHTTP +import Testing + +private enum LoopbackProbe { + static func status(of target: String, port: UInt16) async -> Int? { + guard let url = URL(string: "http://127.0.0.1:\(port)\(target)") else { return nil } + let session = URLSession(configuration: .ephemeral) + defer { session.finishTasksAndInvalidate() } + let request = URLRequest(url: url, timeoutInterval: 5) + guard let (_, response) = try? await session.data(for: request), + let httpResponse = response as? HTTPURLResponse + else { + return nil + } + return httpResponse.statusCode + } + + static func connectIPv4(port: UInt16) -> Int32? { + let descriptor = socket(AF_INET, SOCK_STREAM, 0) + guard descriptor >= 0 else { return nil } + var address = sockaddr_in() + address.sin_len = UInt8(MemoryLayout.size) + address.sin_family = sa_family_t(AF_INET) + address.sin_port = port.bigEndian + _ = inet_pton(AF_INET, "127.0.0.1", &address.sin_addr) + let result = withUnsafePointer(to: &address) { pointer in + pointer.withMemoryRebound(to: sockaddr.self, capacity: 1) { generic in + connect(descriptor, generic, socklen_t(MemoryLayout.size)) + } + } + guard result == 0 else { + close(descriptor) + return nil + } + return descriptor + } + + // The descriptor bound to [::1]:port, or the errno the bind failed with. + static func bindIPv6(port: UInt16, listening: Bool = false) -> Result { + let descriptor = socket(AF_INET6, SOCK_STREAM, 0) + guard descriptor >= 0 else { return .failure(POSIXError(POSIXErrorCode(rawValue: errno) ?? .EIO)) } + var address = sockaddr_in6() + address.sin6_len = UInt8(MemoryLayout.size) + address.sin6_family = sa_family_t(AF_INET6) + address.sin6_port = port.bigEndian + _ = inet_pton(AF_INET6, "::1", &address.sin6_addr) + let result = withUnsafePointer(to: &address) { pointer in + pointer.withMemoryRebound(to: sockaddr.self, capacity: 1) { generic in + bind(descriptor, generic, socklen_t(MemoryLayout.size)) + } + } + guard result == 0 else { + let failure = errno + close(descriptor) + return .failure(POSIXError(POSIXErrorCode(rawValue: failure) ?? .EIO)) + } + if listening { + _ = listen(descriptor, 4) + } + return .success(descriptor) + } + + static func boundPort(of descriptor: Int32) -> UInt16 { + var address = sockaddr_in6() + var length = socklen_t(MemoryLayout.size) + _ = withUnsafeMutablePointer(to: &address) { pointer in + pointer.withMemoryRebound(to: sockaddr.self, capacity: 1) { generic in + getsockname(descriptor, generic, &length) + } + } + return UInt16(bigEndian: address.sin6_port) + } + + // True once the peer closed or reset the connection; false when nothing happened before the timeout. + static func waitForClose(_ descriptor: Int32, seconds: Int) async -> Bool { + await withCheckedContinuation { continuation in + DispatchQueue.global().async { + var timeout = timeval(tv_sec: seconds, tv_usec: 0) + setsockopt(descriptor, SOL_SOCKET, SO_RCVTIMEO, &timeout, socklen_t(MemoryLayout.size)) + var byte: UInt8 = 0 + let received = recv(descriptor, &byte, 1, 0) + let failure = errno + continuation.resume(returning: received == 0 || (received < 0 && failure != EAGAIN && failure != EWOULDBLOCK)) + } + } + } + + static func listener(port: UInt16 = 0, headTimeout: TimeInterval = 5) -> LoopbackRedirectListener { + LoopbackRedirectListener(port: port, headTimeout: headTimeout, answer: answer) + } + + // Settles on a GET to /done and ignores every other request. + static let answer: LoopbackRedirectListener.Answer = { head in + let text = LoopbackRedirectRequest.text(of: head) + guard let target = LoopbackRedirectRequest.getTarget(text), target.hasPrefix("/done") else { + return .ignore(LoopbackRedirectPage.badRequest) + } + return .settle(LoopbackRedirectPage.response(status: "200 OK", title: "Done", message: ""), .success(target)) + } + + static func freeIPv4Port() -> UInt16? { + let descriptor = socket(AF_INET, SOCK_STREAM, 0) + guard descriptor >= 0 else { return nil } + defer { close(descriptor) } + var address = sockaddr_in() + address.sin_len = UInt8(MemoryLayout.size) + address.sin_family = sa_family_t(AF_INET) + _ = inet_pton(AF_INET, "127.0.0.1", &address.sin_addr) + var length = socklen_t(MemoryLayout.size) + let bound = withUnsafeMutablePointer(to: &address) { pointer in + pointer.withMemoryRebound(to: sockaddr.self, capacity: 1) { generic in + bind(descriptor, generic, length) == 0 && getsockname(descriptor, generic, &length) == 0 + } + } + return bound ? UInt16(bigEndian: address.sin_port) : nil + } +} + +private final class PortAttempts: @unchecked Sendable { + private let lock = NSLock() + private var tried: [UInt16] = [] + + var ports: [UInt16] { + lock.withLock { tried } + } + + func record(_ port: UInt16) -> Int { + lock.withLock { + tried.append(port) + return tried.count + } + } +} + +private struct SampleTimeout: Error, Equatable {} + +struct LoopbackRedirectListenerTests { + @Test("Requests the answer ignores get a reply and leave the wait open for the redirect") + func strayRequestsAreAnsweredAndIgnored() async throws { + let listener = LoopbackProbe.listener() + let port = try await listener.start() + defer { listener.stop() } + let outcome = Task { try await listener.outcome(timeout: 30, timeoutError: SampleTimeout()) } + + let stray = await LoopbackProbe.status(of: "/auth?code=forged", port: port) + let favicon = await LoopbackProbe.status(of: "/favicon.ico", port: port) + let redirect = await LoopbackProbe.status(of: "/done?code=real", port: port) + + #expect([stray, favicon, redirect] == [400, 400, 200]) + #expect(try await outcome.value == "/done?code=real") + #expect(await LoopbackProbe.status(of: "/done?again", port: port) == nil) + } + + @Test("A redirect that lands before anyone waits is kept for the wait") + func settledBeforeTheWait() async throws { + let listener = LoopbackProbe.listener() + let port = try await listener.start() + defer { listener.stop() } + + #expect(await LoopbackProbe.status(of: "/done", port: port) == 200) + #expect(try await listener.outcome(timeout: 30, timeoutError: SampleTimeout()) == "/done") + } + + @Test("A connection that sends no request is closed at the head deadline") + func idleConnectionIsClosedAtTheDeadline() async throws { + let listener = LoopbackProbe.listener(headTimeout: 0.2) + let port = try await listener.start() + defer { listener.stop() } + + let idle = try #require(LoopbackProbe.connectIPv4(port: port)) + defer { close(idle) } + + #expect(await LoopbackProbe.waitForClose(idle, seconds: 5)) + } + + @Test("Idle sockets filling every slot cannot keep the redirect out") + func fullTableMakesRoomForTheRedirect() async throws { + let listener = LoopbackProbe.listener(headTimeout: 60) + let port = try await listener.start() + defer { listener.stop() } + let outcome = Task { try await listener.outcome(timeout: 30, timeoutError: SampleTimeout()) } + + let idle = (0..<20).compactMap { _ in LoopbackProbe.connectIPv4(port: port) } + defer { idle.forEach { close($0) } } + #expect(idle.count == 20) + + #expect(await LoopbackProbe.status(of: "/done", port: port) == 200) + #expect(try await outcome.value == "/done") + } + + @Test("While it listens, no other socket can take the IPv6 loopback port, and stopping frees it") + func ipv6LoopbackPortIsHeld() async throws { + let listener = LoopbackProbe.listener() + let port = try await listener.start() + + switch LoopbackProbe.bindIPv6(port: port) { + case .success(let descriptor): + close(descriptor) + Issue.record("Another socket took [::1]:\(port) while the listener held it") + case .failure(let error): + #expect(error.code == .EADDRINUSE) + } + + listener.stop() + switch LoopbackProbe.bindIPv6(port: port) { + case .success(let descriptor): + close(descriptor) + case .failure(let error): + Issue.record("[::1]:\(port) stayed taken after stop: \(error.code)") + } + } + + @Test("A port whose IPv6 loopback side another process holds fails the start") + func takenIPv6LoopbackPortFailsTheStart() async throws { + let held = try LoopbackProbe.bindIPv6(port: 0, listening: true).get() + defer { close(held) } + let listener = LoopbackProbe.listener(port: LoopbackProbe.boundPort(of: held)) + + await #expect(throws: LoopbackRedirectListenerError.portInUse) { + try await listener.start() + } + } + + @Test("A kernel-picked port whose IPv6 loopback side is taken is traded for another") + func kernelPickedPortTriesAnotherWhenIPv6IsTaken() async throws { + let attempts = PortAttempts() + let listener = LoopbackRedirectListener( + port: 0, + headTimeout: 5, + holdIPv6: { port in + attempts.record(port) < 3 ? .taken(EADDRINUSE) : LoopbackPortHold.ipv6(port: port) + }, + answer: LoopbackProbe.answer + ) + let port = try await listener.start() + defer { listener.stop() } + + #expect(attempts.ports.count == 3) + #expect(attempts.ports.last == port) + #expect(await LoopbackProbe.status(of: "/done", port: port) == 200) + } + + @Test("A kernel-picked port stops trying after a bounded number of taken ports") + func kernelPickedPortGivesUp() async throws { + let attempts = PortAttempts() + let listener = LoopbackRedirectListener( + port: 0, + headTimeout: 5, + holdIPv6: { port in + _ = attempts.record(port) + return .taken(EADDRINUSE) + }, + answer: LoopbackProbe.answer + ) + + await #expect(throws: LoopbackRedirectListenerError.portInUse) { + try await listener.start() + } + #expect(attempts.ports.count == LoopbackRedirectLimits.maximumKernelPortAttempts) + } + + @Test("A fixed port whose IPv6 loopback side is taken fails without trying another") + func fixedPortDoesNotTryAnother() async throws { + let fixed = try #require(LoopbackProbe.freeIPv4Port()) + let attempts = PortAttempts() + let listener = LoopbackRedirectListener( + port: fixed, + headTimeout: 5, + holdIPv6: { port in + _ = attempts.record(port) + return .taken(EADDRINUSE) + }, + answer: LoopbackProbe.answer + ) + + await #expect(throws: LoopbackRedirectListenerError.portInUse) { + try await listener.start() + } + #expect(attempts.ports == [fixed]) + } + + @Test("Stopping ends the wait") + func stopEndsTheWait() async throws { + let listener = LoopbackProbe.listener() + try await listener.start() + listener.stop() + + await #expect(throws: CancellationError.self) { + try await listener.outcome(timeout: 30, timeoutError: SampleTimeout()) + } + } + + @Test("A wait with no redirect ends with the caller's timeout error") + func waitTimesOut() async throws { + let listener = LoopbackProbe.listener() + try await listener.start() + defer { listener.stop() } + + await #expect(throws: SampleTimeout()) { + try await listener.outcome(timeout: 0.2, timeoutError: SampleTimeout()) + } + } +} diff --git a/apple/Packages/TableProCore/Tests/TableProSnowflakeCoreTests/SnowflakeBrowserCallbackTests.swift b/apple/Packages/TableProCore/Tests/TableProSnowflakeCoreTests/SnowflakeBrowserCallbackTests.swift new file mode 100644 index 0000000000..c87b161fa8 --- /dev/null +++ b/apple/Packages/TableProCore/Tests/TableProSnowflakeCoreTests/SnowflakeBrowserCallbackTests.swift @@ -0,0 +1,34 @@ +import Foundation +import TableProSnowflakeCore +import Testing + +struct SnowflakeBrowserCallbackTests { + private static func head(_ target: String, method: String = "GET") -> Data { + Data("\(method) \(target) HTTP/1.1\r\nHost: localhost:49152\r\n\r\n".utf8) + } + + @Test("The identity provider's redirect yields its SAML token") + func redirectYieldsTheToken() { + #expect(SnowflakeBrowserCallback.parse(requestHead: Self.head("/?token=ver%3A1-hint%3A2")) == .token("ver:1-hint:2")) + } + + @Test("A request that is not the token redirect is ignored", arguments: [ + "/favicon.ico", + "/", + "/?token=", + "/?token=a&token=b", + "/?error=access_denied", + "/other?token=abc" + ]) + func strayRequestIsIgnored(target: String) { + #expect(SnowflakeBrowserCallback.parse(requestHead: Self.head(target)) == .ignore) + } + + @Test("Only a complete GET head in origin form is read") + func otherRequestShapesAreIgnored() { + #expect(SnowflakeBrowserCallback.parse(requestHead: Self.head("/?token=abc", method: "POST")) == .ignore) + #expect(SnowflakeBrowserCallback.parse(requestHead: Data("GET /?token=abc HTTP/1.1\r\nHost: loc".utf8)) == .ignore) + #expect(SnowflakeBrowserCallback.parse(requestHead: Data("GET http://localhost/?token=abc HTTP/1.1\r\n\r\n".utf8)) == .ignore) + #expect(SnowflakeBrowserCallback.parse(requestHead: Data()) == .ignore) + } +} diff --git a/apple/Packages/TableProCore/Tests/TableProSyncTests/PullAcknowledgementTests.swift b/apple/Packages/TableProCore/Tests/TableProSyncTests/PullAcknowledgementTests.swift new file mode 100644 index 0000000000..9b241e4775 --- /dev/null +++ b/apple/Packages/TableProCore/Tests/TableProSyncTests/PullAcknowledgementTests.swift @@ -0,0 +1,164 @@ +import CloudKit +import Foundation +import Testing + +@testable import TableProSyncTransport + +struct PullAcknowledgementTests { + private static let zoneID = CKRecordZone.ID(zoneName: "TableProSync", ownerName: CKCurrentUserDefaultName) + + private static func id(_ name: String) -> CKRecord.ID { + CKRecord.ID(recordName: name, zoneID: zoneID) + } + + private static func page( + from start: String?, + changed: [String] = [], + deleted: [String] = [], + unread: [String] = [] + ) -> PullAcknowledgement.Page { + PullAcknowledgement.Page( + startToken: start, + changed: Set(changed.map { id($0) }), + deleted: Set(deleted.map { id($0) }), + unread: unread.map { id($0) } + ) + } + + @Test("A pull that read every record acknowledges its final token") + func everyRecordRead() { + let settled = PullAcknowledgement.settle( + [Self.page(from: "t0", changed: ["a"]), Self.page(from: "t1", deleted: ["b"])], + finalToken: "t2" + ) + + #expect(settled.token == "t2") + #expect(settled.unread.isEmpty) + } + + @Test("A record CloudKit could not read holds the token at the start of its page") + func unreadRecordHoldsTheToken() { + let settled = PullAcknowledgement.settle( + [ + Self.page(from: "t0", changed: ["a"]), + Self.page(from: "t1", changed: ["c"], unread: ["b"]), + Self.page(from: "t2", changed: ["d"]) + ], + finalToken: "t3" + ) + + #expect(settled.token == "t1") + #expect(settled.unread == [Self.id("b")]) + } + + @Test("A full fetch whose first page could not read a record acknowledges no token") + func fullFetchKeepsNoToken() { + let settled = PullAcknowledgement.settle([Self.page(from: nil, unread: ["b"])], finalToken: "t1") + + #expect(settled.token == nil) + #expect(settled.unread == [Self.id("b")]) + } + + @Test("A record a later page changed or deleted no longer holds the token") + func laterPageReleasesTheRecord() { + let changedLater = PullAcknowledgement.settle( + [Self.page(from: "t0", unread: ["b"]), Self.page(from: "t1", changed: ["b"])], + finalToken: "t2" + ) + let deletedLater = PullAcknowledgement.settle( + [Self.page(from: "t0", unread: ["b"]), Self.page(from: "t1", deleted: ["b"])], + finalToken: "t2" + ) + + #expect(changedLater.token == "t2") + #expect(changedLater.unread.isEmpty) + #expect(deletedLater.token == "t2") + #expect(deletedLater.unread.isEmpty) + } + + @Test("A record read before a later page failed on it still holds the token") + func earlierReadDoesNotRelease() { + let settled = PullAcknowledgement.settle( + [Self.page(from: "t0", changed: ["b"]), Self.page(from: "t1", unread: ["b"])], + finalToken: "t2" + ) + + #expect(settled.token == "t1") + #expect(settled.unread == [Self.id("b")]) + } + + @Test("A record fetched again by ID no longer holds the token") + func refetchedRecordReleasesTheToken() { + let pages = [Self.page(from: "t0", unread: ["b"]), Self.page(from: "t1", unread: ["c"])] + + let partly = PullAcknowledgement.settle(pages, finalToken: "t2", resolved: [Self.id("b")]) + let fully = PullAcknowledgement.settle(pages, finalToken: "t2", resolved: [Self.id("b"), Self.id("c")]) + + #expect(partly.token == "t1") + #expect(partly.unread == [Self.id("c")]) + #expect(fully.token == "t2") + #expect(fully.unread.isEmpty) + } + + @Test("A record two pages failed on is listed once, and the earlier page holds the token") + func repeatedFailureIsListedOnce() { + let settled = PullAcknowledgement.settle( + [Self.page(from: "t0", unread: ["b"]), Self.page(from: "t1", changed: ["a"]), Self.page(from: "t2", unread: ["b"])], + finalToken: "t3" + ) + + #expect(settled.token == "t0") + #expect(settled.unread == [Self.id("b")]) + } + + @Test("A record unread for the held number of pulls in a row is released, and the token moves past it") + func recordIsReleasedAfterTheHeldPulls() { + var ledger = UnreadRecordLedger() + let pages = [Self.page(from: "t0", changed: ["a"]), Self.page(from: "t1", unread: ["b"])] + + for _ in 1 ..< UnreadRecordLedger.heldPulls { + let held = PullAcknowledgement.settle(pages, finalToken: "t2", resolved: ledger.released) + #expect(held.token == "t1") + let releasing = ledger.countPull(holding: held.unread) + #expect(releasing.isEmpty) + } + let last = PullAcknowledgement.settle(pages, finalToken: "t2", resolved: ledger.released) + let releasing = ledger.countPull(holding: last.unread) + #expect(releasing == [Self.id("b")]) + + let released = PullAcknowledgement.settle(pages, finalToken: "t2", resolved: ledger.released) + #expect(released.token == "t2") + #expect(released.unread.isEmpty) + #expect(ledger.released == [Self.id("b")]) + } + + @Test("A pull that reads the record starts its count over") + func readingStartsTheCountOver() { + var ledger = UnreadRecordLedger() + + var released: Set = [] + for _ in 1 ..< UnreadRecordLedger.heldPulls { + released.formUnion(ledger.countPull(holding: [Self.id("b")])) + } + released.formUnion(ledger.countPull(holding: [])) + for _ in 1 ..< UnreadRecordLedger.heldPulls { + released.formUnion(ledger.countPull(holding: [Self.id("b")])) + } + + #expect(released.isEmpty) + #expect(ledger.released.isEmpty) + } + + @Test("A released record that reads by ID, or is gone, is forgotten") + func releasedRecordIsForgottenOnceResolved() { + var ledger = UnreadRecordLedger() + for _ in 0 ..< UnreadRecordLedger.heldPulls { + _ = ledger.countPull(holding: [Self.id("b"), Self.id("c")]) + } + #expect(ledger.released == [Self.id("b"), Self.id("c")]) + + ledger.forget([Self.id("b")]) + + #expect(ledger.released == [Self.id("c")]) + } +} diff --git a/apple/Plugins/SnowflakeDriverPlugin/SnowflakeBrowserAuthServer.swift b/apple/Plugins/SnowflakeDriverPlugin/SnowflakeBrowserAuthServer.swift index 27380d3583..3e272c9bde 100644 --- a/apple/Plugins/SnowflakeDriverPlugin/SnowflakeBrowserAuthServer.swift +++ b/apple/Plugins/SnowflakeDriverPlugin/SnowflakeBrowserAuthServer.swift @@ -7,213 +7,40 @@ // import Foundation -import Network -import os +import TableProHTTP +import TableProSnowflakeCore -final class SnowflakeBrowserAuthServer: @unchecked Sendable { - private var listener: NWListener? - private var connection: NWConnection? - private var readyContinuation: CheckedContinuation? - private var continuation: CheckedContinuation? - private let lock = NSLock() - private var timeoutTask: Task? - private static let logger = Logger(subsystem: "com.TablePro", category: "SnowflakeBrowserAuthServer") +final class SnowflakeBrowserAuthServer: Sendable { + private let listener = LoopbackRedirectListener(port: 0) { head in + switch SnowflakeBrowserCallback.parse(requestHead: head) { + case .token(let token): + return .settle(SnowflakeBrowserAuthServer.successPage, .success(token)) + case .ignore: + return .ignore(LoopbackRedirectPage.badRequest) + } + } func start(timeout: TimeInterval = 120) async throws -> UInt16 { - try await withCheckedThrowingContinuation { cont in - let task = Task { - try? await Task.sleep(for: .seconds(timeout)) - let continuation = self.lock.withLock { () -> CheckedContinuation? in - let continuation = self.readyContinuation - self.readyContinuation = nil - return continuation - } - continuation?.resume(throwing: SnowflakeError.timeout("Browser authentication listener timed out")) - if continuation != nil { self.stop() } - } - lock.withLock { - readyContinuation = cont - timeoutTask = task - } - do { - try startListener() - } catch { - let continuation = lock.withLock { () -> CheckedContinuation? in - let continuation = readyContinuation - readyContinuation = nil - timeoutTask = nil - return continuation - } - task.cancel() - continuation?.resume(throwing: error) - } + do { + return try await listener.start(readyTimeout: timeout) + } catch LoopbackRedirectListenerError.timedOut { + throw SnowflakeError.timeout("Browser authentication listener timed out") + } catch is LoopbackRedirectListenerError { + throw SnowflakeError.authFailed("Browser auth server failed") } } func waitForToken(timeout: TimeInterval = 120) async throws -> String { - try await withCheckedThrowingContinuation { cont in - lock.withLock { continuation = cont } - let task = Task { - try? await Task.sleep(for: .seconds(timeout)) - self.lock.withLock { - if let cont = self.continuation { - self.continuation = nil - cont.resume(throwing: SnowflakeError.timeout("Browser authentication timed out")) - } - } - self.stop() - } - lock.withLock { timeoutTask = task } - } + try await listener.outcome(timeout: timeout, timeoutError: SnowflakeError.timeout("Browser authentication timed out")) } func stop() { - let (task, conn, lst): (Task?, NWConnection?, NWListener?) = lock.withLock { - let values = (timeoutTask, connection, listener) - timeoutTask = nil - connection = nil - listener = nil - return values - } - task?.cancel() - conn?.cancel() - lst?.cancel() - } - - private func startListener() throws { - let params = NWParameters.tcp - params.requiredLocalEndpoint = NWEndpoint.hostPort(host: .ipv4(.loopback), port: .any) - - let listener = try NWListener(using: params) - lock.withLock { self.listener = listener } - - listener.stateUpdateHandler = { [weak self] state in - switch state { - case .ready: - let port = listener.port?.rawValue ?? 0 - let pending = self?.lock.withLock { - let pending = (self?.readyContinuation, self?.timeoutTask) - self?.readyContinuation = nil - self?.timeoutTask = nil - return pending - } - pending?.1?.cancel() - pending?.0?.resume(returning: port) - case .failed(let error): - Self.logger.error("Browser auth server failed: \(error.localizedDescription)") - let authError = SnowflakeError.authFailed("Browser auth server failed: \(error.localizedDescription)") - self?.lock.withLock { - if let cont = self?.readyContinuation { - self?.readyContinuation = nil - cont.resume(throwing: authError) - } - if let cont = self?.continuation { - self?.continuation = nil - cont.resume(throwing: authError) - } - } - default: - break - } - } - - listener.newConnectionHandler = { [weak self] newConnection in - self?.handleConnection(newConnection) - } - - listener.start(queue: .global(qos: .userInitiated)) - } - - private func handleConnection(_ newConnection: NWConnection) { - lock.withLock { connection = newConnection } - newConnection.stateUpdateHandler = { [weak self] state in - if case .ready = state { - self?.readRequest(from: newConnection) - } - } - newConnection.start(queue: .global(qos: .userInitiated)) - } - - private func readRequest(from connection: NWConnection) { - connection.receive(minimumIncompleteLength: 1, maximumLength: 65_536) { [weak self] content, _, _, error in - guard let self, let data = content, error == nil, - let request = String(data: data, encoding: .utf8) else { - self?.resumeWithError(SnowflakeError.authFailed("Failed to read browser callback")) - return - } - - if let token = self.extractToken(from: request) { - self.sendSuccessResponse(to: connection) - self.resumeWithToken(token) - } else if request.contains("error=") { - let desc = self.extractParam(named: "error", from: request) ?? "unknown" - self.sendErrorResponse(to: connection, error: desc) - self.resumeWithError(SnowflakeError.authFailed("SSO authorization failed: \(desc)")) - } else { - self.readRequest(from: connection) - } - } - } - - private func extractToken(from request: String) -> String? { - extractParam(named: "token", from: request) - } - - private func extractParam(named name: String, from request: String) -> String? { - guard let firstLine = request.components(separatedBy: "\r\n").first, - let pathPart = firstLine.components(separatedBy: " ").dropFirst().first, - let components = URLComponents(string: "http://localhost\(pathPart)") - else { return nil } - return components.queryItems?.first(where: { $0.name == name })?.value - } - - private func sendSuccessResponse(to connection: NWConnection) { - let html = """ - -

Authentication Successful

-

You can close this tab and return to TablePro.

- - """ - let response = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: close\r\n\r\n\(html)" - connection.send(content: Data(response.utf8), completion: .contentProcessed { _ in - connection.cancel() - }) - } - - private func sendErrorResponse(to connection: NWConnection, error: String) { - let escaped = error - .replacingOccurrences(of: "&", with: "&") - .replacingOccurrences(of: "<", with: "<") - .replacingOccurrences(of: ">", with: ">") - let html = """ - -

Authentication Failed

-

\(escaped)

- - """ - let response = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: close\r\n\r\n\(html)" - connection.send(content: Data(response.utf8), completion: .contentProcessed { _ in - connection.cancel() - }) - } - - private func resumeWithToken(_ token: String) { - lock.withLock { - if let cont = continuation { - continuation = nil - cont.resume(returning: token) - } - } - stop() + listener.stop() } - private func resumeWithError(_ error: Error) { - lock.withLock { - if let cont = continuation { - continuation = nil - cont.resume(throwing: error) - } - } - stop() - } + private static let successPage = LoopbackRedirectPage.response( + status: "200 OK", + title: "Authentication Successful", + message: "You can close this tab and return to TablePro." + ) } diff --git a/apple/TablePro/Core/AI/ChatGPTCodex/ChatGPTCodexCallbackServer.swift b/apple/TablePro/Core/AI/ChatGPTCodex/ChatGPTCodexCallbackServer.swift index fcf2eb8d3c..716dced00e 100644 --- a/apple/TablePro/Core/AI/ChatGPTCodex/ChatGPTCodexCallbackServer.swift +++ b/apple/TablePro/Core/AI/ChatGPTCodex/ChatGPTCodexCallbackServer.swift @@ -7,14 +7,65 @@ // import Foundation -import Network import os +import TableProHTTP -final class ChatGPTCodexCallbackServer: @unchecked Sendable { - enum ServerError: Error, LocalizedError { +// Only the redirect carrying this sign-in's `state` settles it (RFC 6749 section 4.1.2, RFC 8252 section 7.3). +enum ChatGPTCodexCallback: Sendable, Equatable { + case code(String) + case denied(error: String, description: String?) + case ignore + + static let callbackPath = URLComponents(string: ChatGPTCodex.redirectURI)?.path ?? "" + + static let unknownError = "unknown_error" + + private static let maximumErrorLength = 300 + + static func parse(requestHead: String, expectedState: String) -> ChatGPTCodexCallback { + guard !expectedState.isEmpty, + !callbackPath.isEmpty, + let target = LoopbackRedirectRequest.getTarget(requestHead), + let components = URLComponents(string: target), + components.host == nil, + components.path == callbackPath + else { + return .ignore + } + let items = components.queryItems ?? [] + guard let state = LoopbackRedirectRequest.singleValue(named: "state", in: items), + LoopbackRedirectRequest.constantTimeEquals(state, expectedState) + else { + return .ignore + } + if items.contains(where: { $0.name == "error" }) { + let error = LoopbackRedirectRequest.singleValue(named: "error", in: items).flatMap(sanitized) ?? unknownError + let formItems = LoopbackRedirectRequest.formDecodedItems(in: components) + let description = LoopbackRedirectRequest.singleValue(named: "error_description", in: formItems).flatMap(sanitized) + return .denied(error: error, description: description) + } + if let code = LoopbackRedirectRequest.singleValue(named: "code", in: items), !code.isEmpty { + return .code(code) + } + return .ignore + } + + // RFC 6749 allows only printable ASCII other than `"` and `\` in `error` and `error_description`. + private static func sanitized(_ value: String) -> String? { + let allowed = value.unicodeScalars.filter { scalar in + (0x20...0x7E).contains(scalar.value) && scalar != "\"" && scalar != "\\" + } + let text = String(String.UnicodeScalarView(allowed.prefix(maximumErrorLength))) + .trimmingCharacters(in: .whitespaces) + return text.isEmpty ? nil : text + } +} + +final class ChatGPTCodexCallbackServer: Sendable { + enum ServerError: Error, LocalizedError, Equatable { case portInUse case timedOut - case stateMismatch + case authorizationDenied(error: String, description: String?) var errorDescription: String? { switch self { @@ -22,8 +73,12 @@ final class ChatGPTCodexCallbackServer: @unchecked Sendable { return String(localized: "Port 1455 is in use. Quit the Codex CLI or another sign-in and try again.") case .timedOut: return String(localized: "Sign-in timed out. Please try again.") - case .stateMismatch: - return String(localized: "Sign-in could not be verified. Please try again.") + case .authorizationDenied(let error, let description): + if error == "access_denied" { + guard let description else { return String(localized: "ChatGPT sign-in was denied.") } + return String(format: String(localized: "ChatGPT sign-in was denied: %@"), description) + } + return String(format: String(localized: "ChatGPT sign-in failed: %@"), description ?? error) } } } @@ -32,182 +87,55 @@ final class ChatGPTCodexCallbackServer: @unchecked Sendable { private static let timeout: TimeInterval = 300 private static let startTimeout: TimeInterval = 10 - private let expectedState: String - private let lock = NSLock() - private var listener: NWListener? - private var connection: NWConnection? - private var readyContinuation: CheckedContinuation? - private var codeContinuation: CheckedContinuation? - private var readyTimeoutTask: Task? - private var timeoutTask: Task? - - init(expectedState: String) { - self.expectedState = expectedState - } - - func start() async throws { - try await withCheckedThrowingContinuation { (continuation: CheckedContinuation) in - let timeout = Task { [weak self] in - try? await Task.sleep(nanoseconds: UInt64(Self.startTimeout * 1_000_000_000)) - self?.finishReady(.failure(ServerError.timedOut)) - } - lock.withLock { - readyContinuation = continuation - readyTimeoutTask = timeout - } - do { - try startListener() - } catch { - finishReady(.failure(ServerError.portInUse)) - } - } - } + private let listener: LoopbackRedirectListener - func waitForCode() async throws -> String { - try await withCheckedThrowingContinuation { (continuation: CheckedContinuation) in - lock.withLock { codeContinuation = continuation } - let task = Task { [weak self] in - try? await Task.sleep(nanoseconds: UInt64(Self.timeout * 1_000_000_000)) - self?.finishCode(.failure(ServerError.timedOut)) - } - lock.withLock { timeoutTask = task } + init(expectedState: String, port: UInt16 = ChatGPTCodex.redirectPort, headTimeout: TimeInterval = 5) { + listener = LoopbackRedirectListener(port: port, headTimeout: headTimeout) { head in + Self.answer(head, expectedState: expectedState) } } - func stop() { - let (task, readyTask, conn, lst): ( - Task?, Task?, NWConnection?, NWListener? - ) = lock.withLock { - let values = (timeoutTask, readyTimeoutTask, connection, listener) - timeoutTask = nil - readyTimeoutTask = nil - connection = nil - listener = nil - return values - } - task?.cancel() - readyTask?.cancel() - conn?.cancel() - lst?.cancel() - } - - private func startListener() throws { - guard let port = NWEndpoint.Port(rawValue: ChatGPTCodex.redirectPort) else { + @discardableResult + func start() async throws -> UInt16 { + do { + return try await listener.start(readyTimeout: Self.startTimeout) + } catch LoopbackRedirectListenerError.timedOut { + throw ServerError.timedOut + } catch is LoopbackRedirectListenerError { throw ServerError.portInUse } - let parameters = NWParameters.tcp - parameters.requiredLocalEndpoint = NWEndpoint.hostPort(host: .ipv4(.loopback), port: port) - - let listener = try NWListener(using: parameters) - lock.withLock { self.listener = listener } - - listener.stateUpdateHandler = { [weak self] state in - switch state { - case .ready: - self?.finishReady(.success(())) - case .failed(let error): - Self.logger.error("Callback listener failed: \(error.publicLogShape, privacy: .public)") - self?.finishReady(.failure(ServerError.portInUse)) - self?.finishCode(.failure(ServerError.portInUse)) - default: - break - } - } - listener.newConnectionHandler = { [weak self] connection in - self?.handleConnection(connection) - } - listener.start(queue: .global(qos: .userInitiated)) - } - - private func handleConnection(_ newConnection: NWConnection) { - lock.withLock { connection = newConnection } - newConnection.stateUpdateHandler = { [weak self] state in - if case .ready = state { - self?.readRequest(from: newConnection) - } - } - newConnection.start(queue: .global(qos: .userInitiated)) } - private func readRequest(from connection: NWConnection) { - connection.receive(minimumIncompleteLength: 1, maximumLength: 16_384) { [weak self] content, _, isComplete, error in - guard let self else { return } - if error != nil { - connection.cancel() - return - } - guard let data = content, let request = String(data: data, encoding: .utf8), - let query = Self.parseCallback(request) else { - if isComplete { - connection.cancel() - } else { - self.readRequest(from: connection) - } - return - } - let matches = query.state == self.expectedState - Self.send(html: matches ? Self.successPage : Self.failurePage, to: connection) - self.finishCode(matches ? .success(query.code) : .failure(ServerError.stateMismatch)) - } - } - - private func finishReady(_ result: Result) { - let (continuation, timeout): (CheckedContinuation?, Task?) = lock.withLock { - let pendingContinuation = readyContinuation - let pendingTimeout = readyTimeoutTask - readyContinuation = nil - readyTimeoutTask = nil - return (pendingContinuation, pendingTimeout) - } - timeout?.cancel() - continuation?.resume(with: result) + func waitForCode() async throws -> String { + try await listener.outcome(timeout: Self.timeout, timeoutError: ServerError.timedOut) } - private func finishCode(_ result: Result) { - let continuation: CheckedContinuation? = lock.withLock { - let pending = codeContinuation - codeContinuation = nil - return pending - } - guard let continuation else { return } - continuation.resume(with: result) - stop() + func stop() { + listener.stop() } - private static func parseCallback(_ request: String) -> (code: String, state: String)? { - guard let requestLine = request.components(separatedBy: "\r\n").first, - let target = requestLine.components(separatedBy: " ").dropFirst().first, - let components = URLComponents(string: "http://127.0.0.1\(target)"), - let items = components.queryItems, - let code = items.first(where: { $0.name == "code" })?.value, - let state = items.first(where: { $0.name == "state" })?.value else { - return nil + private static func answer(_ head: Data, expectedState: String) -> LoopbackRedirectAnswer { + let text = LoopbackRedirectRequest.text(of: head) + switch ChatGPTCodexCallback.parse(requestHead: text, expectedState: expectedState) { + case .ignore: + return .ignore(LoopbackRedirectPage.badRequest) + case .code(let code): + return .settle(successPage, .success(code)) + case .denied(let error, let description): + logger.info("The ChatGPT sign-in redirect carried an OAuth error: \(error, privacy: .private)") + return .settle(failurePage, .failure(ServerError.authorizationDenied(error: error, description: description))) } - return (code, state) - } - - private static func send(html: String, to connection: NWConnection) { - let body = Array(html.utf8) - let headers = "HTTP/1.1 200 OK\r\n" - + "Content-Type: text/html; charset=utf-8\r\n" - + "Content-Length: \(body.count)\r\n" - + "Connection: close\r\n\r\n" - var response = Data(headers.utf8) - response.append(contentsOf: body) - connection.send(content: response, completion: .contentProcessed { _ in - connection.cancel() - }) } - private static let successPage = """ - TablePro - -

Signed in to ChatGPT

You can return to TablePro.

- """ - - private static let failurePage = """ - TablePro - -

Sign-in failed

Please return to TablePro and try again.

- """ + private static let successPage = LoopbackRedirectPage.response( + status: "200 OK", + title: "Signed in to ChatGPT", + message: "You can return to TablePro." + ) + + private static let failurePage = LoopbackRedirectPage.response( + status: "200 OK", + title: "Sign-in failed", + message: "Please return to TablePro and try again." + ) } diff --git a/apple/TablePro/Core/Compare/DataDiffEngine.swift b/apple/TablePro/Core/Compare/DataDiffEngine.swift index 882c746b62..86d8be0d23 100644 --- a/apple/TablePro/Core/Compare/DataDiffEngine.swift +++ b/apple/TablePro/Core/Compare/DataDiffEngine.swift @@ -671,31 +671,3 @@ private final class KeyedRowReader { ) } } - -internal final class ArrayRowProvider: DataRowProviding { - private let rows: [DataRow] - private let rowLimit: Int? - private let holdsRowPastTheLimit: Bool - private var index = 0 - - internal init(rows: [DataRow], rowLimit: Int? = nil) { - self.rows = rowLimit.map { Array(rows.prefix($0)) } ?? rows - self.rowLimit = rowLimit - self.holdsRowPastTheLimit = rowLimit.map { rows.count > $0 } ?? false - } - - internal var endedAtRowLimit: Bool { - guard let rowLimit, index >= rows.count else { return false } - return holdsRowPastTheLimit && rows.count >= rowLimit - } - - internal func nextRow() async throws -> DataRow? { - guard index < rows.count else { return nil } - defer { index += 1 } - return rows[index] - } - - internal func drain() async throws { - index = rows.count - } -} diff --git a/apple/TablePro/Core/Database/Vault/VaultAuthenticator.swift b/apple/TablePro/Core/Database/Vault/VaultAuthenticator.swift index 3f4391e661..1aabae8779 100644 --- a/apple/TablePro/Core/Database/Vault/VaultAuthenticator.swift +++ b/apple/TablePro/Core/Database/Vault/VaultAuthenticator.swift @@ -48,7 +48,7 @@ actor VaultAuthenticator { private let readFile: @Sendable (String) -> String? private let now: @Sendable () -> ContinuousClock.Instant private let openURL: @Sendable (URL) async -> Void - private let makeCallbackReceiver: @Sendable () -> any VaultOIDCCallbackReceiving + private let makeCallbackReceiver: @Sendable (_ expectedState: String) -> any VaultOIDCCallbackReceiving private var minted: [LoginKey: VaultToken] = [:] private var oidcSignIns: [LoginKey: Task] = [:] @@ -61,7 +61,9 @@ actor VaultAuthenticator { openURL: @escaping @Sendable (URL) async -> Void = { url in await MainActor.run { _ = WebPageOpener.open(url, scope: .httpsOrLoopbackHTTP) } }, - makeCallbackReceiver: @escaping @Sendable () -> any VaultOIDCCallbackReceiving = { VaultOIDCLoopback() } + makeCallbackReceiver: @escaping @Sendable (_ expectedState: String) -> any VaultOIDCCallbackReceiving = { + VaultOIDCLoopback(expectedState: $0) + } ) { self.transport = transport self.environment = environment @@ -180,10 +182,6 @@ actor VaultAuthenticator { } private func performOIDCSignIn(_ login: VaultOIDCLogin) async throws -> VaultToken { - let receiver = makeCallbackReceiver() - try await receiver.start() - defer { receiver.stop() } - let client = VaultClient(endpoint: login.endpoint, transport: transport) let nonce = UUID().uuidString.replacingOccurrences(of: "-", with: "") + UUID().uuidString.replacingOccurrences(of: "-", with: "") @@ -198,13 +196,23 @@ actor VaultAuthenticator { } catch let error as VaultRequestError { throw VaultError.oidcFailed(error.message) } + // Vault checks this state on the callback; the loopback lets no other callback end the wait. + guard let state = Self.singleQueryValue(named: "state", in: authURL), !state.isEmpty else { + throw VaultError.oidcFailed(String( + localized: "Vault returned a sign-in page without a state, so TablePro could not check the browser's answer." + )) + } + + let receiver = makeCallbackReceiver(state) + try await receiver.start() + defer { receiver.stop() } await openURL(authURL) let parameters = try await receiver.callback(timeout: Self.oidcCallbackTimeout) if let error = parameters["error"] { throw VaultError.oidcFailed(parameters["error_description"] ?? error) } - guard let state = parameters["state"], let code = parameters["code"], !code.isEmpty else { + guard let code = parameters["code"], !code.isEmpty else { throw VaultError.oidcFailed(String(localized: "The browser returned no authorization code.")) } do { @@ -216,6 +224,11 @@ actor VaultAuthenticator { } } + private static func singleQueryValue(named name: String, in url: URL) -> String? { + let items = URLComponents(url: url, resolvingAgainstBaseURL: false)?.queryItems ?? [] + return LoopbackRedirectRequest.singleValue(named: name, in: items) + } + // MARK: - Minted tokens private func reusable(_ key: LoginKey, validFor: Duration?) async -> VaultToken? { diff --git a/apple/TablePro/Core/Database/Vault/VaultOIDCLoopback.swift b/apple/TablePro/Core/Database/Vault/VaultOIDCLoopback.swift index 3cb80e148f..70a5e695d5 100644 --- a/apple/TablePro/Core/Database/Vault/VaultOIDCLoopback.swift +++ b/apple/TablePro/Core/Database/Vault/VaultOIDCLoopback.swift @@ -4,8 +4,7 @@ // import Foundation -import Network -import os +import TableProHTTP protocol VaultOIDCCallbackReceiving: Sendable { func start() async throws @@ -14,202 +13,71 @@ protocol VaultOIDCCallbackReceiving: Sendable { } // Listens where the vault CLI does, http://localhost:8250/oidc/callback, the redirect URI an OIDC role already allows. -final class VaultOIDCLoopback: VaultOIDCCallbackReceiving, @unchecked Sendable { +final class VaultOIDCLoopback: VaultOIDCCallbackReceiving, Sendable { static let port: UInt16 = 8_250 static let callbackPath = "/oidc/callback" - private static let logger = Logger(subsystem: "com.TablePro", category: "VaultOIDCLoopback") - private static let maximumRequestHeadBytes = 8_192 + private let listener: LoopbackRedirectListener<[String: String]> - private let queue = DispatchQueue(label: "com.TablePro.VaultOIDC.loopback") - private let lock = NSLock() - private var listener: NWListener? - private var connections: [ObjectIdentifier: NWConnection] = [:] - private var readyContinuation: CheckedContinuation? - private var callbackContinuation: CheckedContinuation<[String: String], Error>? - private var outcome: Result<[String: String], Error>? - private var timeoutItem: DispatchWorkItem? - - func start() async throws { - let parameters = NWParameters.tcp - parameters.requiredLocalEndpoint = .hostPort(host: .ipv4(.loopback), port: NWEndpoint.Port(rawValue: Self.port) ?? .any) - parameters.allowLocalEndpointReuse = true - let listener: NWListener - do { - listener = try NWListener(using: parameters) - } catch { - throw VaultError.oidcCallbackPortInUse - } - listener.stateUpdateHandler = { [weak self] state in self?.listenerChanged(state) } - listener.newConnectionHandler = { [weak self] connection in - guard let self else { - connection.cancel() - return - } - self.accept(connection) - } - try await withCheckedThrowingContinuation { (continuation: CheckedContinuation) in - lock.withLock { - self.listener = listener - readyContinuation = continuation - } - listener.start(queue: queue) - } - } - - func callback(timeout: Duration) async throws -> [String: String] { - try await withTaskCancellationHandler { - try await withCheckedThrowingContinuation { (continuation: CheckedContinuation<[String: String], Error>) in - let settled = lock.withLock { () -> Result<[String: String], Error>? in - if let outcome { return outcome } - callbackContinuation = continuation - let item = DispatchWorkItem { [weak self] in - self?.finish(.failure(VaultError.oidcFailed(String(localized: "The browser sign-in timed out.")))) - } - timeoutItem = item - let seconds = Double(timeout.components.seconds) + Double(timeout.components.attoseconds) / 1e18 - queue.asyncAfter(deadline: .now() + seconds, execute: item) - return nil - } - if let settled { continuation.resume(with: settled) } + // Only a callback carrying the `state` from Vault's sign-in page ends the wait. + init(expectedState: String, port: UInt16 = VaultOIDCLoopback.port, headTimeout: TimeInterval = 5) { + listener = LoopbackRedirectListener(port: port, headTimeout: headTimeout) { head in + guard let parameters = Self.callbackParameters(in: head, expectedState: expectedState) else { + return .ignore(LoopbackRedirectPage.badRequest) } - } onCancel: { - finish(.failure(CancellationError())) + return .settle(Self.successPage(), .success(parameters)) } } - func stop() { - finish(.failure(CancellationError())) + func start() async throws { + try await startListening() } - private func listenerChanged(_ state: NWListener.State) { - switch state { - case .ready: - let continuation = lock.withLock { () -> CheckedContinuation? in - defer { readyContinuation = nil } - return readyContinuation - } - continuation?.resume() - case .failed(let error), .waiting(let error): - Self.logger.error("Vault OIDC callback listener failed: \(error.publicLogShape, privacy: .public)") - let continuation = lock.withLock { () -> CheckedContinuation? in - defer { readyContinuation = nil } - return readyContinuation - } - if let continuation { - continuation.resume(throwing: VaultError.oidcCallbackPortInUse) - } - finish(.failure(VaultError.oidcCallbackPortInUse)) - case .cancelled: - let continuation = lock.withLock { () -> CheckedContinuation? in - defer { readyContinuation = nil } - return readyContinuation - } - continuation?.resume(throwing: CancellationError()) - default: - break + @discardableResult + func startListening() async throws -> UInt16 { + do { + return try await listener.start() + } catch is LoopbackRedirectListenerError { + throw VaultError.oidcCallbackPortInUse } } - private func accept(_ connection: NWConnection) { - let admitted = lock.withLock { () -> Bool in - guard outcome == nil, connections.count < 16 else { return false } - connections[ObjectIdentifier(connection)] = connection - return true - } - guard admitted else { - connection.cancel() - return - } - connection.start(queue: queue) - receive(on: connection, head: Data()) + func callback(timeout: Duration) async throws -> [String: String] { + let seconds = Double(timeout.components.seconds) + Double(timeout.components.attoseconds) / 1e18 + return try await listener.outcome( + timeout: seconds, + timeoutError: VaultError.oidcFailed(String(localized: "The browser sign-in timed out.")) + ) } - private func receive(on connection: NWConnection, head: Data) { - let remaining = Self.maximumRequestHeadBytes - head.count - connection.receive(minimumIncompleteLength: 1, maximumLength: remaining) { [weak self] content, _, isComplete, error in - guard let self else { - connection.cancel() - return - } - var received = head - if let content { received.append(content) } - if let parameters = Self.callbackParameters(in: received) { - self.respond(on: connection, status: "200 OK", body: Self.successPage()) - self.finish(.success(parameters)) - return - } - if Self.headIsComplete(received) || received.count >= Self.maximumRequestHeadBytes || isComplete || error != nil { - self.respond(on: connection, status: "404 Not Found", body: "") - return - } - self.receive(on: connection, head: received) - } + func stop() { + listener.stop() } - static func callbackParameters(in head: Data) -> [String: String]? { - guard headIsComplete(head), let text = String(data: head, encoding: .utf8), - let requestLine = text.components(separatedBy: .newlines).first - else { return nil } - let parts = requestLine.split(separator: " ") - guard parts.count >= 2, parts[0] == "GET", - let components = URLComponents(string: "http://localhost" + parts[1]), + static func callbackParameters(in head: Data, expectedState: String) -> [String: String]? { + guard !expectedState.isEmpty, + LoopbackRedirectRequest.isComplete(head), + let target = LoopbackRedirectRequest.getTarget(LoopbackRedirectRequest.text(of: head)), + let components = URLComponents(string: target), + components.host == nil, components.path == callbackPath else { return nil } + let items = LoopbackRedirectRequest.formDecodedItems(in: components) + guard let state = LoopbackRedirectRequest.singleValue(named: "state", in: items), + LoopbackRedirectRequest.constantTimeEquals(state, expectedState) + else { return nil } var parameters: [String: String] = [:] - for item in components.queryItems ?? [] { + for item in items { if let value = item.value { parameters[item.name] = value } } return parameters } - private static func headIsComplete(_ head: Data) -> Bool { - head.range(of: Data("\r\n\r\n".utf8)) != nil || head.range(of: Data("\n\n".utf8)) != nil - } - - private func respond(on connection: NWConnection, status: String, body: String) { - let payload = Data(body.utf8) - let head = "HTTP/1.1 \(status)\r\nContent-Type: text/html; charset=utf-8\r\nContent-Length: \(payload.count)\r\n" - + "Connection: close\r\nCache-Control: no-store\r\n\r\n" - connection.send(content: Data(head.utf8) + payload, completion: .contentProcessed { [weak self] _ in - connection.cancel() - self?.release(connection) - }) - } - - private func release(_ connection: NWConnection) { - lock.withLock { connections[ObjectIdentifier(connection)] = nil } - } - - private func finish(_ result: Result<[String: String], Error>) { - let (continuation, listener, settled, abandoned) = lock.withLock { - () -> (CheckedContinuation<[String: String], Error>?, NWListener?, Result<[String: String], Error>, [NWConnection]) in - let firstOutcome = outcome == nil - if firstOutcome { outcome = result } - timeoutItem?.cancel() - timeoutItem = nil - let continuation = callbackContinuation - callbackContinuation = nil - let listener = self.listener - self.listener = nil - // A connection still sending the success page closes itself once it is sent. - var abandoned: [NWConnection] = [] - if firstOutcome, case .failure = result { - abandoned = Array(connections.values) - } - return (continuation, listener, outcome ?? result, abandoned) - } - listener?.cancel() - abandoned.forEach { $0.cancel() } - continuation?.resume(with: settled) - } - - private static func successPage() -> String { - let message = String(localized: "Signed in to Vault. You can close this window and return to TablePro.") - return """ - TablePro - -

\(message)

- """ + private static func successPage() -> Data { + LoopbackRedirectPage.response( + status: "200 OK", + title: "TablePro", + message: String(localized: "Signed in to Vault. You can close this window and return to TablePro.") + ) } } diff --git a/apple/TablePro/Core/Diagnostics/TableLoadHistoryStore.swift b/apple/TablePro/Core/Diagnostics/TableLoadHistoryStore.swift index d5ae1c5575..bc2b9699ab 100644 --- a/apple/TablePro/Core/Diagnostics/TableLoadHistoryStore.swift +++ b/apple/TablePro/Core/Diagnostics/TableLoadHistoryStore.swift @@ -1,8 +1,3 @@ -// -// TableLoadHistoryStore.swift -// TablePro -// - import Foundation import os @@ -13,14 +8,6 @@ internal protocol TableLoadSummarySink: Sendable { func record(_ record: TableLoadPerformanceRecord) } -/// What `TableLoadTracer.shared` gets under XCTest. Dozens of coordinator suites drive -/// `openTableTab`, which begins real traces, and `AppStorageEnvironment` resolves to the production -/// directory under a unit-test host, so a live sink would append to the developer's own history on -/// every test run. Discarding at the sink means no file is opened and nothing needs cleaning up. -internal struct DiscardingTableLoadSummarySink: TableLoadSummarySink { - internal func record(_ record: TableLoadPerformanceRecord) {} -} - /// A bounded, device-local history of how long table loads took, kept so an intermittent slowdown /// can be looked at after it happened rather than only while `log stream` is running. /// diff --git a/apple/TablePro/Core/Diagnostics/TableLoadTracer.swift b/apple/TablePro/Core/Diagnostics/TableLoadTracer.swift index 1524bc11e2..a9ddee61b4 100644 --- a/apple/TablePro/Core/Diagnostics/TableLoadTracer.swift +++ b/apple/TablePro/Core/Diagnostics/TableLoadTracer.swift @@ -1,8 +1,3 @@ -// -// TableLoadTracer.swift -// TablePro -// - import Foundation import os @@ -18,7 +13,7 @@ import os /// keeps a bounded local history an intermittent slowdown can be looked at in after the fact. @MainActor internal final class TableLoadTracer { - internal static let shared = TableLoadTracer(sink: TableLoadTracer.defaultSink()) + internal static let shared = TableLoadTracer(sink: TableLoadHistoryStore.shared) private struct Interval { let id: OSSignpostID @@ -40,15 +35,6 @@ internal final class TableLoadTracer { self.sink = sink } - /// A unit test that drives a coordinator opens real table tabs and so begins real traces, and - /// `AppStorageEnvironment` resolves to the production directory under a unit-test host. Choosing - /// the sink here keeps those traces off the developer's own history without the store having to - /// know it is being tested. - private static func defaultSink() -> any TableLoadSummarySink { - guard NSClassFromString("XCTestCase") == nil else { return DiscardingTableLoadSummarySink() } - return TableLoadHistoryStore.shared - } - /// A sidebar click that opens a new native window tab finishes in a different coordinator, so the /// click instant is parked here and picked up by whichever window ends up doing the load. Without /// it the trace starts after the window has already mounted and hides that cost. diff --git a/apple/TablePro/Core/MCP/Identity/MCPApprovalLedger.swift b/apple/TablePro/Core/MCP/Identity/MCPApprovalLedger.swift index c602f06ceb..3de9a1253e 100644 --- a/apple/TablePro/Core/MCP/Identity/MCPApprovalLedger.swift +++ b/apple/TablePro/Core/MCP/Identity/MCPApprovalLedger.swift @@ -57,9 +57,7 @@ public actor MCPApprovalLedger { return await store.denial(subject: principal.grantSubject.storageKey, connectionId: connectionId, now: now) } - /// Drops the approvals a revoked token was carrying. Rotating the bridge credential is not a - /// revocation: the bridge re-mints itself roughly every 45 minutes and deletes the token it - /// replaces, so treating that as the user withdrawing consent re-asks them all day. + // A bridge credential rotation never reaches here: it drops the old token without a revocation. func clear(subject: MCPGrantSubject) async { await store.revokeAll(subject: subject.storageKey) } diff --git a/apple/TablePro/Core/MCP/Lifecycle/MCPServerManager.swift b/apple/TablePro/Core/MCP/Lifecycle/MCPServerManager.swift index cc9c7020ee..4c5574605d 100644 --- a/apple/TablePro/Core/MCP/Lifecycle/MCPServerManager.swift +++ b/apple/TablePro/Core/MCP/Lifecycle/MCPServerManager.swift @@ -64,6 +64,8 @@ internal final class MCPServerManager: ObservableObject { @Published private var composition: MCPServerComposition? @Published private var bridgeCredential: BridgeCredential? + private var supersededBridgeCredentials: [BridgeCredential] = [] + private var bridgeDisconnects = 0 @Published private var instanceId = "" @Published private var generation = 0 @Published private var revocationObserverId: UUID? @@ -168,13 +170,40 @@ internal final class MCPServerManager: ObservableObject { return true } - var withdrawn: MCPGrantSubject? = .token(tokenId) - if let ended = await tokenStore?.token(id: tokenId) { - withdrawn = ended.subjectWithdrawnWhenEnded - } - let persisted = await revokeToken(id: tokenId) - if let withdrawn { - await composition.authPolicy.clearApprovals(subject: withdrawn) + let stored = await tokenStore?.token(id: tokenId) + // Read after the await: a rotation that ran meanwhile may have moved this token to the superseded list. + let endsTheBridge = stored?.isBridgeCredential == true + || bridgeCredential?.tokenId == tokenId + || supersededBridgeCredentials.contains { $0.tokenId == tokenId } + + let persisted: Bool + if endsTheBridge { + // Every bridge credential is the same client, so the live one is revoked and all their requests end now. + bridgeDisconnects += 1 + var revoked = [tokenId] + if let live = bridgeCredential?.tokenId, live != tokenId { + revoked.append(live) + } + let ended = Set(revoked + supersededBridgeCredentials.map { $0.tokenId }) + var allPersisted = true + for id in revoked { + let saved = await revokeToken(id: id) + allPersisted = allPersisted && saved + } + for id in ended { + await composition.dispatcher.cancelInflight(matchingTokenId: id) + await composition.activityLedger.forget(tokenId: id) + } + persisted = allPersisted + } else { + var withdrawn: MCPGrantSubject? = .token(tokenId) + if let stored { + withdrawn = stored.subjectWithdrawnWhenEnded + } + persisted = await revokeToken(id: tokenId) + if let withdrawn { + await composition.authPolicy.clearApprovals(subject: withdrawn) + } } await composition.activityLedger.forget(tokenId: tokenId) await refreshClients() @@ -296,6 +325,7 @@ internal final class MCPServerManager: ObservableObject { clientRefreshTask = nil tokenRenewalTask?.cancel() tokenRenewalTask = nil + supersededBridgeCredentials = [] handshake.removeIfWrittenByThisProcess(instanceId: instanceId) MCPServerInstanceIdentity.shared.end() @@ -358,22 +388,30 @@ internal final class MCPServerManager: ObservableObject { private func renewBridgeCredential(generation: Int) async { guard self.generation == generation, let store = tokenStore, - let socketPath = composition?.socketPath, + let composition, case .running = state else { return } let previous = bridgeCredential + let disconnects = bridgeDisconnects guard let credential = await mintBridgeCredential(store: store) else { return } - guard self.generation == generation else { + // A bridge disconnect during the mint keeps the bridge off until the next rotation. + guard self.generation == generation, bridgeDisconnects == disconnects else { await store.delete(tokenId: credential.tokenId) return } bridgeCredential = credential - writeHandshake(socketPath: socketPath, credential: credential) + writeHandshake(socketPath: composition.socketPath, credential: credential) if let previous { - await store.delete(tokenId: previous.tokenId) + // Listed before the await, so a disconnect from here on ends its requests too. + let now = Date() + supersededBridgeCredentials = supersededBridgeCredentials.filter { $0.expiresAt > now } + [previous] + // Not a revocation: the old credential opens nothing new, but what it started may finish until it expires. + await store.dropSupersededBridgeCredential(tokenId: previous.tokenId) + guard self.generation == generation else { return } + await composition.dispatcher.cancelInflight(matchingTokenId: previous.tokenId, at: previous.expiresAt) } Self.logger.info("Rotated the MCP bridge credential") } diff --git a/apple/TablePro/Core/MCP/MCPAuditLogStorage.swift b/apple/TablePro/Core/MCP/MCPAuditLogStorage.swift index ab6a3ec121..461c36c22b 100644 --- a/apple/TablePro/Core/MCP/MCPAuditLogStorage.swift +++ b/apple/TablePro/Core/MCP/MCPAuditLogStorage.swift @@ -37,17 +37,14 @@ struct MCPAuditChainLink: Sendable, Equatable { } actor MCPAuditLogStorage { - static let shared = MCPAuditLogStorage() + static let shared = MCPAuditLogStorage(directory: AppStorageEnvironment.shared.supportDirectory) + static let databaseFileName = "mcp-audit.db" private static let logger = Logger(subsystem: "com.TablePro", category: "MCPAuditLogStorage") private static let retentionDays: Int = 90 private static let SQLITE_TRANSIENT = unsafeBitCast(-1, to: sqlite3_destructor_type.self) - private static var isRunningTests: Bool { - NSClassFromString("XCTestCase") != nil - } - private struct DatabaseHandle: @unchecked Sendable { var pointer: OpaquePointer? } @@ -57,60 +54,38 @@ actor MCPAuditLogStorage { private var db: OpaquePointer? { if !isPrepared { isPrepared = true - setupDatabase() + let setAside = setupDatabase() prune(olderThan: Self.retentionDays) loadChainHead() + if let setAside { + recordSetAside(setAside) + } } return dbHandle.pointer } private var dbPath: String? - private let testDatabaseSuffix: String? + private let directory: URL private var nextSequence: Int = 0 private var lastHash: String = MCPAuditChainLink.genesisHash private var isPrepared = false - init() { - self.testDatabaseSuffix = nil - } - - #if DEBUG - init(isolatedForTesting: Bool) { - self.testDatabaseSuffix = isolatedForTesting ? "_\(UUID().uuidString)" : nil + init(directory: URL) { + self.directory = directory } - var databaseFilePaths: [String] { - guard db != nil, let dbPath else { return [] } - return [dbPath, dbPath + "-wal", dbPath + "-shm"] - } - #endif - deinit { if let pointer = dbHandle.pointer { sqlite3_close(pointer) } - if Self.isRunningTests, let dbPath { - try? FileManager.default.removeItem(atPath: dbPath) - for suffix in ["-wal", "-shm"] { - try? FileManager.default.removeItem(atPath: dbPath + suffix) - } - } } - private func setupDatabase() { + private func setupDatabase() -> SetAsideDamagedStore? { let fileManager = FileManager.default - let directory = AppStorageEnvironment.shared.applicationSupportRoot.appendingPathComponent("TablePro") try? fileManager.createDirectory(at: directory, withIntermediateDirectories: true) Self.restrict(path: directory.path, to: 0o700) - if Self.isRunningTests { - Self.purgeAbandonedTestDatabases(in: directory, fileManager: fileManager) - } - - let suffix = testDatabaseSuffix ?? "" - let fileName = Self.isRunningTests - ? "mcp-audit-test_\(ProcessInfo.processInfo.processIdentifier)\(suffix).db" - : "mcp-audit.db" - let path = directory.appendingPathComponent(fileName).path(percentEncoded: false) + let url = directory.appendingPathComponent(Self.databaseFileName) + let path = url.path(percentEncoded: false) self.dbPath = path if !fileManager.fileExists(atPath: path) { @@ -122,9 +97,16 @@ actor MCPAuditLogStorage { } Self.restrict(path: path, to: 0o600) - if sqlite3_open(path, &dbHandle.pointer) != SQLITE_OK { + // The damaged log is evidence, so it is set aside for review, never deleted. + guard case .opened(let pointer, let setAside) = LocalSQLiteStoreOpener.open(at: url) else { Self.logger.error("Error opening MCP audit database") - return + return nil + } + dbHandle.pointer = pointer + if setAside != nil { + Self.logger.fault("The MCP audit log was damaged and set aside; a new chain starts") + // SQLite created the fresh file with the default mode; its -wal and -shm copy this one. + Self.restrict(path: path, to: 0o600) } execute("PRAGMA journal_mode=WAL;") @@ -133,6 +115,16 @@ actor MCPAuditLogStorage { createTables() backfillChainIfNeeded() restrictDatabaseFiles() + return setAside + } + + private func recordSetAside(_ setAside: SetAsideDamagedStore) { + addEntry(AuditEntry( + category: .admin, + action: "auditLog.setAside", + outcome: AuditOutcome.error, + details: "setAside=\(setAside.setAsideURL.lastPathComponent)" + )) } private func restrictDatabaseFiles() { @@ -143,14 +135,6 @@ actor MCPAuditLogStorage { } } - private static func purgeAbandonedTestDatabases(in directory: URL, fileManager: FileManager) { - let ownMarker = "mcp-audit-test_\(ProcessInfo.processInfo.processIdentifier)" - guard let names = try? fileManager.contentsOfDirectory(atPath: directory.path) else { return } - for name in names where name.hasPrefix("mcp-audit-test_") && !name.hasPrefix(ownMarker) { - try? fileManager.removeItem(atPath: directory.appendingPathComponent(name).path) - } - } - private static func restrict(path: String, to permissions: Int) { guard FileManager.default.fileExists(atPath: path) else { return } do { @@ -287,6 +271,8 @@ actor MCPAuditLogStorage { @discardableResult func addEntry(_ entry: AuditEntry) -> Bool { + // Opening the log loads the chain head, so it has to happen before the head is read. + let handle = db let sequence = nextSequence let previousHash = lastHash let hash = MCPAuditChainLink.digest(entry: entry, sequence: sequence, previousHash: previousHash) @@ -299,7 +285,7 @@ actor MCPAuditLogStorage { """ var statement: OpaquePointer? - guard sqlite3_prepare_v2(db, sql, -1, &statement, nil) == SQLITE_OK else { + guard sqlite3_prepare_v2(handle, sql, -1, &statement, nil) == SQLITE_OK else { Self.logger.warning("Failed to prepare audit insert statement") return false } diff --git a/apple/TablePro/Core/MCP/MCPAuthPolicy.swift b/apple/TablePro/Core/MCP/MCPAuthPolicy.swift index 1fc13c4522..8a8e15b6b9 100644 --- a/apple/TablePro/Core/MCP/MCPAuthPolicy.swift +++ b/apple/TablePro/Core/MCP/MCPAuthPolicy.swift @@ -268,10 +268,7 @@ public actor MCPAuthPolicy { await approvalLedger.record(principal: principal, connectionId: connectionId, approved: true) } - /// Called when a token is revoked. The bundled bridge re-mints its credential roughly every - /// 45 minutes and deletes the one it replaces, which reaches here as a revocation; treating that - /// as the user withdrawing consent is what made the prompt come back all day, so the caller - /// passes the token's name and the bridge's own rotation is skipped. + // The bridge's approvals belong to the bridge, not to one of its rotating credentials. func clearApprovals(tokenId: UUID, wasBridgeCredential: Bool = false) async { guard !wasBridgeCredential else { return } await clearApprovals(subject: .token(tokenId)) diff --git a/apple/TablePro/Core/MCP/MCPConnectionApprovalReading.swift b/apple/TablePro/Core/MCP/MCPConnectionApprovalReading.swift index 9609610e4f..f9f083d6d6 100644 --- a/apple/TablePro/Core/MCP/MCPConnectionApprovalReading.swift +++ b/apple/TablePro/Core/MCP/MCPConnectionApprovalReading.swift @@ -1,9 +1,6 @@ import Foundation -/// Reads the user's MCP approval setting. -/// -/// A seam rather than a direct `AppSettingsManager` read, because the auth policy is an actor and -/// its tests must be able to state the policy without standing up main-actor settings. +// A seam so the auth policy actor and its tests can state the setting without main-actor settings. protocol MCPConnectionApprovalReading: Sendable { func connectionApproval() async -> MCPConnectionApproval } @@ -13,15 +10,3 @@ struct MCPSettingsApprovalReader: MCPConnectionApprovalReading { await MainActor.run { AppSettingsManager.shared.mcp.connectionApproval } } } - -struct MCPFixedApprovalReader: MCPConnectionApprovalReading { - let approval: MCPConnectionApproval - - init(_ approval: MCPConnectionApproval) { - self.approval = approval - } - - func connectionApproval() async -> MCPConnectionApproval { - approval - } -} diff --git a/apple/TablePro/Core/MCP/MCPTokenStore.swift b/apple/TablePro/Core/MCP/MCPTokenStore.swift index 0e01d81646..d75cd00d7f 100644 --- a/apple/TablePro/Core/MCP/MCPTokenStore.swift +++ b/apple/TablePro/Core/MCP/MCPTokenStore.swift @@ -321,6 +321,17 @@ actor MCPTokenStore { remove(tokenId: tokenId, withdrawsApprovals: false) } + // A rotation revokes nothing, so no observer hears of it; any other token goes through delete. + func dropSupersededBridgeCredential(tokenId: UUID) { + guard let index = tokens.firstIndex(where: { $0.id == tokenId }), tokens[index].isBridgeCredential else { + delete(tokenId: tokenId) + return + } + tokens.remove(at: index) + save() + Self.logger.info("Dropped the superseded bridge credential") + } + private func remove(tokenId: UUID, withdrawsApprovals: Bool) { guard let index = tokens.firstIndex(where: { $0.id == tokenId }) else { Self.logger.warning("Attempted to delete non-existent token \(tokenId.uuidString, privacy: .public)") diff --git a/apple/TablePro/Core/MCP/Protocol/MCPProtocolDispatcher.swift b/apple/TablePro/Core/MCP/Protocol/MCPProtocolDispatcher.swift index 10a4d2c6d5..1135d5525e 100644 --- a/apple/TablePro/Core/MCP/Protocol/MCPProtocolDispatcher.swift +++ b/apple/TablePro/Core/MCP/Protocol/MCPProtocolDispatcher.swift @@ -99,6 +99,24 @@ public actor MCPProtocolDispatcher { return count } + // For a superseded credential: its requests may finish, but none outlives the credential's own expiry. + public func cancelInflight(matchingTokenId tokenId: UUID, at deadline: Date) async { + let clock = self.clock + let remaining = deadline.timeIntervalSince(await clock.now()) + guard remaining > 0 else { + await cancelInflight(matchingTokenId: tokenId) + return + } + Task { [weak self] in + do { + try await clock.sleep(for: .seconds(remaining)) + } catch { + return + } + _ = await self?.cancelInflight(matchingTokenId: tokenId) + } + } + @discardableResult public func cancelAllInflight(reason: MCPCancellationReason = .serverShuttingDown) async -> Int { await inflight.cancelAll(reason: reason) diff --git a/apple/TablePro/Core/Plugins/PluginCodeSignatureVerifier.swift b/apple/TablePro/Core/Plugins/PluginCodeSignatureVerifier.swift index a66fef198f..9aee96219d 100644 --- a/apple/TablePro/Core/Plugins/PluginCodeSignatureVerifier.swift +++ b/apple/TablePro/Core/Plugins/PluginCodeSignatureVerifier.swift @@ -1,8 +1,3 @@ -// -// PluginCodeSignatureVerifier.swift -// TablePro -// - import Foundation import os import Security @@ -19,8 +14,11 @@ enum PluginCodeSignatureVerifier { return teamId }() - /// The app keeps `disable-library-validation` for SQLite and DuckDB extensions, so dyld checks - /// nothing and this is the whole load decision for a plugin bundle. + static let validationFlags = kSecCSCheckAllArchitectures | kSecCSCheckNestedCode | kSecCSStrictValidate + | kSecCSRestrictSymlinks + + // The app keeps `disable-library-validation` for SQLite and DuckDB extensions, so dyld checks + // nothing and this is the whole load decision for a plugin bundle. static func verify(bundle: Bundle) throws { #if DEBUG if ProcessInfo.processInfo.environment["TABLEPRO_ALLOW_UNSIGNED_PLUGINS"] == "1" { @@ -30,37 +28,55 @@ enum PluginCodeSignatureVerifier { return } #endif + try verify(bundle: bundle, signingTeam: resolvedSigningTeamId) + } + + static func verify(bundle: Bundle, signingTeam: String) throws { + let requirement = try firstPartyRequirement(signingTeam: signingTeam) + var staticCode: SecStaticCode? let createStatus = SecStaticCodeCreateWithPath( bundle.bundleURL as CFURL, SecCSFlags(), &staticCode ) - guard createStatus == errSecSuccess, let code = staticCode else { throw PluginError.signatureInvalid(detail: describeOSStatus(createStatus)) } - let flags = SecCSFlags(rawValue: Self.validationFlags) - let status = SecStaticCodeCheckValidity(code, flags, requirement(Self.firstPartyRequirement)) + let flags = SecCSFlags(rawValue: validationFlags) + let status = SecStaticCodeCheckValidity(code, flags, requirement) guard status == errSecSuccess else { throw PluginError.signatureInvalid(detail: describeOSStatus(status)) } } - static let validationFlags = kSecCSCheckAllArchitectures | kSecCSCheckNestedCode | kSecCSStrictValidate - | kSecCSRestrictSymlinks - - private static var firstPartyRequirement: String { - "anchor apple generic and certificate leaf[subject.OU] = \"\(resolvedSigningTeamId)\"" + static func firstPartyRequirement(signingTeam team: String) throws -> SecRequirement { + // The team goes into the requirement text, where a quote could append an `or` clause. + guard isTeamIdentifier(team) else { + throw PluginError.signatureInvalid(detail: "TablePro's signing Team ID is not valid") + } + return try compiledRequirement("anchor apple generic and certificate leaf[subject.OU] = \"\(team)\"") } - private static func requirement(_ text: String) -> SecRequirement? { + static func compiledRequirement(_ text: String) throws -> SecRequirement { var requirement: SecRequirement? - SecRequirementCreateWithString(text as CFString, SecCSFlags(), &requirement) + let status = SecRequirementCreateWithString(text as CFString, SecCSFlags(), &requirement) + // A nil requirement makes SecStaticCodeCheckValidity accept any valid signature. + guard status == errSecSuccess, let requirement else { + throw PluginError.signatureInvalid( + detail: "TablePro's signing requirement could not be built (OSStatus \(status))" + ) + } return requirement } + private static func isTeamIdentifier(_ team: String) -> Bool { + team.unicodeScalars.count == 10 && team.unicodeScalars.allSatisfy { scalar in + ("A"..."Z").contains(scalar) || ("0"..."9").contains(scalar) + } + } + private static func teamIdFromBundleSignature() -> String? { var staticCode: SecStaticCode? let createStatus = SecStaticCodeCreateWithPath( diff --git a/apple/TablePro/Core/SSH/LibSSH2TunnelFactory.swift b/apple/TablePro/Core/SSH/LibSSH2TunnelFactory.swift index 048ccbac8d..08f6239f36 100644 --- a/apple/TablePro/Core/SSH/LibSSH2TunnelFactory.swift +++ b/apple/TablePro/Core/SSH/LibSSH2TunnelFactory.swift @@ -20,6 +20,11 @@ internal struct SSHTunnelCredentials: Sendable { let keyboardInteractivePromptProvider: (any KeyboardInteractivePromptProvider)? } +internal struct LocalTunnelListener: Sendable { + let port: Int + let descriptor: Int32 +} + /// Creates fully-connected and authenticated SSH tunnels using libssh2. internal enum LibSSH2TunnelFactory { internal static let logger = Logger(subsystem: "com.TablePro", category: "LibSSH2TunnelFactory") @@ -40,12 +45,36 @@ internal enum LibSSH2TunnelFactory { // MARK: - Public + // Takes ownership of the listener: a failure closes it, success hands it to the tunnel. static func createTunnel( connectionId: UUID, config: SSHConfiguration, credentials: SSHTunnelCredentials, destination: SSHForwardDestination, - localPort: Int, + listener: LocalTunnelListener, + deadline: ConnectionDeadline + ) async throws -> LibSSH2Tunnel { + do { + return try await makeTunnel( + connectionId: connectionId, + config: config, + credentials: credentials, + destination: destination, + listener: listener, + deadline: deadline + ) + } catch { + Darwin.close(listener.descriptor) + throw error + } + } + + private static func makeTunnel( + connectionId: UUID, + config: SSHConfiguration, + credentials: SSHTunnelCredentials, + destination: SSHForwardDestination, + listener: LocalTunnelListener, deadline: ConnectionDeadline ) async throws -> LibSSH2Tunnel { _ = initialized @@ -74,21 +103,14 @@ internal enum LibSSH2TunnelFactory { deadline: deadline, attempt: attempt ) - - let listenFD = try bindListenSocket(port: localPort) - do { - try attempt.check(for: endpoint) - } catch { - Darwin.close(listenFD) - throw error - } + try attempt.check(for: endpoint) let tunnel = LibSSH2Tunnel( connectionId: connectionId, - localPort: localPort, + localPort: listener.port, session: chain.session, socketFD: chain.socketFD, - listenFD: listenFD, + listenFD: listener.descriptor, connectionDeadline: deadline, timeoutEndpoint: chain.timeoutEndpoint, jumpChain: chain.jumpHops.map { hop in @@ -103,7 +125,7 @@ internal enum LibSSH2TunnelFactory { ) logger.info( - "Tunnel created: \(config.host) -> 127.0.0.1:\(localPort) -> \(destination.logDescription)" + "Tunnel created: \(config.host) -> 127.0.0.1:\(listener.port) -> \(destination.logDescription)" ) attempt.finish() @@ -1159,7 +1181,18 @@ internal enum LibSSH2TunnelFactory { // MARK: - Local Socket - private static func bindListenSocket(port: Int) throws -> Int32 { + // Bound before the SSH login, so a port collision never repeats the host key, passphrase or TOTP step. + static func bindLocalListener(candidates: [Int]) throws -> LocalTunnelListener { + for port in candidates { + if let descriptor = try bindListenSocket(port: port) { + return LocalTunnelListener(port: port, descriptor: descriptor) + } + } + throw SSHTunnelError.noAvailablePort + } + + // nil when the port is taken; any other failure throws, since the next port would fail the same way. + private static func bindListenSocket(port: Int) throws -> Int32? { let listenFD = socket(AF_INET, SOCK_STREAM, 0) guard listenFD >= 0 else { throw SSHTunnelError.tunnelCreationFailed("Failed to create listening socket") @@ -1180,12 +1213,18 @@ internal enum LibSSH2TunnelFactory { } guard bindResult == 0 else { + let bindError = errno Darwin.close(listenFD) - throw SSHTunnelError.tunnelCreationFailed("Port \(port) already in use") + if bindError == EADDRINUSE { return nil } + throw SSHTunnelError.tunnelCreationFailed( + "Could not bind local port \(port): \(String(cString: strerror(bindError)))" + ) } guard listen(listenFD, Self.listenBacklogSize) == 0 else { + let listenError = errno Darwin.close(listenFD) + if listenError == EADDRINUSE { return nil } throw SSHTunnelError.tunnelCreationFailed("Failed to listen on port \(port)") } return listenFD diff --git a/apple/TablePro/Core/SSH/SSHTunnelManager.swift b/apple/TablePro/Core/SSH/SSHTunnelManager.swift index 553ea82277..74dc2dd190 100644 --- a/apple/TablePro/Core/SSH/SSHTunnelManager.swift +++ b/apple/TablePro/Core/SSH/SSHTunnelManager.swift @@ -18,8 +18,7 @@ actor SSHTunnelManager: TunnelManaging { private var tunnels: [UUID: LibSSH2Tunnel] = [:] private var creations = TunnelCreationGenerations() - private let portRangeStart = 60_000 - private let portRangeEnd = 65_000 + private let localPortCandidates: @Sendable () -> [Int] /// Static registry for synchronous termination during app shutdown private static let tunnelRegistry = OSAllocatedUnfairLock(initialState: [UUID: LibSSH2Tunnel]()) @@ -28,7 +27,9 @@ actor SSHTunnelManager: TunnelManaging { /// Held as long as at least one tunnel exists; released when the last tunnel closes. private var appNapActivity: NSObjectProtocol? - private init() {} + init(localPortCandidates: @escaping @Sendable () -> [Int] = { Array(60_000...65_000).shuffled() }) { + self.localPortCandidates = localPortCandidates + } func createTunnel( connectionId: UUID, @@ -79,64 +80,51 @@ actor SSHTunnelManager: TunnelManaging { keyboardInteractivePromptProvider: nil ) - // Try ports until one works - for localPort in localPortCandidates() { - try deadline.check(endpoint: timeoutEndpoint) - do { - let creationTask = Task.detached { - try await LibSSH2TunnelFactory.createTunnel( - connectionId: connectionId, - config: config, - credentials: credentials, - destination: destination, - localPort: localPort, - deadline: deadline - ) - } - let tunnel = try await withTaskCancellationHandler { - try await creationTask.value - } onCancel: { - creationTask.cancel() - } - - do { - try deadline.check(endpoint: timeoutEndpoint) - } catch { - tunnel.close() - throw error - } - // A newer createTunnel or a close for this connection came while this one dialed, so it must not adopt. - guard creations.isNewest(generation, for: connectionId) else { - tunnel.close() - throw CancellationError() - } - - tunnel.onDeath = { [weak self] id in - Task { [weak self] in - await self?.handleTunnelDeath(connectionId: id) - } - } - - tunnels[connectionId] = tunnel - Self.tunnelRegistry.withLock { $0[connectionId] = tunnel } + try deadline.check(endpoint: timeoutEndpoint) + let listener = try LibSSH2TunnelFactory.bindLocalListener(candidates: localPortCandidates()) + let creationTask = Task.detached { + try await LibSSH2TunnelFactory.createTunnel( + connectionId: connectionId, + config: config, + credentials: credentials, + destination: destination, + listener: listener, + deadline: deadline + ) + } + let tunnel = try await withTaskCancellationHandler { + try await creationTask.value + } onCancel: { + creationTask.cancel() + } - tunnel.startForwarding(destination: destination) - tunnel.startKeepAlive() + do { + try deadline.check(endpoint: timeoutEndpoint) + } catch { + tunnel.close() + throw error + } + // A newer createTunnel or a close for this connection came while this one dialed, so it must not adopt. + guard creations.isNewest(generation, for: connectionId) else { + tunnel.close() + throw CancellationError() + } - updateAppNapState() - Self.logger.info("Tunnel created for \(connectionId) on local port \(localPort)") - return localPort - } catch let error as SSHTunnelError { - if case .tunnelCreationFailed(let msg) = error, - msg.contains("already in use") { - Self.logger.notice("Port \(localPort) in use, trying another") - continue - } - throw error + tunnel.onDeath = { [weak self] id in + Task { [weak self] in + await self?.handleTunnelDeath(connectionId: id) } } - throw SSHTunnelError.noAvailablePort + tunnels[connectionId] = tunnel + Self.tunnelRegistry.withLock { $0[connectionId] = tunnel } + + tunnel.startForwarding(destination: destination) + tunnel.startKeepAlive() + + updateAppNapState() + Self.logger.info("Tunnel created for \(connectionId) on local port \(listener.port)") + return listener.port } // Also ends a creation still dialing, so nothing it builds is adopted after the close returned. @@ -222,17 +210,8 @@ actor SSHTunnelManager: TunnelManaging { tunnels[connectionId]?.consumeLastForwardFailure() } - /// Check if an error message indicates a local port bind failure - static func isLocalPortBindFailure(_ errorMessage: String) -> Bool { - errorMessage.lowercased().contains("already in use") - } - // MARK: - Private - private func localPortCandidates() -> [Int] { - Array(portRangeStart...portRangeEnd).shuffled() - } - private func handleTunnelDeath(connectionId: UUID) async { guard tunnels.removeValue(forKey: connectionId) != nil else { return } Self.tunnelRegistry.withLock { $0[connectionId] = nil } diff --git a/apple/TablePro/Core/Storage/ConnectionStorage.swift b/apple/TablePro/Core/Storage/ConnectionStorage.swift index c80bb3bb79..8fadc3d6d4 100644 --- a/apple/TablePro/Core/Storage/ConnectionStorage.swift +++ b/apple/TablePro/Core/Storage/ConnectionStorage.swift @@ -444,22 +444,11 @@ final class ConnectionStorage { if connection.participatesInSync { syncTracker.markDeleted(.connection, id: connection.id.uuidString) } - deletePassword(for: connection.id) - deleteSSHPassword(for: connection.id) - deleteKeyPassphrase(for: connection.id) - deleteSSLClientKeyPassphrase(for: connection.id) - deleteTOTPSecret(for: connection.id) - deleteCloudflareTokenId(for: connection.id) - deleteCloudflareTokenSecret(for: connection.id) - deleteCloudSQLProxyServiceAccountKey(for: connection.id) - deleteSOCKSProxyPassword(for: connection.id) - - let secureFieldIds = Self.secureFieldIds(for: connection.type) - deleteAllPluginSecureFields(for: connection.id, fieldIds: secureFieldIds) - + let origin: ConnectionLocalState.Origin = connection.participatesInSync ? .local : .localOnly + deleteSecrets(of: [connection], origin: origin) ConnectionLocalState.purge( connectionIds: [connection.id], - origin: connection.participatesInSync ? .local : .localOnly, + origin: origin, appSettings: appSettingsProvider(), syncTracker: syncTracker ) @@ -478,20 +467,9 @@ final class ConnectionStorage { for conn in connectionsToDelete where conn.participatesInSync { syncTracker.markDeleted(.connection, id: conn.id.uuidString) } - for conn in connectionsToDelete { - deletePassword(for: conn.id) - deleteSSHPassword(for: conn.id) - deleteKeyPassphrase(for: conn.id) - deleteSSLClientKeyPassphrase(for: conn.id) - deleteTOTPSecret(for: conn.id) - deleteCloudflareTokenId(for: conn.id) - deleteCloudflareTokenSecret(for: conn.id) - deleteCloudSQLProxyServiceAccountKey(for: conn.id) - deleteSOCKSProxyPassword(for: conn.id) - let fields = Self.secureFieldIds(for: conn.type) - deleteAllPluginSecureFields(for: conn.id, fieldIds: fields) - } let syncedIds = Set(connectionsToDelete.filter(\.participatesInSync).map(\.id)) + deleteSecrets(of: connectionsToDelete.filter { syncedIds.contains($0.id) }, origin: .local) + deleteSecrets(of: connectionsToDelete.filter { !syncedIds.contains($0.id) }, origin: .localOnly) ConnectionLocalState.purge( connectionIds: syncedIds, origin: .local, @@ -822,6 +800,21 @@ final class ConnectionStorage { + fieldIds.map { "com.TablePro.plugin.\($0).\(id)" } } + // Removing an iCloud Keychain item removes it from every device, so only a delete the other devices follow does. + // One from another device leaves it to that device. A Local only delete cannot tell whether another device still + // syncs the row, so it keeps it. + func deleteSecrets(of connections: [DatabaseConnection], origin: ConnectionLocalState.Origin) { + for connection in connections { + for key in secretKeys(for: connection) { + if origin.leavesTombstones { + deleteSecret(forKey: key, connectionId: connection.id) + } else { + keychain.delete(forKey: key, deviceOnly: true) + } + } + } + } + private func keepsSecretsOnDevice(_ connectionId: UUID) -> Bool { loadConnection(id: connectionId).map { !$0.participatesInSync } ?? false } @@ -900,8 +893,7 @@ final class ConnectionStorage { guard !lastLoadFailed else { return } var complete = true for connection in connections where !connection.participatesInSync { - // A plugin that is not registered yet has secure fields this pass cannot name. - let knowsFields = PluginMetadataRegistry.shared.snapshot(for: connection.type) != nil + let knowsFields = Self.knowsSecureFields(of: connection.type) if !rewriteSecrets(of: connection, readingOwnItemFirst: true, deviceOnly: true) || !knowsFields { complete = false } @@ -1047,29 +1039,63 @@ final class ConnectionStorage { PluginManager.shared.secureConnectionFieldIds(for: databaseType) } - func migratePluginSecureFieldsIfNeeded() { - let migrationKey = "com.TablePro.pluginSecureFieldsMigrated" - guard !defaults.bool(forKey: migrationKey) else { return } - defer { defaults.set(true, forKey: migrationKey) } + // A plugin that is not registered yet has secure fields nothing here can name. + private static func knowsSecureFields(of databaseType: DatabaseType) -> Bool { + PluginMetadataRegistry.shared.snapshot(for: databaseType) != nil + } + + // Not the key older builds read: they set it on every exit, including runs that moved nothing. + private static let pluginSecureFieldsMovedKey = "com.TablePro.pluginSecureFieldsMovedToKeychain" + // Done only once every connection's secure fields could be named and each value reached the Keychain. A field + // leaves the record only after its own write, and a file edited outside TablePro moves nothing. + func migratePluginSecureFieldsIfNeeded() { + guard !defaults.bool(forKey: Self.pluginSecureFieldsMovedKey) else { return } var connections = loadConnections() - var changed = false + guard !lastLoadFailed, storeIsTrusted else { + Self.logger.notice("Plugin secure fields not moved: connections.json is unreadable or changed outside TablePro; retrying next launch") + return + } + var complete = true + var changed = false for index in connections.indices { - let secureFieldIds = Self.secureFieldIds(for: connections[index].type) - for fieldId in secureFieldIds { - if let value = connections[index].additionalFields[fieldId], !value.isEmpty { - savePluginSecureField(value, fieldId: fieldId, for: connections[index].id) - connections[index].additionalFields.removeValue(forKey: fieldId) - changed = true + let connection = connections[index] + if !Self.knowsSecureFields(of: connection.type) { + complete = false + } + for fieldId in Self.secureFieldIds(for: connection.type) { + guard let value = connection.additionalFields[fieldId], !value.isEmpty else { continue } + guard holdPluginSecureFieldInKeychain(value, fieldId: fieldId, for: connection.id) else { + complete = false + continue } + connections[index].additionalFields.removeValue(forKey: fieldId) + changed = true } } - if changed { - if !saveConnections(connections) { - Self.logger.error("Failed to persist plugin secure field migration; will retry on next launch") - } + if changed, !saveConnections(connections, byUser: false) { + complete = false + } + if complete { + defaults.set(true, forKey: Self.pluginSecureFieldsMovedKey) + } else { + Self.logger.notice("Not every plugin secure field has moved to the Keychain yet; retrying next launch") + } + } + + // A stored item wins: with password sync on it can be a newer value another device saved, which the plain-text + // copy here would overwrite everywhere. The plain-text value is written only where no item exists. + private func holdPluginSecureFieldInKeychain(_ value: String, fieldId: String, for connectionId: UUID) -> Bool { + let key = "com.TablePro.plugin.\(fieldId).\(connectionId.uuidString)" + switch readSecret(forKey: key, connectionId: connectionId) { + case .found(let stored) where !stored.isEmpty: + return true + case .found, .notFound: + return savePluginSecureField(value, fieldId: fieldId, for: connectionId) + case .locked, .userCancelled, .authFailed, .error: + return false } } diff --git a/apple/TablePro/Core/Storage/DamagedStoreNotice.swift b/apple/TablePro/Core/Storage/DamagedStoreNotice.swift new file mode 100644 index 0000000000..d358d74d8b --- /dev/null +++ b/apple/TablePro/Core/Storage/DamagedStoreNotice.swift @@ -0,0 +1,47 @@ +import AppKit + +enum DamagedStoreNotice { + static func queryHistorySetAside(_ setAside: SetAsideDamagedStore) { + let message = String( + format: String( + localized: """ + TablePro could not read its query history, so it moved the file aside as “%@” and started a new one. \ + The copy stays until you delete it or clear all of your history. + """ + ), + setAside.setAsideURL.lastPathComponent + ) + Task { @MainActor in + present(title: String(localized: "Query History Was Damaged"), message: message, setAside: setAside) + } + } + + static func savedQueriesSetAside(_ setAside: SetAsideDamagedStore) { + let message = String( + format: String( + localized: """ + TablePro could not read its saved queries, so it moved the file aside as “%@” and started an empty one. \ + Nothing in that file was deleted. + """ + ), + setAside.setAsideURL.lastPathComponent + ) + Task { @MainActor in + present(title: String(localized: "Saved Queries Were Damaged"), message: message, setAside: setAside) + } + } + + @MainActor + private static func present(title: String, message: String, setAside: SetAsideDamagedStore) { + let alert = NSAlert() + alert.messageText = title + alert.informativeText = message + alert.alertStyle = .warning + alert.addButton(withTitle: String(localized: "OK")) + alert.addButton(withTitle: String(localized: "Show in Finder")) + AlertHelper.present(alert, in: nil) { response in + guard response == .alertSecondButtonReturn else { return } + NSWorkspace.shared.activateFileViewerSelecting([setAside.setAsideURL]) + } + } +} diff --git a/apple/TablePro/Core/Storage/KeychainHelper.swift b/apple/TablePro/Core/Storage/KeychainHelper.swift index eba7a3b291..de76462df9 100644 --- a/apple/TablePro/Core/Storage/KeychainHelper.swift +++ b/apple/TablePro/Core/Storage/KeychainHelper.swift @@ -35,6 +35,8 @@ protocol KeychainStoring: Sendable { // deviceOnly reads this Mac's own item before one iCloud Keychain delivered. func readStringResult(forKey key: String, deviceOnly: Bool) -> KeychainStringResult func delete(forKey key: String) + // deviceOnly removes this Mac's own item and leaves the one iCloud Keychain holds, which every device shares. + func delete(forKey key: String, deviceOnly: Bool) } extension KeychainStoring { @@ -42,6 +44,10 @@ extension KeychainStoring { readStringResult(forKey: key) } + func delete(forKey key: String, deviceOnly: Bool) { + delete(forKey: key) + } + @discardableResult func writeString(_ value: String, forKey key: String, deviceOnly: Bool) -> Bool { writeString(value, forKey: key) @@ -230,8 +236,14 @@ final class KeychainHelper: KeychainStoring { } func delete(forKey key: String) { + delete(forKey: key, deviceOnly: false) + } + + func delete(forKey key: String, deviceOnly: Bool) { var query = baseQuery(forKey: key) - query[kSecAttrSynchronizable as String] = kSecAttrSynchronizableAny + if !deviceOnly { + query[kSecAttrSynchronizable as String] = kSecAttrSynchronizableAny + } let status = items.delete(query) if status != errSecSuccess, status != errSecItemNotFound { diff --git a/apple/TablePro/Core/Storage/LinkedSQLIndex.swift b/apple/TablePro/Core/Storage/LinkedSQLIndex.swift index 78ea3b7998..7c37703c7a 100644 --- a/apple/TablePro/Core/Storage/LinkedSQLIndex.swift +++ b/apple/TablePro/Core/Storage/LinkedSQLIndex.swift @@ -62,11 +62,15 @@ internal actor LinkedSQLIndex { let dir = databaseURL.deletingLastPathComponent() try? FileManager.default.createDirectory(at: dir, withIntermediateDirectories: true) - let dbPath = databaseURL.path(percentEncoded: false) - if sqlite3_open(dbPath, &dbHandle.pointer) != SQLITE_OK { + guard case .opened(let pointer, let setAside) = LocalSQLiteStoreOpener.open(at: databaseURL) else { Self.logger.error("Error opening linked SQL index database") return } + dbHandle.pointer = pointer + // Only a cache of the linked files, which the next folder scan rebuilds, so no damaged copy is kept. + if setAside != nil { + LocalSQLiteStoreOpener.removeSetAsideCopies(of: databaseURL) + } execute("PRAGMA journal_mode=WAL;") execute("PRAGMA synchronous=NORMAL;") diff --git a/apple/TablePro/Core/Storage/LocalSQLiteStoreOpener.swift b/apple/TablePro/Core/Storage/LocalSQLiteStoreOpener.swift new file mode 100644 index 0000000000..7f151d8782 --- /dev/null +++ b/apple/TablePro/Core/Storage/LocalSQLiteStoreOpener.swift @@ -0,0 +1,169 @@ +import Foundation +import os +import SQLite3 + +struct SetAsideDamagedStore: Sendable, Equatable { + let storeURL: URL + let setAsideURL: URL +} + +enum LocalSQLiteStoreOpening { + case opened(OpaquePointer, setAside: SetAsideDamagedStore?) + case unavailable +} + +// sqlite3_open succeeds on a damaged file, so quick_check runs before anything reads or writes it. +enum LocalSQLiteStoreOpener { + private static let logger = Logger(subsystem: "com.TablePro", category: "LocalSQLiteStore") + private static let busyTimeoutMilliseconds: Int32 = 3_000 + + private enum Verdict { + case healthy(OpaquePointer) + case damaged(String) + case unavailable(String) + } + + static func open(at url: URL, now: Date = Date()) -> LocalSQLiteStoreOpening { + let path = url.path(percentEncoded: false) + switch openChecked(path: path) { + case .healthy(let handle): + return .opened(handle, setAside: nil) + case .unavailable(let reason): + logger.error("Local store could not be opened: \(reason, privacy: .public)") + return .unavailable + case .damaged(let reason): + logger.fault("Local store is damaged: \(reason, privacy: .public)") + guard let moved = setAside(url, now: now) else { return .unavailable } + guard case .healthy(let handle) = openChecked(path: path) else { + logger.error("A fresh store could not be opened after setting the damaged one aside") + return .unavailable + } + return .opened(handle, setAside: moved) + } + } + + private static func isDamage(_ resultCode: Int32) -> Bool { + let primary = resultCode & 0xFF + return primary == SQLITE_CORRUPT || primary == SQLITE_NOTADB + } + + static func setAsideURL(for url: URL, at date: Date, fileManager: FileManager = .default) -> URL { + let formatter = DateFormatter() + formatter.locale = Locale(identifier: "en_US_POSIX") + formatter.timeZone = .current + formatter.dateFormat = "yyyyMMdd-HHmmss" + let stamp = formatter.string(from: date) + + let directory = url.deletingLastPathComponent() + let stem = url.deletingPathExtension().lastPathComponent + let pathExtension = url.pathExtension + var attempt = 0 + while true { + let marker = attempt == 0 ? stamp : "\(stamp)-\(attempt)" + let name = pathExtension.isEmpty + ? "\(stem).damaged-\(marker)" + : "\(stem).damaged-\(marker).\(pathExtension)" + let candidate = directory.appendingPathComponent(name) + let candidatePath = candidate.path(percentEncoded: false) + let taken = ["", "-wal", "-shm"].contains { fileManager.fileExists(atPath: candidatePath + $0) } + if !taken { return candidate } + attempt += 1 + } + } + + // Every copy setAsideURL(for:) ever named for this store, with its -wal and -shm. + @discardableResult + static func removeSetAsideCopies(of url: URL, fileManager: FileManager = .default) -> Bool { + let directory = url.deletingLastPathComponent() + let prefix = url.deletingPathExtension().lastPathComponent + ".damaged-" + guard let names = try? fileManager.contentsOfDirectory(atPath: directory.path(percentEncoded: false)) else { + return false + } + var removedAll = true + for name in names where name.hasPrefix(prefix) { + do { + try fileManager.removeItem(at: directory.appendingPathComponent(name)) + } catch { + logger.error("Could not delete a set-aside store: \(error.publicLogShape, privacy: .public)") + removedAll = false + } + } + return removedAll + } + + private static func openChecked(path: String) -> Verdict { + var handle: OpaquePointer? + let openResult = sqlite3_open(path, &handle) + guard openResult == SQLITE_OK, let handle else { + let reason = String(cString: sqlite3_errstr(openResult)) + sqlite3_close_v2(handle) + return isDamage(openResult) ? .damaged(reason) : .unavailable(reason) + } + sqlite3_busy_timeout(handle, busyTimeoutMilliseconds) + + let verdict = quickCheck(handle) + if case .healthy = verdict { + return verdict + } + sqlite3_close_v2(handle) + return verdict + } + + private static func quickCheck(_ handle: OpaquePointer) -> Verdict { + var statement: OpaquePointer? + defer { sqlite3_finalize(statement) } + + let prepareResult = sqlite3_prepare_v2(handle, "PRAGMA quick_check(1);", -1, &statement, nil) + guard prepareResult == SQLITE_OK else { + let reason = String(cString: sqlite3_errmsg(handle)) + return isDamage(prepareResult) ? .damaged(reason) : .unavailable(reason) + } + + let stepResult = sqlite3_step(statement) + guard stepResult == SQLITE_ROW else { + let reason = String(cString: sqlite3_errmsg(handle)) + return isDamage(stepResult) ? .damaged(reason) : .unavailable(reason) + } + let report = sqlite3_column_text(statement, 0).map { String(cString: $0) } ?? "" + return report == "ok" ? .healthy(handle) : .damaged(report) + } + + // The -wal moves first: SQLite deletes a -wal it finds beside an empty database. + private static func setAside(_ url: URL, now: Date) -> SetAsideDamagedStore? { + let fileManager = FileManager.default + let path = url.path(percentEncoded: false) + let destination = setAsideURL(for: url, at: now, fileManager: fileManager) + let destinationPath = destination.path(percentEncoded: false) + + let walPath = path + "-wal" + let walDestinationPath = destinationPath + "-wal" + let movedWal = fileManager.fileExists(atPath: walPath) + if movedWal { + do { + try fileManager.moveItem(atPath: walPath, toPath: walDestinationPath) + } catch { + logger.error("Could not set aside a damaged store's log: \(error.publicLogShape, privacy: .public)") + return nil + } + } + + do { + try fileManager.moveItem(atPath: path, toPath: destinationPath) + } catch { + logger.error("Could not set aside a damaged store: \(error.publicLogShape, privacy: .public)") + if movedWal { + try? fileManager.moveItem(atPath: walDestinationPath, toPath: walPath) + } + return nil + } + + let shmPath = path + "-shm" + if fileManager.fileExists(atPath: shmPath), + (try? fileManager.moveItem(atPath: shmPath, toPath: destinationPath + "-shm")) == nil { + try? fileManager.removeItem(atPath: shmPath) + } + + logger.fault("Set a damaged store aside as \(destination.lastPathComponent, privacy: .private(mask: .hash))") + return SetAsideDamagedStore(storeURL: url, setAsideURL: destination) + } +} diff --git a/apple/TablePro/Core/Storage/MCPApprovalStore.swift b/apple/TablePro/Core/Storage/MCPApprovalStore.swift index ead86232cd..4b4c7c4e8d 100644 --- a/apple/TablePro/Core/Storage/MCPApprovalStore.swift +++ b/apple/TablePro/Core/Storage/MCPApprovalStore.swift @@ -1,8 +1,3 @@ -// -// MCPApprovalStore.swift -// TablePro -// - import Foundation import os @@ -108,43 +103,3 @@ actor MCPApprovalStore: MCPApprovalStoring { defaults.set(data, forKey: key) } } - -/// Keeps grants for its own lifetime and never writes them anywhere. For tests, and for any path -/// that must not leave a decision on disk. -actor MCPInMemoryApprovalStore: MCPApprovalStoring { - private var storedGrants: [MCPConnectionGrant] = [] - private var storedDenials: [MCPConnectionDenial] = [] - - init() {} - - func grants() -> [MCPConnectionGrant] { - storedGrants - } - - func record(_ grant: MCPConnectionGrant) { - storedGrants.removeAll { $0.id == grant.id } - storedGrants.append(grant) - } - - func revoke(subject: String, connectionId: UUID) { - storedGrants.removeAll { $0.subject == subject && $0.connectionId == connectionId } - } - - func revokeAll(subject: String) { - storedGrants.removeAll { $0.subject == subject } - } - - func revokeEverything() { - storedGrants.removeAll() - } - - func denial(subject: String, connectionId: UUID, now: Date) -> Date? { - storedDenials.removeAll { $0.expiresAt <= now } - return storedDenials.first { $0.subject == subject && $0.connectionId == connectionId }?.expiresAt - } - - func record(_ denial: MCPConnectionDenial) { - storedDenials.removeAll { $0.subject == denial.subject && $0.connectionId == denial.connectionId } - storedDenials.append(denial) - } -} diff --git a/apple/TablePro/Core/Storage/QueryHistoryManager.swift b/apple/TablePro/Core/Storage/QueryHistoryManager.swift index 1842118b1d..3ea0d54397 100644 --- a/apple/TablePro/Core/Storage/QueryHistoryManager.swift +++ b/apple/TablePro/Core/Storage/QueryHistoryManager.swift @@ -3,7 +3,9 @@ import Foundation import TableProPluginKit final class QueryHistoryManager: QueryHistoryRecording, QueryHistoryReading, QueryPlanSnapshotReading, Sendable { - static let shared = QueryHistoryManager() + static let shared = QueryHistoryManager( + storage: QueryHistoryStorage(onSetAside: { DamagedStoreNotice.queryHistorySetAside($0) }) + ) // Not private: QueryHistoryManager+Rewind reaches it from another file. internal let storage: QueryHistoryStorage diff --git a/apple/TablePro/Core/Storage/QueryHistoryStorage+SetAside.swift b/apple/TablePro/Core/Storage/QueryHistoryStorage+SetAside.swift new file mode 100644 index 0000000000..76150b1f7b --- /dev/null +++ b/apple/TablePro/Core/Storage/QueryHistoryStorage+SetAside.swift @@ -0,0 +1,14 @@ +import Foundation + +extension QueryHistoryStorage { + // A set-aside copy holds rows of every connection and source, so only a clear of all of them reaches it. + static func coversEveryRow(_ filter: QueryHistoryFilter) -> Bool { + filter.scope.connectionId == nil + && filter.sources == Set(QueryHistorySource.allCases) + && filter.outcome == .any + && (filter.searchText ?? "").isEmpty + && filter.since == nil + && filter.until == nil + && filter.allowedConnectionIds == nil + } +} diff --git a/apple/TablePro/Core/Storage/QueryHistoryStorage.swift b/apple/TablePro/Core/Storage/QueryHistoryStorage.swift index 14a8966b21..7394509bfc 100644 --- a/apple/TablePro/Core/Storage/QueryHistoryStorage.swift +++ b/apple/TablePro/Core/Storage/QueryHistoryStorage.swift @@ -37,13 +37,16 @@ actor QueryHistoryStorage { private let databaseURL: URL private let removeDatabaseOnDeinit: Bool + private let onSetAside: @Sendable (SetAsideDamagedStore) -> Void init( databaseURL: URL = QueryHistoryStorage.defaultDatabaseURL(), - removeDatabaseOnDeinit: Bool = false + removeDatabaseOnDeinit: Bool = false, + onSetAside: @escaping @Sendable (SetAsideDamagedStore) -> Void = { _ in } ) { self.databaseURL = databaseURL self.removeDatabaseOnDeinit = removeDatabaseOnDeinit + self.onSetAside = onSetAside } static func defaultDatabaseURL() -> URL { @@ -77,25 +80,29 @@ actor QueryHistoryStorage { let dbPath = databaseURL.path(percentEncoded: false) protectDatabaseFiles(at: dbPath) - guard sqlite3_open(dbPath, &dbHandle.pointer) == SQLITE_OK else { + guard case .opened(let pointer, let setAside) = LocalSQLiteStoreOpener.open(at: databaseURL) else { Self.logger.error("Failed to open query history database at \(dbPath, privacy: .private(mask: .hash))") - if let pointer = dbHandle.pointer { - sqlite3_close_v2(pointer) - } - dbHandle.pointer = nil return } + dbHandle.pointer = pointer + if let setAside { + onSetAside(setAside) + } execute("PRAGMA journal_mode=WAL;") execute("PRAGMA synchronous=NORMAL;") - /// Off by default in SQLite, and `plan_snapshots.history_id` needs it: without it the - /// `ON DELETE SET NULL` never fires and a pruned history row leaves a plan pointing at a - /// row that no longer exists. + // plan_snapshots.history_id's ON DELETE SET NULL only fires with foreign keys on. execute("PRAGMA foreign_keys=ON;") sqlite3_busy_timeout(db, 3_000) createTables() - migrateIfNeeded() + guard migrateIfNeeded() else { + // A half-migrated schema reads as an empty history, so the store stays closed instead. + Self.logger.error("Query history migration failed; the store stays closed") + sqlite3_close_v2(pointer) + dbHandle.pointer = nil + return + } createFingerprintIndex() createPlanSnapshotStorage() createRewindSnapshotStorage() @@ -154,23 +161,31 @@ actor QueryHistoryStorage { createIndexes() } - private func createFtsTriggers() { - execute(""" - CREATE TRIGGER IF NOT EXISTS history_ai AFTER INSERT ON history BEGIN - INSERT INTO history_fts(rowid, query) VALUES (new.rowid, new.query); - END; - """) - execute(""" - CREATE TRIGGER IF NOT EXISTS history_ad AFTER DELETE ON history BEGIN - INSERT INTO history_fts(history_fts, rowid, query) VALUES('delete', old.rowid, old.query); - END; - """) + @discardableResult + private func createFtsTriggers() -> Bool { + let results = [ + execute(""" + CREATE TRIGGER IF NOT EXISTS history_ai AFTER INSERT ON history BEGIN + INSERT INTO history_fts(rowid, query) VALUES (new.rowid, new.query); + END; + """), + execute(""" + CREATE TRIGGER IF NOT EXISTS history_ad AFTER DELETE ON history BEGIN + INSERT INTO history_fts(history_fts, rowid, query) VALUES('delete', old.rowid, old.query); + END; + """), + ] + return !results.contains(false) } - private func createIndexes() { - execute("CREATE INDEX IF NOT EXISTS idx_history_connection_executed ON history(connection_id, executed_at DESC);") - execute("CREATE INDEX IF NOT EXISTS idx_history_executed_at ON history(executed_at DESC);") - execute("CREATE INDEX IF NOT EXISTS idx_history_source ON history(source);") + @discardableResult + private func createIndexes() -> Bool { + let results = [ + execute("CREATE INDEX IF NOT EXISTS idx_history_connection_executed ON history(connection_id, executed_at DESC);"), + execute("CREATE INDEX IF NOT EXISTS idx_history_executed_at ON history(executed_at DESC);"), + execute("CREATE INDEX IF NOT EXISTS idx_history_source ON history(source);"), + ] + return !results.contains(false) } /// Kept out of `createIndexes` because that runs before the migration, when a database written @@ -195,47 +210,26 @@ actor QueryHistoryStorage { // MARK: - Migration - /// `createTables` already builds the current shape, so a fresh database needs no migration at - /// all. Only a table left over from an older release is missing `source`, and the rebuild below - /// copies it forward by name, so every earlier version reaches v3 through the same path. - private func migrateIfNeeded() { - guard db != nil else { return } - - if hasColumn("source", inTable: "history") == false { - migrateToVersion3() - } - migrateToVersion4() - migrateToVersion5() - // Stamped only once the column it describes is actually there. Stamping regardless would - // claim a schema a failed ALTER never produced, and `record`'s insert would then fail to - // prepare against the old column count, silently recording nothing. - if hasColumn("fingerprint_hash", inTable: "history") { - setUserVersion(hasColumn("server_time", inTable: "history") ? 5 : 4) - } - } + private func migrateIfNeeded() -> Bool { + guard db != nil, let hasSource = hasColumn("source", inTable: "history") else { return false } - /// Both columns are nullable and deliberately not backfilled. A row written before the split - /// existed has no honest value to put in either, and `COALESCE` down to `execution_time` reads - /// it exactly as this release's predecessor did. - private func migrateToVersion5() { - if hasColumn("first_row_time", inTable: "history") == false { - execute("ALTER TABLE history ADD COLUMN first_row_time REAL;") + if !hasSource { + guard migrateToVersion3() else { return false } } - if hasColumn("server_time", inTable: "history") == false { - execute("ALTER TABLE history ADD COLUMN server_time REAL;") + guard addColumnIfMissing("fingerprint_hash", definition: "INTEGER NOT NULL DEFAULT 0"), + addColumnIfMissing("first_row_time", definition: "REAL"), + addColumnIfMissing("server_time", definition: "REAL") + else { + return false } + setUserVersion(5) + return true } - /// Grouping a statement by its shape needs the shape stored, because deriving it on every read - /// costs about sixty times what reading a stored column does. The column is added first and - /// filled second so an app killed between the two reopens with an unfilled column and finishes - /// the backfill, rather than with no column and a version number claiming otherwise. - /// - /// A row the v3 rebuild carried forward arrives here with the column's `0` default, so both the - /// upgrade-from-v3 path and the upgrade-from-v2 path are covered by the same backfill. - private func migrateToVersion4() { - guard hasColumn("fingerprint_hash", inTable: "history") == false else { return } - execute("ALTER TABLE history ADD COLUMN fingerprint_hash INTEGER NOT NULL DEFAULT 0;") + // first_row_time and server_time stay NULL for older rows: COALESCE falls back to execution_time. + private func addColumnIfMissing(_ column: String, definition: String) -> Bool { + guard let present = hasColumn(column, inTable: "history") else { return false } + return present || execute("ALTER TABLE history ADD COLUMN \(column) \(definition);") } /// Filling the column is deliberately not part of `init`. @@ -307,69 +301,73 @@ actor QueryHistoryStorage { return pending } - /// Renaming the table takes its triggers with it and dropping it destroys them, so between the - /// rename and `createFtsTriggers` the search index has no way to stay in step. Everything runs - /// in one transaction, and the version is only stamped once that transaction commits, so an - /// app killed midway reopens as the old schema and migrates again instead of stranding a - /// half-built index behind a version number that says the work is done. - private func migrateToVersion3() { - let legacyRows = readLegacyRowsForBackfill() - - beginTransaction() - execute("ALTER TABLE history RENAME TO history_legacy;") - execute(Self.historyTableSql) - execute(""" - INSERT INTO history ( - id, query, connection_id, database_name, database_type, schema_name, - source, statement_type, executed_at, execution_time, row_count, - was_successful, error_message - ) - SELECT - id, query, connection_id, database_name, '', NULL, - 'editor', 'other', executed_at, execution_time, row_count, - was_successful, error_message - FROM history_legacy; - """) - execute("DROP TABLE history_legacy;") - - execute("DROP TRIGGER IF EXISTS history_ai;") - execute("DROP TRIGGER IF EXISTS history_ad;") - execute("DROP TRIGGER IF EXISTS history_au;") - execute("DROP TABLE IF EXISTS history_fts;") - execute(Self.ftsTableSql) - createFtsTriggers() - execute("INSERT INTO history_fts(history_fts) VALUES('rebuild');") - - execute("DROP INDEX IF EXISTS idx_history_connection;") - createIndexes() - - applyStatementTypes(legacyRows) - setUserVersion(3) - - commitTransaction() + // One transaction, stamped inside it: any failed step rolls back to the legacy table intact. + private func migrateToVersion3() -> Bool { + guard let legacyRows = readLegacyRowsForBackfill(), beginTransaction() else { return false } + + let migrated = execute("ALTER TABLE history RENAME TO history_legacy;") + && execute(Self.historyTableSql) + && execute(""" + INSERT INTO history ( + id, query, connection_id, database_name, database_type, schema_name, + source, statement_type, executed_at, execution_time, row_count, + was_successful, error_message + ) + SELECT + id, query, connection_id, database_name, '', NULL, + 'editor', 'other', executed_at, execution_time, row_count, + was_successful, error_message + FROM history_legacy; + """) + && execute("DROP TABLE history_legacy;") + && execute("DROP TRIGGER IF EXISTS history_ai;") + && execute("DROP TRIGGER IF EXISTS history_ad;") + && execute("DROP TRIGGER IF EXISTS history_au;") + && execute("DROP TABLE IF EXISTS history_fts;") + && execute(Self.ftsTableSql) + && createFtsTriggers() + && execute("INSERT INTO history_fts(history_fts) VALUES('rebuild');") + && execute("DROP INDEX IF EXISTS idx_history_connection;") + && createIndexes() + && applyStatementTypes(legacyRows) + && setUserVersion(3) + + guard migrated else { + Self.logger.error("Query history migration to version 3 failed and was rolled back") + rollbackTransaction() + return false + } + return commitTransaction() } - private func readLegacyRowsForBackfill() -> [(id: String, statementType: String)] { + private func readLegacyRowsForBackfill() -> [(id: String, statementType: String)]? { var pending: [(id: String, statementType: String)] = [] var statement: OpaquePointer? defer { sqlite3_finalize(statement) } guard sqlite3_prepare_v2(db, "SELECT id, query FROM history;", -1, &statement, nil) == SQLITE_OK else { logSqliteError(context: "prepare backfill read") - return pending + return nil } - while sqlite3_step(statement) == SQLITE_ROW { - guard let id = sqlite3_column_text(statement, 0).map({ String(cString: $0) }), - let query = sqlite3_column_text(statement, 1).map({ String(cString: $0) }) - else { continue } - // A legacy row has no engine; standard SQL comments are enough to find a label's keyword. - pending.append((id, QueryHistoryStatementType.classify(query, grammar: .ansi).rawValue)) + while true { + switch sqlite3_step(statement) { + case SQLITE_ROW: + guard let id = sqlite3_column_text(statement, 0).map({ String(cString: $0) }), + let query = sqlite3_column_text(statement, 1).map({ String(cString: $0) }) + else { continue } + // A legacy row has no engine; standard SQL comments are enough to find a label's keyword. + pending.append((id, QueryHistoryStatementType.classify(query, grammar: .ansi).rawValue)) + case SQLITE_DONE: + return pending + default: + logSqliteError(context: "backfill read") + return nil + } } - return pending } - private func applyStatementTypes(_ rows: [(id: String, statementType: String)]) { - guard !rows.isEmpty else { return } + private func applyStatementTypes(_ rows: [(id: String, statementType: String)]) -> Bool { + guard !rows.isEmpty else { return true } var statement: OpaquePointer? defer { sqlite3_finalize(statement) } @@ -381,33 +379,42 @@ actor QueryHistoryStorage { nil ) == SQLITE_OK else { logSqliteError(context: "prepare backfill write") - return + return false } for row in rows { QueryHistorySqlBinding.text(row.statementType).bind(to: statement, at: 1) QueryHistorySqlBinding.text(row.id).bind(to: statement, at: 2) - if sqlite3_step(statement) != SQLITE_DONE { + guard sqlite3_step(statement) == SQLITE_DONE else { logSqliteError(context: "backfill statement type") + return false } sqlite3_reset(statement) sqlite3_clear_bindings(statement) } + return true } - private func hasColumn(_ column: String, inTable table: String) -> Bool { - guard db != nil else { return false } + // nil when the schema cannot be read: a guess of false would rebuild a current table as legacy. + private func hasColumn(_ column: String, inTable table: String) -> Bool? { + guard db != nil else { return nil } var statement: OpaquePointer? defer { sqlite3_finalize(statement) } guard sqlite3_prepare_v2(db, "PRAGMA table_info(\(table))", -1, &statement, nil) == SQLITE_OK else { - return false + return nil } - while sqlite3_step(statement) == SQLITE_ROW { - if let name = sqlite3_column_text(statement, 1), String(cString: name) == column { - return true + while true { + switch sqlite3_step(statement) { + case SQLITE_ROW: + if let name = sqlite3_column_text(statement, 1), String(cString: name) == column { + return true + } + case SQLITE_DONE: + return false + default: + return nil } } - return false } private func userVersion() -> Int32 { @@ -422,25 +429,29 @@ actor QueryHistoryStorage { return sqlite3_column_int(statement, 0) } - private func setUserVersion(_ version: Int32) { + @discardableResult + private func setUserVersion(_ version: Int32) -> Bool { execute("PRAGMA user_version = \(version);") } // MARK: - Statement Helpers - func execute(_ sql: String) { - guard let db else { return } + @discardableResult + func execute(_ sql: String) -> Bool { + guard let db else { return false } var statement: OpaquePointer? defer { sqlite3_finalize(statement) } guard sqlite3_prepare_v2(db, sql, -1, &statement, nil) == SQLITE_OK else { logSqliteError(context: "prepare") - return + return false } let result = sqlite3_step(statement) - if result != SQLITE_DONE, result != SQLITE_ROW { + guard result == SQLITE_DONE || result == SQLITE_ROW else { logSqliteError(context: "step") + return false } + return true } func logSqliteError(context: String) { @@ -448,19 +459,30 @@ actor QueryHistoryStorage { Self.logger.error("Query history SQL \(context, privacy: .public) failed: \(String(cString: message), privacy: .private)") } - private func beginTransaction() { - guard let db else { return } - if sqlite3_exec(db, "BEGIN IMMEDIATE;", nil, nil, nil) != SQLITE_OK { + @discardableResult + private func beginTransaction() -> Bool { + guard let db else { return false } + guard sqlite3_exec(db, "BEGIN IMMEDIATE;", nil, nil, nil) == SQLITE_OK else { logSqliteError(context: "begin") + return false } + return true } - private func commitTransaction() { - guard let db else { return } - if sqlite3_exec(db, "COMMIT;", nil, nil, nil) != SQLITE_OK { + @discardableResult + private func commitTransaction() -> Bool { + guard let db else { return false } + guard sqlite3_exec(db, "COMMIT;", nil, nil, nil) == SQLITE_OK else { logSqliteError(context: "commit") - sqlite3_exec(db, "ROLLBACK;", nil, nil, nil) + rollbackTransaction() + return false } + return true + } + + private func rollbackTransaction() { + guard let db else { return } + sqlite3_exec(db, "ROLLBACK;", nil, nil, nil) } // MARK: - Writes @@ -1101,6 +1123,9 @@ actor QueryHistoryStorage { logSqliteError(context: "clear") return false } + if Self.coversEveryRow(filter), !LocalSQLiteStoreOpener.removeSetAsideCopies(of: databaseURL) { + Self.logger.fault("History was cleared, but a damaged copy set aside earlier could not be deleted") + } return true } diff --git a/apple/TablePro/Core/Storage/SQLFavoriteManager.swift b/apple/TablePro/Core/Storage/SQLFavoriteManager.swift index a0ed19d3ff..fca41a492b 100644 --- a/apple/TablePro/Core/Storage/SQLFavoriteManager.swift +++ b/apple/TablePro/Core/Storage/SQLFavoriteManager.swift @@ -11,7 +11,9 @@ import TableProSyncTransport import TableProUtilities internal final class SQLFavoriteManager: @unchecked Sendable { - static let shared = SQLFavoriteManager() + static let shared = SQLFavoriteManager( + storage: SQLFavoriteStorage(onSetAside: { DamagedStoreNotice.savedQueriesSetAside($0) }) + ) private static let logger = Logger(subsystem: "com.TablePro", category: "SQLFavoriteManager") private let storage: SQLFavoriteStorage @@ -146,11 +148,19 @@ internal final class SQLFavoriteManager: @unchecked Sendable { } func favoritesForSync() async -> [SQLFavorite]? { - await storage.readAllFavorites() + await storage.readAllFavoritesForSync() } func foldersForSync() async -> [SQLFavoriteFolder]? { - await storage.readAllFolders() + await storage.readAllFoldersForSync() + } + + func isAwaitingSyncRefill() async -> Bool { + await storage.isAwaitingSyncRefill() + } + + func finishSyncRefill() async { + await storage.finishSyncRefill() } func ledgerEntries() async -> [SavedQueryLedger.Entry]? { @@ -271,7 +281,8 @@ internal final class SQLFavoriteManager: @unchecked Sendable { ) async -> RemoteApplyOutcome { guard !batch.isEmpty else { return .skipped } return await operations.run { [self] in - let admitted = admitting(batch, echoGuard: echoGuard) + let refilling = await storage.isAwaitingSyncRefill() + let admitted = admitting(batch, echoGuard: echoGuard, refilling: refilling) guard !admitted.isEmpty else { return .skipped } guard await applyRemoteFavoriteDeletions(admitted.deletedFavoriteIds), await applyRemoteFolders(admitted.folders), @@ -285,9 +296,13 @@ internal final class SQLFavoriteManager: @unchecked Sendable { } @MainActor - private func admitting(_ batch: RemoteSQLFavoriteBatch, echoGuard: SyncEchoGuard?) -> RemoteSQLFavoriteBatch { - let deletedFavoriteIds = syncTracker.tombstonedIds(for: .favorite) - let deletedFolderIds = syncTracker.tombstonedIds(for: .favoriteFolder) + private func admitting( + _ batch: RemoteSQLFavoriteBatch, + echoGuard: SyncEchoGuard?, + refilling: Bool + ) -> RemoteSQLFavoriteBatch { + let deletedFavoriteIds = locallyDeletedIds(.favorite, refilling: refilling) + let deletedFolderIds = locallyDeletedIds(.favoriteFolder, refilling: refilling) var admitted = batch admitted.favorites = batch.favorites.filter { favorite in let id = favorite.id.uuidString @@ -302,6 +317,14 @@ internal final class SQLFavoriteManager: @unchecked Sendable { return admitted } + // While a replaced store refills, a record deleted and then edited again here comes back: + // the edit is the later intent, and pushing the deletion would remove it on every device. + @MainActor + private func locallyDeletedIds(_ type: SyncRecordType, refilling: Bool) -> Set { + let tombstoned = syncTracker.tombstonedIds(for: type) + return refilling ? tombstoned.subtracting(syncTracker.dirtyRecords(for: type)) : tombstoned + } + @MainActor private func applyRemoteFavoriteDeletions(_ ids: Set) async -> Bool { guard !ids.isEmpty else { return true } diff --git a/apple/TablePro/Core/Storage/SQLFavoriteStorage+Migration.swift b/apple/TablePro/Core/Storage/SQLFavoriteStorage+Migration.swift new file mode 100644 index 0000000000..0bbc68c7c8 --- /dev/null +++ b/apple/TablePro/Core/Storage/SQLFavoriteStorage+Migration.swift @@ -0,0 +1,59 @@ +import Foundation + +extension SQLFavoriteStorage { + // Run in one transaction by migrateIfNeeded; the version stamp is the last step. + static let version2MigrationStatements: [String] = [ + "ALTER TABLE favorites RENAME TO favorites_old", + """ + CREATE TABLE IF NOT EXISTS favorites ( + id TEXT PRIMARY KEY, name TEXT NOT NULL, query TEXT NOT NULL, + keyword TEXT, folder_id TEXT, connection_id TEXT, + sort_order INTEGER NOT NULL DEFAULT 0, created_at REAL NOT NULL, updated_at REAL NOT NULL + ) + """, + """ + INSERT INTO favorites SELECT id, name, query, keyword, folder_id, connection_id, + sort_order, created_at, updated_at FROM favorites_old + """, + "DROP TABLE favorites_old", + "ALTER TABLE folders RENAME TO folders_old", + """ + CREATE TABLE IF NOT EXISTS folders ( + id TEXT PRIMARY KEY, name TEXT NOT NULL, parent_id TEXT, connection_id TEXT, + sort_order INTEGER NOT NULL DEFAULT 0, created_at REAL NOT NULL, updated_at REAL NOT NULL + ) + """, + """ + INSERT INTO folders SELECT id, name, parent_id, connection_id, + sort_order, created_at, updated_at FROM folders_old + """, + "DROP TABLE folders_old", + "CREATE INDEX IF NOT EXISTS idx_favorites_connection ON favorites(connection_id);", + "CREATE INDEX IF NOT EXISTS idx_favorites_folder ON favorites(folder_id);", + "CREATE INDEX IF NOT EXISTS idx_favorites_keyword ON favorites(keyword);", + "CREATE UNIQUE INDEX IF NOT EXISTS idx_favorites_keyword_scope ON favorites(keyword, connection_id) WHERE keyword IS NOT NULL;", + "CREATE INDEX IF NOT EXISTS idx_folders_connection ON folders(connection_id);", + "CREATE INDEX IF NOT EXISTS idx_folders_parent ON folders(parent_id);", + "DROP TRIGGER IF EXISTS favorites_ai;", + "DROP TRIGGER IF EXISTS favorites_ad;", + "DROP TRIGGER IF EXISTS favorites_au;", + """ + CREATE TRIGGER IF NOT EXISTS favorites_ai AFTER INSERT ON favorites BEGIN + INSERT INTO favorites_fts(rowid, name, query, keyword) VALUES (new.rowid, new.name, new.query, new.keyword); + END; + """, + """ + CREATE TRIGGER IF NOT EXISTS favorites_ad AFTER DELETE ON favorites BEGIN + INSERT INTO favorites_fts(favorites_fts, rowid, name, query, keyword) VALUES('delete', old.rowid, old.name, old.query, old.keyword); + END; + """, + """ + CREATE TRIGGER IF NOT EXISTS favorites_au AFTER UPDATE ON favorites BEGIN + INSERT INTO favorites_fts(favorites_fts, rowid, name, query, keyword) VALUES('delete', old.rowid, old.name, old.query, old.keyword); + INSERT INTO favorites_fts(rowid, name, query, keyword) VALUES (new.rowid, new.name, new.query, new.keyword); + END; + """, + "INSERT INTO favorites_fts(favorites_fts) VALUES('rebuild');", + "PRAGMA user_version = 2;", + ] +} diff --git a/apple/TablePro/Core/Storage/SQLFavoriteStorage+SyncRefill.swift b/apple/TablePro/Core/Storage/SQLFavoriteStorage+SyncRefill.swift new file mode 100644 index 0000000000..a64c61ff33 --- /dev/null +++ b/apple/TablePro/Core/Storage/SQLFavoriteStorage+SyncRefill.swift @@ -0,0 +1,53 @@ +import Foundation +import os +import SQLite3 + +// A store that replaced a damaged one is not the user's library for sync until a full fetch refills it. +extension SQLFavoriteStorage { + private static let syncRefillLogger = Logger(subsystem: "com.TablePro", category: "SQLFavoriteSyncRefill") + + func isAwaitingSyncRefill() -> Bool { + _ = db + return syncRefillPending + } + + func readAllFavoritesForSync() -> [SQLFavorite]? { + isAwaitingSyncRefill() ? nil : readAllFavorites() + } + + func readAllFoldersForSync() -> [SQLFavoriteFolder]? { + isAwaitingSyncRefill() ? nil : readAllFolders() + } + + func finishSyncRefill() { + guard isAwaitingSyncRefill() else { return } + guard run("DROP TABLE IF EXISTS sync_refill;") else { + Self.syncRefillLogger.error("The saved queries refill marker could not be cleared; sync keeps waiting") + return + } + syncRefillPending = false + } + + func loadSyncRefillState(afterSetAside setAside: Bool) { + guard setAside else { + syncRefillPending = hasSyncRefillMarker() + return + } + // Pending for this session even when the marker cannot be written. + syncRefillPending = true + let marked = run("CREATE TABLE IF NOT EXISTS sync_refill (id INTEGER PRIMARY KEY CHECK (id = 1));") + && run("INSERT OR IGNORE INTO sync_refill (id) VALUES (1);") + if !marked { + Self.syncRefillLogger.fault("The saved queries refill marker could not be written") + } + } + + private func hasSyncRefillMarker() -> Bool { + var statement: OpaquePointer? + defer { sqlite3_finalize(statement) } + guard sqlite3_prepare_v2(db, "SELECT 1 FROM sync_refill LIMIT 1;", -1, &statement, nil) == SQLITE_OK else { + return false + } + return sqlite3_step(statement) == SQLITE_ROW + } +} diff --git a/apple/TablePro/Core/Storage/SQLFavoriteStorage.swift b/apple/TablePro/Core/Storage/SQLFavoriteStorage.swift index 9350f4e701..da0246b1b4 100644 --- a/apple/TablePro/Core/Storage/SQLFavoriteStorage.swift +++ b/apple/TablePro/Core/Storage/SQLFavoriteStorage.swift @@ -26,13 +26,17 @@ internal actor SQLFavoriteStorage { private let databaseURL: URL private let removeDatabaseOnDeinit: Bool + private let onSetAside: @Sendable (SetAsideDamagedStore) -> Void + var syncRefillPending = false init( databaseURL: URL = SQLFavoriteStorage.defaultDatabaseURL(), - removeDatabaseOnDeinit: Bool = false + removeDatabaseOnDeinit: Bool = false, + onSetAside: @escaping @Sendable (SetAsideDamagedStore) -> Void = { _ in } ) { self.databaseURL = databaseURL self.removeDatabaseOnDeinit = removeDatabaseOnDeinit + self.onSetAside = onSetAside } static func defaultDatabaseURL() -> URL { @@ -61,16 +65,14 @@ internal actor SQLFavoriteStorage { let dir = databaseURL.deletingLastPathComponent() try? FileManager.default.createDirectory(at: dir, withIntermediateDirectories: true) - let dbPath = databaseURL.path(percentEncoded: false) - - var pointer: OpaquePointer? - let openResult = sqlite3_open(dbPath, &pointer) - guard openResult == SQLITE_OK else { - Self.logger.error("Error opening database: \(String(cString: sqlite3_errstr(openResult)), privacy: .public)") - sqlite3_close_v2(pointer) + guard case .opened(let pointer, let setAside) = LocalSQLiteStoreOpener.open(at: databaseURL) else { + Self.logger.error("The saved queries database could not be opened") return } dbHandle.pointer = pointer + if let setAside { + onSetAside(setAside) + } execute("PRAGMA journal_mode=WAL;") execute("PRAGMA synchronous=NORMAL;") @@ -78,12 +80,16 @@ internal actor SQLFavoriteStorage { createTables() migrateIfNeeded() Self.versionSchemaStatements.forEach { execute($0) } + loadSyncRefillState(afterSetAside: setAside != nil) } // MARK: - Schema Migration private func migrateIfNeeded() { - let currentVersion = getUserVersion() + guard let currentVersion = getUserVersion() else { + Self.logger.error("The saved queries schema version could not be read, so no migration ran") + return + } if currentVersion < 1 { setUserVersion(2) @@ -91,73 +97,24 @@ internal actor SQLFavoriteStorage { } if currentVersion < 2 { - execute("ALTER TABLE favorites RENAME TO favorites_old") - execute(""" - CREATE TABLE IF NOT EXISTS favorites ( - id TEXT PRIMARY KEY, name TEXT NOT NULL, query TEXT NOT NULL, - keyword TEXT, folder_id TEXT, connection_id TEXT, - sort_order INTEGER NOT NULL DEFAULT 0, created_at REAL NOT NULL, updated_at REAL NOT NULL - ) - """) - execute(""" - INSERT INTO favorites SELECT id, name, query, keyword, folder_id, connection_id, - sort_order, created_at, updated_at FROM favorites_old - """) - execute("DROP TABLE favorites_old") - - execute("ALTER TABLE folders RENAME TO folders_old") - execute(""" - CREATE TABLE IF NOT EXISTS folders ( - id TEXT PRIMARY KEY, name TEXT NOT NULL, parent_id TEXT, connection_id TEXT, - sort_order INTEGER NOT NULL DEFAULT 0, created_at REAL NOT NULL, updated_at REAL NOT NULL - ) - """) - execute(""" - INSERT INTO folders SELECT id, name, parent_id, connection_id, - sort_order, created_at, updated_at FROM folders_old - """) - execute("DROP TABLE folders_old") - - execute("CREATE INDEX IF NOT EXISTS idx_favorites_connection ON favorites(connection_id);") - execute("CREATE INDEX IF NOT EXISTS idx_favorites_folder ON favorites(folder_id);") - execute("CREATE INDEX IF NOT EXISTS idx_favorites_keyword ON favorites(keyword);") - execute("CREATE UNIQUE INDEX IF NOT EXISTS idx_favorites_keyword_scope ON favorites(keyword, connection_id) WHERE keyword IS NOT NULL;") - execute("CREATE INDEX IF NOT EXISTS idx_folders_connection ON folders(connection_id);") - execute("CREATE INDEX IF NOT EXISTS idx_folders_parent ON folders(parent_id);") - - execute("DROP TRIGGER IF EXISTS favorites_ai;") - execute("DROP TRIGGER IF EXISTS favorites_ad;") - execute("DROP TRIGGER IF EXISTS favorites_au;") - execute(""" - CREATE TRIGGER IF NOT EXISTS favorites_ai AFTER INSERT ON favorites BEGIN - INSERT INTO favorites_fts(rowid, name, query, keyword) VALUES (new.rowid, new.name, new.query, new.keyword); - END; - """) - execute(""" - CREATE TRIGGER IF NOT EXISTS favorites_ad AFTER DELETE ON favorites BEGIN - INSERT INTO favorites_fts(favorites_fts, rowid, name, query, keyword) VALUES('delete', old.rowid, old.name, old.query, old.keyword); - END; - """) - execute(""" - CREATE TRIGGER IF NOT EXISTS favorites_au AFTER UPDATE ON favorites BEGIN - INSERT INTO favorites_fts(favorites_fts, rowid, name, query, keyword) VALUES('delete', old.rowid, old.name, old.query, old.keyword); - INSERT INTO favorites_fts(rowid, name, query, keyword) VALUES (new.rowid, new.name, new.query, new.keyword); - END; - """) - - execute("INSERT INTO favorites_fts(favorites_fts) VALUES('rebuild');") - - setUserVersion(2) + // Stamped inside the transaction, so a failed step rolls back to the old tables intact. + let migrated = inTransaction { () -> Bool? in + let applied = Self.version2MigrationStatements.allSatisfy { execute($0) } + return applied ? true : nil + } + if migrated == nil { + Self.logger.error("The saved queries migration to version 2 failed and was rolled back") + } } } - private func getUserVersion() -> Int32 { + private func getUserVersion() -> Int32? { var statement: OpaquePointer? defer { sqlite3_finalize(statement) } guard sqlite3_prepare_v2(db, "PRAGMA user_version", -1, &statement, nil) == SQLITE_OK, sqlite3_step(statement) == SQLITE_ROW else { - return 0 + return nil } return sqlite3_column_int(statement, 0) } @@ -242,18 +199,21 @@ internal actor SQLFavoriteStorage { // MARK: - Helper Methods - private func execute(_ sql: String) { + @discardableResult + private func execute(_ sql: String) -> Bool { var statement: OpaquePointer? + defer { sqlite3_finalize(statement) } let prepareResult = sqlite3_prepare_v2(db, sql, -1, &statement, nil) - if prepareResult == SQLITE_OK { - let stepResult = sqlite3_step(statement) - if stepResult != SQLITE_DONE && stepResult != SQLITE_ROW { - Self.logger.error("sqlite3_step failed (\(stepResult)): \(String(cString: sqlite3_errmsg(self.db)))") - } - } else { + guard prepareResult == SQLITE_OK else { Self.logger.error("sqlite3_prepare_v2 failed (\(prepareResult)): \(String(cString: sqlite3_errmsg(self.db)))") + return false + } + let stepResult = sqlite3_step(statement) + guard stepResult == SQLITE_DONE || stepResult == SQLITE_ROW else { + Self.logger.error("sqlite3_step failed (\(stepResult)): \(String(cString: sqlite3_errmsg(self.db)))") + return false } - sqlite3_finalize(statement) + return true } // MARK: - Favorite Operations diff --git a/apple/TablePro/Core/Sync/Extensions/SyncCoordinator+FullFetchDeletions.swift b/apple/TablePro/Core/Sync/Extensions/SyncCoordinator+FullFetchDeletions.swift index 31ee8e5537..f5be2f2cac 100644 --- a/apple/TablePro/Core/Sync/Extensions/SyncCoordinator+FullFetchDeletions.swift +++ b/apple/TablePro/Core/Sync/Extensions/SyncCoordinator+FullFetchDeletions.swift @@ -37,8 +37,9 @@ extension SyncCoordinator { pushed: Set ) async -> [CKRecord.ID] { let fetchedCreations = result.changedRecords.compactMap(recordCreationDate) - guard result.unreadRecordIDs.isEmpty, !fetchedCreations.isEmpty else { return [] } - let fetchedNames = Set(result.changedRecords.map(\.recordID.recordName)) + guard !fetchedCreations.isEmpty else { return [] } + // A record the fetch listed but could not read is still in the zone, so it counts as listed. + let fetchedNames = Set((result.changedRecords.map(\.recordID) + result.unreadRecordIDs).map(\.recordName)) let boundary = syncBoundary(settings: settings) var candidates: [SyncFullFetchDeletions.Candidate] = [] for (type, ids) in await localRecordIds() diff --git a/apple/TablePro/Core/Sync/Extensions/SyncCoordinator+RemoteDeletions.swift b/apple/TablePro/Core/Sync/Extensions/SyncCoordinator+RemoteDeletions.swift index e042c7bd28..47207d218b 100644 --- a/apple/TablePro/Core/Sync/Extensions/SyncCoordinator+RemoteDeletions.swift +++ b/apple/TablePro/Core/Sync/Extensions/SyncCoordinator+RemoteDeletions.swift @@ -98,12 +98,14 @@ extension SyncCoordinator { var connections = services.connectionStorage.loadConnections() let deletedIds = ids.subtracting(connections.filter(\.localOnly).map(\.id)) guard !deletedIds.isEmpty else { return true } + let removed = connections.filter { deletedIds.contains($0.id) } connections.removeAll { deletedIds.contains($0.id) } guard services.connectionStorage.saveConnections(connections, byUser: false) else { Self.logger.error("Failed to apply remote connection deletions: persistence error") return false } changeTracker.discardDirty(.connection, ids: deletedIds.map(\.uuidString)) + services.connectionStorage.deleteSecrets(of: removed, origin: .remote) ConnectionLocalState.purge( connectionIds: deletedIds, origin: .remote, diff --git a/apple/TablePro/Core/Sync/Extensions/SyncCoordinator+SavedQueriesRefill.swift b/apple/TablePro/Core/Sync/Extensions/SyncCoordinator+SavedQueriesRefill.swift new file mode 100644 index 0000000000..c462235b79 --- /dev/null +++ b/apple/TablePro/Core/Sync/Extensions/SyncCoordinator+SavedQueriesRefill.swift @@ -0,0 +1,29 @@ +import CloudKit +import Foundation +import os +import TableProSyncTransport + +// A replaced saved queries store refills only from a full fetch: a delta never resends what this Mac pulled. +extension SyncCoordinator { + private static let refillLogger = Logger(subsystem: "com.TablePro", category: "SyncSavedQueriesRefill") + + func clearTokenForSavedQueriesRefill(settings: SyncSettings) async { + guard settings.syncSQLFavorites, await services.sqlFavoriteManager.isAwaitingSyncRefill() else { return } + metadataStorage.saveToken(nil) + Self.refillLogger.info("Saved queries were set aside as damaged: cleared sync token for full fetch") + } + + // A saved query the fetch could not read never reached the store, so the refill waits for the next full fetch. + func finishSavedQueriesRefill(after result: PullResult, settings: SyncSettings) async { + guard settings.syncSQLFavorites else { return } + let leftSavedQueriesUnread = result.unreadRecordIDs.contains { recordID in + guard let type = SyncRecordMapper.parse(recordName: recordID.recordName)?.type else { return false } + return type == .favorite || type == .favoriteFolder + } + guard !leftSavedQueriesUnread else { + Self.refillLogger.info("A full fetch left saved queries unread: the refill waits for the next one") + return + } + await services.sqlFavoriteManager.finishSyncRefill() + } +} diff --git a/apple/TablePro/Core/Sync/SyncCoordinator.swift b/apple/TablePro/Core/Sync/SyncCoordinator.swift index 3cb725ec6b..a1734725af 100644 --- a/apple/TablePro/Core/Sync/SyncCoordinator.swift +++ b/apple/TablePro/Core/Sync/SyncCoordinator.swift @@ -686,6 +686,7 @@ final class SyncCoordinator: ObservableObject { // Applied with the categories recorded here, so one turned off during the download is still read once. let settings = services.appSettingsStorage.loadSync() adoptReadableContent(for: settings) + await clearTokenForSavedQueriesRefill(settings: settings) let token = metadataStorage.loadToken() let tokenStatus = token == nil ? "nil (full fetch)" : "present (delta)" Self.logger.info("Pull starting, token: \(tokenStatus)") @@ -722,6 +723,7 @@ final class SyncCoordinator: ObservableObject { } if isFullFetch { seedLocalThemesIfPending(after: result) + await finishSavedQueriesRefill(after: result, settings: settings) } return true } diff --git a/apple/TablePro/Resources/Localizable.xcstrings b/apple/TablePro/Resources/Localizable.xcstrings index 4ba0fefbde..543ff28e9b 100644 --- a/apple/TablePro/Resources/Localizable.xcstrings +++ b/apple/TablePro/Resources/Localizable.xcstrings @@ -37160,6 +37160,15 @@ } } } + }, + "ChatGPT sign-in failed: %@" : { + + }, + "ChatGPT sign-in was denied: %@" : { + + }, + "ChatGPT sign-in was denied." : { + }, "Check" : { @@ -141433,6 +141442,9 @@ } } } + }, + "Query History Was Damaged" : { + }, "Query Insights" : { "localizations" : { @@ -158258,6 +158270,9 @@ }, "Saved queries import for every connection, in a folder named “%@”." : { + }, + "Saved Queries Were Damaged" : { + }, "Saved Query" : { "localizations" : { @@ -181426,6 +181441,12 @@ }, "TablePro could not reach the Schema Registry: %@" : { + }, + "TablePro could not read its query history, so it moved the file aside as “%@” and started a new one. The copy stays until you delete it or clear all of your history." : { + + }, + "TablePro could not read its saved queries, so it moved the file aside as “%@” and started an empty one. Nothing in that file was deleted." : { + }, "TablePro could not read its Table Health history for this connection." : { @@ -213414,6 +213435,9 @@ }, "Vault returned a sign-in page that is not an https address, so TablePro did not open it." : { + }, + "Vault returned a sign-in page without a state, so TablePro could not check the browser's answer." : { + }, "Vault returned no sign-in page. The OIDC role must allow the redirect URI http://localhost:8250/oidc/callback." : { diff --git a/apple/TablePro/Theme/ThemeEngine.swift b/apple/TablePro/Theme/ThemeEngine.swift index 4c773ce87d..60126547c1 100644 --- a/apple/TablePro/Theme/ThemeEngine.swift +++ b/apple/TablePro/Theme/ThemeEngine.swift @@ -6,13 +6,6 @@ import SwiftUI import TableProEditorKit import TableProTheme -internal enum DataGridFontVariant { - static let regular = 0 - static let italic = 1 - static let medium = 2 - static let rowNumber = 3 -} - internal struct EditorFontCache: Equatable { internal let font: NSFont internal let lineNumberFont: NSFont diff --git a/apple/TablePro/ViewModels/AIChatViewModel+Streaming.swift b/apple/TablePro/ViewModels/AIChatViewModel+Streaming.swift index 208ee830fb..9bbc597baa 100644 --- a/apple/TablePro/ViewModels/AIChatViewModel+Streaming.swift +++ b/apple/TablePro/ViewModels/AIChatViewModel+Streaming.swift @@ -663,6 +663,12 @@ extension AIChatViewModel { streamingTask = nil } + nonisolated static let unparsableToolArgumentsContent = """ + This tool call did not run: its arguments are not a complete JSON object, which usually means the \ + response hit the output token limit. Call the tool again with complete arguments. + """ + + // A call whose arguments do not parse comes back already denied, so it never reaches approval or execution. nonisolated static func assembleToolUseBlocks( order: [String], names: [String: String], @@ -671,25 +677,39 @@ extension AIChatViewModel { ) -> [ToolUseBlock] { order.compactMap { id -> ToolUseBlock? in guard let name = names[id] else { return nil } - let inputString = inputs[id] ?? "{}" - let inputValue: JsonValue - if inputString.isEmpty { - inputValue = .object([:]) - } else if let data = inputString.data(using: .utf8), - let decoded = try? JSONDecoder().decode(JsonValue.self, from: data) { - inputValue = decoded - } else { - inputValue = .object([:]) + guard let arguments = toolArguments(from: inputs[id] ?? "") else { + return ToolUseBlock( + id: id, + name: name, + input: .object([:]), + approvalState: .denied(reason: unparsableToolArgumentsContent), + providerMetadata: metadata[id] + ) } return ToolUseBlock( id: id, name: name, - input: inputValue, + input: arguments, providerMetadata: metadata[id] ) } } + // Every provider sends a call's arguments as one JSON object; no arguments at all reads as an empty one. + nonisolated private static func toolArguments(from raw: String) -> JsonValue? { + let trimmed = raw.trimmingCharacters(in: .whitespacesAndNewlines) + guard !trimmed.isEmpty else { return .object([:]) } + guard let decoded = try? JSONDecoder().decode(JsonValue.self, from: Data(trimmed.utf8)) else { return nil } + switch decoded { + case .object: + return decoded + case .null: + return .object([:]) + case .bool, .int, .double, .string, .array: + return nil + } + } + nonisolated static func executeToolUses( _ blocks: [ToolUseBlock], scope: ChatToolScope, diff --git a/apple/TablePro/ViewModels/AIChatViewModel+ToolApproval.swift b/apple/TablePro/ViewModels/AIChatViewModel+ToolApproval.swift index fb13b16d62..3ff38336b6 100644 --- a/apple/TablePro/ViewModels/AIChatViewModel+ToolApproval.swift +++ b/apple/TablePro/ViewModels/AIChatViewModel+ToolApproval.swift @@ -44,7 +44,12 @@ extension AIChatViewModel { let initialBlocks = await MainActor.run { [weak self] () -> [ToolUseBlock] in guard let self else { return assembledBlocks } let initial = assembledBlocks.map { block -> ToolUseBlock in - let state = self.computeInitialApprovalState(for: block.name, registry: registry) + let state: ToolApprovalState + if case .denied = block.approvalState { + state = block.approvalState + } else { + state = self.computeInitialApprovalState(for: block.name, registry: registry) + } return ToolUseBlock( id: block.id, name: block.name, diff --git a/apple/TableProMobile/TableProMobile/Coordinators/ConnectionCoordinator.swift b/apple/TableProMobile/TableProMobile/Coordinators/ConnectionCoordinator.swift index 8555d70ad1..cf501acb3f 100644 --- a/apple/TableProMobile/TableProMobile/Coordinators/ConnectionCoordinator.swift +++ b/apple/TableProMobile/TableProMobile/Coordinators/ConnectionCoordinator.swift @@ -71,6 +71,7 @@ final class ConnectionCoordinator { var selectedTable: TableInfo? private(set) var queryHistory: [QueryHistoryItem] = [] + private(set) var keepsDamagedHistoryCopy = false private var historyStorage: QueryHistoryStorage { appState.queryHistory } private let appState: AppState @@ -1179,6 +1180,7 @@ final class ConnectionCoordinator { func loadHistory() { queryHistory = historyStorage.load(for: connection.id) + keepsDamagedHistoryCopy = historyStorage.hasSetAsideCopies } func addHistoryItem(_ item: QueryHistoryItem) { @@ -1192,8 +1194,9 @@ final class ConnectionCoordinator { } func clearHistory() { - historyStorage.clearAll(for: [connection.id]) + historyStorage.clearAll(for: [connection.id], removingSetAsideCopies: keepsDamagedHistoryCopy) queryHistory = [] + keepsDamagedHistoryCopy = historyStorage.hasSetAsideCopies } func navigateToPendingTable() { diff --git a/apple/TableProMobile/TableProMobile/Helpers/QueryHistoryStorage.swift b/apple/TableProMobile/TableProMobile/Helpers/QueryHistoryStorage.swift index 61b791ab28..3675636728 100644 --- a/apple/TableProMobile/TableProMobile/Helpers/QueryHistoryStorage.swift +++ b/apple/TableProMobile/TableProMobile/Helpers/QueryHistoryStorage.swift @@ -1,4 +1,5 @@ import Foundation +import os nonisolated struct QueryHistoryItem: Identifiable, Codable, Hashable { let id: UUID @@ -37,6 +38,7 @@ nonisolated struct QueryHistoryItem: Identifiable, Codable, Hashable { nonisolated struct QueryHistoryStorage { private static let maxEntries = 200 + private static let logger = Logger(subsystem: "com.TablePro", category: "QueryHistoryStorage") private let directory: URL @@ -50,7 +52,7 @@ nonisolated struct QueryHistoryStorage { @discardableResult func save(_ item: QueryHistoryItem) -> Bool { - var items = loadAll() + guard var items = readAll() else { return false } if let last = items.last, last.query == item.query, last.connectionId == item.connectionId, @@ -65,11 +67,7 @@ nonisolated struct QueryHistoryStorage { } func loadAll() -> [QueryHistoryItem] { - guard let data = try? Data(contentsOf: fileURL), - let items = try? JSONDecoder().decode([QueryHistoryItem].self, from: data) else { - return [] - } - return items + readAll() ?? [] } func load(for connectionId: UUID) -> [QueryHistoryItem] { @@ -77,16 +75,71 @@ nonisolated struct QueryHistoryStorage { } func delete(_ id: UUID) { - var items = loadAll() + guard var items = readAll() else { return } items.removeAll { $0.id == id } writeAll(items) } - func clearAll(for connectionIds: Set) { - let items = loadAll() + // The set-aside copy holds every connection's history: only a clear asked for after the footer named it removes it. + func clearAll(for connectionIds: Set, removingSetAsideCopies: Bool = false) { + guard let items = readAll() else { return } let kept = items.filter { !connectionIds.contains($0.connectionId) } - guard kept.count != items.count else { return } - writeAll(kept) + if kept.count != items.count, !writeAll(kept) { return } + if removingSetAsideCopies { + removeSetAsideCopies() + } + } + + var hasSetAsideCopies: Bool { + !setAsideCopies().isEmpty + } + + private func setAsideCopies() -> [URL] { + let names = (try? FileManager.default.contentsOfDirectory(atPath: directory.path(percentEncoded: false))) ?? [] + return names + .filter { $0.hasPrefix("query-history.damaged-") } + .map { directory.appendingPathComponent($0) } + } + + private func removeSetAsideCopies() { + for copy in setAsideCopies() { + do { + try FileManager.default.removeItem(at: copy) + } catch { + Self.logger.error("A set-aside query history copy could not be deleted: \(error.localizedDescription, privacy: .private)") + } + } + } + + // nil while the file cannot be read (a locked device), so no write replaces it with a partial list. + private func readAll() -> [QueryHistoryItem]? { + guard FileManager.default.fileExists(atPath: fileURL.path(percentEncoded: false)) else { return [] } + guard let data = try? Data(contentsOf: fileURL) else { return nil } + if let items = try? JSONDecoder().decode([QueryHistoryItem].self, from: data) { + return items + } + return setAsideUndecodableFile() ? [] : nil + } + + private func setAsideUndecodableFile() -> Bool { + let formatter = DateFormatter() + formatter.locale = Locale(identifier: "en_US_POSIX") + formatter.dateFormat = "yyyyMMdd-HHmmss" + let stamp = formatter.string(from: Date()) + var attempt = 0 + var destination = directory.appendingPathComponent("query-history.damaged-\(stamp).json") + while FileManager.default.fileExists(atPath: destination.path(percentEncoded: false)) { + attempt += 1 + destination = directory.appendingPathComponent("query-history.damaged-\(stamp)-\(attempt).json") + } + do { + try FileManager.default.moveItem(at: fileURL, to: destination) + Self.logger.fault("Query history did not decode and was set aside") + return true + } catch { + Self.logger.error("Query history did not decode and could not be set aside: \(error.localizedDescription, privacy: .private)") + return false + } } @discardableResult diff --git a/apple/TableProMobile/TableProMobile/Localizable.xcstrings b/apple/TableProMobile/TableProMobile/Localizable.xcstrings index d76dcd4161..e504e0f12a 100644 --- a/apple/TableProMobile/TableProMobile/Localizable.xcstrings +++ b/apple/TableProMobile/TableProMobile/Localizable.xcstrings @@ -1784,6 +1784,9 @@ }, "A cursor has no .%@() modifier." : { + }, + "A damaged history file from every connection was kept on this device. Clear All History deletes it." : { + }, "A database name must be shorter than %ld bytes." : { diff --git a/apple/TableProMobile/TableProMobile/Views/QueryHistoryView.swift b/apple/TableProMobile/TableProMobile/Views/QueryHistoryView.swift index cf189a40a3..a253415db8 100644 --- a/apple/TableProMobile/TableProMobile/Views/QueryHistoryView.swift +++ b/apple/TableProMobile/TableProMobile/Views/QueryHistoryView.swift @@ -50,11 +50,15 @@ struct QueryHistoryView: View { } } - if !coordinator.queryHistory.isEmpty { + if !coordinator.queryHistory.isEmpty || coordinator.keepsDamagedHistoryCopy { Section { Button(String(localized: "Clear All History"), role: .destructive) { showClearConfirmation = true } + } footer: { + if coordinator.keepsDamagedHistoryCopy { + Text("A damaged history file from every connection was kept on this device. Clear All History deletes it.") + } } } } @@ -65,7 +69,7 @@ struct QueryHistoryView: View { } } .overlay { - if coordinator.queryHistory.isEmpty { + if coordinator.queryHistory.isEmpty, !coordinator.keepsDamagedHistoryCopy { ContentUnavailableView( "No History", systemImage: "clock", diff --git a/apple/TableProMobile/TableProMobileTests/QueryHistoryStorageDamageTests.swift b/apple/TableProMobile/TableProMobileTests/QueryHistoryStorageDamageTests.swift new file mode 100644 index 0000000000..11b45ce935 --- /dev/null +++ b/apple/TableProMobile/TableProMobileTests/QueryHistoryStorageDamageTests.swift @@ -0,0 +1,91 @@ +import Foundation +import Testing +@testable import TableProMobile + +struct QueryHistoryStorageDamageTests { + private let directory: URL + + init() throws { + directory = FileManager.default.temporaryDirectory + .appendingPathComponent("tablepro-tests") + .appendingPathComponent("ios-history-damage-\(UUID().uuidString)", isDirectory: true) + try FileManager.default.createDirectory(at: directory, withIntermediateDirectories: true) + } + + private var historyURL: URL { + directory.appendingPathComponent("query-history.json") + } + + private func setAsideCopies() throws -> [URL] { + try FileManager.default.contentsOfDirectory(at: directory, includingPropertiesForKeys: nil) + .filter { $0.lastPathComponent.hasPrefix("query-history.damaged-") } + } + + @Test("A history file that does not decode is set aside, not written over") + func undecodableFileIsSetAside() throws { + let damaged = Data("[{\"id\": \"not-a-uuid\"".utf8) + try damaged.write(to: historyURL) + let storage = QueryHistoryStorage(directory: directory) + + #expect(storage.save(QueryHistoryItem(query: "SELECT 1", connectionId: UUID()))) + + let copies = try setAsideCopies() + #expect(copies.count == 1) + #expect(try copies.first.map { try Data(contentsOf: $0) } == damaged) + #expect(storage.loadAll().map(\.query) == ["SELECT 1"]) + } + + @Test("Only a clear that asks for it deletes the set-aside copy, even once no history is left") + func onlyAnExplicitClearDeletesTheSetAsideCopy() throws { + try Data("not json".utf8).write(to: historyURL) + let storage = QueryHistoryStorage(directory: directory) + let first = UUID() + let second = UUID() + #expect(storage.save(QueryHistoryItem(query: "SELECT 1", connectionId: first))) + #expect(storage.save(QueryHistoryItem(query: "SELECT 2", connectionId: second))) + #expect(storage.hasSetAsideCopies) + + storage.clearAll(for: [first]) + storage.clearAll(for: [second]) + #expect(storage.loadAll().isEmpty) + #expect(storage.hasSetAsideCopies) + + storage.clearAll(for: [first], removingSetAsideCopies: true) + #expect(!storage.hasSetAsideCopies) + #expect(try setAsideCopies().isEmpty) + } + + @Test("Deleting a connection keeps the set-aside copy, which holds every connection's history") + @MainActor + func deletingAConnectionKeepsTheSetAsideCopy() throws { + try Data("not json".utf8).write(to: historyURL) + let storage = QueryHistoryStorage(directory: directory) + let deleted = UUID() + #expect(storage.save(QueryHistoryItem(query: "SELECT 1", connectionId: deleted))) + let suite = "QueryHistoryStorageDamageTests.\(UUID().uuidString)" + let defaults = try #require(UserDefaults(suiteName: suite)) + defer { defaults.removePersistentDomain(forName: suite) } + let localState = ConnectionLocalState( + secrets: ConnectionSecrets(secureStore: MockSecureStore()), + queryHistory: storage, + defaults: defaults + ) + + localState.purge([deleted]) + + #expect(storage.loadAll().isEmpty) + #expect(storage.hasSetAsideCopies) + #expect(try setAsideCopies().count == 1) + } + + @Test("A history file that decodes is kept and appended to") + func readableFileIsKept() throws { + let storage = QueryHistoryStorage(directory: directory) + #expect(storage.save(QueryHistoryItem(query: "SELECT 1", connectionId: UUID()))) + + #expect(storage.save(QueryHistoryItem(query: "SELECT 2", connectionId: UUID()))) + + #expect(try setAsideCopies().isEmpty) + #expect(storage.loadAll().map(\.query) == ["SELECT 1", "SELECT 2"]) + } +} diff --git a/apple/TableProTests/Core/AI/AssembleToolUseBlocksTests.swift b/apple/TableProTests/Core/AI/AssembleToolUseBlocksTests.swift index 31b68b6a9e..e2008f4af7 100644 --- a/apple/TableProTests/Core/AI/AssembleToolUseBlocksTests.swift +++ b/apple/TableProTests/Core/AI/AssembleToolUseBlocksTests.swift @@ -62,7 +62,7 @@ struct AssembleToolUseBlocksTests { #expect(blocks[0].id == "call_2") } - @Test("Malformed JSON falls back to empty object") + @Test("Malformed JSON is denied with an explanation instead of running with empty arguments") func malformedJSON() { let blocks = AIChatViewModel.assembleToolUseBlocks( order: ["call_1"], @@ -71,5 +71,42 @@ struct AssembleToolUseBlocksTests { ) #expect(blocks.count == 1) #expect(blocks[0].input == .object([:])) + #expect(blocks[0].approvalState == .denied(reason: AIChatViewModel.unparsableToolArgumentsContent)) + } + + @Test("Arguments cut off at the output limit are denied, and the calls around them are not") + func truncatedArguments() { + let blocks = AIChatViewModel.assembleToolUseBlocks( + order: ["call_1", "call_2"], + names: ["call_1": "execute_query", "call_2": "list_tables"], + inputs: ["call_1": #"{"query":"SELECT * FROM orders WHERE"#, "call_2": #"{}"#] + ) + #expect(blocks.count == 2) + #expect(blocks[0].approvalState == .denied(reason: AIChatViewModel.unparsableToolArgumentsContent)) + #expect(blocks[1].approvalState == .approved) + #expect(blocks[1].input == .object([:])) + } + + @Test("Arguments that parse to something other than an object are denied", arguments: [#""users""#, "42", "[1]", "true"]) + func nonObjectArguments(raw: String) { + let blocks = AIChatViewModel.assembleToolUseBlocks( + order: ["call_1"], + names: ["call_1": "describe_table"], + inputs: ["call_1": raw] + ) + #expect(blocks.count == 1) + #expect(blocks[0].approvalState == .denied(reason: AIChatViewModel.unparsableToolArgumentsContent)) + } + + @Test("A null or blank argument string reads as a call with no arguments", arguments: ["null", " "]) + func nullArgumentsAreEmpty(raw: String) { + let blocks = AIChatViewModel.assembleToolUseBlocks( + order: ["call_1"], + names: ["call_1": "list_tables"], + inputs: ["call_1": raw] + ) + #expect(blocks.count == 1) + #expect(blocks[0].input == .object([:])) + #expect(blocks[0].approvalState == .approved) } } diff --git a/apple/TableProTests/Core/AI/ChatGPTCodexCallbackServerTests.swift b/apple/TableProTests/Core/AI/ChatGPTCodexCallbackServerTests.swift new file mode 100644 index 0000000000..03160cd745 --- /dev/null +++ b/apple/TableProTests/Core/AI/ChatGPTCodexCallbackServerTests.swift @@ -0,0 +1,137 @@ +// +// ChatGPTCodexCallbackServerTests.swift +// TableProTests +// + +import Foundation +@testable import TablePro +import Testing + +struct ChatGPTCodexCallbackTests { + private static let state = "expected-state" + + private static func head(_ target: String, method: String = "GET") -> String { + "\(method) \(target) HTTP/1.1\r\nHost: localhost:1455\r\n\r\n" + } + + private static func parse(_ head: String) -> ChatGPTCodexCallback { + ChatGPTCodexCallback.parse(requestHead: head, expectedState: state) + } + + @Test("The redirect carrying this sign-in's state yields its code") + func matchingStateYieldsTheCode() { + #expect(Self.parse(Self.head("/auth/callback?code=abc&state=expected-state")) == .code("abc")) + } + + @Test("A request that is not this sign-in's redirect is ignored", arguments: [ + "/auth/callback?code=abc&state=forged", + "/auth/callback?code=abc", + "/auth/callback?code=abc&state=expected-state&state=expected-state", + "/auth/callback?error=access_denied&state=forged", + "/auth/callback?error=access_denied", + "/auth/callback?state=expected-state", + "/favicon.ico", + "/?code=abc&state=expected-state", + "/auth/callback/extra?code=abc&state=expected-state" + ]) + func strayRequestIsIgnored(target: String) { + #expect(Self.parse(Self.head(target)) == .ignore) + } + + @Test("Only a GET in origin form is read") + func otherRequestShapesAreIgnored() { + #expect(Self.parse(Self.head("/auth/callback?code=abc&state=expected-state", method: "POST")) == .ignore) + #expect(Self.parse("GET http://localhost:1455/auth/callback?code=abc&state=expected-state HTTP/1.1\r\n\r\n") == .ignore) + #expect(Self.parse("") == .ignore) + } + + @Test("An empty expected state matches nothing") + func emptyExpectedStateMatchesNothing() { + let head = Self.head("/auth/callback?code=abc&state=") + #expect(ChatGPTCodexCallback.parse(requestHead: head, expectedState: "") == .ignore) + } + + @Test("An error redirect with this sign-in's state is a denial carrying the server's reason") + func errorRedirectIsADenial() { + let head = Self.head("/auth/callback?error=access_denied&error_description=User+denied+access%2E&state=expected-state") + #expect(Self.parse(head) == .denied(error: "access_denied", description: "User denied access.")) + } + + @Test("An error sent beside a code wins, so the code is never exchanged") + func errorWinsOverCode() { + let head = Self.head("/auth/callback?code=abc&error=server_error&state=expected-state") + #expect(Self.parse(head) == .denied(error: "server_error", description: nil)) + } + + @Test("The server's error text keeps only the characters RFC 6749 allows") + func errorTextIsSanitized() { + let head = Self.head("/auth/callback?error=%22%0Abad%5C&error_description=a%0D%0Ab%22c&state=expected-state") + #expect(Self.parse(head) == .denied(error: "bad", description: "abc")) + } + + @Test("A denial tells the user what the server said") + func denialMessages() { + typealias ServerError = ChatGPTCodexCallbackServer.ServerError + let bare = ServerError.authorizationDenied(error: "access_denied", description: nil).errorDescription + let explained = ServerError.authorizationDenied(error: "access_denied", description: "No Codex access").errorDescription + let other = ServerError.authorizationDenied(error: "server_error", description: nil).errorDescription + #expect(bare?.isEmpty == false) + #expect(explained?.contains("No Codex access") == true) + #expect(other?.contains("server_error") == true) + } +} + +struct ChatGPTCodexCallbackServerTests { + private static let state = "expected-state" + + private static func status(of target: String, port: UInt16) async -> Int? { + guard let url = URL(string: "http://127.0.0.1:\(port)\(target)") else { return nil } + let session = URLSession(configuration: .ephemeral) + defer { session.finishTasksAndInvalidate() } + let request = URLRequest(url: url, timeoutInterval: 5) + guard let (_, response) = try? await session.data(for: request), + let httpResponse = response as? HTTPURLResponse + else { + return nil + } + return httpResponse.statusCode + } + + @Test("Stray requests are answered and ignored, and the real redirect still completes the sign-in") + func strayRequestsDoNotEndTheSignIn() async throws { + let server = ChatGPTCodexCallbackServer(expectedState: Self.state, port: 0) + let port = try await server.start() + defer { server.stop() } + let code = Task { try await server.waitForCode() } + + let forged = await Self.status(of: "/auth/callback?code=forged&state=wrong", port: port) + let deniedElsewhere = await Self.status(of: "/auth/callback?error=access_denied&state=wrong", port: port) + let favicon = await Self.status(of: "/favicon.ico", port: port) + let redirect = await Self.status(of: "/auth/callback?code=real-code&state=\(Self.state)", port: port) + + #expect([forged, deniedElsewhere, favicon, redirect] == [400, 400, 400, 200]) + #expect(try await code.value == "real-code") + } + + @Test("An error redirect ends the sign-in at once with the server's reason") + func errorRedirectFailsAtOnce() async throws { + let server = ChatGPTCodexCallbackServer(expectedState: Self.state, port: 0) + let port = try await server.start() + defer { server.stop() } + let code = Task { try await server.waitForCode() } + + let redirect = await Self.status( + of: "/auth/callback?error=access_denied&error_description=User+denied+access&state=\(Self.state)", + port: port + ) + + #expect(redirect == 200) + let expected = ChatGPTCodexCallbackServer.ServerError.authorizationDenied( + error: "access_denied", + description: "User denied access" + ) + await #expect(throws: expected) { + try await code.value + } + } +} diff --git a/apple/TableProTests/Core/Database/Vault/VaultAuthenticatorTests.swift b/apple/TableProTests/Core/Database/Vault/VaultAuthenticatorTests.swift index 7f97f034c6..db1320948d 100644 --- a/apple/TableProTests/Core/Database/Vault/VaultAuthenticatorTests.swift +++ b/apple/TableProTests/Core/Database/Vault/VaultAuthenticatorTests.swift @@ -153,7 +153,7 @@ struct VaultAuthenticatorTests { @Test("OIDC sign-in opens the browser, trades the callback code for a token and keeps it for the launch") func oidcSignIn() async throws { let transport = VaultFakeTransport() - transport.reply("POST", "auth/oidc/oidc/auth_url", #"{"data":{"auth_url":"https://idp.test/authorize?x=1"}}"#) + transport.reply("POST", "auth/oidc/oidc/auth_url", #"{"data":{"auth_url":"https://idp.test/authorize?x=1&state=st"}}"#) transport.reply("GET", "auth/oidc/oidc/callback", VaultFixtures.authReply(token: "oidc-token", ttl: 3_600)) let receiver = VaultFakeCallbackReceiver(parameters: ["state": "st", "code": "cd"]) let opened = VaultFixtures.OpenedURLs() @@ -165,8 +165,9 @@ struct VaultAuthenticatorTests { #expect(token.value == "oidc-token") #expect(again == token) - #expect(opened.all.map(\.absoluteString) == ["https://idp.test/authorize?x=1"]) + #expect(opened.all.map(\.absoluteString) == ["https://idp.test/authorize?x=1&state=st"]) #expect(receiver.wasStopped) + #expect(receiver.expectedStates == ["st"]) let authURLRequest = try #require(transport.requests(to: "auth/oidc/oidc/auth_url").first) #expect(authURLRequest.body["redirect_uri"] == "http://localhost:8250/oidc/callback") #expect(authURLRequest.body["role"] == "dba") @@ -179,7 +180,7 @@ struct VaultAuthenticatorTests { @Test("An identity provider error ends the OIDC sign-in with its description") func oidcProviderError() async { let transport = VaultFakeTransport() - transport.reply("POST", "auth/oidc/oidc/auth_url", #"{"data":{"auth_url":"https://idp.test/authorize"}}"#) + transport.reply("POST", "auth/oidc/oidc/auth_url", #"{"data":{"auth_url":"https://idp.test/authorize?state=st"}}"#) let receiver = VaultFakeCallbackReceiver(parameters: ["error": "access_denied", "error_description": "User cancelled"]) let authenticator = VaultFixtures.authenticator(transport: transport, receiver: receiver) @@ -213,7 +214,7 @@ struct VaultAuthenticatorTests { @Test("A local identity provider over plain http on this Mac still opens") func oidcAllowsLoopbackSignInPage() async throws { let transport = VaultFakeTransport() - transport.reply("POST", "auth/oidc/oidc/auth_url", #"{"data":{"auth_url":"http://127.0.0.1:5556/auth?x=1"}}"#) + transport.reply("POST", "auth/oidc/oidc/auth_url", #"{"data":{"auth_url":"http://127.0.0.1:5556/auth?x=1&state=st"}}"#) transport.reply("GET", "auth/oidc/oidc/callback", VaultFixtures.authReply(token: "oidc-token")) let receiver = VaultFakeCallbackReceiver(parameters: ["state": "st", "code": "cd"]) let opened = VaultFixtures.OpenedURLs() @@ -222,18 +223,79 @@ struct VaultAuthenticatorTests { let token = try await authenticator.token(for: .oidc(mount: "oidc", role: ""), endpoint: endpoint, interactive: true) #expect(token.value == "oidc-token") - #expect(opened.all.map { $0.absoluteString } == ["http://127.0.0.1:5556/auth?x=1"]) + #expect(opened.all.map { $0.absoluteString } == ["http://127.0.0.1:5556/auth?x=1&state=st"]) + } + + @Test("A sign-in page without a state is refused before the browser opens anything") + func oidcRefusesSignInPageWithoutState() async { + let transport = VaultFakeTransport() + transport.reply("POST", "auth/oidc/oidc/auth_url", #"{"data":{"auth_url":"https://idp.test/authorize?x=1"}}"#) + let receiver = VaultFakeCallbackReceiver(parameters: ["state": "st", "code": "cd"]) + let opened = VaultFixtures.OpenedURLs() + let authenticator = VaultFixtures.authenticator(transport: transport, receiver: receiver, opened: opened) + + await #expect(throws: VaultError.self) { + try await authenticator.token(for: .oidc(mount: "oidc", role: ""), endpoint: endpoint, interactive: true) + } + #expect(opened.all.isEmpty) + #expect(receiver.expectedStates.isEmpty) + #expect(transport.requests(to: "auth/oidc/oidc/callback").isEmpty) } - @Test("The callback listener hands back the query of GET /oidc/callback only") + @Test("The callback listener hands back the query of GET /oidc/callback carrying the expected state") func loopbackParsesCallback() { let head = Data("GET /oidc/callback?code=abc&state=xyz HTTP/1.1\r\nHost: localhost:8250\r\n\r\n".utf8) - #expect(VaultOIDCLoopback.callbackParameters(in: head) == ["code": "abc", "state": "xyz"]) + #expect(VaultOIDCLoopback.callbackParameters(in: head, expectedState: "xyz") == ["code": "abc", "state": "xyz"]) + + let denied = Data("GET /oidc/callback?error=access_denied&error_description=User+cancelled&state=xyz HTTP/1.1\r\n\r\n".utf8) + #expect( + VaultOIDCLoopback.callbackParameters(in: denied, expectedState: "xyz") + == ["error": "access_denied", "error_description": "User cancelled", "state": "xyz"] + ) let favicon = Data("GET /favicon.ico HTTP/1.1\r\nHost: localhost:8250\r\n\r\n".utf8) - #expect(VaultOIDCLoopback.callbackParameters(in: favicon) == nil) + #expect(VaultOIDCLoopback.callbackParameters(in: favicon, expectedState: "xyz") == nil) - let partial = Data("GET /oidc/callback?code=abc HTTP/1.1\r\nHost: local".utf8) - #expect(VaultOIDCLoopback.callbackParameters(in: partial) == nil) + let partial = Data("GET /oidc/callback?code=abc&state=xyz HTTP/1.1\r\nHost: local".utf8) + #expect(VaultOIDCLoopback.callbackParameters(in: partial, expectedState: "xyz") == nil) + } + + @Test("A callback without this sign-in's state is ignored", arguments: [ + "/oidc/callback?code=abc&state=forged", + "/oidc/callback?code=abc", + "/oidc/callback?code=abc&state=xyz&state=xyz", + "/oidc/callback?error=access_denied&state=forged", + "/oidc/callback?error=access_denied" + ]) + func loopbackIgnoresStrayCallback(target: String) { + let head = Data("GET \(target) HTTP/1.1\r\nHost: localhost:8250\r\n\r\n".utf8) + #expect(VaultOIDCLoopback.callbackParameters(in: head, expectedState: "xyz") == nil) + } + + @Test("A stray callback is answered and ignored, and the real one still ends the wait") + func loopbackKeepsListeningAfterAStrayCallback() async throws { + let loopback = VaultOIDCLoopback(expectedState: "xyz", port: 0) + let port = try await loopback.startListening() + defer { loopback.stop() } + let parameters = Task { try await loopback.callback(timeout: .seconds(30)) } + + let forged = await Self.status(of: "/oidc/callback?code=forged&state=wrong", port: port) + let redirect = await Self.status(of: "/oidc/callback?code=real&state=xyz", port: port) + + #expect([forged, redirect] == [400, 200]) + #expect(try await parameters.value == ["code": "real", "state": "xyz"]) + } + + private static func status(of target: String, port: UInt16) async -> Int? { + guard let url = URL(string: "http://127.0.0.1:\(port)\(target)") else { return nil } + let session = URLSession(configuration: .ephemeral) + defer { session.finishTasksAndInvalidate() } + let request = URLRequest(url: url, timeoutInterval: 5) + guard let (_, response) = try? await session.data(for: request), + let httpResponse = response as? HTTPURLResponse + else { + return nil + } + return httpResponse.statusCode } } diff --git a/apple/TableProTests/Core/Database/Vault/VaultFakeTransport.swift b/apple/TableProTests/Core/Database/Vault/VaultFakeTransport.swift index 2fc57da8a5..ebe1bfcae0 100644 --- a/apple/TableProTests/Core/Database/Vault/VaultFakeTransport.swift +++ b/apple/TableProTests/Core/Database/Vault/VaultFakeTransport.swift @@ -69,6 +69,7 @@ final class VaultFakeCallbackReceiver: VaultOIDCCallbackReceiving, @unchecked Se private let parameters: [String: String] private let lock = NSLock() private var stopped = false + private var states: [String] = [] init(parameters: [String: String]) { self.parameters = parameters @@ -76,6 +77,13 @@ final class VaultFakeCallbackReceiver: VaultOIDCCallbackReceiving, @unchecked Se var wasStopped: Bool { lock.withLock { stopped } } + var expectedStates: [String] { lock.withLock { states } } + + func expect(state: String) -> VaultFakeCallbackReceiver { + lock.withLock { states.append(state) } + return self + } + func start() async throws {} func callback(timeout: Duration) async throws -> [String: String] { parameters } @@ -118,7 +126,7 @@ enum VaultFixtures { readFile: { path in files.first { path.hasSuffix($0.key) }?.value }, now: now, openURL: { opened.append($0) }, - makeCallbackReceiver: { receiver } + makeCallbackReceiver: { receiver.expect(state: $0) } ) } diff --git a/apple/TableProTests/Core/Diagnostics/TableLoadTracerSinkTests.swift b/apple/TableProTests/Core/Diagnostics/TableLoadTracerSinkTests.swift index e726c64734..64cd3786e0 100644 --- a/apple/TableProTests/Core/Diagnostics/TableLoadTracerSinkTests.swift +++ b/apple/TableProTests/Core/Diagnostics/TableLoadTracerSinkTests.swift @@ -1,10 +1,6 @@ -// -// TableLoadTracerSinkTests.swift -// TableProTests -// - import Foundation @testable import TablePro +import TableProTestSupport import Testing private final class RecordingSink: TableLoadSummarySink, @unchecked Sendable { @@ -152,4 +148,23 @@ struct TableLoadTracerSinkTests { tracer.finish(token: stranger, outcome: .completed) #expect(sink.records.isEmpty) } + + @Test("The shared tracer keeps a finished load in the history file, under a test host as in the app") + func sharedTracerRecordsToTheHistoryFile() async throws { + let marker = "TracerProbe-\(UUID().uuidString)" + let tracer = TableLoadTracer.shared + let token = tracer.begin( + tabId: UUID(), + table: "users", + origin: .sidebar, + environment: TableLoadEnvironment(databaseTypeId: marker, usesSSH: false, openTabCount: 1) + ) + tracer.finish(token: token, outcome: .completed) + + let fileURL = TableLoadHistoryStore.defaultFileURL() + try await waitUntil { + let data = (try? Data(contentsOf: fileURL)) ?? Data() + return String(decoding: data, as: UTF8.self).contains(marker) + } + } } diff --git a/apple/TableProTests/Core/MCP/MCPAuditChainTests.swift b/apple/TableProTests/Core/MCP/MCPAuditChainTests.swift index 732eaaecfd..1ca36bbb94 100644 --- a/apple/TableProTests/Core/MCP/MCPAuditChainTests.swift +++ b/apple/TableProTests/Core/MCP/MCPAuditChainTests.swift @@ -1,12 +1,3 @@ -// -// MCPAuditChainTests.swift -// TableProTests -// -// Every row hashes its own fields, its sequence number and the hash before it, so the log is only -// evidence while that chain holds. Editing a row, reordering two rows and deleting one all break -// it, and `verify()` names the sequence where the break starts. -// - import Foundation import SQLite3 import TableProPluginKit @@ -15,6 +6,13 @@ import Testing @testable import TablePro struct MCPAuditChainTests { + private static func makeStorage() -> (storage: MCPAuditLogStorage, path: String) { + let directory = FileManager.default.temporaryDirectory + .appendingPathComponent("MCPAuditChainTests-\(UUID().uuidString)", isDirectory: true) + let path = directory.appendingPathComponent(MCPAuditLogStorage.databaseFileName).path + return (MCPAuditLogStorage(directory: directory), path) + } + private func entry(action: String, details: String? = nil) -> AuditEntry { AuditEntry( timestamp: Date(timeIntervalSince1970: 1_700_000_000), @@ -30,7 +28,7 @@ struct MCPAuditChainTests { @Test("An appended log verifies intact") func chainVerifiesIntact() async { - let storage = MCPAuditLogStorage(isolatedForTesting: true) + let storage = Self.makeStorage().storage for index in 0..<5 { await storage.addEntry(entry(action: "tool.run\(index)")) } @@ -39,7 +37,7 @@ struct MCPAuditChainTests { @Test("An empty log verifies intact") func emptyChainVerifiesIntact() async { - let storage = MCPAuditLogStorage(isolatedForTesting: true) + let storage = Self.makeStorage().storage #expect(await storage.verify() == .intact(count: 0)) } @@ -87,7 +85,7 @@ struct MCPAuditChainTests { @Test("Clearing the log restarts the chain at genesis") func deleteAllResetsTheChain() async { - let storage = MCPAuditLogStorage(isolatedForTesting: true) + let storage = Self.makeStorage().storage await storage.addEntry(entry(action: "tool.first")) #expect(await storage.deleteAll() == true) await storage.addEntry(entry(action: "tool.second")) @@ -107,11 +105,10 @@ struct MCPAuditChainTests { @Test("A row edited behind the app's back is detected") func editedRowIsDetected() async throws { let marker = "tool.edited.\(UUID().uuidString)" - let storage = MCPAuditLogStorage(isolatedForTesting: true) + let (storage, path) = Self.makeStorage() for index in 0..<3 { await storage.addEntry(entry(action: index == 1 ? marker : "tool.run\(index)")) } - let path = try #require(Self.databasePath(containing: marker)) #expect( Self.execute( @@ -126,11 +123,10 @@ struct MCPAuditChainTests { @Test("A row removed behind the app's back is detected") func removedRowIsDetected() async throws { let marker = "tool.removed.\(UUID().uuidString)" - let storage = MCPAuditLogStorage(isolatedForTesting: true) + let (storage, path) = Self.makeStorage() for index in 0..<3 { await storage.addEntry(entry(action: index == 1 ? marker : "tool.gone\(index)")) } - let path = try #require(Self.databasePath(containing: marker)) #expect(Self.execute("DELETE FROM audit_entries WHERE action = '\(marker)';", at: path)) @@ -140,41 +136,15 @@ struct MCPAuditChainTests { @Test("A row reordered behind the app's back is detected") func reorderedRowIsDetected() async throws { let marker = "tool.reordered.\(UUID().uuidString)" - let storage = MCPAuditLogStorage(isolatedForTesting: true) + let (storage, path) = Self.makeStorage() await storage.addEntry(entry(action: marker)) await storage.addEntry(entry(action: "tool.second")) - let path = try #require(Self.databasePath(containing: marker)) #expect(Self.execute("UPDATE audit_entries SET sequence = 9 WHERE sequence = 1;", at: path)) #expect(await storage.verify() == .broken(atSequence: 9)) } - private static func databasePath(containing action: String) -> String? { - let directory = AppStorageEnvironment.shared.applicationSupportRoot.appendingPathComponent("TablePro") - let prefix = "mcp-audit-test_\(ProcessInfo.processInfo.processIdentifier)" - let names = ((try? FileManager.default.contentsOfDirectory(atPath: directory.path)) ?? []) - .filter { $0.hasPrefix(prefix) && $0.hasSuffix(".db") } - return names - .map { directory.appendingPathComponent($0).path } - .first { contains(action: action, at: $0) } - } - - private static func contains(action: String, at path: String) -> Bool { - var db: OpaquePointer? - guard sqlite3_open(path, &db) == SQLITE_OK else { return false } - defer { sqlite3_close(db) } - - var statement: OpaquePointer? - let sql = "SELECT COUNT(*) FROM audit_entries WHERE action = ?;" - guard sqlite3_prepare_v2(db, sql, -1, &statement, nil) == SQLITE_OK else { return false } - defer { sqlite3_finalize(statement) } - - sqlite3_bind_text(statement, 1, action, -1, unsafeBitCast(-1, to: sqlite3_destructor_type.self)) - guard sqlite3_step(statement) == SQLITE_ROW else { return false } - return sqlite3_column_int(statement, 0) > 0 - } - private static func execute(_ sql: String, at path: String) -> Bool { var db: OpaquePointer? guard sqlite3_open(path, &db) == SQLITE_OK else { return false } diff --git a/apple/TableProTests/Core/MCP/MCPAuditLogStorageTests.swift b/apple/TableProTests/Core/MCP/MCPAuditLogStorageTests.swift index b9d2151684..1bdadb090d 100644 --- a/apple/TableProTests/Core/MCP/MCPAuditLogStorageTests.swift +++ b/apple/TableProTests/Core/MCP/MCPAuditLogStorageTests.swift @@ -1,13 +1,3 @@ -// -// MCPAuditLogStorageTests.swift -// TableProTests -// -// The audit log is evidence, so it has to survive being read by the wrong person and being edited -// by one. Every row carries the token id that caused it, so a token can be traced; the statement -// is reduced to a digest, so a CREATE USER with an inline password never lands on disk in -// cleartext; and the store and its journal files are owner-only. -// - import Foundation import TableProMCP import TableProPluginKit @@ -18,7 +8,12 @@ import Testing @Suite("MCP Audit Log Storage", .serialized) struct MCPAuditLogStorageTests { private func makeStorage() -> MCPAuditLogStorage { - MCPAuditLogStorage(isolatedForTesting: true) + MCPAuditLogStorage(directory: Self.makeDirectory()) + } + + private static func makeDirectory() -> URL { + FileManager.default.temporaryDirectory + .appendingPathComponent("MCPAuditLogStorageTests-\(UUID().uuidString)", isDirectory: true) } private func makeEntry( @@ -180,16 +175,16 @@ struct MCPAuditLogStorageTests { @Test("The store and its journal files stay readable by their owner alone") func databaseFilesAreNotWorldReadable() async throws { - let storage = makeStorage() + let directory = Self.makeDirectory() + let storage = MCPAuditLogStorage(directory: directory) await storage.addEntry(makeEntry(action: "tool.permissions")) - let directory = Self.auditDirectory() let directoryPermissions = try #require(Self.permissions(ofItemAt: directory.path)) #expect(directoryPermissions & 0o077 == 0) - let paths = await storage.databaseFilePaths - #expect(paths.isEmpty == false) - for path in paths { + let databasePath = directory.appendingPathComponent(MCPAuditLogStorage.databaseFileName).path + #expect(FileManager.default.fileExists(atPath: databasePath)) + for path in [databasePath, databasePath + "-wal", databasePath + "-shm"] { guard let permissions = Self.permissions(ofItemAt: path) else { continue } #expect( permissions & 0o077 == 0, @@ -261,8 +256,58 @@ struct MCPAuditLogStorageTests { #expect(row.tokenName == "client") } - private static func auditDirectory() -> URL { - AppStorageEnvironment.shared.applicationSupportRoot.appendingPathComponent("TablePro") + @Test("A damaged log is kept beside a fresh one, which starts by recording the reset") + func damagedLogIsSetAsideAndAFreshOneStarts() async throws { + let directory = Self.makeDirectory() + try FileManager.default.createDirectory(at: directory, withIntermediateDirectories: true) + let databaseURL = directory.appendingPathComponent(MCPAuditLogStorage.databaseFileName) + let damaged = Data(repeating: 0x2A, count: 4_096) + try damaged.write(to: databaseURL) + + let storage = MCPAuditLogStorage(directory: directory) + #expect(await storage.addEntry(makeEntry(action: "tool.afterReset"))) + + let names = try FileManager.default.contentsOfDirectory(atPath: directory.path) + let copyName = try #require(names.first { $0.hasPrefix("mcp-audit.damaged-") && $0.hasSuffix(".db") }) + let copyURL = directory.appendingPathComponent(copyName) + #expect(try Data(contentsOf: copyURL) == damaged) + let copyPermissions = try #require(Self.permissions(ofItemAt: copyURL.path)) + #expect(copyPermissions & 0o077 == 0) + let freshPermissions = try #require(Self.permissions(ofItemAt: databaseURL.path)) + #expect(freshPermissions & 0o077 == 0) + + let entries = await storage.query() + let actions = Set(entries.map { $0.action }) + #expect(actions == ["auditLog.setAside", "tool.afterReset"]) + let notice = try #require(entries.first { $0.action == "auditLog.setAside" }) + #expect(notice.details == "setAside=\(copyName)") + #expect(await storage.verify() == .intact(count: 2)) + } + + @Test("The first entry after the log is reopened continues its chain") + func reopenedLogContinuesItsChain() async { + let directory = Self.makeDirectory() + do { + let first = MCPAuditLogStorage(directory: directory) + #expect(await first.addEntry(makeEntry(action: "tool.beforeRelaunch"))) + #expect(await first.addEntry(makeEntry(action: "tool.beforeRelaunch2"))) + } + + let reopened = MCPAuditLogStorage(directory: directory) + #expect(await reopened.addEntry(makeEntry(action: "tool.afterRelaunch"))) + #expect(await reopened.count() == 3) + #expect(await reopened.verify() == .intact(count: 3)) + } + + @Test("The shared log writes the storage folder's mcp-audit.db, under a test host as in the app") + func sharedLogUsesTheStorageFolder() async throws { + MCPAuditLogger.logTokenCreated(tokenId: UUID(), tokenName: "paired client") + await MCPAuditLogger.flush() + + let directory = AppStorageEnvironment.shared.supportDirectory + let names = try FileManager.default.contentsOfDirectory(atPath: directory.path) + #expect(names.contains(MCPAuditLogStorage.databaseFileName)) + #expect(names.allSatisfy { !$0.hasPrefix("mcp-audit-test_") }) } private static func permissions(ofItemAt path: String) -> Int? { diff --git a/apple/TableProTests/Core/MCP/MCPTokenStoreTests.swift b/apple/TableProTests/Core/MCP/MCPTokenStoreTests.swift index 7bea3598b0..863bec24e8 100644 --- a/apple/TableProTests/Core/MCP/MCPTokenStoreTests.swift +++ b/apple/TableProTests/Core/MCP/MCPTokenStoreTests.swift @@ -490,6 +490,66 @@ struct MCPTokenStoreTests { #expect(await store.validate(bearerToken: result.plaintext) == nil) } + @Test("A superseded bridge credential stops working at once and no revocation observer hears of it") + func supersededBridgeCredentialIsDroppedWithoutARevocation() async throws { + let store = makeStore() + let superseded = try await store.generate( + name: MCPTokenStore.stdioBridgeTokenName, + permissions: MCPTokenStore.bridgeTokenPermissions, + connectionAccess: .all, + expiresAt: Date.now.addingTimeInterval(3_600), + isBridgeCredential: true, + isClaudeAgentCredential: false, + extraScopes: [] + ) + let paired = try await store.generate( + name: "paired", + permissions: .readOnly, + connectionAccess: .all, + expiresAt: nil, + isBridgeCredential: false, + isClaudeAgentCredential: false, + extraScopes: [] + ) + let recorder = RevocationRecorder() + await store.addRevocationObserver { key, _ in + await recorder.append(key) + } + + await store.dropSupersededBridgeCredential(tokenId: superseded.token.id) + await store.delete(tokenId: paired.token.id) + let pairedKey = paired.token.id.uuidString + try await waitUntil { await recorder.keys().contains(pairedKey) } + + #expect(await store.validate(bearerToken: superseded.plaintext) == nil) + #expect(await store.list().contains(where: { $0.id == superseded.token.id }) == false) + #expect(await recorder.keys() == [pairedKey]) + } + + @Test("Only a bridge credential is dropped quietly: any other token passed in is deleted as a revocation") + func supersedingAPairedTokenStillRevokesIt() async throws { + let store = makeStore() + let paired = try await store.generate( + name: "paired", + permissions: .readWrite, + connectionAccess: .all, + expiresAt: nil, + isBridgeCredential: false, + isClaudeAgentCredential: false, + extraScopes: [] + ) + let recorder = RevocationRecorder() + await store.addRevocationObserver { key, _ in + await recorder.append(key) + } + + await store.dropSupersededBridgeCredential(tokenId: paired.token.id) + let key = paired.token.id.uuidString + try await waitUntil { await recorder.keys().contains(key) } + + #expect(await store.validate(bearerToken: paired.plaintext) == nil) + } + @Test("A second token claiming the same client name leaves the first one working") func sameClientNameDoesNotRevokeTheStandingToken() async throws { let store = makeStore() diff --git a/apple/TableProTests/Core/MCP/Protocol/MCPProtocolDispatcherTests.swift b/apple/TableProTests/Core/MCP/Protocol/MCPProtocolDispatcherTests.swift index a719153081..6ab320fac6 100644 --- a/apple/TableProTests/Core/MCP/Protocol/MCPProtocolDispatcherTests.swift +++ b/apple/TableProTests/Core/MCP/Protocol/MCPProtocolDispatcherTests.swift @@ -3,6 +3,7 @@ import Foundation import TableProMCP import TableProMCPTestSupport import TableProPluginKit +import TableProTestSupport import Testing struct MCPProtocolDispatcherTests { @@ -564,6 +565,41 @@ struct MCPProtocolDispatcherTests { _ = await run } + @Test + func aSupersededTokenKeepsItsInFlightRequestsUntilItsExpiry() async throws { + let clock = MCPTestClock() + let handler = StubTestHandler(behavior: .waitForCancellation) + let dispatcher = MCPProtocolTestSupport.makeDispatcher( + handlers: [handler], + clock: clock, + handlerTimeout: .seconds(86_400) + ) + let request = MCPProtocolTestSupport.makeModernRequest(id: .number(12), method: StubTestHandler.method) + let (exchange, sink) = MCPProtocolTestSupport.makeExchange(message: request, principal: alice) + + async let run: Void = dispatcher.dispatch(exchange) + _ = await waitForInflight(dispatcher, count: 1) + + let tokenId = try #require(alice.tokenId) + let expiry = await clock.now().addingTimeInterval(900) + await dispatcher.cancelInflight(matchingTokenId: tokenId, at: expiry) + + #expect(await dispatcher.inflightCount() == 1) + #expect(await sink.jsonWrites.isEmpty) + + try await waitUntil { + await clock.advance(by: .seconds(60)) + return await dispatcher.inflightCount() == 0 + } + #expect(await clock.now() >= expiry) + + await sink.waitForCompletion() + let errorValue = try await sink.errorEnvelope() + let error = try #require(errorValue) + #expect(error.code == JsonRpcErrorCode.requestCancelled) + _ = await run + } + @Test func anInputRequiredSignalReachesTheClientAsAnInputRequiredResult() async throws { let signal = MCPInputRequired( diff --git a/apple/TableProTests/Core/Plugins/PluginCodeSignatureVerifierTests.swift b/apple/TableProTests/Core/Plugins/PluginCodeSignatureVerifierTests.swift index a25965d1c6..f1e316d423 100644 --- a/apple/TableProTests/Core/Plugins/PluginCodeSignatureVerifierTests.swift +++ b/apple/TableProTests/Core/Plugins/PluginCodeSignatureVerifierTests.swift @@ -1,8 +1,3 @@ -// -// PluginCodeSignatureVerifierTests.swift -// TableProTests -// - import Foundation @testable import TablePro import Testing @@ -14,10 +9,14 @@ struct PluginCodeSignatureVerifierTests { // Calculator carries a valid Apple signature that is not TablePro's, the same shape as a // bundle signed with another developer's Developer ID. + private static func calculator() throws -> Bundle { + try #require(Bundle(path: "/System/Applications/Calculator.app")) + } + @Test("A validly signed bundle from another signer is refused") func refusesAnotherSigner() throws { guard !Self.verifierIsBypassed else { return } - let bundle = try #require(Bundle(path: "/System/Applications/Calculator.app")) + let bundle = try Self.calculator() let error = #expect(throws: PluginError.self) { try PluginCodeSignatureVerifier.verify(bundle: bundle) @@ -50,4 +49,60 @@ struct PluginCodeSignatureVerifierTests { return } } + + @Test("The app's own Team ID builds a signing requirement") + func resolvedTeamBuildsRequirement() throws { + _ = try PluginCodeSignatureVerifier.firstPartyRequirement( + signingTeam: PluginCodeSignatureVerifier.resolvedSigningTeamId + ) + } + + // A lone quote used to leave no requirement, and the `or anchor apple` team one every Apple-signed bundle meets. + @Test( + "A validly signed bundle is refused when the Team ID would break TablePro's requirement", + arguments: [ + "\"", + "ABCDEFGHIJ\" or anchor apple or certificate leaf[subject.OU] = \"X" + ] + ) + func refusesWhenTeamBreaksRequirement(team: String) throws { + let bundle = try Self.calculator() + + let error = #expect(throws: PluginError.self) { + try PluginCodeSignatureVerifier.verify(bundle: bundle, signingTeam: team) + } + guard case .signatureInvalid(let detail) = error else { + Issue.record("Expected signatureInvalid, got \(String(describing: error))") + return + } + #expect(detail == "TablePro's signing Team ID is not valid") + } + + @Test( + "A Team ID that is not ten capital letters or digits builds no requirement", + arguments: ["", "d7hj5tfycu", "D7HJ5TFYC", "D7HJ5TFYCU1", "D7HJ5-FYCU"] + ) + func refusesMalformedTeam(team: String) { + let error = #expect(throws: PluginError.self) { + _ = try PluginCodeSignatureVerifier.firstPartyRequirement(signingTeam: team) + } + guard case .signatureInvalid = error else { + Issue.record("Expected signatureInvalid, got \(String(describing: error))") + return + } + } + + @Test("Requirement text that does not compile is an error, never an empty requirement") + func uncompilableRequirementThrows() { + let error = #expect(throws: PluginError.self) { + _ = try PluginCodeSignatureVerifier.compiledRequirement( + "anchor apple generic and certificate leaf[subject.OU] = \"\"\"" + ) + } + guard case .signatureInvalid(let detail) = error else { + Issue.record("Expected signatureInvalid, got \(String(describing: error))") + return + } + #expect(detail.hasPrefix("TablePro's signing requirement could not be built")) + } } diff --git a/apple/TableProTests/Core/SSH/SSHTunnelErrorTests.swift b/apple/TableProTests/Core/SSH/SSHTunnelErrorTests.swift index 1b655b854a..b3474eb573 100644 --- a/apple/TableProTests/Core/SSH/SSHTunnelErrorTests.swift +++ b/apple/TableProTests/Core/SSH/SSHTunnelErrorTests.swift @@ -7,26 +7,6 @@ import TableProTestSupport import Testing struct SSHTunnelErrorTests { - // MARK: - Port Bind Failure Classification - - @Test("isLocalPortBindFailure detects 'already in use' pattern") - func bindFailureAlreadyInUse() { - #expect(SSHTunnelManager.isLocalPortBindFailure("Address already in use")) - } - - @Test("isLocalPortBindFailure is case-insensitive") - func bindFailureCaseInsensitive() { - #expect(SSHTunnelManager.isLocalPortBindFailure("ADDRESS ALREADY IN USE")) - } - - @Test("isLocalPortBindFailure returns false for unrelated SSH errors") - func nonBindFailures() { - #expect(!SSHTunnelManager.isLocalPortBindFailure("Permission denied")) - #expect(!SSHTunnelManager.isLocalPortBindFailure("Connection refused")) - #expect(!SSHTunnelManager.isLocalPortBindFailure("Host key verification failed")) - #expect(!SSHTunnelManager.isLocalPortBindFailure("")) - } - // MARK: - Error Descriptions @Test("SSHTunnelError.noAvailablePort has a localized description") diff --git a/apple/TableProTests/Core/SSH/SSHTunnelLocalPortTests.swift b/apple/TableProTests/Core/SSH/SSHTunnelLocalPortTests.swift new file mode 100644 index 0000000000..72da006f95 --- /dev/null +++ b/apple/TableProTests/Core/SSH/SSHTunnelLocalPortTests.swift @@ -0,0 +1,104 @@ +import Foundation +import TableProSSHConfig +import TableProSSHTransport +import Testing + +@testable import TablePro + +private final class LoopbackSocket { + let descriptor: Int32 + let port: Int + + init?(listening: Bool) { + let fd = socket(AF_INET, SOCK_STREAM, 0) + guard fd >= 0 else { return nil } + + var address = sockaddr_in() + address.sin_family = sa_family_t(AF_INET) + address.sin_port = 0 + address.sin_addr.s_addr = inet_addr("127.0.0.1") + let bound = withUnsafePointer(to: &address) { + $0.withMemoryRebound(to: sockaddr.self, capacity: 1) { + bind(fd, $0, socklen_t(MemoryLayout.size)) + } + } + guard bound == 0, !listening || listen(fd, 4) == 0 else { + Darwin.close(fd) + return nil + } + + var assigned = sockaddr_in() + var length = socklen_t(MemoryLayout.size) + let named = withUnsafeMutablePointer(to: &assigned) { + $0.withMemoryRebound(to: sockaddr.self, capacity: 1) { getsockname(fd, $0, &length) } + } + guard named == 0 else { + Darwin.close(fd) + return nil + } + descriptor = fd + port = Int(UInt16(bigEndian: assigned.sin_port)) + } + + var hasPendingConnection: Bool { + var pollDescriptor = pollfd(fd: descriptor, events: Int16(POLLIN), revents: 0) + return poll(&pollDescriptor, 1, 0) > 0 + } + + deinit { + Darwin.close(descriptor) + } +} + +struct SSHTunnelLocalPortTests { + private func freeLoopbackPort() throws -> Int { + try #require(LoopbackSocket(listening: false)).port + } + + @Test("A taken local port is skipped for the next free one") + func takenPortIsSkipped() throws { + let taken = try #require(LoopbackSocket(listening: true)) + let freePort = try freeLoopbackPort() + + let listener = try LibSSH2TunnelFactory.bindLocalListener(candidates: [taken.port, freePort]) + defer { Darwin.close(listener.descriptor) } + + #expect(listener.port == freePort) + withExtendedLifetime(taken) {} + } + + @Test("Every candidate port taken reports that no local port is available") + func everyPortTakenReportsNoPort() throws { + let taken = try #require(LoopbackSocket(listening: true)) + + #expect(throws: SSHTunnelError.noAvailablePort) { + try LibSSH2TunnelFactory.bindLocalListener(candidates: [taken.port]) + } + withExtendedLifetime(taken) {} + } + + @Test("A tunnel with no free local port fails before it dials or signs in to the SSH server") + func noLocalPortFailsBeforeDialing() async throws { + let taken = try #require(LoopbackSocket(listening: true)) + let sshServer = try #require(LoopbackSocket(listening: true)) + let takenPort = taken.port + let sshPort = sshServer.port + let manager = SSHTunnelManager(localPortCandidates: { [takenPort] }) + + await #expect(throws: SSHTunnelError.noAvailablePort) { + try await manager.createTunnel( + connectionId: UUID(), + sshHost: "127.0.0.1", + sshPort: sshPort, + sshUsername: "tester", + authMethod: .password, + sshPassword: "secret", + destination: .tcp(host: "127.0.0.1", port: 5_432), + deadline: ConnectionDeadline(configuredSeconds: 2) + ) + } + + #expect(!sshServer.hasPendingConnection) + withExtendedLifetime(taken) {} + } +} diff --git a/apple/TableProTests/Core/Storage/ConnectionSecretScopeTests.swift b/apple/TableProTests/Core/Storage/ConnectionSecretScopeTests.swift index 9c7ebc52b3..a32ae7449c 100644 --- a/apple/TableProTests/Core/Storage/ConnectionSecretScopeTests.swift +++ b/apple/TableProTests/Core/Storage/ConnectionSecretScopeTests.swift @@ -1,102 +1,14 @@ import Foundation @testable import TablePro import TableProSyncTransport +import TableProTestSupport import Testing -// One synced and one local value per key, as the keychain holds them. Nothing here removes a synced value except delete. -private final class ScopeRecordingKeychain: KeychainStoring, @unchecked Sendable { - private let lock = NSLock() - private var synced: [String: String] = [:] - private var local: [String: String] = [:] - private var unreadableKeys: Set = [] - private var failingKeys: Set = [] - - func seedSynced(_ value: String, forKey key: String) { - lock.lock() - defer { lock.unlock() } - synced[key] = value - } - - func seedLocal(_ value: String, forKey key: String) { - lock.lock() - defer { lock.unlock() } - local[key] = value - } - - func makeUnreadable(_ key: String) { - lock.lock() - defer { lock.unlock() } - unreadableKeys.insert(key) - } - - func makeReadable(_ key: String) { - lock.lock() - defer { lock.unlock() } - unreadableKeys.remove(key) - } - - func failWrites(to key: String) { - lock.lock() - defer { lock.unlock() } - failingKeys.insert(key) - } - - func syncedValue(forKey key: String) -> String? { - lock.lock() - defer { lock.unlock() } - return synced[key] - } - - func localValue(forKey key: String) -> String? { - lock.lock() - defer { lock.unlock() } - return local[key] - } - - @discardableResult - func writeString(_ value: String, forKey key: String) -> Bool { - writeString(value, forKey: key, deviceOnly: false) - } - - // Password sync is on in this model, so a write that is not device-only is a synced one. - @discardableResult - func writeString(_ value: String, forKey key: String, deviceOnly: Bool) -> Bool { - lock.lock() - defer { lock.unlock() } - guard !failingKeys.contains(key) else { return false } - if deviceOnly { - local[key] = value - } else { - synced[key] = value - local[key] = nil - } - return true - } - - func readStringResult(forKey key: String) -> KeychainStringResult { - readStringResult(forKey: key, deviceOnly: false) - } - - func readStringResult(forKey key: String, deviceOnly: Bool) -> KeychainStringResult { - lock.lock() - defer { lock.unlock() } - guard !unreadableKeys.contains(key) else { return .locked } - let value = deviceOnly ? local[key] ?? synced[key] : synced[key] ?? local[key] - return value.map { .found($0) } ?? .notFound - } - - func delete(forKey key: String) { - lock.lock() - defer { lock.unlock() } - synced[key] = nil - local[key] = nil - } -} - @MainActor struct ConnectionSecretScopeTests { private let keychain = ScopeRecordingKeychain() private let suite: EphemeralDefaults + private let tracker: SyncChangeTracker private let storage: ConnectionStorage init() throws { @@ -105,10 +17,11 @@ struct ConnectionSecretScopeTests { .appendingPathComponent("tablepro-tests") .appendingPathComponent("secret-scope-\(UUID().uuidString)", isDirectory: true) try FileManager.default.createDirectory(at: directory, withIntermediateDirectories: true) + tracker = SyncChangeTracker(metadataStorage: SyncMetadataStorage(userDefaults: suite.defaults)) storage = ConnectionStorage( fileURL: directory.appendingPathComponent("connections.json"), userDefaults: suite.defaults, - syncTracker: SyncChangeTracker(metadataStorage: SyncMetadataStorage(userDefaults: suite.defaults)), + syncTracker: tracker, keychain: keychain, appEvents: AppEvents(), integrity: ConnectionStoreIntegrity(keySource: StoredIntegrityKeySource(store: InMemoryKeychain())) @@ -373,4 +286,82 @@ struct ConnectionSecretScopeTests { #expect(keychain.syncedValue(forKey: passwordKey(id)) == nil) #expect(keychain.syncedValue(forKey: tokenSecretKey) == nil) } + + @Test("A delete from another device removes this Mac's own secrets and leaves the ones iCloud Keychain holds") + func remoteDeleteKeepsTheSyncedSecrets() { + let connection = TestFixtures.makeConnection(type: .mongodb) + let tokenKey = "com.TablePro.plugin.mongoAWSSessionToken.\(connection.id.uuidString)" + for key in [passwordKey(connection.id), totpKey(connection.id), tokenKey] { + keychain.seedLocal("local", forKey: key) + keychain.seedSynced("synced", forKey: key) + } + + storage.deleteSecrets(of: [connection], origin: .remote) + + for key in [passwordKey(connection.id), totpKey(connection.id), tokenKey] { + #expect(keychain.localValue(forKey: key) == nil) + #expect(keychain.syncedValue(forKey: key) == "synced") + } + } + + @Test("Deleting a connection here removes every secret it has, the synced ones too") + func localDeleteRemovesEverySecret() { + let connection = TestFixtures.makeConnection(type: .mongodb) + #expect(storage.addConnection(connection, password: "hunter2")) + let tokenKey = "com.TablePro.plugin.mongoAWSSessionToken.\(connection.id.uuidString)" + for key in [totpKey(connection.id), sslKey(connection.id), tokenKey] { + keychain.seedLocal("local", forKey: key) + keychain.seedSynced("synced", forKey: key) + } + + #expect(storage.deleteConnection(connection)) + + for key in [passwordKey(connection.id), totpKey(connection.id), sslKey(connection.id), tokenKey] { + #expect(keychain.localValue(forKey: key) == nil) + #expect(keychain.syncedValue(forKey: key) == nil) + } + } + + enum LocalOnlyHistory: CaseIterable { + case pushedBeforeMarkedLocalOnly + case pulledFromAnotherDevice + case localOnlyFromTheStart + } + + // Local only from the start too: nothing on this Mac can prove no other device holds the row. + @Test("A Local only delete never removes the synced secrets, whatever the row's history", + arguments: LocalOnlyHistory.allCases, [false, true]) + func localOnlyDeleteKeepsTheSyncedSecrets(_ history: LocalOnlyHistory, inBatch: Bool) async throws { + var localOnly = makeConnection(localOnly: history == .localOnlyFromTheStart) + if history == .pulledFromAnotherDevice { + localOnly.origin = .synced + localOnly.localOnly = true + } + let synced = makeConnection(localOnly: false) + #expect(storage.saveConnections([localOnly, synced])) + if history == .pushedBeforeMarkedLocalOnly { + let marked = storage.mutateConnections(ids: [localOnly.id]) { $0.localOnly = true } + #expect(marked) + localOnly.localOnly = true + } + for connection in [localOnly, synced] { + keychain.seedLocal("local", forKey: totpKey(connection.id)) + keychain.seedSynced("synced", forKey: totpKey(connection.id)) + } + + if inBatch { + #expect(storage.deleteConnections([localOnly, synced])) + } else { + #expect(storage.deleteConnection(localOnly)) + } + + #expect(keychain.localValue(forKey: totpKey(localOnly.id)) == nil) + #expect(keychain.syncedValue(forKey: totpKey(localOnly.id)) == "synced") + if inBatch { + #expect(keychain.localValue(forKey: totpKey(synced.id)) == nil) + #expect(keychain.syncedValue(forKey: totpKey(synced.id)) == nil) + } + // The purge releases a Local only owner from a Task; waiting keeps that defaults write inside the case. + try await waitUntil { tracker.ownersKeptOffSync.isEmpty } + } } diff --git a/apple/TableProTests/Core/Storage/KeychainItemScopeTests.swift b/apple/TableProTests/Core/Storage/KeychainItemScopeTests.swift index e364b79c35..90700e4ac3 100644 --- a/apple/TableProTests/Core/Storage/KeychainItemScopeTests.swift +++ b/apple/TableProTests/Core/Storage/KeychainItemScopeTests.swift @@ -246,4 +246,15 @@ struct KeychainItemScopeTests { #expect(keychain.readStringResult(forKey: key) == .notFound) } + + @Test("A device-only delete removes this Mac's item and leaves the one iCloud Keychain holds", arguments: [true, false]) + func deviceOnlyDeleteKeepsTheSyncedItem(passwordSync: Bool) { + store.seed("local", account: key, synchronizable: false) + store.seed("synced", account: key, synchronizable: true) + + helper(passwordSync: passwordSync).delete(forKey: key, deviceOnly: true) + + #expect(store.value(account: key, synchronizable: false) == nil) + #expect(store.value(account: key, synchronizable: true) == "synced") + } } diff --git a/apple/TableProTests/Core/Storage/LinkedSQLIndexDamageTests.swift b/apple/TableProTests/Core/Storage/LinkedSQLIndexDamageTests.swift new file mode 100644 index 0000000000..ff79aaa9a0 --- /dev/null +++ b/apple/TableProTests/Core/Storage/LinkedSQLIndexDamageTests.swift @@ -0,0 +1,33 @@ +import Foundation +@testable import TablePro +import Testing + +struct LinkedSQLIndexDamageTests { + @Test("A damaged linked SQL index is discarded and indexing works again") + func damagedIndexIsRebuilt() async throws { + let scratch = FileManager.default.temporaryDirectory + .appendingPathComponent("tablepro-tests") + .appendingPathComponent("linked-index-damage-\(UUID().uuidString)", isDirectory: true) + try FileManager.default.createDirectory(at: scratch, withIntermediateDirectories: true) + let url = scratch.appendingPathComponent("linked_sql_index.db") + try Data(repeating: 0x2A, count: 4_096).write(to: url) + let index = LinkedSQLIndex(databaseURL: url, removeDatabaseOnDeinit: true) + let folderId = UUID() + let file = LinkedSQLIndex.IndexedFile( + relativePath: "revenue.sql", + name: "revenue", + keyword: "rev", + description: nil, + mtime: Date(timeIntervalSince1970: 1_800_000_000), + fileSize: 8, + encoding: .utf8 + ) + + await index.replaceAll(folderId: folderId, files: [file], folderURL: scratch) + + #expect(await index.allFolderIds() == [folderId]) + let leftovers = try FileManager.default.contentsOfDirectory(atPath: scratch.path(percentEncoded: false)) + .filter { $0.contains(".damaged-") } + #expect(leftovers.isEmpty) + } +} diff --git a/apple/TableProTests/Core/Storage/LocalSQLiteStoreOpenerTests.swift b/apple/TableProTests/Core/Storage/LocalSQLiteStoreOpenerTests.swift new file mode 100644 index 0000000000..2c496a36d8 --- /dev/null +++ b/apple/TableProTests/Core/Storage/LocalSQLiteStoreOpenerTests.swift @@ -0,0 +1,147 @@ +import Foundation +import SQLite3 +@testable import TablePro +import Testing + +struct LocalSQLiteStoreOpenerTests { + private let scratch: URL + private let fixedDate = Date(timeIntervalSince1970: 1_800_000_000) + + init() throws { + scratch = FileManager.default.temporaryDirectory + .appendingPathComponent("tablepro-tests") + .appendingPathComponent("local-store-opener-\(UUID().uuidString)", isDirectory: true) + try FileManager.default.createDirectory(at: scratch, withIntermediateDirectories: true) + } + + private func makeDatabase(at url: URL, rows: Int) { + var db: OpaquePointer? + guard sqlite3_open(url.path(percentEncoded: false), &db) == SQLITE_OK else { return } + defer { sqlite3_close_v2(db) } + sqlite3_exec(db, "CREATE TABLE t (id INTEGER PRIMARY KEY, v TEXT);", nil, nil, nil) + sqlite3_exec(db, """ + WITH RECURSIVE n(i) AS (SELECT 1 UNION ALL SELECT i + 1 FROM n WHERE i < \(rows)) + INSERT INTO t (v) SELECT hex(randomblob(100)) FROM n; + """, nil, nil, nil) + } + + private func scalarInt(_ db: OpaquePointer, _ sql: String) -> Int? { + var statement: OpaquePointer? + defer { sqlite3_finalize(statement) } + guard sqlite3_prepare_v2(db, sql, -1, &statement, nil) == SQLITE_OK, + sqlite3_step(statement) == SQLITE_ROW + else { return nil } + return Int(sqlite3_column_int64(statement, 0)) + } + + private func directoryEntries() throws -> [String] { + try FileManager.default.contentsOfDirectory(atPath: scratch.path(percentEncoded: false)).sorted() + } + + @Test("A healthy store opens in place and nothing is set aside") + func healthyStoreOpensInPlace() throws { + let url = scratch.appendingPathComponent("store.db") + makeDatabase(at: url, rows: 10) + + let opening = LocalSQLiteStoreOpener.open(at: url, now: fixedDate) + guard case .opened(let handle, let setAside) = opening else { + Issue.record("A healthy store did not open") + return + } + defer { sqlite3_close_v2(handle) } + + #expect(setAside == nil) + #expect(scalarInt(handle, "SELECT COUNT(*) FROM t;") == 10) + } + + @Test("A file that is not a database is set aside unchanged and a fresh store opens in its place") + func notADatabaseIsSetAside() throws { + let url = scratch.appendingPathComponent("store.db") + let damaged = Data(repeating: 0x2A, count: 4_096) + try damaged.write(to: url) + let expectedSetAside = LocalSQLiteStoreOpener.setAsideURL(for: url, at: fixedDate) + + let opening = LocalSQLiteStoreOpener.open(at: url, now: fixedDate) + guard case .opened(let handle, let setAside) = opening else { + Issue.record("No fresh store opened after setting the damaged one aside") + return + } + defer { sqlite3_close_v2(handle) } + + let moved = try #require(setAside) + #expect(moved.storeURL == url) + #expect(moved.setAsideURL == expectedSetAside) + #expect(try Data(contentsOf: moved.setAsideURL) == damaged) + #expect(sqlite3_exec(handle, "CREATE TABLE fresh (id INTEGER);", nil, nil, nil) == SQLITE_OK) + #expect(scalarInt(handle, "SELECT COUNT(*) FROM fresh;") == 0) + } + + @Test("A database with a damaged page is set aside byte for byte") + func damagedPageIsSetAside() throws { + let url = scratch.appendingPathComponent("store.db") + makeDatabase(at: url, rows: 2_000) + let handle = try FileHandle(forWritingTo: url) + try handle.seek(toOffset: 4_096 * 2) + try handle.write(contentsOf: Data(repeating: 0xFF, count: 4_096)) + try handle.close() + let damaged = try Data(contentsOf: url) + + let opening = LocalSQLiteStoreOpener.open(at: url, now: fixedDate) + guard case .opened(let fresh, let setAside) = opening else { + Issue.record("No fresh store opened after setting the damaged one aside") + return + } + defer { sqlite3_close_v2(fresh) } + + let moved = try #require(setAside) + #expect(try Data(contentsOf: moved.setAsideURL) == damaged) + #expect(scalarInt(fresh, "SELECT COUNT(*) FROM sqlite_master;") == 0) + } + + @Test("A store that cannot be opened is reported unavailable and nothing is created") + func unopenableStoreIsUnavailable() throws { + let blocker = scratch.appendingPathComponent("not-a-directory") + #expect(FileManager.default.createFile(atPath: blocker.path(percentEncoded: false), contents: Data())) + + let opening = LocalSQLiteStoreOpener.open(at: blocker.appendingPathComponent("store.db"), now: fixedDate) + + guard case .unavailable = opening else { + Issue.record("A store under a file opened") + return + } + #expect(try directoryEntries() == ["not-a-directory"]) + } + + @Test("A damaged store that cannot be moved stays where it is and nothing replaces it") + func damagedStoreThatCannotMoveStaysInPlace() throws { + let url = scratch.appendingPathComponent("store.db") + let damaged = Data(repeating: 0x2A, count: 4_096) + try damaged.write(to: url) + let scratchPath = scratch.path(percentEncoded: false) + try FileManager.default.setAttributes([.posixPermissions: 0o555], ofItemAtPath: scratchPath) + defer { try? FileManager.default.setAttributes([.posixPermissions: 0o755], ofItemAtPath: scratchPath) } + + let opening = LocalSQLiteStoreOpener.open(at: url, now: fixedDate) + + guard case .unavailable = opening else { + Issue.record("A damaged store was replaced although it could not be set aside") + return + } + #expect(try Data(contentsOf: url) == damaged) + #expect(try directoryEntries() == ["store.db"]) + } + + @Test("A set-aside name already taken gets the next free one") + func setAsideNameSkipsATakenOne() throws { + let url = scratch.appendingPathComponent("store.db") + let first = LocalSQLiteStoreOpener.setAsideURL(for: url, at: fixedDate) + #expect(FileManager.default.createFile(atPath: first.path(percentEncoded: false) + "-wal", contents: Data())) + + let second = LocalSQLiteStoreOpener.setAsideURL(for: url, at: fixedDate) + + #expect(second != first) + #expect(second.deletingLastPathComponent() == first.deletingLastPathComponent()) + #expect(second.lastPathComponent.hasPrefix("store.damaged-")) + #expect(second.lastPathComponent.hasSuffix("-1.db")) + } +} diff --git a/apple/TableProTests/Core/Storage/PluginSecureFieldMigrationTests.swift b/apple/TableProTests/Core/Storage/PluginSecureFieldMigrationTests.swift new file mode 100644 index 0000000000..8bf5826f86 --- /dev/null +++ b/apple/TableProTests/Core/Storage/PluginSecureFieldMigrationTests.swift @@ -0,0 +1,156 @@ +import Foundation +@testable import TablePro +import TableProSyncTransport +import Testing + +@MainActor +struct PluginSecureFieldMigrationTests { + private static let fieldId = "mongoAWSSessionToken" + private static let doneKey = "com.TablePro.pluginSecureFieldsMovedToKeychain" + + private let keychain = ScopeRecordingKeychain() + private let suite: EphemeralDefaults + private let fileURL: URL + private let storage: ConnectionStorage + + init() throws { + suite = try EphemeralDefaults() + let directory = FileManager.default.temporaryDirectory + .appendingPathComponent("tablepro-tests") + .appendingPathComponent("plugin-secure-fields-\(UUID().uuidString)", isDirectory: true) + try FileManager.default.createDirectory(at: directory, withIntermediateDirectories: true) + fileURL = directory.appendingPathComponent("connections.json") + storage = ConnectionStorage( + fileURL: fileURL, + userDefaults: suite.defaults, + syncTracker: SyncChangeTracker(metadataStorage: SyncMetadataStorage(userDefaults: suite.defaults)), + keychain: keychain, + appEvents: AppEvents(), + integrity: ConnectionStoreIntegrity(keySource: StoredIntegrityKeySource(store: InMemoryKeychain())) + ) + } + + private func tokenKey(_ id: UUID) -> String { + "com.TablePro.plugin.\(Self.fieldId).\(id.uuidString)" + } + + private func legacyConnection() -> DatabaseConnection { + var connection = TestFixtures.makeConnection(type: .mongodb) + connection.additionalFields = [Self.fieldId: "FQoG"] + return connection + } + + private func fieldOnDisk(_ id: UUID) -> String? { + storage.invalidateCache() + return storage.loadConnection(id: id)?.additionalFields[Self.fieldId] + } + + private var isDone: Bool { + suite.defaults.bool(forKey: Self.doneKey) + } + + @Test("A plain-text secure field moves to the Keychain and leaves the record") + func fieldMovesToTheKeychain() { + let connection = legacyConnection() + #expect(storage.saveConnections([connection])) + + storage.migratePluginSecureFieldsIfNeeded() + + #expect(keychain.syncedValue(forKey: tokenKey(connection.id)) == "FQoG") + #expect(fieldOnDisk(connection.id) == nil) + #expect(isDone) + } + + @Test("A field whose Keychain write fails stays in the record, and the next launch moves it") + func failedWriteKeepsTheField() { + let connection = legacyConnection() + #expect(storage.saveConnections([connection])) + keychain.failWrites(to: tokenKey(connection.id)) + + storage.migratePluginSecureFieldsIfNeeded() + + #expect(fieldOnDisk(connection.id) == "FQoG") + #expect(!isDone) + + keychain.allowWrites(to: tokenKey(connection.id)) + storage.migratePluginSecureFieldsIfNeeded() + + #expect(keychain.syncedValue(forKey: tokenKey(connection.id)) == "FQoG") + #expect(fieldOnDisk(connection.id) == nil) + #expect(isDone) + } + + @Test("A value already in the Keychain wins over the plain-text copy, which only leaves the record") + func storedValueWins() { + let connection = legacyConnection() + #expect(storage.saveConnections([connection])) + keychain.seedSynced("saved-on-another-device", forKey: tokenKey(connection.id)) + + storage.migratePluginSecureFieldsIfNeeded() + + #expect(keychain.syncedValue(forKey: tokenKey(connection.id)) == "saved-on-another-device") + #expect(fieldOnDisk(connection.id) == nil) + #expect(isDone) + } + + @Test("A Keychain item that cannot be read keeps the plain-text field until it can") + func unreadableItemKeepsTheField() { + let connection = legacyConnection() + #expect(storage.saveConnections([connection])) + keychain.seedSynced("saved-on-another-device", forKey: tokenKey(connection.id)) + keychain.makeUnreadable(tokenKey(connection.id)) + + storage.migratePluginSecureFieldsIfNeeded() + + #expect(keychain.syncedValue(forKey: tokenKey(connection.id)) == "saved-on-another-device") + #expect(fieldOnDisk(connection.id) == "FQoG") + #expect(!isDone) + } + + @Test("An unreadable connections.json is not marked done, so the move runs once it reads again") + func unreadableLibraryRetries() throws { + try Data("not json".utf8).write(to: fileURL) + + storage.migratePluginSecureFieldsIfNeeded() + #expect(!isDone) + + try FileManager.default.removeItem(at: fileURL) + storage.invalidateCache() + let connection = legacyConnection() + #expect(storage.saveConnections([connection])) + storage.migratePluginSecureFieldsIfNeeded() + + #expect(keychain.syncedValue(forKey: tokenKey(connection.id)) == "FQoG") + #expect(fieldOnDisk(connection.id) == nil) + } + + @Test("A connection whose plugin is not registered keeps the move pending") + func unregisteredPluginKeepsItPending() { + let known = legacyConnection() + let unknown = TestFixtures.makeConnection(type: DatabaseType(rawValue: "UnregisteredEngine-\(UUID().uuidString)")) + #expect(storage.saveConnections([known, unknown])) + + storage.migratePluginSecureFieldsIfNeeded() + + #expect(keychain.syncedValue(forKey: tokenKey(known.id)) == "FQoG") + #expect(fieldOnDisk(known.id) == nil) + #expect(!isDone) + } + + @Test("A connections.json changed outside TablePro moves nothing into the Keychain") + func untrustedLibraryMovesNothing() throws { + let connection = legacyConnection() + #expect(storage.saveConnections([connection])) + var edited = try Data(contentsOf: fileURL) + edited.append(contentsOf: Array(" ".utf8)) + try edited.write(to: fileURL) + storage.invalidateCache() + + storage.migratePluginSecureFieldsIfNeeded() + + #expect(!storage.storeIsTrusted) + #expect(keychain.syncedValue(forKey: tokenKey(connection.id)) == nil) + #expect(fieldOnDisk(connection.id) == "FQoG") + #expect(!isDone) + } +} diff --git a/apple/TableProTests/Core/Storage/QueryHistoryStoreRecoveryTests.swift b/apple/TableProTests/Core/Storage/QueryHistoryStoreRecoveryTests.swift new file mode 100644 index 0000000000..6890b01285 --- /dev/null +++ b/apple/TableProTests/Core/Storage/QueryHistoryStoreRecoveryTests.swift @@ -0,0 +1,140 @@ +import Foundation +import os +import SQLite3 +@testable import TablePro +import Testing + +struct QueryHistoryStoreRecoveryTests { + private static let transient = unsafeBitCast(-1, to: sqlite3_destructor_type.self) + private let scratch: URL + + init() throws { + scratch = FileManager.default.temporaryDirectory + .appendingPathComponent("tablepro-tests") + .appendingPathComponent("history-recovery-\(UUID().uuidString)", isDirectory: true) + try FileManager.default.createDirectory(at: scratch, withIntermediateDirectories: true) + } + + // The pre-v3 shape, except that database_name allows NULL, which the v3 table refuses. + private func makeLegacyDatabase(rows: [(query: String, databaseName: String?)], connectionId: UUID) -> URL { + let url = scratch.appendingPathComponent("query_history.db") + var db: OpaquePointer? + guard sqlite3_open(url.path(percentEncoded: false), &db) == SQLITE_OK else { return url } + defer { sqlite3_close_v2(db) } + + sqlite3_exec(db, """ + CREATE TABLE history ( + id TEXT PRIMARY KEY, + query TEXT NOT NULL, + connection_id TEXT NOT NULL, + database_name TEXT, + executed_at REAL NOT NULL, + execution_time REAL NOT NULL, + row_count INTEGER NOT NULL, + was_successful INTEGER NOT NULL, + error_message TEXT + ); + """, nil, nil, nil) + sqlite3_exec(db, "PRAGMA user_version = 2;", nil, nil, nil) + + for row in rows { + var statement: OpaquePointer? + let insert = """ + INSERT INTO history (id, query, connection_id, database_name, executed_at, + execution_time, row_count, was_successful, error_message) + VALUES (?, ?, ?, ?, 1700000000, 0.5, 1, 1, NULL); + """ + guard sqlite3_prepare_v2(db, insert, -1, &statement, nil) == SQLITE_OK else { continue } + sqlite3_bind_text(statement, 1, UUID().uuidString, -1, Self.transient) + sqlite3_bind_text(statement, 2, row.query, -1, Self.transient) + sqlite3_bind_text(statement, 3, connectionId.uuidString, -1, Self.transient) + if let databaseName = row.databaseName { + sqlite3_bind_text(statement, 4, databaseName, -1, Self.transient) + } else { + sqlite3_bind_null(statement, 4) + } + sqlite3_step(statement) + sqlite3_finalize(statement) + } + return url + } + + private func scalarInt(in url: URL, _ sql: String) -> Int? { + var db: OpaquePointer? + guard sqlite3_open(url.path(percentEncoded: false), &db) == SQLITE_OK else { return nil } + defer { sqlite3_close_v2(db) } + var statement: OpaquePointer? + defer { sqlite3_finalize(statement) } + guard sqlite3_prepare_v2(db, sql, -1, &statement, nil) == SQLITE_OK, + sqlite3_step(statement) == SQLITE_ROW + else { return nil } + return Int(sqlite3_column_int64(statement, 0)) + } + + @Test("A version 3 migration whose copy fails keeps every legacy row and leaves the store closed") + func failedMigrationKeepsLegacyRows() async { + let url = makeLegacyDatabase( + rows: [(query: "SELECT 1", databaseName: "legacydb"), (query: "SELECT 2", databaseName: nil)], + connectionId: UUID() + ) + let storage = QueryHistoryStorage(databaseURL: url) + + #expect(await storage.isStoreAvailable() == false) + #expect(scalarInt(in: url, "SELECT COUNT(*) FROM history;") == 2) + #expect(scalarInt(in: url, "SELECT COUNT(*) FROM sqlite_master WHERE name = 'history_legacy';") == 0) + #expect(scalarInt(in: url, "SELECT COUNT(*) FROM pragma_table_info('history') WHERE name = 'source';") == 0) + #expect(scalarInt(in: url, "PRAGMA user_version;") == 2) + } + + @Test("A damaged history database is set aside once and recording works again") + func damagedStoreIsSetAside() async throws { + let url = scratch.appendingPathComponent("query_history.db") + let damaged = Data(repeating: 0x2A, count: 4_096) + try damaged.write(to: url) + let reports = OSAllocatedUnfairLock(initialState: [SetAsideDamagedStore]()) + let storage = QueryHistoryStorage( + databaseURL: url, + removeDatabaseOnDeinit: true, + onSetAside: { report in reports.withLock { $0.append(report) } } + ) + let entry = QueryHistoryEntry( + query: "SELECT 1", + connectionId: UUID(), + databaseName: "app", + databaseType: .postgresql, + source: .editor, + executionTime: 0.1, + rowCount: 1, + wasSuccessful: true + ) + + #expect(await storage.record(entry)) + #expect(await storage.isStoreAvailable()) + #expect(await storage.count(scope: .all) == 1) + + let setAside = try #require(reports.withLock { $0 }.first) + #expect(reports.withLock { $0.count } == 1) + #expect(setAside.storeURL == url) + #expect(try Data(contentsOf: setAside.setAsideURL) == damaged) + } + + @Test("Clearing every row also deletes the damaged copy set aside earlier, and a narrower clear keeps it") + func fullClearDeletesTheSetAsideCopy() async throws { + let url = scratch.appendingPathComponent("query_history.db") + try Data(repeating: 0x2A, count: 4_096).write(to: url) + let reports = OSAllocatedUnfairLock(initialState: [SetAsideDamagedStore]()) + let storage = QueryHistoryStorage( + databaseURL: url, + removeDatabaseOnDeinit: true, + onSetAside: { report in reports.withLock { $0.append(report) } } + ) + #expect(await storage.isStoreAvailable()) + let setAsidePath = try #require(reports.withLock { $0 }.first).setAsideURL.path(percentEncoded: false) + + #expect(await storage.clear(matching: QueryHistoryFilter(scope: .all, sources: QueryHistorySource.userAuthored))) + #expect(FileManager.default.fileExists(atPath: setAsidePath)) + + #expect(await storage.clear(matching: QueryHistoryFilter(scope: .all))) + #expect(!FileManager.default.fileExists(atPath: setAsidePath)) + } +} diff --git a/apple/TableProTests/Core/Storage/SQLFavoriteMigrationTests.swift b/apple/TableProTests/Core/Storage/SQLFavoriteMigrationTests.swift new file mode 100644 index 0000000000..16f6403c00 --- /dev/null +++ b/apple/TableProTests/Core/Storage/SQLFavoriteMigrationTests.swift @@ -0,0 +1,114 @@ +import Foundation +import SQLite3 +@testable import TablePro +import Testing + +struct SQLFavoriteMigrationTests { + private static let transient = unsafeBitCast(-1, to: sqlite3_destructor_type.self) + private let scratch: URL + + init() throws { + scratch = FileManager.default.temporaryDirectory + .appendingPathComponent("tablepro-tests") + .appendingPathComponent("sql-favorite-migration-\(UUID().uuidString)", isDirectory: true) + try FileManager.default.createDirectory(at: scratch, withIntermediateDirectories: true) + } + + // A version 1 store whose favorites.name allows NULL, which the version 2 table refuses. + private func makeVersion1Database(favorites: [(id: UUID, name: String?, query: String)], folderId: UUID) -> URL { + let url = scratch.appendingPathComponent("sql_favorites.db") + var db: OpaquePointer? + guard sqlite3_open(url.path(percentEncoded: false), &db) == SQLITE_OK else { return url } + defer { sqlite3_close_v2(db) } + + let schema = [ + """ + CREATE TABLE favorites ( + id TEXT PRIMARY KEY, name TEXT, query TEXT NOT NULL, + keyword TEXT, folder_id TEXT, connection_id TEXT, + sort_order INTEGER NOT NULL DEFAULT 0, created_at REAL NOT NULL, updated_at REAL NOT NULL + ); + """, + """ + CREATE TABLE folders ( + id TEXT PRIMARY KEY, name TEXT NOT NULL, parent_id TEXT, connection_id TEXT, + sort_order INTEGER NOT NULL DEFAULT 0, created_at REAL NOT NULL, updated_at REAL NOT NULL + ); + """, + "INSERT INTO folders (id, name, created_at, updated_at) VALUES ('\(folderId.uuidString)', 'Reports', 100, 100);", + "PRAGMA user_version = 1;", + ] + for sql in schema { + sqlite3_exec(db, sql, nil, nil, nil) + } + + for favorite in favorites { + var statement: OpaquePointer? + let insert = "INSERT INTO favorites (id, name, query, created_at, updated_at) VALUES (?, ?, ?, 100, 100);" + guard sqlite3_prepare_v2(db, insert, -1, &statement, nil) == SQLITE_OK else { continue } + sqlite3_bind_text(statement, 1, favorite.id.uuidString, -1, Self.transient) + if let name = favorite.name { + sqlite3_bind_text(statement, 2, name, -1, Self.transient) + } else { + sqlite3_bind_null(statement, 2) + } + sqlite3_bind_text(statement, 3, favorite.query, -1, Self.transient) + sqlite3_step(statement) + sqlite3_finalize(statement) + } + return url + } + + private func scalarInt(in url: URL, _ sql: String) -> Int? { + var db: OpaquePointer? + guard sqlite3_open(url.path(percentEncoded: false), &db) == SQLITE_OK else { return nil } + defer { sqlite3_close_v2(db) } + var statement: OpaquePointer? + defer { sqlite3_finalize(statement) } + guard sqlite3_prepare_v2(db, sql, -1, &statement, nil) == SQLITE_OK, + sqlite3_step(statement) == SQLITE_ROW + else { return nil } + return Int(sqlite3_column_int64(statement, 0)) + } + + @Test("A version 1 store migrates to version 2 with every saved query, folder and search entry") + func version1StoreMigrates() async { + let revenue = UUID() + let churn = UUID() + let folderId = UUID() + let url = makeVersion1Database( + favorites: [(id: revenue, name: "Revenue", query: "SELECT 1"), (id: churn, name: "Churn", query: "SELECT 2")], + folderId: folderId + ) + let storage = SQLFavoriteStorage(databaseURL: url) + + let favoriteIds = await storage.readAllFavorites()?.map(\.id) + let folderIds = await storage.readAllFolders()?.map(\.id) + let searched = await storage.fetchFavorites(searchText: "Churn").map(\.id) + + #expect(Set(favoriteIds ?? []) == [revenue, churn]) + #expect(folderIds == [folderId]) + #expect(searched == [churn]) + #expect(scalarInt(in: url, "PRAGMA user_version;") == 2) + #expect(scalarInt(in: url, "SELECT COUNT(*) FROM sqlite_master WHERE name IN ('favorites_old', 'folders_old');") == 0) + } + + @Test("A version 2 migration whose copy fails rolls back and keeps every saved query") + func failedMigrationKeepsEverySavedQuery() async { + let named = UUID() + let unnamed = UUID() + let url = makeVersion1Database( + favorites: [(id: named, name: "Revenue", query: "SELECT 1"), (id: unnamed, name: nil, query: "SELECT 2")], + folderId: UUID() + ) + let storage = SQLFavoriteStorage(databaseURL: url) + + let readable = await storage.readAllFavorites()?.map(\.id) + + #expect(readable == [named]) + #expect(scalarInt(in: url, "SELECT COUNT(*) FROM favorites;") == 2) + #expect(scalarInt(in: url, "SELECT COUNT(*) FROM folders;") == 1) + #expect(scalarInt(in: url, "SELECT COUNT(*) FROM sqlite_master WHERE name IN ('favorites_old', 'folders_old');") == 0) + #expect(scalarInt(in: url, "PRAGMA user_version;") == 1) + } +} diff --git a/apple/TableProTests/Core/Storage/SQLFavoriteStorageOpenTests.swift b/apple/TableProTests/Core/Storage/SQLFavoriteStorageOpenTests.swift index beadb3cc17..01d54bf85f 100644 --- a/apple/TableProTests/Core/Storage/SQLFavoriteStorageOpenTests.swift +++ b/apple/TableProTests/Core/Storage/SQLFavoriteStorageOpenTests.swift @@ -1,4 +1,5 @@ import Foundation +import os @testable import TablePro import Testing @@ -57,13 +58,72 @@ struct SQLFavoriteStorageOpenTests { #expect(await storage.readAllFolders()?.map(\.id) == [folder.id]) } - @Test("A file that is not a database reads as unreadable, not as an empty store") - func notADatabaseReadsAsUnreadable() async throws { - let url = scratch.appendingPathComponent("sql_favorites.db") - try Data(repeating: 0x2A, count: 4_096).write(to: url) - let storage = SQLFavoriteStorage(databaseURL: url, removeDatabaseOnDeinit: true) + @Test("A store that cannot be opened reads as unreadable, not as an empty store") + func unopenableStoreReadsAsUnreadable() async throws { + let blocker = scratch.appendingPathComponent("favorites-directory") + #expect(FileManager.default.createFile(atPath: blocker.path(percentEncoded: false), contents: Data())) + let storage = SQLFavoriteStorage( + databaseURL: blocker.appendingPathComponent("sql_favorites.db"), + removeDatabaseOnDeinit: true + ) #expect(await storage.readAllFavorites() == nil) #expect(await storage.readAllFolders() == nil) } + + @Test("A file that is not a database is set aside once, and saving queries works again") + func notADatabaseIsSetAside() async throws { + let url = scratch.appendingPathComponent("sql_favorites.db") + let damaged = Data(repeating: 0x2A, count: 4_096) + try damaged.write(to: url) + let reports = OSAllocatedUnfairLock(initialState: [SetAsideDamagedStore]()) + let storage = SQLFavoriteStorage( + databaseURL: url, + removeDatabaseOnDeinit: true, + onSetAside: { report in reports.withLock { $0.append(report) } } + ) + let favorite = SQLFavorite(name: "Revenue", query: "SELECT 1") + + #expect(await storage.addFavorite(favorite)) + #expect(await storage.readAllFavorites()?.map(\.id) == [favorite.id]) + + let setAside = try #require(reports.withLock { $0 }.first) + #expect(reports.withLock { $0.count } == 1) + #expect(setAside.storeURL == url) + #expect(try Data(contentsOf: setAside.setAsideURL) == damaged) + } + + @Test("A store that replaced a damaged one reads as unreadable for sync, across a relaunch, until refilled") + func replacedStoreWaitsForRefill() async throws { + let url = scratch.appendingPathComponent("sql_favorites.db") + try Data(repeating: 0x2A, count: 4_096).write(to: url) + let favorite = SQLFavorite(name: "Revenue", query: "SELECT 1") + let first = SQLFavoriteStorage(databaseURL: url) + #expect(await first.addFavorite(favorite)) + + #expect(await first.isAwaitingSyncRefill()) + #expect(await first.readAllFavoritesForSync() == nil) + #expect(await first.readAllFoldersForSync() == nil) + #expect(await first.readAllFavorites()?.map(\.id) == [favorite.id]) + + let relaunched = SQLFavoriteStorage(databaseURL: url) + #expect(await relaunched.isAwaitingSyncRefill()) + await relaunched.finishSyncRefill() + #expect(await relaunched.isAwaitingSyncRefill() == false) + #expect(await relaunched.readAllFavoritesForSync()?.map(\.id) == [favorite.id]) + + let afterRefill = SQLFavoriteStorage(databaseURL: url) + #expect(await afterRefill.isAwaitingSyncRefill() == false) + } + + @Test("A healthy store is never waiting for a refill") + func healthyStoreIsNotWaiting() async { + let storage = SQLFavoriteStorage( + databaseURL: scratch.appendingPathComponent("sql_favorites.db"), + removeDatabaseOnDeinit: true + ) + + #expect(await storage.isAwaitingSyncRefill() == false) + #expect(await storage.readAllFavoritesForSync()?.isEmpty == true) + } } diff --git a/apple/TableProTests/Core/Sync/SyncFullFetchDeletionTests.swift b/apple/TableProTests/Core/Sync/SyncFullFetchDeletionTests.swift index c7a6b21ec5..40104d1065 100644 --- a/apple/TableProTests/Core/Sync/SyncFullFetchDeletionTests.swift +++ b/apple/TableProTests/Core/Sync/SyncFullFetchDeletionTests.swift @@ -95,11 +95,12 @@ struct SyncFullFetchDeletionTests { #expect(await environment.favorites.fetchFavorite(id: edited.id) != nil) } - @Test("A full fetch that could not read a record deletes nothing") - func unreadRecordStopsTheDeletions() async throws { + @Test("A record a full fetch listed but could not read is kept, and the deletions the fetch proves still apply") + func unreadRecordIsKeptAndDeletionsStillApply() async throws { let kept = SQLFavorite(name: "Churn", query: "SELECT 2") let unread = SQLFavorite(name: "Revenue", query: "SELECT 1") - let coordinator = try await seededCoordinator(with: [kept, unread]) + let deleted = SQLFavorite(name: "Retention", query: "SELECT 3") + let coordinator = try await seededCoordinator(with: [kept, unread, deleted]) let result = PullResult( changedRecords: [Self.record(kept)], deletedRecordIDs: [], @@ -109,7 +110,9 @@ struct SyncFullFetchDeletionTests { #expect(await coordinator.applyPullResult(result, settings: Self.syncSettings(), isFullFetch: true)) + #expect(await environment.favorites.fetchFavorite(id: kept.id) != nil) #expect(await environment.favorites.fetchFavorite(id: unread.id) != nil) + #expect(await environment.favorites.fetchFavorite(id: deleted.id) == nil) } @Test("A delta is never read for what it does not list") diff --git a/apple/TableProTests/Core/Sync/SyncRemoteConnectionSecretsTests.swift b/apple/TableProTests/Core/Sync/SyncRemoteConnectionSecretsTests.swift new file mode 100644 index 0000000000..d3c157479d --- /dev/null +++ b/apple/TableProTests/Core/Sync/SyncRemoteConnectionSecretsTests.swift @@ -0,0 +1,75 @@ +import CloudKit +import Foundation +@testable import TablePro +import TableProSyncTransport +import Testing + +@MainActor +struct SyncRemoteConnectionSecretsTests { + private static let zoneID = SyncTestEnvironment.zoneID + + private let environment: SyncTestEnvironment + + init() throws { + environment = try SyncTestEnvironment(label: "sync-remote-connection-secrets") + } + + private var storage: ConnectionStorage { environment.connections } + + private func secretKeys(of id: UUID) -> [String] { + [ + "password", "sshpassword", "keypassphrase", "totpsecret", "cloudflaretokensecret", + "cloudsqlproxyserviceaccountkey", "socksproxypassword", "plugin.mongoAWSSessionToken" + ].map { "com.TablePro.\($0).\(id.uuidString)" } + } + + private func addConnectionWithSecrets(localOnly: Bool = false) -> DatabaseConnection { + var connection = TestFixtures.makeConnection(type: .mongodb) + connection.localOnly = localOnly + #expect(storage.addConnection(connection, password: "hunter2")) + storage.saveSSHPassword("ssh", for: connection.id) + storage.saveKeyPassphrase("passphrase", for: connection.id) + storage.saveTOTPSecret("JBSWY3DP", for: connection.id) + storage.saveCloudflareTokenSecret("cf", for: connection.id) + storage.saveCloudSQLProxyServiceAccountKey("{}", for: connection.id) + storage.saveSOCKSProxyPassword("proxy", for: connection.id) + storage.savePluginSecureField("FQoG", fieldId: "mongoAWSSessionToken", for: connection.id) + return connection + } + + private func deletion(of connection: DatabaseConnection) -> PullResult { + PullResult( + changedRecords: [], + deletedRecordIDs: [SyncRecordMapper.recordID(type: .connection, id: connection.id.uuidString, in: Self.zoneID)], + newToken: nil + ) + } + + @Test("A connection deleted on another device takes its secrets out of this Mac's Keychain") + func remoteDeletionRemovesTheSecrets() async { + let connection = addConnectionWithSecrets() + + let acknowledged = await environment.makeCoordinator(transport: ScriptedSyncTransport(zoneID: Self.zoneID)) + .applyPullResult(deletion(of: connection)) + + #expect(acknowledged) + #expect(storage.loadConnection(id: connection.id) == nil) + for key in secretKeys(of: connection.id) { + #expect(environment.keychain.readStringResult(forKey: key) == .notFound, "\(key)") + } + } + + @Test("A Local only connection named by a deletion from another device keeps its row and its secrets") + func localOnlyConnectionKeepsItsSecrets() async { + let connection = addConnectionWithSecrets(localOnly: true) + + let acknowledged = await environment.makeCoordinator(transport: ScriptedSyncTransport(zoneID: Self.zoneID)) + .applyPullResult(deletion(of: connection)) + + #expect(acknowledged) + #expect(storage.loadConnection(id: connection.id) != nil) + for key in secretKeys(of: connection.id) { + #expect(environment.keychain.readStringResult(forKey: key) != .notFound, "\(key)") + } + } +} diff --git a/apple/TableProTests/Core/Sync/SyncSavedQueriesRefillTests.swift b/apple/TableProTests/Core/Sync/SyncSavedQueriesRefillTests.swift new file mode 100644 index 0000000000..ca72933878 --- /dev/null +++ b/apple/TableProTests/Core/Sync/SyncSavedQueriesRefillTests.swift @@ -0,0 +1,195 @@ +import CloudKit +import Foundation +@testable import TablePro +import TableProSyncTransport +import Testing + +@MainActor +struct SyncSavedQueriesRefillTests { + private static let zoneID = SyncTestEnvironment.zoneID + + private let environment: SyncTestEnvironment + + init() throws { + environment = try SyncTestEnvironment(label: "sync-saved-queries-refill") + var settings = SyncSettings.default + settings.enabled = true + AppSettingsStorage(userDefaults: environment.defaults).saveSync(settings) + _ = environment.metadata.adoptReadableRecordTypes(SyncCoordinator.readableContent(for: settings)) + } + + @Test("A damaged saved queries store is refilled by a full fetch although a sync token is stored") + func damagedStoreIsRefilledByAFullFetch() async throws { + let favorite = SQLFavorite(name: "Revenue", query: "SELECT 1") + let token = try Self.makeChangeToken() + environment.metadata.saveToken(token) + let server = RefillZoneServer(zoneID: Self.zoneID, records: [Self.record(favorite)], token: token) + let damaged = try damagedFavorites() + let coordinator = makeCoordinator(transport: server, favorites: damaged) + + #expect(await coordinator.runSyncCycle() == nil) + + #expect(await server.pulls == [.full]) + #expect(await damaged.fetchFavorite(id: favorite.id)?.name == "Revenue") + #expect(await damaged.isAwaitingSyncRefill() == false) + + #expect(await coordinator.runSyncCycle() == nil) + #expect(await server.pulls == [.full, .delta]) + } + + @Test("A saved query deleted and then edited again before the damage comes back instead of being deleted everywhere") + func restoredSavedQuerySurvivesTheRefill() async throws { + let restored = SQLFavorite(name: "Revenue", query: "SELECT 1") + let id = restored.id.uuidString + environment.tracker.markDeleted(.favorite, id: id) + environment.tracker.markDirty(.favorite, id: id) + let token = try Self.makeChangeToken() + environment.metadata.saveToken(token) + let server = RefillZoneServer(zoneID: Self.zoneID, records: [Self.record(restored)], token: token) + let damaged = try damagedFavorites() + let coordinator = makeCoordinator(transport: server, favorites: damaged) + + #expect(await coordinator.runSyncCycle() == nil) + #expect(await coordinator.runSyncCycle() == nil) + + #expect(await server.pushedDeletionNames.isEmpty) + #expect(await damaged.fetchFavorite(id: restored.id)?.name == "Revenue") + } + + @Test("A full fetch that left a saved query unread keeps the refill going until a fetch delivers it") + func unreadSavedQueryKeepsTheRefill() async throws { + let delivered = SQLFavorite(name: "Revenue", query: "SELECT 1") + let late = SQLFavorite(name: "Churn", query: "SELECT 2") + let token = try Self.makeChangeToken() + environment.metadata.saveToken(token) + let server = RefillZoneServer( + zoneID: Self.zoneID, + records: [Self.record(delivered)], + token: token, + unread: [Self.record(late).recordID] + ) + let damaged = try damagedFavorites() + let coordinator = makeCoordinator(transport: server, favorites: damaged) + + #expect(await coordinator.runSyncCycle() == nil) + #expect(await damaged.isAwaitingSyncRefill()) + + await server.serve([Self.record(delivered), Self.record(late)], unread: []) + #expect(await coordinator.runSyncCycle() == nil) + + #expect(await server.pulls == [.full, .full]) + #expect(await damaged.isAwaitingSyncRefill() == false) + #expect(await damaged.fetchFavorite(id: late.id)?.name == "Churn") + } + + private func damagedFavorites() throws -> SQLFavoriteManager { + let url = environment.directory.appendingPathComponent("damaged-sql_favorites.db") + try Data(repeating: 0x2A, count: 4_096).write(to: url) + return SQLFavoriteManager( + storage: SQLFavoriteStorage(databaseURL: url, removeDatabaseOnDeinit: true), + syncTracker: environment.tracker + ) + } + + private func makeCoordinator(transport: any SyncTransport, favorites: SQLFavoriteManager) -> SyncCoordinator { + let base = environment.makeCoordinator(transport: ScriptedSyncTransport(zoneID: Self.zoneID), favorites: favorites) + return SyncCoordinator( + services: base.services, + recordCache: environment.recordCache, + transport: transport, + makeNetworkMonitor: { nil }, + columnLayouts: environment.columnLayouts, + licenseGate: { nil } + ) + } + + private static func record(_ favorite: SQLFavorite) -> CKRecord { + SyncRecordMapper.toCKRecord(sqlFavorite: favorite, in: zoneID) + } + + private static func makeChangeToken() throws -> CKServerChangeToken { + let archiver = NSKeyedArchiver(requiringSecureCoding: true) + archiver.setClassName("CKServerChangeToken", for: RefillChangeTokenArchive.self) + archiver.encode(RefillChangeTokenArchive(), forKey: NSKeyedArchiveRootObjectKey) + archiver.finishEncoding() + let token = try NSKeyedUnarchiver.unarchivedObject(ofClass: CKServerChangeToken.self, from: archiver.encodedData) + return try #require(token) + } +} + +// Serves every record on a full fetch and nothing on a delta, and records what was asked and pushed. +private actor RefillZoneServer: SyncTransport { + enum Pull: Equatable { + case full + case delta + } + + let currentZoneID: CKRecordZone.ID + private var records: [CKRecord] + private var unread: [CKRecord.ID] + private let token: CKServerChangeToken + private(set) var pulls: [Pull] = [] + private(set) var pushedDeletionNames: [String] = [] + + init(zoneID: CKRecordZone.ID, records: [CKRecord], token: CKServerChangeToken, unread: [CKRecord.ID] = []) { + currentZoneID = zoneID + self.records = records + self.token = token + self.unread = unread + } + + func serve(_ records: [CKRecord], unread: [CKRecord.ID]) { + self.records = records + self.unread = unread + } + + func accountStatus() async throws -> CKAccountStatus { + .available + } + + func currentAccountId() async throws -> String { + ScriptedSyncTransport.accountId + } + + func ensureZoneExists() async throws {} + + func push(records: [CKRecord], deletions: [CKRecord.ID]) async throws -> PushOutcome { + var outcome = PushOutcome() + for record in records { + outcome.recordSave(record) + } + for recordID in deletions { + pushedDeletionNames.append(recordID.recordName) + outcome.recordDeletion(recordID) + } + return outcome + } + + func fetch(recordIDs: [CKRecord.ID]) async throws -> [CKRecord.ID: CKRecord] { + [:] + } + + func pull(since token: CKServerChangeToken?) async throws -> PullResult { + guard token != nil else { + pulls.append(.full) + return PullResult(changedRecords: records, deletedRecordIDs: [], newToken: self.token, unreadRecordIDs: unread) + } + pulls.append(.delta) + return PullResult(changedRecords: [], deletedRecordIDs: [], newToken: self.token) + } +} + +@objc(SyncSavedQueriesRefillTestsChangeTokenArchive) +private final class RefillChangeTokenArchive: NSObject, NSSecureCoding { + static var supportsSecureCoding: Bool { true } + + override init() { + super.init() + } + + required init?(coder: NSCoder) { + super.init() + } + + func encode(with coder: NSCoder) {} +} diff --git a/apple/TableProTests/Helpers/ArrayRowProvider.swift b/apple/TableProTests/Helpers/ArrayRowProvider.swift new file mode 100644 index 0000000000..c7295ae291 --- /dev/null +++ b/apple/TableProTests/Helpers/ArrayRowProvider.swift @@ -0,0 +1,29 @@ +@testable import TablePro + +final class ArrayRowProvider: DataRowProviding { + private let rows: [DataRow] + private let rowLimit: Int? + private let holdsRowPastTheLimit: Bool + private var index = 0 + + init(rows: [DataRow], rowLimit: Int? = nil) { + self.rows = rowLimit.map { Array(rows.prefix($0)) } ?? rows + self.rowLimit = rowLimit + self.holdsRowPastTheLimit = rowLimit.map { rows.count > $0 } ?? false + } + + var endedAtRowLimit: Bool { + guard let rowLimit, index >= rows.count else { return false } + return holdsRowPastTheLimit && rows.count >= rowLimit + } + + func nextRow() async throws -> DataRow? { + guard index < rows.count else { return nil } + defer { index += 1 } + return rows[index] + } + + func drain() async throws { + index = rows.count + } +} diff --git a/apple/TableProTests/Helpers/MCPFixedApprovalReader.swift b/apple/TableProTests/Helpers/MCPFixedApprovalReader.swift new file mode 100644 index 0000000000..bd34ea2226 --- /dev/null +++ b/apple/TableProTests/Helpers/MCPFixedApprovalReader.swift @@ -0,0 +1,13 @@ +@testable import TablePro + +struct MCPFixedApprovalReader: MCPConnectionApprovalReading { + let approval: MCPConnectionApproval + + init(_ approval: MCPConnectionApproval) { + self.approval = approval + } + + func connectionApproval() async -> MCPConnectionApproval { + approval + } +} diff --git a/apple/TableProTests/Helpers/MCPInMemoryApprovalStore.swift b/apple/TableProTests/Helpers/MCPInMemoryApprovalStore.swift new file mode 100644 index 0000000000..28c495afa9 --- /dev/null +++ b/apple/TableProTests/Helpers/MCPInMemoryApprovalStore.swift @@ -0,0 +1,40 @@ +import Foundation +@testable import TablePro + +actor MCPInMemoryApprovalStore: MCPApprovalStoring { + private var storedGrants: [MCPConnectionGrant] = [] + private var storedDenials: [MCPConnectionDenial] = [] + + init() {} + + func grants() -> [MCPConnectionGrant] { + storedGrants + } + + func record(_ grant: MCPConnectionGrant) { + storedGrants.removeAll { $0.id == grant.id } + storedGrants.append(grant) + } + + func revoke(subject: String, connectionId: UUID) { + storedGrants.removeAll { $0.subject == subject && $0.connectionId == connectionId } + } + + func revokeAll(subject: String) { + storedGrants.removeAll { $0.subject == subject } + } + + func revokeEverything() { + storedGrants.removeAll() + } + + func denial(subject: String, connectionId: UUID, now: Date) -> Date? { + storedDenials.removeAll { $0.expiresAt <= now } + return storedDenials.first { $0.subject == subject && $0.connectionId == connectionId }?.expiresAt + } + + func record(_ denial: MCPConnectionDenial) { + storedDenials.removeAll { $0.subject == denial.subject && $0.connectionId == denial.connectionId } + storedDenials.append(denial) + } +} diff --git a/apple/TableProTests/Helpers/ScopeRecordingKeychain.swift b/apple/TableProTests/Helpers/ScopeRecordingKeychain.swift new file mode 100644 index 0000000000..12d980e421 --- /dev/null +++ b/apple/TableProTests/Helpers/ScopeRecordingKeychain.swift @@ -0,0 +1,105 @@ +import Foundation +@testable import TablePro + +// One synced and one local value per key, as the keychain holds them. Only a delete that is not device-only removes +// a synced value. +final class ScopeRecordingKeychain: KeychainStoring, @unchecked Sendable { + private let lock = NSLock() + private var synced: [String: String] = [:] + private var local: [String: String] = [:] + private var unreadableKeys: Set = [] + private var failingKeys: Set = [] + + func seedSynced(_ value: String, forKey key: String) { + lock.lock() + defer { lock.unlock() } + synced[key] = value + } + + func seedLocal(_ value: String, forKey key: String) { + lock.lock() + defer { lock.unlock() } + local[key] = value + } + + func makeUnreadable(_ key: String) { + lock.lock() + defer { lock.unlock() } + unreadableKeys.insert(key) + } + + func makeReadable(_ key: String) { + lock.lock() + defer { lock.unlock() } + unreadableKeys.remove(key) + } + + func failWrites(to key: String) { + lock.lock() + defer { lock.unlock() } + failingKeys.insert(key) + } + + func allowWrites(to key: String) { + lock.lock() + defer { lock.unlock() } + failingKeys.remove(key) + } + + func syncedValue(forKey key: String) -> String? { + lock.lock() + defer { lock.unlock() } + return synced[key] + } + + func localValue(forKey key: String) -> String? { + lock.lock() + defer { lock.unlock() } + return local[key] + } + + @discardableResult + func writeString(_ value: String, forKey key: String) -> Bool { + writeString(value, forKey: key, deviceOnly: false) + } + + // Password sync is on in this model, so a write that is not device-only is a synced one. + @discardableResult + func writeString(_ value: String, forKey key: String, deviceOnly: Bool) -> Bool { + lock.lock() + defer { lock.unlock() } + guard !failingKeys.contains(key) else { return false } + if deviceOnly { + local[key] = value + } else { + synced[key] = value + local[key] = nil + } + return true + } + + func readStringResult(forKey key: String) -> KeychainStringResult { + readStringResult(forKey: key, deviceOnly: false) + } + + func readStringResult(forKey key: String, deviceOnly: Bool) -> KeychainStringResult { + lock.lock() + defer { lock.unlock() } + guard !unreadableKeys.contains(key) else { return .locked } + let value = deviceOnly ? local[key] ?? synced[key] : synced[key] ?? local[key] + return value.map { .found($0) } ?? .notFound + } + + func delete(forKey key: String) { + delete(forKey: key, deviceOnly: false) + } + + func delete(forKey key: String, deviceOnly: Bool) { + lock.lock() + defer { lock.unlock() } + local[key] = nil + if !deviceOnly { + synced[key] = nil + } + } +} diff --git a/apple/TableProTests/ViewModels/AIChatViewModelToolLoopTests.swift b/apple/TableProTests/ViewModels/AIChatViewModelToolLoopTests.swift index 0a7cd2519d..22fd5980cb 100644 --- a/apple/TableProTests/ViewModels/AIChatViewModelToolLoopTests.swift +++ b/apple/TableProTests/ViewModels/AIChatViewModelToolLoopTests.swift @@ -46,6 +46,32 @@ struct AIChatViewModelToolLoopTests { } } + private final class ExecutionCounter: @unchecked Sendable { + private let lock = NSLock() + private var count = 0 + + var value: Int { + lock.withLock { count } + } + + func increment() { + lock.withLock { count += 1 } + } + } + + private struct CountingTool: ChatTool { + let name = "counting" + let description = "" + let inputSchema: JsonValue = .object(["type": .string("object")]) + let mode: ChatToolMode = .readOnly + let counter: ExecutionCounter + + func execute(input: JsonValue, context: ChatToolContext) async throws -> ChatToolResult { + counter.increment() + return ChatToolResult(content: "ran", isError: false) + } + } + private static func toolRound(id: String) -> [ChatStreamEvent] { [ .toolUseStart(id: id, name: "noop", providerMetadata: nil), @@ -81,10 +107,13 @@ struct AIChatViewModelToolLoopTests { private func runLoop( viewModel: AIChatViewModel, transport: ChatTransport, - settings: AISettings + settings: AISettings, + tools: [any ChatTool] = [NoopTool()] ) async { let registry = ChatToolRegistry() - registry.register(NoopTool()) + for tool in tools { + registry.register(tool) + } let assistant = ChatTurn(role: .assistant, blocks: [], modelId: "test-model", providerId: nil) viewModel.messages.append(assistant) viewModel.streamingState = .streaming(assistantID: assistant.id) @@ -218,6 +247,44 @@ struct AIChatViewModelToolLoopTests { #expect(viewModel.messages.last?.plainText.contains("finished") == true) } + @Test("A call whose arguments were cut off never runs, and the model gets an error saying why") + func truncatedToolCallNeverRuns() async { + let counter = ExecutionCounter() + let transport = ScriptedTransport(rounds: [ + [ + .toolUseStart(id: "cut", name: "counting", providerMetadata: nil), + .toolUseDelta(id: "cut", inputJSONDelta: #"{"query":"SELECT * FROM orders WH"#), + .toolUseEnd(id: "cut") + ], + [.textDelta("retrying")] + ]) + let viewModel = AIChatViewModel() + + await runLoop( + viewModel: viewModel, + transport: transport, + settings: Self.makeSettings(limit: Self.testLimit, enabled: true), + tools: [CountingTool(counter: counter)] + ) + + #expect(counter.value == 0) + let denial = AIChatViewModel.unparsableToolArgumentsContent + let calls = viewModel.messages.flatMap { $0.blocks }.compactMap { block -> ToolUseBlock? in + guard case .toolUse(let call) = block.kind else { return nil } + return call + } + let callIds = calls.map { $0.id } + #expect(callIds == ["cut"]) + #expect(calls.first?.approvalState == .denied(reason: denial)) + let results = viewModel.messages.flatMap { $0.blocks }.compactMap { block -> ToolResultBlock? in + guard case .toolResult(let result) = block.kind else { return nil } + return result + } + #expect(results == [ToolResultBlock(toolUseId: "cut", content: denial, isError: true)]) + #expect(transport.roundsRequested == 2) + #expect(viewModel.messages.last?.plainText.contains("retrying") == true) + } + @Test("continueToolLoop only acts while paused") func continueIsInertWhenNotPaused() { let viewModel = AIChatViewModel() diff --git a/apple/changelog.d/4184.fixed.1.md b/apple/changelog.d/4184.fixed.1.md new file mode 100644 index 0000000000..ba9a7c9dea --- /dev/null +++ b/apple/changelog.d/4184.fixed.1.md @@ -0,0 +1 @@ +A synced change CloudKit failed to deliver during a pull was skipped for good instead of arriving on the next sync. diff --git a/apple/changelog.d/4184.fixed.10.md b/apple/changelog.d/4184.fixed.10.md new file mode 100644 index 0000000000..d01be0fc3f --- /dev/null +++ b/apple/changelog.d/4184.fixed.10.md @@ -0,0 +1 @@ +An AI chat tool call whose arguments did not parse ran with empty arguments. diff --git a/apple/changelog.d/4184.fixed.11.md b/apple/changelog.d/4184.fixed.11.md new file mode 100644 index 0000000000..b26586b441 --- /dev/null +++ b/apple/changelog.d/4184.fixed.11.md @@ -0,0 +1 @@ +ChatGPT sign-in failed if any other request reached its callback port, and waited 5 minutes after you denied access. diff --git a/apple/changelog.d/4184.fixed.12.md b/apple/changelog.d/4184.fixed.12.md new file mode 100644 index 0000000000..6d4796b5e1 --- /dev/null +++ b/apple/changelog.d/4184.fixed.12.md @@ -0,0 +1 @@ +Vault OIDC sign-in ended early when any other request reached its callback port. diff --git a/apple/changelog.d/4184.fixed.13.md b/apple/changelog.d/4184.fixed.13.md new file mode 100644 index 0000000000..fb5b3a0f4f --- /dev/null +++ b/apple/changelog.d/4184.fixed.13.md @@ -0,0 +1 @@ +Snowflake browser sign-in failed when any other request reached its local callback port. diff --git a/apple/changelog.d/4184.fixed.2.md b/apple/changelog.d/4184.fixed.2.md new file mode 100644 index 0000000000..5c09552a5c --- /dev/null +++ b/apple/changelog.d/4184.fixed.2.md @@ -0,0 +1 @@ +Upgrading from an old release could delete saved queries or query history when copying them failed. diff --git a/apple/changelog.d/4184.fixed.3.md b/apple/changelog.d/4184.fixed.3.md new file mode 100644 index 0000000000..edf719bad3 --- /dev/null +++ b/apple/changelog.d/4184.fixed.3.md @@ -0,0 +1 @@ +A damaged query history or saved queries file left both empty and silently stopped saving anything. diff --git a/apple/changelog.d/4184.fixed.4.md b/apple/changelog.d/4184.fixed.4.md new file mode 100644 index 0000000000..0dcea2f734 --- /dev/null +++ b/apple/changelog.d/4184.fixed.4.md @@ -0,0 +1 @@ +A taken local port made an SSH tunnel sign in twice, asking for the passphrase or TOTP code again. diff --git a/apple/changelog.d/4184.fixed.5.md b/apple/changelog.d/4184.fixed.5.md new file mode 100644 index 0000000000..291a877e12 --- /dev/null +++ b/apple/changelog.d/4184.fixed.5.md @@ -0,0 +1 @@ +A damaged query history file on iPhone or iPad was replaced by the next query run. diff --git a/apple/changelog.d/4184.fixed.6.md b/apple/changelog.d/4184.fixed.6.md new file mode 100644 index 0000000000..85a154693a --- /dev/null +++ b/apple/changelog.d/4184.fixed.6.md @@ -0,0 +1 @@ +A damaged linked folder index left linked SQL files and their keywords missing until the file was deleted. diff --git a/apple/changelog.d/4184.fixed.7.md b/apple/changelog.d/4184.fixed.7.md new file mode 100644 index 0000000000..de8a0f3864 --- /dev/null +++ b/apple/changelog.d/4184.fixed.7.md @@ -0,0 +1 @@ +A damaged MCP activity log stopped recording MCP activity until the file was deleted. diff --git a/apple/changelog.d/4184.fixed.8.md b/apple/changelog.d/4184.fixed.8.md new file mode 100644 index 0000000000..f7ec2f9d15 --- /dev/null +++ b/apple/changelog.d/4184.fixed.8.md @@ -0,0 +1 @@ +The first MCP activity after each launch could be missing from the activity log. diff --git a/apple/changelog.d/4184.fixed.9.md b/apple/changelog.d/4184.fixed.9.md new file mode 100644 index 0000000000..0e0c7eeee7 --- /dev/null +++ b/apple/changelog.d/4184.fixed.9.md @@ -0,0 +1 @@ +A request from an MCP client using the bridge could be cancelled when the bridge renewed its credential. diff --git a/apple/changelog.d/4184.fixed.md b/apple/changelog.d/4184.fixed.md new file mode 100644 index 0000000000..0cf0b208f0 --- /dev/null +++ b/apple/changelog.d/4184.fixed.md @@ -0,0 +1 @@ +Deleting a Local only connection removed its passwords from your other devices that still sync it. diff --git a/apple/changelog.d/4184.security.1.md b/apple/changelog.d/4184.security.1.md new file mode 100644 index 0000000000..9f8e493e79 --- /dev/null +++ b/apple/changelog.d/4184.security.1.md @@ -0,0 +1 @@ +A connection deleted on another device left its passwords and other secrets in this Mac's Keychain. diff --git a/apple/changelog.d/4184.security.2.md b/apple/changelog.d/4184.security.2.md new file mode 100644 index 0000000000..739d565123 --- /dev/null +++ b/apple/changelog.d/4184.security.2.md @@ -0,0 +1 @@ +Plugin secure fields could stay in plain text in connections.json after the upgrade that moves them to the Keychain. diff --git a/apple/changelog.d/4184.security.3.md b/apple/changelog.d/4184.security.3.md new file mode 100644 index 0000000000..d441f56093 --- /dev/null +++ b/apple/changelog.d/4184.security.3.md @@ -0,0 +1 @@ +Another app on the Mac could take the IPv6 side of a sign-in callback port and receive the browser's redirect. diff --git a/apple/changelog.d/4184.security.md b/apple/changelog.d/4184.security.md new file mode 100644 index 0000000000..5a36175cfd --- /dev/null +++ b/apple/changelog.d/4184.security.md @@ -0,0 +1 @@ +A plugin could load with any valid signature when TablePro's signing requirement failed to build.