diff --git a/apple/Packages/TableProCore/Sources/TableProSSHTransport/KnownHostsFile.swift b/apple/Packages/TableProCore/Sources/TableProSSHTransport/KnownHostsFile.swift new file mode 100644 index 0000000000..50f06023f9 --- /dev/null +++ b/apple/Packages/TableProCore/Sources/TableProSSHTransport/KnownHostsFile.swift @@ -0,0 +1,105 @@ +import Foundation + +// The apps' own known_hosts, one `[host]:port keyType base64` entry per line. Lines are written back as they were +// read, so a rewrite never drops one this build cannot parse. +public struct KnownHostsFile: Sendable { + public struct Entry: Sendable, Equatable { + public let host: String + public let keyType: String + public let keyData: Data + + public init(host: String, keyType: String, keyData: Data) { + self.host = host + self.keyType = keyType + self.keyData = keyData + } + } + + public enum Match: Sendable, Equatable { + case trusted + case unknown + case changed(storedKey: Data) + // A line names the host but its key cannot be parsed, so the host is not new. + case unreadable + } + + private struct Line: Sendable { + let text: String + let host: String? + let entry: Entry? + } + + private var lines: [Line] + + public init() { + lines = [] + } + + public init(contents: String) { + lines = contents.components(separatedBy: "\n").compactMap(Self.line) + } + + // Only a missing file reads as empty: after any other failure every stored host would look new. + public static func load(from url: URL) throws -> KnownHostsFile { + let data: Data + do { + data = try Data(contentsOf: url) + } catch let error as CocoaError where error.code == .fileReadNoSuchFile { + return KnownHostsFile() + } + guard let contents = String(data: data, encoding: .utf8) else { + throw CocoaError(.fileReadInapplicableStringEncoding) + } + return KnownHostsFile(contents: contents) + } + + public var entries: [Entry] { + lines.compactMap(\.entry) + } + + public var contents: String { + lines.isEmpty ? "" : lines.map(\.text).joined(separator: "\n") + "\n" + } + + public func match(host: String, keyType: String, keyData: Data) -> Match { + let named = lines.filter { $0.host == host } + let stored = named.compactMap(\.entry) + if stored.contains(where: { $0.keyData == keyData }) { + return .trusted + } + if let previous = stored.first(where: { $0.keyType == keyType }) ?? stored.first { + return .changed(storedKey: previous.keyData) + } + return named.isEmpty ? .unknown : .unreadable + } + + public mutating func trust(host: String, keyType: String, keyData: Data) { + lines.removeAll { $0.entry?.host == host && $0.entry?.keyType == keyType } + lines.append(Line( + text: "\(host) \(keyType) \(keyData.base64EncodedString())", + host: host, + entry: Entry(host: host, keyType: keyType, keyData: keyData) + )) + } + + @discardableResult + public mutating func remove(host: String) -> Bool { + let countBefore = lines.count + lines.removeAll { $0.host == host } + return lines.count < countBefore + } + + private static func line(_ text: String) -> Line? { + let trimmed = text.trimmingCharacters(in: .whitespacesAndNewlines) + guard !trimmed.isEmpty else { return nil } + guard !trimmed.hasPrefix("#") else { return Line(text: text, host: nil, entry: nil) } + + let fields = trimmed.split(whereSeparator: \.isWhitespace).map(String.init) + let host = fields[0] + // Anything after the key is a comment, as in OpenSSH. + guard fields.count >= 3, let keyData = Data(base64Encoded: fields[2]) else { + return Line(text: text, host: host, entry: nil) + } + return Line(text: text, host: host, entry: Entry(host: host, keyType: fields[1], keyData: keyData)) + } +} diff --git a/apple/Packages/TableProCore/Tests/TableProSSHTransportTests/KnownHostsFileTests.swift b/apple/Packages/TableProCore/Tests/TableProSSHTransportTests/KnownHostsFileTests.swift new file mode 100644 index 0000000000..6f33c5d644 --- /dev/null +++ b/apple/Packages/TableProCore/Tests/TableProSSHTransportTests/KnownHostsFileTests.swift @@ -0,0 +1,132 @@ +import Foundation +import Testing + +@testable import TableProSSHTransport + +struct KnownHostsFileTests { + private static let host = "[db.example.com]:22" + + private func key(_ seed: UInt8) -> Data { + Data(repeating: seed, count: 32) + } + + private func line(_ host: String, _ keyType: String, _ key: Data) -> String { + "\(host) \(keyType) \(key.base64EncodedString())" + } + + private func temporaryURL() -> URL { + FileManager.default.temporaryDirectory.appendingPathComponent("known_hosts_\(UUID().uuidString)") + } + + @Test("A missing file is an empty store") + func missingFileIsEmpty() throws { + let file = try KnownHostsFile.load(from: temporaryURL()) + + #expect(file.entries.isEmpty) + #expect(file.match(host: Self.host, keyType: "ssh-ed25519", keyData: key(1)) == .unknown) + } + + @Test("A file that exists but cannot be read throws instead of reading as empty") + func unreadableFileThrows() throws { + let url = temporaryURL() + defer { try? FileManager.default.removeItem(at: url) } + try Data(line(Self.host, "ssh-ed25519", key(1)).utf8).write(to: url) + try FileManager.default.setAttributes([.posixPermissions: 0], ofItemAtPath: url.path) + + let error = #expect(throws: CocoaError.self) { + _ = try KnownHostsFile.load(from: url) + } + #expect(error?.code == .fileReadNoPermission) + } + + @Test("A file that is not UTF-8 throws instead of reading as empty") + func undecodableFileThrows() throws { + let url = temporaryURL() + defer { try? FileManager.default.removeItem(at: url) } + try Data([0x5B, 0xFF, 0xFE, 0x0A]).write(to: url) + + let error = #expect(throws: CocoaError.self) { + _ = try KnownHostsFile.load(from: url) + } + #expect(error?.code == .fileReadInapplicableStringEncoding) + } + + @Test("A stored key matches, and another key for the host is a change") + func storedKeyMatches() { + let file = KnownHostsFile(contents: line(Self.host, "ssh-ed25519", key(1)) + "\n") + + #expect(file.match(host: Self.host, keyType: "ssh-ed25519", keyData: key(1)) == .trusted) + #expect(file.match(host: Self.host, keyType: "ssh-ed25519", keyData: key(2)) == .changed(storedKey: key(1))) + #expect(file.match(host: Self.host, keyType: "ssh-rsa", keyData: key(2)) == .changed(storedKey: key(1))) + #expect(file.match(host: "[db.example.com]:2222", keyType: "ssh-ed25519", keyData: key(1)) == .unknown) + } + + @Test("A line whose key cannot be parsed keeps its host from reading as new", arguments: [ + "[db.example.com]:22 ssh-ed25519 !!not-base64!!", + "[db.example.com]:22 ssh-ed25519", + "[db.example.com]:22", + ]) + func unparsedLineIsNotANewHost(unparsed: String) { + let file = KnownHostsFile(contents: unparsed + "\n" + line("[other.example.com]:22", "ssh-ed25519", key(3)) + "\n") + + #expect(file.match(host: Self.host, keyType: "ssh-ed25519", keyData: key(1)) == .unreadable) + #expect(file.match(host: "[new.example.com]:22", keyType: "ssh-ed25519", keyData: key(1)) == .unknown) + #expect(file.entries.map { $0.host } == ["[other.example.com]:22"]) + } + + @Test("Trusting a host writes back every line it did not replace, as it was read") + func trustKeepsOtherLines() { + let unparsed = "[broken.example.com]:22 ssh-ed25519 !!not-base64!!" + let comment = "# added by hand" + let commented = line("[commented.example.com]:22", "ssh-rsa", key(4)) + " laptop" + let replaced = line(Self.host, "ssh-ed25519", key(1)) + var file = KnownHostsFile(contents: [unparsed, comment, commented, replaced].joined(separator: "\n") + "\n") + + file.trust(host: Self.host, keyType: "ssh-ed25519", keyData: key(2)) + + let expected = [unparsed, comment, commented, line(Self.host, "ssh-ed25519", key(2))] + #expect(file.contents == expected.joined(separator: "\n") + "\n") + #expect(file.match(host: "[commented.example.com]:22", keyType: "ssh-rsa", keyData: key(4)) == .trusted) + } + + @Test("Trusting a second key type keeps the first") + func trustAddsKeyType() { + var file = KnownHostsFile(contents: line(Self.host, "ssh-rsa", key(1)) + "\n") + + file.trust(host: Self.host, keyType: "ssh-ed25519", keyData: key(2)) + + #expect(file.match(host: Self.host, keyType: "ssh-rsa", keyData: key(1)) == .trusted) + #expect(file.match(host: Self.host, keyType: "ssh-ed25519", keyData: key(2)) == .trusted) + } + + @Test("Removing a host drops every line naming it, the unparsed ones too") + func removeDropsEveryLineForTheHost() { + let kept = line("[other.example.com]:22", "ssh-ed25519", key(3)) + var file = KnownHostsFile(contents: [ + line(Self.host, "ssh-rsa", key(1)), + "[db.example.com]:22 ssh-ed25519 !!not-base64!!", + kept, + ].joined(separator: "\n")) + + let removed = file.remove(host: Self.host) + let removedAgain = file.remove(host: Self.host) + + #expect(removed) + #expect(!removedAgain) + #expect(file.contents == kept + "\n") + #expect(file.match(host: Self.host, keyType: "ssh-rsa", keyData: key(1)) == .unknown) + } + + @Test("Lines ending in a carriage return or split by tabs still parse") + func lenientWhitespace() { + let file = KnownHostsFile(contents: "\(Self.host)\tssh-ed25519\t\(key(1).base64EncodedString())\r\n") + + #expect(file.match(host: Self.host, keyType: "ssh-ed25519", keyData: key(1)) == .trusted) + } + + @Test("An empty store writes an empty file") + func emptyContents() { + #expect(KnownHostsFile().contents.isEmpty) + #expect(KnownHostsFile(contents: "\n\n").contents.isEmpty) + } +} diff --git a/apple/Packages/TableProOracle/Sources/TableProOracleCore/OracleCoreConnection.swift b/apple/Packages/TableProOracle/Sources/TableProOracleCore/OracleCoreConnection.swift index fa25fee925..19c7fd4a89 100644 --- a/apple/Packages/TableProOracle/Sources/TableProOracleCore/OracleCoreConnection.swift +++ b/apple/Packages/TableProOracle/Sources/TableProOracleCore/OracleCoreConnection.swift @@ -418,6 +418,10 @@ public final class OracleCoreConnection: @unchecked Sendable { } } + public var queryTimeoutSeconds: Int { + state.withLock { $0.queryTimeoutSeconds } + } + // A debug session's statement is a routine that waits at a breakpoint for a person: no query timeout, no // staleness limit on the health check, and no silent redial, since a new session would not be the one debugged. public func reserveForDebugging() { diff --git a/apple/TablePro/Core/SSH/HostKeyStore.swift b/apple/TablePro/Core/SSH/HostKeyStore.swift index 1e2eaeca97..73d2b31ccb 100644 --- a/apple/TablePro/Core/SSH/HostKeyStore.swift +++ b/apple/TablePro/Core/SSH/HostKeyStore.swift @@ -1,18 +1,8 @@ -// -// HostKeyStore.swift -// TablePro -// -// Manages SSH host key verification for known hosts. -// Stores trusted host keys in a line-based file at -// ~/Library/Application Support/TablePro/known_hosts -// - import CryptoKit import Foundation import os import TableProSSHTransport -/// Manages SSH host key verification for known hosts internal final class HostKeyStore: @unchecked Sendable { static let shared = HostKeyStore() @@ -25,9 +15,11 @@ internal final class HostKeyStore: @unchecked Sendable { // The file is one entry per line split on spaces, so a host with a line break would plant // an entry for another host. case invalidHost(SSHDestinationName.Problem) + // The saved keys could not be read, so a changed key would look like a new host. + case unreadable } - private let filePath: String + let filePath: String private let lock = NSLock() private init() { @@ -36,13 +28,10 @@ internal final class HostKeyStore: @unchecked Sendable { self.filePath = tableProDir.appendingPathComponent("known_hosts").path } - /// Testing initializer with a custom file path init(filePath: String) { self.filePath = filePath } - // MARK: - Public API - func verify(keyData: Data, keyType: String, hostname: String, port: Int) -> VerificationResult { lock.lock() defer { lock.unlock() } @@ -50,38 +39,31 @@ internal final class HostKeyStore: @unchecked Sendable { guard let hostKey = hostIdentifier(hostname, port) else { return .invalidHost(.host(hostname)) } - let currentFingerprint = Self.fingerprint(of: keyData) - let entries = loadEntries() + guard let file = loadFile() else { return .unreadable } - let hostEntries = entries.filter { $0.host == hostKey } - guard !hostEntries.isEmpty else { - Self.logger.info("Unknown host key for \(hostKey)") - return .unknown(fingerprint: currentFingerprint, keyType: keyType) - } - - if hostEntries.contains(where: { Self.fingerprint(of: $0.keyData) == currentFingerprint }) { + switch file.match(host: hostKey, keyType: keyType, keyData: keyData) { + case .trusted: Self.logger.debug("Host key trusted for \(hostKey)") return .trusted + case .unknown: + Self.logger.info("Unknown host key for \(hostKey)") + return .unknown(fingerprint: Self.fingerprint(of: keyData), keyType: keyType) + case .changed(let storedKey): + Self.logger.warning("Host key mismatch for \(hostKey)") + return .mismatch(expected: Self.fingerprint(of: storedKey), actual: Self.fingerprint(of: keyData)) + case .unreadable: + Self.logger.error("The saved host key for \(hostKey) could not be parsed") + return .unreadable } - - let sameType = hostEntries.first { $0.keyType == keyType } - let storedFingerprint = Self.fingerprint(of: (sameType ?? hostEntries[0]).keyData) - - Self.logger.warning("Host key mismatch for \(hostKey)") - return .mismatch(expected: storedFingerprint, actual: currentFingerprint) } func trust(hostname: String, port: Int, key: Data, keyType: String) { lock.lock() defer { lock.unlock() } - guard let hostKey = hostIdentifier(hostname, port) else { return } - var entries = loadEntries() - - entries.removeAll { $0.host == hostKey && $0.keyType == keyType } - - entries.append((host: hostKey, keyType: keyType, keyData: key)) - saveEntries(entries) + guard let hostKey = hostIdentifier(hostname, port), var file = loadFile() else { return } + file.trust(host: hostKey, keyType: keyType, keyData: key) + saveFile(file) Self.logger.info("Trusted host key for \(hostKey) (\(keyType))") } @@ -90,21 +72,13 @@ internal final class HostKeyStore: @unchecked Sendable { lock.lock() defer { lock.unlock() } - guard let hostKey = hostIdentifier(hostname, port) else { return } - var entries = loadEntries() - - let countBefore = entries.count - entries.removeAll { $0.host == hostKey } - - if entries.count < countBefore { - saveEntries(entries) + guard let hostKey = hostIdentifier(hostname, port), var file = loadFile() else { return } + if file.remove(host: hostKey) { + saveFile(file) Self.logger.info("Removed host key for \(hostKey)") } } - // MARK: - Key Type Mapping - - /// Convert a numeric key type to its string name static func keyTypeName(_ type: Int32) -> String { switch type { case 1: return "ssh-rsa" @@ -117,21 +91,12 @@ internal final class HostKeyStore: @unchecked Sendable { } } - // MARK: - Fingerprint - - /// Compute a SHA-256 fingerprint of a host key - /// - Parameter key: The raw key bytes - /// - Returns: Fingerprint in "SHA256:base64" format (matches ssh-keygen -l output) + // The OpenSSH form `ssh-keygen -l` prints: SHA-256, base64 without padding. static func fingerprint(of key: Data) -> String { - let hash = SHA256.hash(data: key) - let base64 = Data(hash).base64EncodedString() - // Remove trailing '=' padding to match OpenSSH format - let trimmed = base64.replacingOccurrences(of: "=", with: "") - return "SHA256:\(trimmed)" + let digest = SHA256.hash(data: key) + return "SHA256:" + Data(digest).base64EncodedString().replacingOccurrences(of: "=", with: "") } - // MARK: - Private Helpers - private func hostIdentifier(_ hostname: String, _ port: Int) -> String? { guard SSHDestinationName.isValidHost(hostname) else { Self.logger.warning("Refusing a host key entry for a host ssh would refuse") @@ -140,45 +105,19 @@ internal final class HostKeyStore: @unchecked Sendable { return "[\(hostname)]:\(port)" } - /// Load all entries from the known_hosts file - /// File format: [hostname]:port keyType base64EncodedKey - private func loadEntries() -> [(host: String, keyType: String, keyData: Data)] { - guard let content = try? String(contentsOfFile: filePath, encoding: .utf8) else { - return [] - } - - var entries: [(host: String, keyType: String, keyData: Data)] = [] - - for line in content.components(separatedBy: "\n") { - let trimmed = line.trimmingCharacters(in: .whitespaces) - guard !trimmed.isEmpty, !trimmed.hasPrefix("#") else { - continue - } - - let parts = trimmed.components(separatedBy: " ") - guard parts.count == 3, - let keyData = Data(base64Encoded: parts[2]) else { - Self.logger.warning("Skipping malformed known_hosts line: \(trimmed)") - continue - } - - entries.append((host: parts[0], keyType: parts[1], keyData: keyData)) + // nil means the file is there but unreadable, and nothing may be written over it. + private func loadFile() -> KnownHostsFile? { + do { + return try KnownHostsFile.load(from: URL(fileURLWithPath: filePath)) + } catch { + Self.logger.error("Failed to read known_hosts file: \(error.localizedDescription)") + return nil } - - return entries } - /// Save entries to the known_hosts file - private func saveEntries(_ entries: [(host: String, keyType: String, keyData: Data)]) { - let lines = entries.map { entry in - let base64Key = entry.keyData.base64EncodedString() - return "\(entry.host) \(entry.keyType) \(base64Key)" - } - - let content = lines.joined(separator: "\n") + (lines.isEmpty ? "" : "\n") - + private func saveFile(_ file: KnownHostsFile) { do { - try content.write(toFile: filePath, atomically: true, encoding: .utf8) + try file.contents.write(toFile: filePath, atomically: true, encoding: .utf8) } catch { Self.logger.error("Failed to write known_hosts file: \(error.localizedDescription)") } diff --git a/apple/TablePro/Core/SSH/HostKeyVerifier.swift b/apple/TablePro/Core/SSH/HostKeyVerifier.swift index 52ad7f9004..ea4238aa71 100644 --- a/apple/TablePro/Core/SSH/HostKeyVerifier.swift +++ b/apple/TablePro/Core/SSH/HostKeyVerifier.swift @@ -1,27 +1,12 @@ -// -// HostKeyVerifier.swift -// TablePro -// -// Handles SSH host key verification with UI prompts. -// Called during SSH tunnel establishment, after handshake but before auth. -// - import AppKit import Foundation import os import TableProSSHTransport -/// Handles host key verification with UI prompts +// Runs after the handshake and before authentication, so no credential reaches a server the user has not trusted. internal enum HostKeyVerifier { private static let logger = Logger(subsystem: "com.TablePro", category: "HostKeyVerifier") - /// Verify the host key, prompting the user if needed. - /// - Parameters: - /// - keyData: The raw host key bytes from the SSH session - /// - keyType: The key type string (e.g. "ssh-rsa", "ssh-ed25519") - /// - hostname: The remote hostname - /// - port: The remote port - /// - Throws: `SSHTunnelError.hostKeyVerificationFailed` if the user rejects the key static func verify( keyData: Data, keyType: String, @@ -46,6 +31,14 @@ internal enum HostKeyVerifier { case .invalidHost(let problem): throw SSHTunnelError.tunnelCreationFailed(problem.explanation) + // Asking would offer a server whose key changed as a new one. + case .unreadable: + logger.error("Saved host keys unreadable, refusing [\(hostname)]:\(port)") + throw SSHTunnelError.tunnelCreationFailed(String( + format: String(localized: "The SSH host keys saved in %@ could not be read, so this server's key could not be checked."), + (HostKeyStore.shared.filePath as NSString).abbreviatingWithTildeInPath + )) + case .unknown(let fingerprint, let keyType): logger.info("Unknown host key for [\(hostname)]:\(port), prompting user") let accepted = try await promptUnknownHost( @@ -90,8 +83,6 @@ internal enum HostKeyVerifier { } } - // MARK: - UI Prompts - @MainActor private static func promptUnknownHost( hostname: String, diff --git a/apple/TablePro/Resources/Localizable.xcstrings b/apple/TablePro/Resources/Localizable.xcstrings index 01982c4328..549c36a1a9 100644 --- a/apple/TablePro/Resources/Localizable.xcstrings +++ b/apple/TablePro/Resources/Localizable.xcstrings @@ -193965,6 +193965,9 @@ "The SSH host is empty." : { "extractionState" : "manual", "generatesSymbol" : false + }, + "The SSH host keys saved in %@ could not be read, so this server's key could not be checked." : { + }, "the SSH host, user or port of “%@” is not one TablePro allows" : { "extractionState" : "manual", diff --git a/apple/TableProMobile/TableProMobile/Drivers/CappedRead.swift b/apple/TableProMobile/TableProMobile/Drivers/CappedRead.swift new file mode 100644 index 0000000000..b669d3899f --- /dev/null +++ b/apple/TableProMobile/TableProMobile/Drivers/CappedRead.swift @@ -0,0 +1,23 @@ +import Foundation +import TableProModels +import TableProQuery +import TableProSQLGrammar + +// The Mac's capped read (`QueryExecutor.qualifiesForRowCap`): one statement that only reads, so stopping it on the +// server at the row cap loses nothing but rows. Anything else, a `CALL` or a `DELETE ... RETURNING`, is read to its end +// so the work after its rows still happens. +nonisolated enum CappedRead { + private static let rowProducingKeywords: Set = ["SELECT", "WITH", "TABLE", "VALUES"] + + static func qualifies(_ sql: String, databaseType: DatabaseType, session: SQLSessionLexicalFacts? = nil) -> Bool { + let grammar = SQLLexicalReadings.resolve( + databaseTypeId: databaseType.rawValue, declared: nil, session: session + ).execution + let statements = SQLStatementClassifier.statements(of: sql, grammar: grammar) + guard statements.count == 1, let statement = statements.first, + let keyword = SQLStatementRowLimit.leadingKeyword(statement, grammar: grammar), + rowProducingKeywords.contains(keyword) + else { return false } + return !SQLWriteClassifier.isWriteQuery(sql, databaseType: databaseType) + } +} diff --git a/apple/TableProMobile/TableProMobile/Drivers/DuckDBDriver+Decode.swift b/apple/TableProMobile/TableProMobile/Drivers/DuckDBDriver+Decode.swift index 7432fc368c..c9f143bcce 100644 --- a/apple/TableProMobile/TableProMobile/Drivers/DuckDBDriver+Decode.swift +++ b/apple/TableProMobile/TableProMobile/Drivers/DuckDBDriver+Decode.swift @@ -3,19 +3,25 @@ import Foundation import TableProModels extension DuckDBActor { - static func decodeCell(vector: duckdb_vector, row: idx_t, column: DuckDBStreamColumn, options: StreamOptions) -> Cell { + static func decodeCell( + vector: duckdb_vector, + row: idx_t, + column: DuckDBStreamColumn, + options: StreamOptions, + ref: CellRef? = nil + ) -> Cell { if let validity = duckdb_vector_get_validity(vector), !duckdb_validity_row_is_valid(validity, row) { return .null } guard let data = duckdb_vector_get_data(vector) else { return .null } if column.castToText { - return decodeVarchar(data, row: row, options: options) + return decodeVarchar(data, row: row, options: options, ref: ref) } switch column.type { case DUCKDB_TYPE_VARCHAR: - return decodeVarchar(data, row: row, options: options) + return decodeVarchar(data, row: row, options: options, ref: ref) case DUCKDB_TYPE_BLOB: return decodeBlob(data, row: row) case DUCKDB_TYPE_BOOLEAN: @@ -58,7 +64,7 @@ extension DuckDBActor { case DUCKDB_TYPE_TIMESTAMP, DUCKDB_TYPE_TIMESTAMP_S, DUCKDB_TYPE_TIMESTAMP_MS, DUCKDB_TYPE_TIMESTAMP_NS: return .text(formatTimestamp(normalizedTimestamp(data, row: row, type: column.type))) default: - return decodeVarchar(data, row: row, options: options) + return decodeVarchar(data, row: row, options: options, ref: ref) } } @@ -66,7 +72,12 @@ extension DuckDBActor { data.load(fromByteOffset: Int(row) * MemoryLayout.stride, as: T.self) } - private static func decodeVarchar(_ data: UnsafeMutableRawPointer, row: idx_t, options: StreamOptions) -> Cell { + private static func decodeVarchar( + _ data: UnsafeMutableRawPointer, + row: idx_t, + options: StreamOptions, + ref: CellRef? + ) -> Cell { let strings = data.assumingMemoryBound(to: duckdb_string_t.self) var value = strings[Int(row)] let length = Int(duckdb_string_t_length(value)) @@ -74,7 +85,7 @@ extension DuckDBActor { let text = String(bytes: UnsafeRawBufferPointer(start: pointer, count: length), encoding: .utf8) ?? "" if length > options.textTruncationBytes { let prefix = String(text.prefix(options.textTruncationBytes)) - return .truncatedText(prefix: prefix, totalBytes: length, ref: nil) + return .truncatedText(prefix: prefix, totalBytes: length, ref: ref) } return .text(text) } diff --git a/apple/TableProMobile/TableProMobile/Drivers/DuckDBDriver+Streaming.swift b/apple/TableProMobile/TableProMobile/Drivers/DuckDBDriver+Streaming.swift index 75e8b0386f..79056a3882 100644 --- a/apple/TableProMobile/TableProMobile/Drivers/DuckDBDriver+Streaming.swift +++ b/apple/TableProMobile/TableProMobile/Drivers/DuckDBDriver+Streaming.swift @@ -200,8 +200,12 @@ extension DuckDBActor { for index in 0.. String? { + guard let vector, column.castToText || column.type != DUCKDB_TYPE_BLOB, + case .text(let value) = decodeCell(vector: vector, row: row, column: column, options: wholeText) + else { return nil } + return value + } + func endStream(_ stream: UInt64) { guard stream == streamGeneration else { return } closeStreamResult() diff --git a/apple/TableProMobile/TableProMobile/Drivers/LazyCellKey.swift b/apple/TableProMobile/TableProMobile/Drivers/LazyCellKey.swift new file mode 100644 index 0000000000..49939e63e4 --- /dev/null +++ b/apple/TableProMobile/TableProMobile/Drivers/LazyCellKey.swift @@ -0,0 +1,41 @@ +import Foundation +import TableProModels + +// The key a browsed row's long values are read back by: the table's key columns found in the result, valued from the +// row as the driver read it, so a key cut for display still carries its whole text. +nonisolated struct LazyCellKey: Sendable { + private struct KeyColumn: Sendable { + let name: String + let index: Int + } + + private let table: String + private let keyColumns: [KeyColumn] + + // Nil without a key, or when the result leaves out one of its columns. + init?(_ context: LazyContext?, columnNames: [String]) { + guard let context, !context.primaryKeyColumns.isEmpty else { return nil } + var keyColumns: [KeyColumn] = [] + for name in context.primaryKeyColumns { + guard let index = columnNames.firstIndex(of: name) else { return nil } + keyColumns.append(KeyColumn(name: name, index: index)) + } + table = context.table + self.keyColumns = keyColumns + } + + // Nil when a key value is NULL or not text, which no key predicate matches. + func primaryKey(_ value: (Int) -> String?) -> [PrimaryKeyComponent]? { + var components: [PrimaryKeyComponent] = [] + components.reserveCapacity(keyColumns.count) + for key in keyColumns { + guard let text = value(key.index) else { return nil } + components.append(PrimaryKeyComponent(column: key.name, value: text)) + } + return components + } + + func ref(column: String, primaryKey: [PrimaryKeyComponent]?) -> CellRef? { + primaryKey.map { CellRef(table: table, column: column, primaryKey: $0) } + } +} diff --git a/apple/TableProMobile/TableProMobile/Drivers/MSSQLDriver.swift b/apple/TableProMobile/TableProMobile/Drivers/MSSQLDriver.swift index 319879be36..11186e7582 100644 --- a/apple/TableProMobile/TableProMobile/Drivers/MSSQLDriver.swift +++ b/apple/TableProMobile/TableProMobile/Drivers/MSSQLDriver.swift @@ -1,4 +1,5 @@ import Foundation +import os import TableProDatabase import TableProModels import TableProMSSQLCore @@ -20,8 +21,11 @@ nonisolated private extension MSSQLRawResult { } nonisolated final class MSSQLDriver: DatabaseDriver, @unchecked Sendable { + private static let logger = Logger(subsystem: "com.TablePro", category: "MSSQLDriver") + nonisolated(unsafe) private var conn: FreeTDSConnection let options: MSSQLConnectionOptions + let queryTimeoutSeconds: Int? private let additionalFields: [String: String] private let host: String private let authMethod: MSSQLAuthMethod @@ -56,6 +60,7 @@ nonisolated final class MSSQLDriver: DatabaseDriver, @unchecked Sendable { ) self.conn = FreeTDSConnection(options: options) self.options = options + self.queryTimeoutSeconds = connection.queryTimeoutSeconds self.additionalFields = connection.additionalFields self.host = connection.host self.authMethod = authMethod @@ -90,6 +95,17 @@ nonisolated final class MSSQLDriver: DatabaseDriver, @unchecked Sendable { throw mapToConnectionError(error) } + // A server that refuses the limit still runs queries, as on the Mac. + for statement in Self.sessionSetupStatements(queryTimeoutSeconds: queryTimeoutSeconds) { + do { + _ = try await runQuery(statement) + } catch { + Self.logger.warning( + "Session setup failed with \(statement, privacy: .private): \(error.localizedDescription, privacy: .private)" + ) + } + } + if let serverSchema = try? await runQuery(MSSQLSchemaQueries.currentSchema).rows.first?.first ?? nil, !serverSchema.isEmpty { currentSchema = serverSchema @@ -100,6 +116,12 @@ nonisolated final class MSSQLDriver: DatabaseDriver, @unchecked Sendable { } } + // The Mac reads a query timeout on SQL Server as the session's lock wait. None, or 0, keeps the server's own wait. + static func sessionSetupStatements(queryTimeoutSeconds: Int?) -> [String] { + guard let queryTimeoutSeconds, queryTimeoutSeconds > 0 else { return [] } + return ["SET LOCK_TIMEOUT \(PluginQueryTimeout.milliseconds(queryTimeoutSeconds))"] + } + func disconnect() async throws { await conn.disconnectAndWait() } @@ -149,8 +171,7 @@ nonisolated final class MSSQLDriver: DatabaseDriver, @unchecked Sendable { } var emitted = 0 var headerColumns: [ColumnInfo] = [] - // Past the row cap the batch keeps running, so a later UPDATE or COMMIT in it still happens and its - // error still reaches the run; the rows are read and dropped. Only a reader that stops aborts it. + var rowKey: LazyCellKey? var isCapped = false do { for try await element in coreStream { @@ -165,6 +186,7 @@ nonisolated final class MSSQLDriver: DatabaseDriver, @unchecked Sendable { headerColumns = columns.enumerated().map { idx, col in ColumnInfo(name: col.name, typeName: col.type.canonicalName, ordinalPosition: idx) } + rowKey = LazyCellKey(options.lazyContext, columnNames: headerColumns.map(\.name)) continuation.yield(.columns(headerColumns)) case .rows(let batch): for row in batch { @@ -173,12 +195,27 @@ nonisolated final class MSSQLDriver: DatabaseDriver, @unchecked Sendable { isCapped = true break } + let primaryKey = rowKey?.primaryKey { row[safe: $0]?.keyText } let cells = zip(headerColumns, row).map { columnInfo, rawCell in - cell(from: rawCell, columnTypeName: columnInfo.typeName, options: options) + cell( + from: rawCell, + columnTypeName: columnInfo.typeName, + options: options, + ref: rowKey?.ref(column: columnInfo.name, primaryKey: primaryKey) + ) } continuation.yield(.row(Row(cells: cells))) emitted += 1 } + // Any batch but a capped read runs on past the cap with its rows dropped, so a later UPDATE + // or COMMIT in it still runs and its error still reaches the run. Classified here, off the + // caller's thread and once. The abort ends the request with an attention, or reads the rest + // where the session's transaction would not survive one (MSSQLRequestEnding). + if isCapped, CappedRead.qualifies(query, databaseType: .mssql) { + abort.abort() + continuation.finish() + return + } case .affectedRows(let count): continuation.yield(.rowsAffected(count)) } @@ -196,12 +233,13 @@ nonisolated final class MSSQLDriver: DatabaseDriver, @unchecked Sendable { } } - private func cell(from raw: MSSQLRawCell, columnTypeName: String, options: StreamOptions) -> Cell { + // A binary value reads back through `execute` as text, not its bytes, so it offers no full value to load. + private func cell(from raw: MSSQLRawCell, columnTypeName: String, options: StreamOptions, ref: CellRef?) -> Cell { switch raw { case .null: return .null case .string(let value): - return Cell.from(legacyValue: value, columnTypeName: columnTypeName, options: options) + return Cell.from(legacyValue: value, columnTypeName: columnTypeName, options: options, ref: ref) case .bytes(let data): return .binary(byteCount: data.count, ref: nil) } @@ -343,6 +381,14 @@ nonisolated private extension Array { } } +nonisolated private extension MSSQLRawCell { + // A binary key is no text a key predicate can match. + var keyText: String? { + guard case .string(let value) = self else { return nil } + return value + } +} + nonisolated private final class MSSQLStreamAbort: @unchecked Sendable { private let lock = NSLock() private var aborted = false diff --git a/apple/TableProMobile/TableProMobile/Drivers/MySQLDriver.swift b/apple/TableProMobile/TableProMobile/Drivers/MySQLDriver.swift index db2d147650..3170d07c7d 100644 --- a/apple/TableProMobile/TableProMobile/Drivers/MySQLDriver.swift +++ b/apple/TableProMobile/TableProMobile/Drivers/MySQLDriver.swift @@ -205,6 +205,8 @@ nonisolated final class MySQLDriver: DatabaseDriver, @unchecked Sendable { let actor = self.actor let killer = actor.killer let statement = killer.reserve() + let databaseType = self.databaseType + let session = settledLexicalFacts return AsyncThrowingStream { continuation in let task = Task { var openStream: MySQLStreamID? @@ -218,22 +220,33 @@ nonisolated final class MySQLDriver: DatabaseDriver, @unchecked Sendable { openStream = stream continuation.yield(.columns(columns)) var emitted = 0 - while !Task.isCancelled, emitted < options.maxRows { + // A row after the cap tells a result that only fills it from one with rows still to stop. + var rowsLeft = false + while !Task.isCancelled { guard let cells = try await actor.fetchNextRow( stream, options: options, columns: columns ) else { break } + guard emitted < options.maxRows else { + rowsLeft = true + break + } continuation.yield(.row(Row(cells: cells))) emitted += 1 } if Task.isCancelled { continuation.yield(.truncated(reason: .cancelled)) - } else if emitted >= options.maxRows { + } else if rowsLeft { continuation.yield(.truncated(reason: .rowCap(options.maxRows))) } openStream = nil - try await actor.endStream(stream) + // Classified here, off the caller's thread and only for a statement that passed the cap. + if rowsLeft, CappedRead.qualifies(query, databaseType: databaseType, session: session) { + try await actor.endStreamAtRowCap(stream) + } else { + try await actor.endStream(stream) + } } await self.settleLexicalFacts() continuation.finish() @@ -1098,6 +1111,20 @@ private actor MySQLActor { try releaseStream(of: mysql) } + // Only for a `CappedRead`: its rows past the cap would only be read and dropped, so it is stopped on the server + // first, as on the Mac, and the kill's 1317 is the cut the reader was already told of. + func endStreamAtRowCap(_ stream: MySQLStreamID) throws { + guard let mysql, stream.generation == streamGeneration else { return } + if let sent = streamingStatement { + killer.rowCapReached(sent.id) + } + do { + try releaseStream(of: mysql) + } catch { + guard flavor.isInterruptedByKill(errno: mysql_errno(mysql), message: message(from: mysql)) else { throw error } + } + } + /// The producer of a superseded stream only learns that its rows were cut off, so an error in the rest of /// its statement has no one to reach and is logged. private func endLeftoverStream(of mysql: UnsafeMutablePointer) { diff --git a/apple/TableProMobile/TableProMobile/Drivers/MySQLStatementKiller.swift b/apple/TableProMobile/TableProMobile/Drivers/MySQLStatementKiller.swift index e6159c3d2f..4241b575e4 100644 --- a/apple/TableProMobile/TableProMobile/Drivers/MySQLStatementKiller.swift +++ b/apple/TableProMobile/TableProMobile/Drivers/MySQLStatementKiller.swift @@ -302,11 +302,15 @@ nonisolated final class MySQLStatementKiller: @unchecked Sendable { func readerLeft(_ statement: UInt64) { deliver(lock.withLock { () -> Accepted? in requests.readerLeft(statement) - guard target?.killsWhenReaderLeaves == true else { return nil } - return accepted(requests.kill(statement) ? statement : nil) + return acceptedReaderKill(statement) }) } + // The reader has every row it keeps, and the statement may still be sending the rest. + func rowCapReached(_ statement: UInt64) { + deliver(lock.withLock { acceptedReaderKill(statement) }) + } + func stop() { deliver(lock.withLock { accepted(requests.killLeftInFlight()) }) } @@ -319,6 +323,11 @@ nonisolated final class MySQLStatementKiller: @unchecked Sendable { } } + private func acceptedReaderKill(_ statement: UInt64) -> Accepted? { + guard target?.killsWhenReaderLeaves == true else { return nil } + return accepted(requests.kill(statement) ? statement : nil) + } + // Entered in the same critical section that takes the kill, so a statement that ends right after still finds the // kill pending and the next one waits for it. private func accepted(_ statement: UInt64?) -> Accepted? { diff --git a/apple/TableProMobile/TableProMobile/Drivers/OracleDriver.swift b/apple/TableProMobile/TableProMobile/Drivers/OracleDriver.swift index 9884d8d6b2..8a7bc1df36 100644 --- a/apple/TableProMobile/TableProMobile/Drivers/OracleDriver.swift +++ b/apple/TableProMobile/TableProMobile/Drivers/OracleDriver.swift @@ -52,8 +52,14 @@ nonisolated final class OracleDriver: DatabaseDriver, @unchecked Sendable { self.ssl = ssl fallbackSchema = connection.username.uppercased() currentSchema = fallbackSchema + // The core closes the channel on a statement that runs past it, as on the Mac, and keeps it across the redial. + if let seconds = connection.queryTimeoutSeconds, seconds > 0 { + core.applyQueryTimeout(seconds) + } } + var queryTimeoutSeconds: Int { core.queryTimeoutSeconds } + // MARK: - Connection func connect() async throws { @@ -132,6 +138,7 @@ nonisolated final class OracleDriver: DatabaseDriver, @unchecked Sendable { } var emitted = 0 var headerColumns: [ColumnInfo] = [] + var rowKey: LazyCellKey? do { for try await element in coreStream { if Task.isCancelled { @@ -144,6 +151,7 @@ nonisolated final class OracleDriver: DatabaseDriver, @unchecked Sendable { headerColumns = columns.enumerated().map { index, column in ColumnInfo(name: column.name, typeName: column.typeName, ordinalPosition: index) } + rowKey = LazyCellKey(options.lazyContext, columnNames: headerColumns.map(\.name)) continuation.yield(.columns(headerColumns)) case .rows(let batch): for row in batch { @@ -152,8 +160,17 @@ nonisolated final class OracleDriver: DatabaseDriver, @unchecked Sendable { continuation.finish() return } + let primaryKey = rowKey?.primaryKey { index in + guard row.indices.contains(index), case .string(let value) = row[index] else { return nil } + return value + } let cells = zip(headerColumns, row).map { columnInfo, rawCell in - cell(from: rawCell, columnTypeName: columnInfo.typeName, options: options) + cell( + from: rawCell, + columnTypeName: columnInfo.typeName, + options: options, + ref: rowKey?.ref(column: columnInfo.name, primaryKey: primaryKey) + ) } continuation.yield(.row(Row(cells: cells))) emitted += 1 @@ -177,12 +194,13 @@ nonisolated final class OracleDriver: DatabaseDriver, @unchecked Sendable { } } - private func cell(from raw: OracleRawCell, columnTypeName: String, options: StreamOptions) -> Cell { + // A binary value reads back through `execute` as text, not its bytes, so it offers no full value to load. + private func cell(from raw: OracleRawCell, columnTypeName: String, options: StreamOptions, ref: CellRef?) -> Cell { switch raw { case .null: return .null case .string(let value): - return Cell.from(legacyValue: value, columnTypeName: columnTypeName, options: options) + return Cell.from(legacyValue: value, columnTypeName: columnTypeName, options: options, ref: ref) case .bytes(let data): return .binary(byteCount: data.count, ref: nil) } diff --git a/apple/TableProMobile/TableProMobile/Drivers/PostgreSQLDriver.swift b/apple/TableProMobile/TableProMobile/Drivers/PostgreSQLDriver.swift index eb5153fcd1..f36df9f778 100644 --- a/apple/TableProMobile/TableProMobile/Drivers/PostgreSQLDriver.swift +++ b/apple/TableProMobile/TableProMobile/Drivers/PostgreSQLDriver.swift @@ -20,6 +20,7 @@ nonisolated final class PostgreSQLDriver: DatabaseDriver, @unchecked Sendable { private let database: String private let ssl: DriverSSLConfiguration let databaseType: DatabaseType + let queryTimeoutSeconds: Int? var supportsSchemas: Bool { true } var supportsTransactions: Bool { true } @@ -54,7 +55,8 @@ nonisolated final class PostgreSQLDriver: DatabaseDriver, @unchecked Sendable { password: String, database: String, ssl: DriverSSLConfiguration = .disabled, - databaseType: DatabaseType = .postgresql + databaseType: DatabaseType = .postgresql, + queryTimeoutSeconds: Int? = nil ) { self.host = host self.port = port @@ -63,14 +65,31 @@ nonisolated final class PostgreSQLDriver: DatabaseDriver, @unchecked Sendable { self.database = database self.ssl = ssl self.databaseType = databaseType + self.queryTimeoutSeconds = queryTimeoutSeconds } // MARK: - Connection + // None, or 0, keeps the limit the server or the role sets. + static func sessionSetupStatements(queryTimeoutSeconds: Int?) -> [String] { + guard let queryTimeoutSeconds, queryTimeoutSeconds > 0 else { return [] } + return ["SET statement_timeout = \(PluginQueryTimeout.milliseconds(queryTimeoutSeconds))"] + } + func connect() async throws { try await LocalNetworkPermission.shared.ensureAccess(for: host) try await actor.connect(host: host, port: port, user: user, password: password, database: database, ssl: ssl) _ = try? await actor.execute("SET standard_conforming_strings = on") + // A server that refuses the limit still runs queries, as on the Mac. + for statement in Self.sessionSetupStatements(queryTimeoutSeconds: queryTimeoutSeconds) { + do { + _ = try await actor.execute(statement) + } catch { + Self.logger.warning( + "Session setup failed with \(statement, privacy: .private): \(error.localizedDescription, privacy: .private)" + ) + } + } serverVersion = await actor.serverVersion() serverVersionNumber = await actor.serverVersionNumber() catalogPresence = await probeCatalogPresence() diff --git a/apple/TableProMobile/TableProMobile/Localizable.xcstrings b/apple/TableProMobile/TableProMobile/Localizable.xcstrings index 869b707d46..d76dcd4161 100644 --- a/apple/TableProMobile/TableProMobile/Localizable.xcstrings +++ b/apple/TableProMobile/TableProMobile/Localizable.xcstrings @@ -232,6 +232,9 @@ "The SSH host is empty." : { "extractionState" : "manual", "generatesSymbol" : false + }, + "The SSH host keys saved on this device could not be read, so this server's key could not be checked." : { + }, "The SSH port %lld is not between 1 and 65535." : { "extractionState" : "manual", diff --git a/apple/TableProMobile/TableProMobile/Platform/IOSDriverFactory.swift b/apple/TableProMobile/TableProMobile/Platform/IOSDriverFactory.swift index d2d895b1bb..4227583c61 100644 --- a/apple/TableProMobile/TableProMobile/Platform/IOSDriverFactory.swift +++ b/apple/TableProMobile/TableProMobile/Platform/IOSDriverFactory.swift @@ -170,7 +170,8 @@ nonisolated final class IOSDriverFactory: DriverFactory { password: password ?? "", database: connection.database, ssl: try ssl(for: connection, serverName: preTunnelHost), - databaseType: connection.type + databaseType: connection.type, + queryTimeoutSeconds: connection.queryTimeoutSeconds ) case .redis: let dbIndex = RedisDatabaseIndex.resolve( diff --git a/apple/TableProMobile/TableProMobile/SSH/HostKeyStore.swift b/apple/TableProMobile/TableProMobile/SSH/HostKeyStore.swift index e75ee733d7..cad6cd0f96 100644 --- a/apple/TableProMobile/TableProMobile/SSH/HostKeyStore.swift +++ b/apple/TableProMobile/TableProMobile/SSH/HostKeyStore.swift @@ -1,11 +1,3 @@ -// -// HostKeyStore.swift -// TableProMobile -// -// Trusted SSH host keys, stored line-based in the app's Application Support -// directory. Mirrors the Mac store's file format and verification semantics. -// - import CryptoKit import Foundation import os @@ -23,6 +15,8 @@ nonisolated final class HostKeyStore: @unchecked Sendable { // The file is one entry per line split on spaces, so a host with a line break would plant // an entry for another host. case invalidHost(SSHDestinationName.Problem) + // The saved keys could not be read, so a changed key would look like a new host. + case unreadable } private let filePath: String @@ -50,51 +44,46 @@ nonisolated final class HostKeyStore: @unchecked Sendable { guard let hostKey = hostIdentifier(hostname, port) else { return .invalidHost(.host(hostname)) } - let currentFingerprint = Self.fingerprint(of: keyData) - let entries = loadEntries() + guard let file = loadFile() else { return .unreadable } - let hostEntries = entries.filter { $0.host == hostKey } - guard !hostEntries.isEmpty else { - Self.logger.info("Unknown host key for \(hostKey)") - return .unknown(fingerprint: currentFingerprint, keyType: keyType) - } - - if hostEntries.contains(where: { Self.fingerprint(of: $0.keyData) == currentFingerprint }) { + switch file.match(host: hostKey, keyType: keyType, keyData: keyData) { + case .trusted: return .trusted + case .unknown: + Self.logger.info("Unknown host key for \(hostKey)") + return .unknown(fingerprint: Self.fingerprint(of: keyData), keyType: keyType) + case .changed(let storedKey): + Self.logger.warning("Host key mismatch for \(hostKey)") + return .mismatch(expected: Self.fingerprint(of: storedKey), actual: Self.fingerprint(of: keyData)) + case .unreadable: + Self.logger.error("The saved host key for \(hostKey) could not be parsed") + return .unreadable } - - let sameType = hostEntries.first { $0.keyType == keyType } - let storedFingerprint = Self.fingerprint(of: (sameType ?? hostEntries[0]).keyData) - - Self.logger.warning("Host key mismatch for \(hostKey)") - return .mismatch(expected: storedFingerprint, actual: currentFingerprint) } func trust(hostname: String, port: Int, key: Data, keyType: String) { lock.lock() defer { lock.unlock() } - guard let hostKey = hostIdentifier(hostname, port) else { return } - var entries = loadEntries() - entries.removeAll { $0.host == hostKey && $0.keyType == keyType } - entries.append((host: hostKey, keyType: keyType, keyData: key)) - saveEntries(entries) + guard let hostKey = hostIdentifier(hostname, port), var file = loadFile() else { return } + file.trust(host: hostKey, keyType: keyType, keyData: key) + saveFile(file) } func remove(hostname: String, port: Int) { lock.lock() defer { lock.unlock() } - guard let hostKey = hostIdentifier(hostname, port) else { return } - var entries = loadEntries() - entries.removeAll { $0.host == hostKey } - saveEntries(entries) + guard let hostKey = hostIdentifier(hostname, port), var file = loadFile() else { return } + if file.remove(host: hostKey) { + saveFile(file) + } } func trustedHosts() -> [String] { lock.lock() defer { lock.unlock() } - return Array(Set(loadEntries().map(\.host))).sorted() + return Array(Set((loadFile()?.entries ?? []).map(\.host))).sorted() } static func keyTypeName(_ type: Int32) -> String { @@ -122,35 +111,19 @@ nonisolated final class HostKeyStore: @unchecked Sendable { return "[\(hostname)]:\(port)" } - private func loadEntries() -> [(host: String, keyType: String, keyData: Data)] { - guard let content = try? String(contentsOfFile: filePath, encoding: .utf8) else { - return [] - } - - var entries: [(host: String, keyType: String, keyData: Data)] = [] - - for line in content.components(separatedBy: "\n") { - let trimmed = line.trimmingCharacters(in: .whitespaces) - guard !trimmed.isEmpty, !trimmed.hasPrefix("#") else { continue } - - let parts = trimmed.components(separatedBy: " ") - guard parts.count == 3, let keyData = Data(base64Encoded: parts[2]) else { - Self.logger.warning("Skipping malformed known_hosts line") - continue - } - - entries.append((host: parts[0], keyType: parts[1], keyData: keyData)) + // nil means the file is there but unreadable, and nothing may be written over it. + private func loadFile() -> KnownHostsFile? { + do { + return try KnownHostsFile.load(from: URL(fileURLWithPath: filePath)) + } catch { + Self.logger.error("Failed to read known_hosts file: \(error.localizedDescription)") + return nil } - - return entries } - private func saveEntries(_ entries: [(host: String, keyType: String, keyData: Data)]) { - let lines = entries.map { "\($0.host) \($0.keyType) \($0.keyData.base64EncodedString())" } - let content = lines.joined(separator: "\n") + (lines.isEmpty ? "" : "\n") - + private func saveFile(_ file: KnownHostsFile) { do { - try content.write(toFile: filePath, atomically: true, encoding: .utf8) + try file.contents.write(toFile: filePath, atomically: true, encoding: .utf8) } catch { Self.logger.error("Failed to write known_hosts file: \(error.localizedDescription)") } diff --git a/apple/TableProMobile/TableProMobile/SSH/HostKeyVerifier.swift b/apple/TableProMobile/TableProMobile/SSH/HostKeyVerifier.swift index 408d61eb0d..8c5d301a22 100644 --- a/apple/TableProMobile/TableProMobile/SSH/HostKeyVerifier.swift +++ b/apple/TableProMobile/TableProMobile/SSH/HostKeyVerifier.swift @@ -1,11 +1,3 @@ -// -// HostKeyVerifier.swift -// TableProMobile -// -// Checks a presented SSH host key against the known-hosts store before any -// credential is sent, and asks the user when the key is new or has changed. -// - import Foundation import TableProDatabase import TableProSSHTransport @@ -33,6 +25,13 @@ enum HostKeyVerifier { case let .invalidHost(problem): throw SSHTunnelError.hostKeyRejected(problem.explanation) + // Asking would offer a server whose key changed as a new one. + case .unreadable: + throw SSHTunnelError.hostKeyUnverified(String(localized: """ + The SSH host keys saved on this device could not be read, so this server's key could not be \ + checked. + """)) + case let .unknown(fingerprint, presentedType): try await decide( .unknownHostKey(host: hostname, port: port, keyType: presentedType, fingerprint: fingerprint), @@ -63,9 +62,7 @@ enum HostKeyVerifier { } } - /// Without a prompter there is no screen to ask from, which is every background caller: a - /// Shortcut, Siri, or a widget. Those end with an error naming what to do instead of waiting on - /// a question nobody can answer. + // No prompter means a background caller (a Shortcut, Siri, a widget), which gets an error instead of a question. private static func decide( _ question: ConnectionQuestion, prompter: (any ConnectionPrompter)?, diff --git a/apple/TableProMobile/TableProMobile/ViewModels/DataBrowserViewModel.swift b/apple/TableProMobile/TableProMobile/ViewModels/DataBrowserViewModel.swift index 3a7de9d6ee..924322f039 100644 --- a/apple/TableProMobile/TableProMobile/ViewModels/DataBrowserViewModel.swift +++ b/apple/TableProMobile/TableProMobile/ViewModels/DataBrowserViewModel.swift @@ -44,9 +44,7 @@ final class DataBrowserViewModel { @ObservationIgnored private var session: ConnectionSession? @ObservationIgnored private var table: TableInfo? @ObservationIgnored private var databaseType: DatabaseType = .mysql - /// The schema the rows on screen were fetched with. Every statement the browser and its - /// children issue reads this one value, so an edit cannot target a schema the visible rows did - /// not come from. + // The schema the rows on screen came from. Every statement reads it, so an edit cannot target another schema. private(set) var schema: String? @ObservationIgnored private var host: String = "" @ObservationIgnored private var trackRead: SessionReadTracker? @@ -92,8 +90,9 @@ final class DataBrowserViewModel { var paginationLabel: String { guard !legacyRows.isEmpty else { return "" } - let start = pagination.currentOffset + 1 - let end = pagination.currentOffset + legacyRows.count + // A memory trim drops rows from the head of the page, so the range starts at the first row still held. + let start = pagination.currentOffset + window.firstAbsoluteIndex + 1 + let end = start + legacyRows.count - 1 guard let total = pagination.totalRows else { return "\(start)-\(end)" } return Self.pageRangeLabel(start: start, end: end, total: total) } @@ -328,9 +327,7 @@ final class DataBrowserViewModel { ) } - /// SQL Server's `OFFSET FETCH` requires `ORDER BY`. When the user has not picked an explicit - /// sort, inject a stable order (first primary-key column, falling back to the first column) - /// so paging is deterministic across requests. Other databases tolerate an empty ORDER BY. + // SQL Server's `OFFSET FETCH` requires `ORDER BY`. Without a user sort, a key column keeps paging stable. private func effectiveSortState() -> SortState { if sortState.isSorting { return sortState } guard databaseType == .mssql || databaseType == .oracle else { return sortState } @@ -726,6 +723,8 @@ final class DataBrowserViewModel { buffer.reset() pageEndedOnItsOwn = false pageWasTrimmed = false + // The notice belongs to the page that was trimmed, not to the empty result of a later load. + memoryWarning = nil let task = Task { [weak self] in guard let self else { return } diff --git a/apple/TableProMobile/TableProMobileTests/DataBrowserViewModelTests.swift b/apple/TableProMobile/TableProMobileTests/DataBrowserViewModelTests.swift index 5754510e9e..9de74f2a30 100644 --- a/apple/TableProMobile/TableProMobileTests/DataBrowserViewModelTests.swift +++ b/apple/TableProMobile/TableProMobileTests/DataBrowserViewModelTests.swift @@ -867,6 +867,52 @@ struct DataBrowserViewModelTests { #expect(vm.canGoToNextPage == false) } + private func numberedPage(from first: Int, count: Int) -> QueryResult { + let rows: [[String?]] = (first ..< first + count).map { [String($0), "user\($0)"] } + return QueryResult(columns: makeColumns(), rows: rows, rowsAffected: 0, executionTime: 0) + } + + @Test("A page trimmed under memory pressure labels the rows it kept, not the ones it dropped") + func trimmedPageLabelsTheRowsItKept() async { + let driver = MockDatabaseDriver() + driver.scriptedColumns = makeColumns() + let vm = DataBrowserViewModel() + vm.attach(session: makeSession(driver: driver), table: TableInfo(name: "users"), databaseType: .mysql, host: "localhost") + driver.scriptedExecuteResults = [ + .success(numberedPage(from: 1, count: 500)), + .success(QueryResult(columns: [], rows: [["3503"]], rowsAffected: 0, executionTime: 0)) + ] + await vm.changePageSize(500) + driver.scriptedExecuteResults = [.success(numberedPage(from: 501, count: 500))] + await vm.goToNextPage() + #expect(vm.paginationLabel == DataBrowserViewModel.pageRangeLabel(start: 501, end: 1_000, total: 3_503)) + + await vm.handlePressure(.warning) + + #expect(vm.legacyRows.count == 100) + #expect(vm.legacyRows.first?.first == "901") + #expect(vm.paginationLabel == DataBrowserViewModel.pageRangeLabel(start: 901, end: 1_000, total: 3_503)) + } + + @Test("A memory notice from a trimmed page does not cover the result of a later load") + func memoryNoticeEndsWithItsPage() async { + let driver = MockDatabaseDriver() + driver.scriptedColumns = makeColumns() + let vm = DataBrowserViewModel() + vm.attach(session: makeSession(driver: driver), table: TableInfo(name: "users"), databaseType: .mysql, host: "localhost") + driver.scriptedExecuteResults = [.success(numberedPage(from: 1, count: 2))] + await vm.load(isInitial: true) + await vm.handlePressure(.warning) + #expect(vm.memoryWarning != nil) + + vm.filters = [TableFilter(columnName: "name", value: "nobody")] + driver.scriptedExecuteResults = [emptyResult()] + await vm.applyFilters() + + #expect(vm.legacyRows.isEmpty) + #expect(vm.memoryWarning == nil) + } + private func keyPage(from start: Int, count: Int, total: Int) -> KeyContentsPage { let columns = [ ColumnInfo(name: "index", typeName: "integer", ordinalPosition: 0), diff --git a/apple/TableProMobile/TableProMobileTests/Drivers/FakeMySQLServer.swift b/apple/TableProMobile/TableProMobileTests/Drivers/FakeMySQLServer.swift index 82c0fd9fc6..cc15f357aa 100644 --- a/apple/TableProMobile/TableProMobileTests/Drivers/FakeMySQLServer.swift +++ b/apple/TableProMobile/TableProMobileTests/Drivers/FakeMySQLServer.swift @@ -11,7 +11,9 @@ import TableProTLSTestFixtures // it answers the SSL request with bytes that are not TLS. A query starting with `holdsQueriesStarting` waits // up to `holdSeconds` for `KILL QUERY ` from another session, then fails with 1317 as a killed // statement does, or with `killedQueriesComplete` succeeds as one that finished before the kill landed. Unkilled, -// or after `release`, it succeeds. A cleartext password passed to `refuse` is answered with 1045, as an expired token is. +// or after `release`, it succeeds. A query starting with `streamsRowsStarting` returns `streamedRows` one-column rows, +// cut by a kill for its session with 1317. A cleartext password passed to `refuse` is answered with 1045, as an expired +// token is. final class FakeMySQLServer: @unchecked Sendable { struct SetupFailure: Error {} @@ -34,6 +36,8 @@ final class FakeMySQLServer: @unchecked Sendable { var queries: [String] = [] var quit = false var connectionId: UInt32 = 0 + var streamedRowsSent = 0 + var streamWasKilled = false func sawInPlaintext(_ secret: String) -> Bool { let needle = Array(secret.utf8) @@ -74,6 +78,13 @@ final class FakeMySQLServer: @unchecked Sendable { // ER_ACCESS_DENIED_ERROR (1045), SQLSTATE 28000. private static let accessDeniedPacket: [UInt8] = [0xFF, 0x15, 0x04] + Array("#28000".utf8) + Array("Access denied for user (using password: YES)".utf8) + // No warnings, SERVER_STATUS_AUTOCOMMIT. + private static let eofPacket: [UInt8] = [0xFE, 0x00, 0x00, 0x02, 0x00] + // A utf8mb4 VARCHAR(255) named `n`: catalog, schema, table, org_table, name and org_name, then charset 45, length + // 1020, type 0xFD, no flags or decimals. + private static let textColumnDefinition: [UInt8] = ["def", "", "", "", "n", ""] + .flatMap { FakeMySQLServer.lengthEncoded($0) } + + [0x0C, 45, 0x00, 0xFC, 0x03, 0x00, 0x00, 0xFD, 0x00, 0x00, 0x00, 0x00, 0x00] let port: Int private let listener: Int32 @@ -83,6 +94,8 @@ final class FakeMySQLServer: @unchecked Sendable { private let heldPrefix: String? private let holdSeconds: TimeInterval private let killedQueriesComplete: Bool + private let streamedPrefix: String? + private let streamedRows: Int private let fixtures: URL? private let kills = KillBoard() private let refusedPasswords = OSAllocatedUnfairLock>(initialState: []) @@ -107,7 +120,9 @@ final class FakeMySQLServer: @unchecked Sendable { breaksHandshake: Bool = false, holdsQueriesStarting heldPrefix: String? = nil, holdSeconds: TimeInterval = 10, - killedQueriesComplete: Bool = false + killedQueriesComplete: Bool = false, + streamsRowsStarting streamedPrefix: String? = nil, + streamedRows: Int = 0 ) throws { self.asksForCleartext = asksForCleartext self.asksForFullSHA2Auth = asksForFullSHA2Auth @@ -115,6 +130,8 @@ final class FakeMySQLServer: @unchecked Sendable { self.heldPrefix = heldPrefix self.holdSeconds = holdSeconds self.killedQueriesComplete = killedQueriesComplete + self.streamedPrefix = streamedPrefix + self.streamedRows = streamedRows if let identity { let directory = FileManager.default.temporaryDirectory .appendingPathComponent("FakeMySQLServer-\(UUID().uuidString)", isDirectory: true) @@ -271,6 +288,10 @@ final class FakeMySQLServer: @unchecked Sendable { if command.payload.first == Self.comQuery { let query = String(decoding: command.payload.dropFirst(), as: UTF8.self) session.queries.append(query) + if let streamedPrefix, query.hasPrefix(streamedPrefix) { + guard streamRows(on: channel, after: command.sequence, into: &session) else { return } + continue + } if let killed = Self.killedSession(in: query) { kills.kill(killed) } else if let heldPrefix, query.hasPrefix(heldPrefix), @@ -282,6 +303,31 @@ final class FakeMySQLServer: @unchecked Sendable { } } + // A text result set without CLIENT_DEPRECATE_EOF, which the handshake does not offer: the column count, the column, + // an EOF, the rows and a closing EOF, or 1317 in place of the rest once a kill for the session arrives. + private func streamRows(on channel: ServerChannel, after sequence: UInt8, into session: inout Session) -> Bool { + var next = sequence + func send(_ payload: [UInt8]) -> Bool { + next &+= 1 + return channel.send(payload, sequence: next) + } + guard send([1]), send(Self.textColumnDefinition), send(Self.eofPacket) else { return false } + for index in 0.. [UInt8] { + let bytes = Array(text.utf8) + return [UInt8(bytes.count)] + bytes + } + private static func killedSession(in query: String) -> UInt32? { let words = query.split(separator: " ") guard words.count == 3, words[0] == "KILL", words[1] == "QUERY" else { return nil } @@ -346,6 +392,15 @@ final class FakeMySQLServer: @unchecked Sendable { end(connectionId, as: .released) } + // Without waiting: a session sending rows looks for a kill between them. + func takeKill(_ connectionId: UInt32) -> Bool { + condition.lock() + defer { condition.unlock() } + guard ends[connectionId] == .killed else { return false } + ends[connectionId] = nil + return true + } + func hold(_ connectionId: UInt32, seconds: TimeInterval) -> HoldEnd { condition.lock() defer { condition.unlock() } diff --git a/apple/TableProMobile/TableProMobileTests/Drivers/LazyCellKeyTests.swift b/apple/TableProMobile/TableProMobileTests/Drivers/LazyCellKeyTests.swift new file mode 100644 index 0000000000..ff6afb9c36 --- /dev/null +++ b/apple/TableProMobile/TableProMobileTests/Drivers/LazyCellKeyTests.swift @@ -0,0 +1,79 @@ +import Foundation +@testable import TableProMobile +import TableProModels +import Testing + +@Suite(.timeLimit(.minutes(1))) +struct LazyCellKeyTests { + private static let notes = LazyContext(table: "notes", primaryKeyColumns: ["tenant", "id"]) + + @Test("A row's key is read from the columns the result carries, in the table's key order") + func keyFromResultColumns() throws { + let key = try #require(LazyCellKey(Self.notes, columnNames: ["id", "body", "tenant"])) + let values = ["7", "long text", "acme"] + let primaryKey = key.primaryKey { values[$0] } + let ref = try #require(key.ref(column: "body", primaryKey: primaryKey)) + + #expect(ref.table == "notes") + #expect(ref.column == "body") + #expect(ref.primaryKey == [ + PrimaryKeyComponent(column: "tenant", value: "acme"), + PrimaryKeyComponent(column: "id", value: "7"), + ]) + } + + @Test("A result without every key column, or a read without a key, offers no ref") + func noKeyNoRef() { + #expect(LazyCellKey(Self.notes, columnNames: ["id", "body"]) == nil) + #expect(LazyCellKey(nil, columnNames: ["id", "body"]) == nil) + #expect(LazyCellKey(LazyContext(table: "notes", primaryKeyColumns: []), columnNames: ["id"]) == nil) + } + + @Test("A NULL or binary key value names no row") + func unreadableKeyNamesNoRow() throws { + let key = try #require(LazyCellKey(Self.notes, columnNames: ["tenant", "id", "body"])) + let primaryKey = key.primaryKey { $0 == 0 ? "acme" : nil } + + #expect(primaryKey == nil) + #expect(key.ref(column: "body", primaryKey: primaryKey) == nil) + } + + @Test("A DuckDB value cut for display carries the key it is read back by") + func duckDBCutValueCarriesRef() async throws { + let cells = try await Self.duckDBBody(selecting: "SELECT id, body FROM notes") + let ref = try #require(cells.last?.fullValueRef) + + #expect(ref.table == "notes") + #expect(ref.column == "body") + #expect(ref.primaryKey == [PrimaryKeyComponent(column: "id", value: "7")]) + } + + @Test("A DuckDB result without the key column offers nothing to load") + func duckDBWithoutKeyOffersNoRef() async throws { + let cells = try await Self.duckDBBody(selecting: "SELECT body FROM notes") + guard case .truncatedText(_, let totalBytes, let ref)? = cells.last else { + Issue.record("expected the body cut for display, got \(cells)") + return + } + #expect(totalBytes == 5_000) + #expect(ref == nil) + } + + private static func duckDBBody(selecting query: String) async throws -> [Cell] { + let driver = DuckDBDriver(source: .inMemory) + try await driver.connect() + _ = try await driver.execute(query: "CREATE TABLE notes (id INTEGER PRIMARY KEY, body VARCHAR)") + _ = try await driver.execute(query: "INSERT INTO notes VALUES (7, repeat('x', 5000))") + let options = StreamOptions( + textTruncationBytes: 4_096, + maxRows: 10, + lazyContext: LazyContext(table: "notes", primaryKeyColumns: ["id"]) + ) + var cells: [Cell] = [] + for try await element in driver.executeStreaming(query: query, options: options) { + if case .row(let row) = element { cells = row.cells } + } + try await driver.disconnect() + return cells + } +} diff --git a/apple/TableProMobile/TableProMobileTests/Drivers/MSSQLRowCapTests.swift b/apple/TableProMobile/TableProMobileTests/Drivers/MSSQLRowCapTests.swift new file mode 100644 index 0000000000..0c3e2ea05a --- /dev/null +++ b/apple/TableProMobile/TableProMobileTests/Drivers/MSSQLRowCapTests.swift @@ -0,0 +1,29 @@ +@testable import TableProMobile +import TableProModels +import Testing + +struct MSSQLRowCapTests { + @Test("A lone query ends on the server at the row cap", arguments: [ + "SELECT * FROM events", + "SELECT * FROM events;", + "WITH recent AS (SELECT * FROM events) SELECT * FROM recent", + "-- newest first\nSELECT * FROM events ORDER BY id DESC", + ]) + func loneQueryEndsAtCap(batch: String) { + #expect(CappedRead.qualifies(batch, databaseType: .mssql)) + } + + @Test("A batch that may still write or run more reads past the row cap", arguments: [ + "SELECT * FROM events; UPDATE events SET seen = 1", + "SELECT * FROM events UPDATE events SET seen = 1", + "SELECT * FROM events; SELECT * FROM users", + "SELECT * INTO #recent FROM events", + "WITH old AS (SELECT * FROM events) DELETE FROM old", + "DELETE FROM events OUTPUT deleted.id WHERE seen = 1", + "SET NOCOUNT ON; SELECT * FROM events", + "EXEC dbo.monthly_report", + ]) + func batchReadsPastCap(batch: String) { + #expect(!CappedRead.qualifies(batch, databaseType: .mssql)) + } +} diff --git a/apple/TableProMobile/TableProMobileTests/Drivers/MySQLRowCapTests.swift b/apple/TableProMobile/TableProMobileTests/Drivers/MySQLRowCapTests.swift new file mode 100644 index 0000000000..9b959a2fd2 --- /dev/null +++ b/apple/TableProMobile/TableProMobileTests/Drivers/MySQLRowCapTests.swift @@ -0,0 +1,156 @@ +import Foundation +import TableProDatabase +@testable import TableProMobile +import TableProModels +import Testing + +// The fake server gives the first session thread id 1; a kill arrives on a second session. +@Suite(.timeLimit(.minutes(1))) +struct MySQLRowCapTests { + private static let scan = "SELECT body FROM events" + + @Test("At the row cap the statement is stopped on the server instead of read to its end") + func rowCapKillsTheRest() async throws { + let server = try FakeMySQLServer(identity: nil, streamsRowsStarting: Self.scan, streamedRows: 1_000_000) + defer { server.close() } + async let sessions = server.serveConcurrently(sessions: 2) + let driver = Self.driver(port: server.port) + try await driver.connect() + + let read = try await Self.read(driver.executeStreaming(query: Self.scan, options: StreamOptions(maxRows: 100))) + _ = try await driver.execute(query: "SELECT 2") + try await driver.disconnect() + + let served = await sessions + #expect(read.rows == 100) + #expect(read.cappedAt == 100) + #expect(served[1].queries == ["KILL QUERY 1"]) + #expect(served[0].streamWasKilled) + #expect(served[0].streamedRowsSent < 1_000_000) + #expect(Array(served[0].queries.suffix(2)) == [Self.scan, "SELECT 2"]) + } + + @Test("A result that exactly fills the cap is complete, and nothing is killed") + func filledCapIsComplete() async throws { + let server = try FakeMySQLServer(identity: nil, streamsRowsStarting: Self.scan, streamedRows: 100) + defer { server.close() } + async let session = server.serveOneSession() + let driver = Self.driver(port: server.port) + try await driver.connect() + + let read = try await Self.read(driver.executeStreaming(query: Self.scan, options: StreamOptions(maxRows: 100))) + _ = try await driver.execute(query: "SELECT 2") + try await driver.disconnect() + + let served = await session + #expect(read.rows == 100) + #expect(read.cappedAt == nil) + #expect(!served.streamWasKilled) + #expect(Array(served.queries.suffix(2)) == [Self.scan, "SELECT 2"]) + } + + @Test("On TiDB the cap reads the rest rather than kill, which would drop the session once the statement ended") + func tidbRowCapSendsNoKill() async throws { + let server = try FakeMySQLServer(identity: nil, streamsRowsStarting: Self.scan, streamedRows: 300) + defer { server.close() } + async let sessions = server.serveConcurrently(sessions: 2, acceptingWithin: 2_000) + let driver = Self.driver(port: server.port, databaseType: .tidb) + try await driver.connect() + + let read = try await Self.read(driver.executeStreaming(query: Self.scan, options: StreamOptions(maxRows: 100))) + _ = try await driver.execute(query: "SELECT 2") + try await driver.disconnect() + + let served = await sessions + #expect(read.rows == 100) + #expect(read.cappedAt == 100) + #expect(served[1].queries.isEmpty) + #expect(served[0].streamedRowsSent == 300) + #expect(Array(served[0].queries.suffix(2)) == [Self.scan, "SELECT 2"]) + } + + // A kill would roll back the delete on InnoDB, or leave it half done on Aria or MyISAM. + @Test("A DELETE ... RETURNING past the cap is read to its end, with no kill") + func deleteReturningPastCapRunsToEnd() async throws { + try await Self.expectReadToEnd( + "DELETE FROM events WHERE created < '2020-01-01' RETURNING id", databaseType: .mariadb + ) + } + + // A kill would stop the procedure in its SELECT, or in the statements after it. + @Test("A CALL whose first result passes the cap is read to its end, with no kill") + func callPastCapRunsToEnd() async throws { + try await Self.expectReadToEnd("CALL archive_events()", databaseType: .mysql) + } + + @Test("Only a lone read is stopped at the cap", arguments: [ + ("SELECT body FROM events", DatabaseType.mysql), + ("WITH recent AS (SELECT * FROM events) SELECT * FROM recent", DatabaseType.mysql), + ("TABLE events", DatabaseType.mysql), + ("VALUES ROW(1), ROW(2)", DatabaseType.mysql), + ("SELECT body FROM events", DatabaseType.mariadb), + ]) + func loneReadIsCapped(sql: String, databaseType: DatabaseType) { + #expect(CappedRead.qualifies(sql, databaseType: databaseType)) + } + + @Test("A statement that writes or runs more is never stopped at the cap", arguments: [ + ("DELETE FROM events RETURNING id", DatabaseType.mariadb), + ("INSERT INTO archive SELECT * FROM events RETURNING id", DatabaseType.mariadb), + ("REPLACE INTO archive SELECT * FROM events RETURNING id", DatabaseType.mariadb), + ("CALL archive_events()", DatabaseType.mysql), + ("WITH old AS (SELECT id FROM events) DELETE FROM events WHERE id IN (SELECT id FROM old)", DatabaseType.mysql), + ("SELECT * FROM events INTO OUTFILE '/tmp/events.csv'", DatabaseType.mysql), + ("SHOW PROCESSLIST", DatabaseType.mysql), + ]) + func writeIsNeverCapped(sql: String, databaseType: DatabaseType) { + #expect(!CappedRead.qualifies(sql, databaseType: databaseType)) + } + + private static func expectReadToEnd(_ statement: String, databaseType: DatabaseType) async throws { + let server = try FakeMySQLServer(identity: nil, streamsRowsStarting: statement, streamedRows: 300) + defer { server.close() } + async let sessions = server.serveConcurrently(sessions: 2, acceptingWithin: 2_000) + let driver = Self.driver(port: server.port, databaseType: databaseType) + try await driver.connect() + + let read = try await Self.read(driver.executeStreaming(query: statement, options: StreamOptions(maxRows: 100))) + _ = try await driver.execute(query: "SELECT 2") + try await driver.disconnect() + + let served = await sessions + #expect(read.rows == 100) + #expect(read.cappedAt == 100) + #expect(served[1].queries.isEmpty) + #expect(!served[0].streamWasKilled) + #expect(served[0].streamedRowsSent == 300) + #expect(Array(served[0].queries.suffix(2)) == [statement, "SELECT 2"]) + } + + private struct Read { + var rows = 0 + var cappedAt: Int? + } + + private static func read(_ stream: AsyncThrowingStream) async throws -> Read { + var read = Read() + for try await element in stream { + switch element { + case .row: + read.rows += 1 + case .truncated(let reason): + if case .rowCap(let cap) = reason { read.cappedAt = cap } + default: + break + } + } + return read + } + + private static func driver(port: Int, databaseType: DatabaseType = .mysql) -> MySQLDriver { + MySQLDriver( + host: "127.0.0.1", port: port, user: "app", password: "app-password", database: "", + databaseType: databaseType + ) + } +} diff --git a/apple/TableProMobile/TableProMobileTests/Drivers/NativeDriverQueryTimeoutTests.swift b/apple/TableProMobile/TableProMobileTests/Drivers/NativeDriverQueryTimeoutTests.swift new file mode 100644 index 0000000000..0dcce27f40 --- /dev/null +++ b/apple/TableProMobile/TableProMobileTests/Drivers/NativeDriverQueryTimeoutTests.swift @@ -0,0 +1,67 @@ +import Foundation +@testable import TableProMobile +import TableProModels +import Testing + +@Suite(.timeLimit(.minutes(1))) +struct NativeDriverQueryTimeoutTests { + @Test("A PostgreSQL query timeout reaches the server as the session's statement_timeout") + func postgreSQLTimeoutReachesServer() async throws { + let statements = try await Self.postgreSQLSession(queryTimeoutSeconds: 45) + #expect(statements.contains("SET statement_timeout = 45000")) + } + + @Test("Without a query timeout a PostgreSQL session keeps the server's own limit") + func postgreSQLWithoutTimeoutSendsNone() async throws { + let statements = try await Self.postgreSQLSession(queryTimeoutSeconds: nil) + #expect(!statements.contains { $0.contains("statement_timeout") }) + #expect(PostgreSQLDriver.sessionSetupStatements(queryTimeoutSeconds: 0).isEmpty) + } + + @Test("The factory hands the connection's query timeout to PostgreSQL and Redshift", arguments: [ + DatabaseType.postgresql, .redshift, + ]) + func factoryPassesPostgreSQLTimeout(type: DatabaseType) throws { + let connection = DatabaseConnection(name: "Shop", type: type, host: "db.example.com", queryTimeoutSeconds: 45) + let driver = try #require(try IOSDriverFactory().createDriver(for: connection, password: nil) as? PostgreSQLDriver) + #expect(driver.queryTimeoutSeconds == 45) + } + + @Test("SQL Server takes a query timeout as its lock wait, as the Mac does") + func sqlServerTimeoutIsLockWait() throws { + let connection = DatabaseConnection(name: "Shop", type: .mssql, host: "db.example.com", queryTimeoutSeconds: 45) + let driver = try #require(try IOSDriverFactory().createDriver(for: connection, password: nil) as? MSSQLDriver) + + #expect(driver.queryTimeoutSeconds == 45) + #expect(MSSQLDriver.sessionSetupStatements(queryTimeoutSeconds: 45) == ["SET LOCK_TIMEOUT 45000"]) + #expect(MSSQLDriver.sessionSetupStatements(queryTimeoutSeconds: nil).isEmpty) + #expect(MSSQLDriver.sessionSetupStatements(queryTimeoutSeconds: 0).isEmpty) + } + + @Test("An Oracle query timeout reaches the core that ends a statement past it") + func oracleTimeoutReachesCore() throws { + let timed = DatabaseConnection(name: "Shop", type: .oracle, host: "db.example.com", queryTimeoutSeconds: 45) + let untimed = DatabaseConnection(name: "Shop", type: .oracle, host: "db.example.com") + + let timedDriver = try #require(try IOSDriverFactory().createDriver(for: timed, password: nil) as? OracleDriver) + let untimedDriver = try #require(try IOSDriverFactory().createDriver(for: untimed, password: nil) as? OracleDriver) + + #expect(timedDriver.queryTimeoutSeconds == 45) + #expect(untimedDriver.queryTimeoutSeconds == 0) + } + + private static func postgreSQLSession(queryTimeoutSeconds: Int?) async throws -> [String] { + let server = try FakePostgreSQLServer() + defer { server.close() } + async let session = server.serveOneSession() + let driver = PostgreSQLDriver( + host: "127.0.0.1", port: server.port, user: "app", password: "app-password", database: "app", + queryTimeoutSeconds: queryTimeoutSeconds + ) + try await driver.connect() + try await driver.disconnect() + let served = await session + #expect(served.startedUp) + return served.queries + } +} diff --git a/apple/TableProMobile/TableProMobileTests/SSH/HostKeyStoreTests.swift b/apple/TableProMobile/TableProMobileTests/SSH/HostKeyStoreTests.swift index 53c751d900..2cd9ff8df5 100644 --- a/apple/TableProMobile/TableProMobileTests/SSH/HostKeyStoreTests.swift +++ b/apple/TableProMobile/TableProMobileTests/SSH/HostKeyStoreTests.swift @@ -1,8 +1,3 @@ -// -// HostKeyStoreTests.swift -// TableProMobileTests -// - import Foundation import TableProSSHTransport import Testing @@ -118,6 +113,49 @@ struct HostKeyStoreTests { #expect(store.trustedHosts().isEmpty) } + @Test("A saved key file that cannot be read checks no key and is never written over") + func unreadableFileChecksNothing() throws { + let path = makeTempFilePath() + defer { try? FileManager.default.removeItem(atPath: path) } + + let store = HostKeyStore(filePath: path) + store.trust(hostname: "db.example.com", port: 22, key: makeTestKey(0x31), keyType: "ssh-ed25519") + let saved = try String(contentsOfFile: path, encoding: .utf8) + try FileManager.default.setAttributes([.posixPermissions: 0], ofItemAtPath: path) + + #expect( + store.verify(keyData: makeTestKey(0x32), keyType: "ssh-ed25519", hostname: "db.example.com", port: 22) + == .unreadable + ) + store.trust(hostname: "other.example.com", port: 22, key: makeTestKey(0x33), keyType: "ssh-ed25519") + store.remove(hostname: "db.example.com", port: 22) + + try FileManager.default.setAttributes([.posixPermissions: 0o600], ofItemAtPath: path) + let after = try String(contentsOfFile: path, encoding: .utf8) + #expect(after == saved) + #expect(store.verify(keyData: makeTestKey(0x31), keyType: "ssh-ed25519", hostname: "db.example.com", port: 22) == .trusted) + } + + @Test("A saved line that cannot be parsed keeps its host from looking new, and stays in the file") + func unparsedLineIsKept() throws { + let path = makeTempFilePath() + defer { try? FileManager.default.removeItem(atPath: path) } + + let unparsed = "[db.example.com]:22 ssh-ed25519 !!not-base64!!" + try (unparsed + "\n").write(toFile: path, atomically: true, encoding: .utf8) + let store = HostKeyStore(filePath: path) + + #expect( + store.verify(keyData: makeTestKey(0x41), keyType: "ssh-ed25519", hostname: "db.example.com", port: 22) + == .unreadable + ) + store.trust(hostname: "other.example.com", port: 22, key: makeTestKey(0x42), keyType: "ssh-ed25519") + + let contents = try String(contentsOfFile: path, encoding: .utf8) + #expect(contents.hasPrefix(unparsed + "\n")) + #expect(store.verify(keyData: makeTestKey(0x42), keyType: "ssh-ed25519", hostname: "other.example.com", port: 22) == .trusted) + } + @Test("Fingerprints use the OpenSSH SHA256 form") func fingerprintFormat() { let fingerprint = HostKeyStore.fingerprint(of: Data("tablepro".utf8)) diff --git a/apple/TableProMobile/TableProMobileTests/SSH/HostKeyVerifierTests.swift b/apple/TableProMobile/TableProMobileTests/SSH/HostKeyVerifierTests.swift index 0bc878a7c9..015811f074 100644 --- a/apple/TableProMobile/TableProMobileTests/SSH/HostKeyVerifierTests.swift +++ b/apple/TableProMobile/TableProMobileTests/SSH/HostKeyVerifierTests.swift @@ -88,6 +88,27 @@ struct HostKeyVerifierTests { #expect(previous != current) } + @Test("Saved keys that cannot be read fail the tunnel without asking, so a changed key is never offered as new") + func unreadableStoreFailsWithoutAsking() async throws { + let path = FileManager.default.temporaryDirectory + .appendingPathComponent("known_hosts-\(UUID().uuidString)").path + defer { try? FileManager.default.removeItem(atPath: path) } + let store = HostKeyStore(filePath: path) + store.trust(hostname: "db.example.com", port: 22, key: Data("old-key".utf8), keyType: "ssh-ed25519") + try FileManager.default.setAttributes([.posixPermissions: 0], ofItemAtPath: path) + let prompter = RecordingPrompter(answer: true) + + let error = await #expect(throws: SSHTunnelError.self) { + try await verify(store: store, prompter: prompter, key: Data("new-key".utf8)) + } + + guard case .some(.hostKeyUnverified) = error else { + Issue.record("expected the unverified host key error, got \(String(describing: error))") + return + } + #expect(prompter.asked.isEmpty) + } + @Test("With no screen to ask from, an unknown host fails at once instead of waiting") func noPrompterFailsImmediately() async { let store = makeStore() diff --git a/apple/TableProTests/Core/SSH/HostKeyStoreTests.swift b/apple/TableProTests/Core/SSH/HostKeyStoreTests.swift index 761c47f152..3b0ede681d 100644 --- a/apple/TableProTests/Core/SSH/HostKeyStoreTests.swift +++ b/apple/TableProTests/Core/SSH/HostKeyStoreTests.swift @@ -1,10 +1,3 @@ -// -// HostKeyStoreTests.swift -// TableProTests -// -// Tests for HostKeyStore file-based SSH host key storage. -// - import Foundation import TableProPluginKit import TableProSSHTransport @@ -13,13 +6,11 @@ import Testing @testable import TablePro struct HostKeyStoreTests { - /// Create a temporary file path for test isolation private func makeTempFilePath() -> String { let tempDir = NSTemporaryDirectory() return (tempDir as NSString).appendingPathComponent("test_known_hosts_\(UUID().uuidString)") } - /// Create a deterministic test key private func makeTestKey(_ seed: UInt8 = 0x42, length: Int = 32) -> Data { Data(repeating: seed, count: length) } @@ -119,11 +110,9 @@ struct HostKeyStoreTests { store.remove(hostname: "example.com", port: 22) let result = store.verify(keyData: key, keyType: "ssh-rsa", hostname: "example.com", port: 22) - switch result { - case .unknown: - break // expected - default: + guard case .unknown = result else { Issue.record("Expected .unknown after removal, got \(result)") + return } } @@ -133,15 +122,11 @@ struct HostKeyStoreTests { let fingerprint = HostKeyStore.fingerprint(of: key) #expect(fingerprint.hasPrefix("SHA256:")) - - // Fingerprint should not contain '=' padding (matches OpenSSH format) #expect(!fingerprint.contains("=")) - // Same key should produce the same fingerprint let fingerprint2 = HostKeyStore.fingerprint(of: key) #expect(fingerprint == fingerprint2) - // Different key should produce a different fingerprint let otherKey = makeTestKey(0x00, length: 64) let otherFingerprint = HostKeyStore.fingerprint(of: otherKey) #expect(fingerprint != otherFingerprint) @@ -165,7 +150,6 @@ struct HostKeyStoreTests { #expect(store.verify(keyData: key2, keyType: "ssh-ed25519", hostname: "host-b.com", port: 22) == .trusted) #expect(store.verify(keyData: key3, keyType: "ecdsa-sha2-nistp256", hostname: "host-c.com", port: 22) == .trusted) - // Removing one host should not affect others store.remove(hostname: "host-b.com", port: 22) #expect(store.verify(keyData: key1, keyType: "ssh-rsa", hostname: "host-a.com", port: 22) == .trusted) #expect(store.verify(keyData: key3, keyType: "ecdsa-sha2-nistp256", hostname: "host-c.com", port: 22) == .trusted) @@ -186,13 +170,10 @@ struct HostKeyStoreTests { #expect(store.verify(keyData: key22, keyType: "ssh-rsa", hostname: "example.com", port: 22) == .trusted) #expect(store.verify(keyData: key2222, keyType: "ssh-ed25519", hostname: "example.com", port: 2222) == .trusted) - // Key from port 22 should not match port 2222 let result = store.verify(keyData: key22, keyType: "ssh-rsa", hostname: "example.com", port: 2222) - switch result { - case .mismatch: - break // expected — different key stored for this port - default: + guard case .mismatch = result else { Issue.record("Expected .mismatch when using wrong port's key, got \(result)") + return } } @@ -220,20 +201,59 @@ struct HostKeyStoreTests { store.trust(hostname: "example.com", port: 22, key: oldKey, keyType: "ssh-rsa") #expect(store.verify(keyData: oldKey, keyType: "ssh-rsa", hostname: "example.com", port: 22) == .trusted) - // Trust with new key (same key type) store.trust(hostname: "example.com", port: 22, key: newKey, keyType: "ssh-rsa") #expect(store.verify(keyData: newKey, keyType: "ssh-rsa", hostname: "example.com", port: 22) == .trusted) - // Old key should no longer match let result = store.verify(keyData: oldKey, keyType: "ssh-rsa", hostname: "example.com", port: 22) - switch result { - case .mismatch: - break // expected - default: + guard case .mismatch = result else { Issue.record("Expected .mismatch for old key after update, got \(result)") + return } } + @Test("A saved key file that cannot be read checks no key and is never written over") + func testUnreadableFileChecksNothing() throws { + let path = makeTempFilePath() + defer { try? FileManager.default.removeItem(atPath: path) } + + let store = HostKeyStore(filePath: path) + store.trust(hostname: "example.com", port: 22, key: makeTestKey(0x31), keyType: "ssh-ed25519") + let saved = try String(contentsOfFile: path, encoding: .utf8) + try FileManager.default.setAttributes([.posixPermissions: 0], ofItemAtPath: path) + + #expect( + store.verify(keyData: makeTestKey(0x32), keyType: "ssh-ed25519", hostname: "example.com", port: 22) + == .unreadable + ) + store.trust(hostname: "other.example.com", port: 22, key: makeTestKey(0x33), keyType: "ssh-ed25519") + store.remove(hostname: "example.com", port: 22) + + try FileManager.default.setAttributes([.posixPermissions: 0o600], ofItemAtPath: path) + let after = try String(contentsOfFile: path, encoding: .utf8) + #expect(after == saved) + #expect(store.verify(keyData: makeTestKey(0x31), keyType: "ssh-ed25519", hostname: "example.com", port: 22) == .trusted) + } + + @Test("A saved line that cannot be parsed keeps its host from looking new, and stays in the file") + func testUnparsedLineIsKept() throws { + let path = makeTempFilePath() + defer { try? FileManager.default.removeItem(atPath: path) } + + let unparsed = "[example.com]:22 ssh-ed25519 !!not-base64!!" + try (unparsed + "\n").write(toFile: path, atomically: true, encoding: .utf8) + let store = HostKeyStore(filePath: path) + + #expect( + store.verify(keyData: makeTestKey(0x41), keyType: "ssh-ed25519", hostname: "example.com", port: 22) + == .unreadable + ) + store.trust(hostname: "other.example.com", port: 22, key: makeTestKey(0x42), keyType: "ssh-ed25519") + + let contents = try String(contentsOfFile: path, encoding: .utf8) + #expect(contents.hasPrefix(unparsed + "\n")) + #expect(store.verify(keyData: makeTestKey(0x42), keyType: "ssh-ed25519", hostname: "other.example.com", port: 22) == .trusted) + } + @Test("A host with a line break plants no key for another host", arguments: [ "replica.attacker.example\n[prod.corp.example", "replica.attacker.example\r\n[prod.corp.example", diff --git a/apple/changelog.d/4179.fixed.1.md b/apple/changelog.d/4179.fixed.1.md new file mode 100644 index 0000000000..192043a55e --- /dev/null +++ b/apple/changelog.d/4179.fixed.1.md @@ -0,0 +1 @@ +A MySQL result on iOS that exactly filled the row cap said more rows were left out. diff --git a/apple/changelog.d/4179.fixed.2.md b/apple/changelog.d/4179.fixed.2.md new file mode 100644 index 0000000000..50763630d7 --- /dev/null +++ b/apple/changelog.d/4179.fixed.2.md @@ -0,0 +1 @@ +Text over 4 KB in a SQL Server, Oracle or DuckDB table on iOS could not be loaded in full, edited or copied. diff --git a/apple/changelog.d/4179.fixed.3.md b/apple/changelog.d/4179.fixed.3.md new file mode 100644 index 0000000000..dfc6dba624 --- /dev/null +++ b/apple/changelog.d/4179.fixed.3.md @@ -0,0 +1 @@ +The query timeout on iOS was ignored on PostgreSQL, Redshift and Oracle, and as a lock timeout on SQL Server. diff --git a/apple/changelog.d/4179.fixed.4.md b/apple/changelog.d/4179.fixed.4.md new file mode 100644 index 0000000000..7c161d57ca --- /dev/null +++ b/apple/changelog.d/4179.fixed.4.md @@ -0,0 +1 @@ +After a low-memory trim on iOS, the data browser's page range named rows it no longer showed. diff --git a/apple/changelog.d/4179.fixed.md b/apple/changelog.d/4179.fixed.md new file mode 100644 index 0000000000..6ad40ac948 --- /dev/null +++ b/apple/changelog.d/4179.fixed.md @@ -0,0 +1 @@ +A large MySQL or SQL Server query on iOS kept downloading every remaining row after the row cap was reached. diff --git a/apple/changelog.d/4179.security.md b/apple/changelog.d/4179.security.md new file mode 100644 index 0000000000..988bcaa1c6 --- /dev/null +++ b/apple/changelog.d/4179.security.md @@ -0,0 +1 @@ +When the saved SSH host keys could not be read, a server whose key had changed was offered as a new server to trust.