Skip to content

Linux + iPhone Continuity / iMessage / SMS

fix(ios): pairing bypass

Zack Bartela65ff0056288
diff --git a/apple/Tether/ViewModels/TetherViewModel.swift b/apple/Tether/ViewModels/TetherViewModel.swift
index 1fd031c..00f2a39 100644
--- a/apple/Tether/ViewModels/TetherViewModel.swift
+++ b/apple/Tether/ViewModels/TetherViewModel.swift
@@ -133,6 +133,10 @@ final class TetherViewModel {
private var pendingReconnectTask: Task<Void, Never>?
private var connectTimeoutTask: Task<Void, Never>?
private var autoConnectingFingerprint: String?
+
+ // Direction of the current transport, set only from the connection state.
+ // The trust gate keys off this; `pairingIsInbound` is UI state that messages move.
+ private var connectionIsInbound = false
private var manualDisconnect = false
private var currentScenePhase: ScenePhase = .active
@@ -470,7 +474,13 @@ final class TetherViewModel {
private func setupServerHandlers() {
server.onNewConnection = { [weak self] incomingConn, incomingFingerprint in
guard let self = self else { return }
-
+
+ // A live or in-flight session is not displaced by a stranger dialling in.
+ guard self.appState == .disconnected || self.appState == .discovering else {
+ incomingConn.cancel()
+ return
+ }
+
// We just received an incoming connection from a peer!
// We give it to our connection manager and start decoding.
self.connection.accept(incomingConnection: incomingConn, fingerprint: incomingFingerprint)
@@ -490,6 +500,7 @@ final class TetherViewModel {
}
private func handleConnected(isInbound: Bool = false) {
+ connectionIsInbound = isInbound
let serverFP = connection.serverFingerprint
if certificateManager.isHostKnown(serverFP) {
@@ -612,6 +623,13 @@ final class TetherViewModel {
}
private func handleMessage(_ message: TetherMessage) {
+ // An unpinned peer may only speak the pairing half of the protocol, and only
+ // the half its direction allows. Same boundary tetherd enforces.
+ if !certificateManager.isHostKnown(connection.serverFingerprint),
+ !TetherCommand.allowedWhileUnpaired(message.parsedCommand, inbound: connectionIsInbound) {
+ return
+ }
+
switch message.parsedCommand {
case .clipboardUpdated:
if let content = message.content {
@@ -730,6 +748,7 @@ final class TetherViewModel {
// Approve a request from a peer that dialled us. This device is the approver
// here, so the tap is the real decision and the peer is told about it.
func acceptIncomingPairing() {
+ guard connectionIsInbound else { return }
connection.send(.pairAccepted)
finishPairing()
}
diff --git a/apple/TetherFramework/Sources/TetherFramework/TetherMessage.swift b/apple/TetherFramework/Sources/TetherFramework/TetherMessage.swift
index ef60764..b587b6f 100644
--- a/apple/TetherFramework/Sources/TetherFramework/TetherMessage.swift
+++ b/apple/TetherFramework/Sources/TetherFramework/TetherMessage.swift
@@ -27,6 +27,17 @@ public enum TetherCommand: String, Codable, Sendable {
case pairPending = "pair_pending"
case pairAccepted = "pair_accepted"
case error = "error"
+
+ // Commands a peer may send before it is pinned as a known host. Inbound means
+ // the peer dialled us and the local user is the approver, so only its request
+ // is allowed. Outbound means we asked and the peer's user decides, so only its
+ // verdict is. Mirrors the boundary tetherd enforces.
+ public static func allowedWhileUnpaired(_ command: TetherCommand?, inbound: Bool) -> Bool {
+ guard let command else { return false }
+ return inbound
+ ? command == .pairRequest
+ : command == .pairAccepted || command == .pairPending || command == .error
+ }
}
// MARK: - Message
diff --git a/apple/TetherFramework/Tests/TetherFrameworkTests/PairingPolicyTests.swift b/apple/TetherFramework/Tests/TetherFrameworkTests/PairingPolicyTests.swift
new file mode 100644
index 0000000..26ebaa6
--- /dev/null
+++ b/apple/TetherFramework/Tests/TetherFrameworkTests/PairingPolicyTests.swift
@@ -0,0 +1,38 @@
+//
+// PairingPolicyTests.swift
+// TetherFrameworkTests
+//
+
+import Testing
+@testable import TetherFramework
+
+struct PairingPolicyTests {
+ // A peer that dialled us is the one asking; the local user approves it. Anything
+ // beyond its request — a self-issued pair_accepted above all — must be dropped.
+ @Test func inboundPeerMaySendOnlyItsRequest() {
+ #expect(TetherCommand.allowedWhileUnpaired(.pairRequest, inbound: true))
+
+ #expect(!TetherCommand.allowedWhileUnpaired(.pairAccepted, inbound: true))
+ #expect(!TetherCommand.allowedWhileUnpaired(.pairPending, inbound: true))
+ #expect(!TetherCommand.allowedWhileUnpaired(.error, inbound: true))
+ #expect(!TetherCommand.allowedWhileUnpaired(.clipboardUpdated, inbound: true))
+ #expect(!TetherCommand.allowedWhileUnpaired(.clipboardContent, inbound: true))
+ #expect(!TetherCommand.allowedWhileUnpaired(.fileStart, inbound: true))
+ #expect(!TetherCommand.allowedWhileUnpaired(.fileChunk, inbound: true))
+ #expect(!TetherCommand.allowedWhileUnpaired(.fileEnd, inbound: true))
+ #expect(!TetherCommand.allowedWhileUnpaired(nil, inbound: true))
+ }
+
+ // We dialled, so the peer's user holds the verdict and we may hear it.
+ @Test func outboundPeerMaySendOnlyItsVerdict() {
+ #expect(TetherCommand.allowedWhileUnpaired(.pairAccepted, inbound: false))
+ #expect(TetherCommand.allowedWhileUnpaired(.pairPending, inbound: false))
+ #expect(TetherCommand.allowedWhileUnpaired(.error, inbound: false))
+
+ #expect(!TetherCommand.allowedWhileUnpaired(.pairRequest, inbound: false))
+ #expect(!TetherCommand.allowedWhileUnpaired(.clipboardUpdated, inbound: false))
+ #expect(!TetherCommand.allowedWhileUnpaired(.clipboardContent, inbound: false))
+ #expect(!TetherCommand.allowedWhileUnpaired(.fileStart, inbound: false))
+ #expect(!TetherCommand.allowedWhileUnpaired(nil, inbound: false))
+ }
+}