// SPDX-License-Identifier: GPL-3.0-or-later import Foundation import CryptoKit import Darwin /// Downloads source only after an explicit action. It never opens a workspace, /// executes Make, starts a compiler, or touches the emulator's media/state. actor SoftwareLibrarySourceImporter { static let maximumPackageBytes: Int64 = 64 * 1024 * 1024 static let maximumExpandedBytes: UInt64 = 256 * 1024 * 1024 static let maximumFileBytes: Int64 = 32 * 1024 * 1024 static let maximumEntries: UInt = 8192 struct Progress: Sendable { enum Stage: String, Sendable { case downloading, verifying, extracting, checking, complete } let stage: Stage let receivedBytes: Int64 let totalBytes: Int64 var fraction: Double? { stage == .downloading && totalBytes > 0 ? min(1, Double(receivedBytes) / Double(totalBytes)) : nil } } struct Result: Sendable { let projectDirectory: URL let importedDirectory: URL let originalPackage: URL let receiptURL: URL } enum Failure: LocalizedError { case metadata, response, size, fingerprint, unsafeLocation, existingDestination, invalidProject, busy, insufficientSpace var errorDescription: String? { switch self { case .metadata: "This source package has invalid download or project details." case .response: "The source server did not return the expected download." case .size: "The source download or expanded files exceed their expected size." case .fingerprint: "The source download does not match its saved fingerprint." case .unsafeLocation: "The source location contains an unexpected file or symbolic link. Existing files were preserved." case .existingDestination: "That project folder already exists. Choose a new folder; existing projects are never replaced." case .invalidProject: "The downloaded source does not contain its declared project, entry source and license files." case .busy: "This project folder is already being prepared." case .insufficientSpace: "There is not enough free space to prepare this source project." } } } private let cacheDirectory: URL private let configuration: URLSessionConfiguration private var activeDestinations = Set() private let manager = FileManager.default init(cacheDirectory: URL, configuration: URLSessionConfiguration? = nil) { self.cacheDirectory = cacheDirectory.standardizedFileURL let config = configuration?.copy() as? URLSessionConfiguration ?? .ephemeral config.urlCache = nil config.requestCachePolicy = .reloadIgnoringLocalCacheData config.timeoutIntervalForRequest = 60 config.timeoutIntervalForResource = 600 self.configuration = config } static func acceptsDownloadURL(_ url: URL) -> Bool { url.scheme?.lowercased() == "https" && url.host?.lowercased() == "vccplus.brianholman.com" && url.user == nil && url.password == nil && (url.port == nil || url.port == 443) && url.query == nil && url.fragment == nil && url.path.hasPrefix("/dl/software/CoCoLibrary/Sources/Exclusives/") && url.pathExtension.lowercased() == "zip" && !url.path.contains("\\") && !url.pathComponents.contains(where: { $0 == "." || $0 == ".." }) && !url.path.unicodeScalars.contains(where: CharacterSet.controlCharacters.contains) } private static func validRelative(_ path: String) -> Bool { let components = path.split(separator: "/", omittingEmptySubsequences: false) return !path.isEmpty && path.utf8.count <= 1024 && components.count <= 32 && !path.contains("\\") && !path.contains(":") && !path.unicodeScalars.contains(where: CharacterSet.controlCharacters.contains) && !components.contains(where: { $0.isEmpty || $0 == "." || $0 == ".." }) } private static func validate(_ source: SoftwareLibraryOwnedGame.Source) throws { guard acceptsDownloadURL(source.package.url), source.package.byteSize > 0, source.package.byteSize <= maximumPackageBytes, source.package.sha256.count == 64, source.package.sha256.allSatisfy({ "0123456789abcdef".contains($0) }), validRelative(source.projectDirectory), validRelative(source.entrySource), source.entrySource.lowercased().hasSuffix(".c"), source.configurationFile == source.projectDirectory + "/" + CMOCProject.configurationFilename, !source.licenseFiles.isEmpty, source.licenseFiles.count <= 32, source.licenseFiles.allSatisfy(validRelative), Set(source.licenseFiles).count == source.licenseFiles.count else { throw Failure.metadata } } func importProject(_ source: SoftwareLibraryOwnedGame.Source, to destination: URL, progress: @escaping @Sendable (Progress) -> Void = { _ in }) async throws -> Result { try Self.validate(source) try Task.checkCancellation() guard destination.isFileURL, destination.query == nil, destination.fragment == nil, destination.host == nil || destination.host == "" || destination.host == "localhost", Self.validRelative(destination.lastPathComponent), destination.lastPathComponent.utf8.count <= 255 else { throw Failure.unsafeLocation } // Resolve the chosen parent once. The new leaf itself must never be a link // or an existing file, even if the user's chosen parent has a Finder alias. let parent = try CMOCProject.physicalURL(destination.deletingLastPathComponent()) try checkDirectory(parent) let target = parent.appendingPathComponent(destination.lastPathComponent, isDirectory: true) guard !activeDestinations.contains(target.path) else { throw Failure.busy } activeDestinations.insert(target.path) defer { activeDestinations.remove(target.path) } try requireAbsent(target) let parentFD = open(parent.path, O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC) guard parentFD >= 0 else { throw Failure.unsafeLocation } defer { close(parentFD) } var parentIdentity = stat() guard fstat(parentFD, &parentIdentity) == 0 else { throw Failure.unsafeLocation } if let available = (try? manager.attributesOfFileSystem(forPath: parent.path)[.systemFreeSize]) as? NSNumber, available.uint64Value < Self.maximumExpandedBytes + UInt64(source.package.byteSize) { throw Failure.insufficientSpace } let cache = try prepareCache() let retention = cache.appendingPathComponent(UUID().uuidString, isDirectory: true) try manager.createDirectory(at: retention, withIntermediateDirectories: false, attributes: [.posixPermissions: 0o700]) let partial = retention.appendingPathComponent("Download.partial") var verifiedOriginal = false defer { if verifiedOriginal { try? manager.removeItem(at: partial) } else { try? manager.removeItem(at: retention) } } let original = retention.appendingPathComponent("Original.zip") progress(.init(stage: .downloading, receivedBytes: 0, totalBytes: source.package.byteSize)) do { try await download(source.package, to: partial, progress: progress) } catch { if Task.isCancelled { throw CancellationError() } throw error } progress(.init(stage: .verifying, receivedBytes: source.package.byteSize, totalBytes: source.package.byteSize)) try verify(partial, descriptor: source.package) try Task.checkCancellation() try manager.moveItem(at: partial, to: original) guard chmod(original.path, 0o444) == 0 else { throw Failure.unsafeLocation } verifiedOriginal = true // Extraction and promotion share the destination filesystem. Only this // unique staging tree is removed on cancellation or validation failure. let stageName = ".vcc-source-" + UUID().uuidString guard mkdirat(parentFD, stageName, 0o700) == 0 else { throw Failure.unsafeLocation } let stage = parent.appendingPathComponent(stageName, isDirectory: true) defer { try? manager.removeItem(at: stage) } let tree = stage.appendingPathComponent("Project", isDirectory: true) progress(.init(stage: .extracting, receivedBytes: 0, totalBytes: 0)) let files: [String] do { files = try VCCLibraryArchive.extractZIP(at: original, toNewDirectory: tree, maximumExpandedBytes: Self.maximumExpandedBytes, maximumEntries: Self.maximumEntries, shouldCancel: { Task.isCancelled }) } catch { if Task.isCancelled { throw CancellationError() } throw error } try Task.checkCancellation() progress(.init(stage: .checking, receivedBytes: 0, totalBytes: 0)) try Self.validateExtractedProject(source, root: tree, files: files) try Task.checkCancellation() let receiptURL = retention.appendingPathComponent("Receipt.json") let receipt = Receipt(version: 1, packageURL: source.package.url, packageSHA256: source.package.sha256, packageByteSize: source.package.byteSize, projectDirectory: source.projectDirectory, entrySource: source.entrySource, licenseFiles: source.licenseFiles, files: files, verifiedAt: Date()) try JSONEncoder().encode(receipt).write(to: receiptURL, options: .withoutOverwriting) var currentParent = stat() guard lstat(parent.path, ¤tParent) == 0, currentParent.st_dev == parentIdentity.st_dev, currentParent.st_ino == parentIdentity.st_ino, currentParent.st_mode & S_IFMT == S_IFDIR else { throw Failure.unsafeLocation } // RENAME_EXCL closes the race between checking the destination and // promotion. Never merge, remove or overwrite a late-created project. guard renameatx_np(AT_FDCWD, tree.path, parentFD, target.lastPathComponent, UInt32(RENAME_EXCL)) == 0 else { if errno == EEXIST || errno == ENOTEMPTY { throw Failure.existingDestination } throw Failure.unsafeLocation } progress(.init(stage: .complete, receivedBytes: source.package.byteSize, totalBytes: source.package.byteSize)) return Result(projectDirectory: target.appendingPathComponent(source.projectDirectory, isDirectory: true), importedDirectory: target, originalPackage: original, receiptURL: receiptURL) } private func download(_ descriptor: SoftwareLibraryOwnedGame.HostedFile, to path: URL, progress: @escaping @Sendable (Progress) -> Void) async throws { let delegate = SourceImportRedirects() let session = URLSession(configuration: configuration, delegate: delegate, delegateQueue: nil) defer { session.invalidateAndCancel() } let request = URLRequest(url: descriptor.url) let (bytes, response) = try await session.bytes(for: request) guard let response = response as? HTTPURLResponse, response.statusCode == 200, let finalURL = response.url, Self.acceptsDownloadURL(finalURL) else { throw Failure.response } if response.expectedContentLength >= 0 && response.expectedContentLength != descriptor.byteSize { throw Failure.size } let fd = open(path.path, O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC, 0o600) guard fd >= 0 else { throw Failure.unsafeLocation } let output = FileHandle(fileDescriptor: fd, closeOnDealloc: true) defer { try? output.close() } var received: Int64 = 0 var chunk = Data(); chunk.reserveCapacity(64 * 1024) for try await byte in bytes { try Task.checkCancellation() received += 1 guard received <= descriptor.byteSize else { throw Failure.size } chunk.append(byte) if chunk.count == 64 * 1024 { try output.write(contentsOf: chunk); chunk.removeAll(keepingCapacity: true) progress(.init(stage: .downloading, receivedBytes: received, totalBytes: descriptor.byteSize)) } } try Task.checkCancellation() guard received == descriptor.byteSize else { throw Failure.size } if !chunk.isEmpty { try output.write(contentsOf: chunk) } try output.synchronize() progress(.init(stage: .downloading, receivedBytes: received, totalBytes: descriptor.byteSize)) } private func verify(_ path: URL, descriptor: SoftwareLibraryOwnedGame.HostedFile) throws { guard try Self.regularSize(path) == descriptor.byteSize else { throw Failure.size } let input = try FileHandle(forReadingFrom: path); defer { try? input.close() } var hash = SHA256() while let data = try input.read(upToCount: 1024 * 1024), !data.isEmpty { try Task.checkCancellation(); hash.update(data: data) } guard hash.finalize().map({ String(format: "%02x", $0) }).joined() == descriptor.sha256 else { throw Failure.fingerprint } } /// Reusable authored-test seam: reads regular extracted files and parses the /// declared project, without executing its Makefile or opening an editor. static func validateExtractedProject(_ source: SoftwareLibraryOwnedGame.Source, root: URL, files: [String]) throws { try validate(source) guard !files.isEmpty, files.count <= maximumEntries, Set(files).count == files.count, files.allSatisfy(validRelative) else { throw Failure.invalidProject } var total: UInt64 = 0 for path in files { try Task.checkCancellation() let components = path.split(separator: "/").map { $0.lowercased() } guard !components.contains(where: { [".git", ".hg", ".svn", ".ds_store", "__macosx"].contains($0) }) else { throw Failure.invalidProject } let size = try regularSize(root.appendingPathComponent(path)) guard size <= maximumFileBytes else { throw Failure.size } total += UInt64(size) guard total <= maximumExpandedBytes else { throw Failure.size } } let known = Set(files) guard known.contains(source.configurationFile), known.contains(source.projectDirectory + "/" + source.entrySource), source.licenseFiles.allSatisfy(known.contains) else { throw Failure.invalidProject } let projectURL = root.appendingPathComponent(source.projectDirectory, isDirectory: true) let project = try CMOCProject.open(at: projectURL) guard project.configuration.entrySource == source.entrySource else { throw Failure.invalidProject } if let configuration = source.buildConfiguration { guard project.configuration.configurations.contains(where: { $0.name == configuration }) else { throw Failure.invalidProject } } let canonicalRoot = try CMOCProject.physicalURL(root).path for declared in project.configuration.buildSupportDirectories { let support = try CMOCProject.physicalURL(projectURL.appendingPathComponent(declared, isDirectory: true)) guard support.path.hasPrefix(canonicalRoot + "/") else { throw Failure.invalidProject } var info = stat() guard lstat(support.path, &info) == 0, info.st_mode & S_IFMT == S_IFDIR else { throw Failure.invalidProject } } } private func prepareCache() throws -> URL { guard cacheDirectory.isFileURL else { throw Failure.unsafeLocation } var info = stat() if lstat(cacheDirectory.path, &info) == 0 { guard info.st_mode & S_IFMT == S_IFDIR, info.st_uid == geteuid() else { throw Failure.unsafeLocation } } else { guard errno == ENOENT else { throw Failure.unsafeLocation } try manager.createDirectory(at: cacheDirectory, withIntermediateDirectories: true, attributes: [.posixPermissions: 0o700]) } let physical = try CMOCProject.physicalURL(cacheDirectory) try checkDirectory(physical) return physical } private func checkDirectory(_ url: URL) throws { var info = stat() guard lstat(url.path, &info) == 0, info.st_mode & S_IFMT == S_IFDIR, info.st_uid == geteuid() else { throw Failure.unsafeLocation } } private func requireAbsent(_ url: URL) throws { var info = stat() if lstat(url.path, &info) == 0 { throw Failure.existingDestination } guard errno == ENOENT else { throw Failure.unsafeLocation } } private static func regularSize(_ url: URL) throws -> Int64 { var info = stat() guard lstat(url.path, &info) == 0, info.st_mode & S_IFMT == S_IFREG, info.st_size >= 0 else { throw Failure.unsafeLocation } return info.st_size } private struct Receipt: Codable { let version: Int let packageURL: URL let packageSHA256: String let packageByteSize: Int64 let projectDirectory: String let entrySource: String let licenseFiles: [String] let files: [String] let verifiedAt: Date } } private final class SourceImportRedirects: NSObject, URLSessionTaskDelegate { func urlSession(_ session: URLSession, task: URLSessionTask, willPerformHTTPRedirection response: HTTPURLResponse, newRequest request: URLRequest, completionHandler: @escaping (URLRequest?) -> Void) { completionHandler(request.url.map(SoftwareLibrarySourceImporter.acceptsDownloadURL) == true ? request : nil) } }