Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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))
}
}
Original file line number Diff line number Diff line change
@@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Loading
Loading