90 lines
4.7 KiB
Swift
90 lines
4.7 KiB
Swift
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
import Foundation
|
|
import Darwin
|
|
|
|
/// Explicit, isolated native companion fixtures. Shipping identities cannot opt
|
|
/// into this mode, and its peers still need normal same-team Apple signatures.
|
|
enum CompanionValidation {
|
|
static let vccPlusBundleID = "com.brianholman.vccplus.oss.mac.uitesting"
|
|
static let driveWireBundleID = "com.brianholman.vccplus.oss.drivewire.companionpreview"
|
|
private static let rootArgument = "--companion-validation-root"
|
|
private static let peerArgument = "--companion-validation-peer"
|
|
|
|
static var requested: Bool {
|
|
CommandLine.arguments.contains(rootArgument) || CommandLine.arguments.contains(peerArgument)
|
|
}
|
|
static var enabled: Bool { (try? context()) != nil }
|
|
static var root: URL? { (try? context())?.directory }
|
|
static var vccApplicationURL: URL? { (try? context())?.peer }
|
|
|
|
/// Invalid explicit fixture arguments never fall through to a real listener.
|
|
/// Both apps use this helper for every server and client endpoint selection.
|
|
static func configuration(forVCCPlus: Bool) throws -> CompanionTransport.Configuration {
|
|
let production: CompanionTransport.Configuration = forVCCPlus ? .vccPlus : .driveWire
|
|
guard requested else {
|
|
// An isolated preview accidentally launched without its arguments
|
|
// must never occupy the real application's service endpoint.
|
|
guard ![vccPlusBundleID, driveWireBundleID].contains(Bundle.main.bundleIdentifier ?? "") else {
|
|
throw CompanionTransport.Failure.unsafeEndpoint
|
|
}
|
|
return production
|
|
}
|
|
guard let fixture = try context() else { throw CompanionTransport.Failure.unsafeEndpoint }
|
|
var config = production
|
|
config.directory = fixture.directory
|
|
config.expectedServerBundleIDs = [forVCCPlus ? vccPlusBundleID : driveWireBundleID]
|
|
config.expectedClientBundleIDs = [forVCCPlus ? driveWireBundleID : vccPlusBundleID]
|
|
return config
|
|
}
|
|
|
|
private struct Context { let directory: URL; let peer: URL? }
|
|
private static func context() throws -> Context? {
|
|
guard requested else { return nil }
|
|
#if DEBUG
|
|
guard let identifier = Bundle.main.bundleIdentifier,
|
|
[vccPlusBundleID, driveWireBundleID].contains(identifier) else {
|
|
throw CompanionTransport.Failure.untrustedPeer
|
|
}
|
|
let arguments = CommandLine.arguments
|
|
func value(_ flag: String) throws -> String? {
|
|
let positions = arguments.indices.filter { arguments[$0] == flag }
|
|
guard positions.count <= 1 else { throw CompanionTransport.Failure.unsafeEndpoint }
|
|
guard let position = positions.first else { return nil }
|
|
guard arguments.indices.contains(position + 1), !arguments[position + 1].hasPrefix("--") else {
|
|
throw CompanionTransport.Failure.unsafeEndpoint
|
|
}
|
|
return arguments[position + 1]
|
|
}
|
|
guard let supplied = try value(rootArgument), supplied.hasPrefix("/") else {
|
|
throw CompanionTransport.Failure.unsafeEndpoint
|
|
}
|
|
let url = URL(fileURLWithPath: supplied, isDirectory: true).standardizedFileURL
|
|
let canonical = url.resolvingSymlinksInPath()
|
|
var info = stat()
|
|
// Foundation canonicalizes /private/tmp to /tmp on some macOS builds.
|
|
// Both aliases name the system temporary directory; the final component
|
|
// is checked without following a supplied root symlink.
|
|
var rawRoot = supplied
|
|
while rawRoot.count > 1 && rawRoot.hasSuffix("/") { rawRoot.removeLast() }
|
|
guard (canonical.path.hasPrefix("/private/tmp/") || canonical.path.hasPrefix("/tmp/")),
|
|
lstat(rawRoot, &info) == 0, info.st_uid == geteuid(),
|
|
info.st_mode & S_IFMT == S_IFDIR, info.st_mode & 0o777 == 0o700,
|
|
canonical.appendingPathComponent("drivewire.sock").path.utf8.count < MemoryLayout.size(ofValue: sockaddr_un().sun_path) else {
|
|
throw CompanionTransport.Failure.unsafeEndpoint
|
|
}
|
|
var peer: URL?
|
|
if let path = try value(peerArgument) {
|
|
guard path.hasPrefix("/") else { throw CompanionTransport.Failure.untrustedPeer }
|
|
let url = URL(fileURLWithPath: path).standardizedFileURL
|
|
guard Bundle(url: url)?.bundleIdentifier == vccPlusBundleID else { throw CompanionTransport.Failure.untrustedPeer }
|
|
// The launch path is additionally checked with validateApplication
|
|
// off the main actor before it is ever executed.
|
|
peer = url
|
|
}
|
|
return Context(directory: canonical, peer: peer)
|
|
#else
|
|
throw CompanionTransport.Failure.untrustedPeer
|
|
#endif
|
|
}
|
|
}
|