Run a virtual iPhone on an Apple Silicon Mac.
Swift
Read remote IPSWs from CDN edges that omit Accept-Ranges
Jacky189b438be2cb
diff --git a/VPhoneKit/VPhoneCoreKit/Firmware/VPhoneRemoteZip.swift b/VPhoneKit/VPhoneCoreKit/Firmware/VPhoneRemoteZip.swiftindex 19770e60..656b1fc8 100644--- a/VPhoneKit/VPhoneCoreKit/Firmware/VPhoneRemoteZip.swift+++ b/VPhoneKit/VPhoneCoreKit/Firmware/VPhoneRemoteZip.swift@@ -20,7 +20,7 @@ import Compressionimport Foundationpublic struct VPhoneRemoteZip: Sendable {- private static let session = URLSession(configuration: .ephemeral)+ private static let sharedSession = URLSession(configuration: .ephemeral)public struct Entry: Sendable {public let name: String@@ -60,16 +60,21 @@ public struct VPhoneRemoteZip: Sendable {public let url: URLpublic let entries: [Entry]+ let session: URLSession// MARK: - Openingpublic static func open(_ url: URL) async throws -> VPhoneRemoteZip {- let size = try await contentLength(of: url)+ try await open(url, session: sharedSession)+ }++ static func open(_ url: URL, session: URLSession) async throws -> VPhoneRemoteZip {+ let size = try await contentLength(of: url, session: session)// The EOCD is 22 bytes plus up to 64 KiB of comment, so the last 64 KiB// and change always contains it.let tailLength = min(size, 66000)- let tail = try await range(of: url, from: size - tailLength, count: tailLength)+ let tail = try await range(of: url, from: size - tailLength, count: tailLength, session: session)guard let eocd = lastIndex(of: [0x50, 0x4B, 0x05, 0x06], in: tail) else {throw Error.noEndOfCentralDirectory(url)@@ -87,7 +92,7 @@ public struct VPhoneRemoteZip: Sendable {throw Error.malformed("ZIP64 fields present but no ZIP64 locator")}let zip64Offset = u64(tail, locator + 8)- let record = try await range(of: url, from: zip64Offset, count: 56)+ let record = try await range(of: url, from: zip64Offset, count: 56, session: session)guard record.count >= 56, record.prefix(4) == Data([0x50, 0x4B, 0x06, 0x06]) else {throw Error.malformed("ZIP64 end-of-central-directory not where the locator says")}@@ -96,10 +101,11 @@ public struct VPhoneRemoteZip: Sendable {directoryOffset = u64(record, 48)}- let directory = try await range(of: url, from: directoryOffset, count: directorySize)+ let directory = try await range(of: url, from: directoryOffset, count: directorySize, session: session)return try VPhoneRemoteZip(url: url,entries: parseCentralDirectory(directory, expected: entryCount),+ session: session,)}@@ -129,7 +135,7 @@ public struct VPhoneRemoteZip: Sendable {// The local header repeats the name and carries its own extra field,// whose length usually differs from the central directory's — so the// data offset has to be computed from the local header, not assumed.- let header = try await Self.range(of: url, from: entry.localHeaderOffset, count: 30)+ let header = try await Self.range(of: url, from: entry.localHeaderOffset, count: 30, session: session)guard header.count == 30, header.prefix(4) == Data([0x50, 0x4B, 0x03, 0x04]) else {throw Error.malformed("no local file header at \(entry.localHeaderOffset)")}@@ -137,7 +143,7 @@ public struct VPhoneRemoteZip: Sendable {let extraLength = UInt64(Self.u16(header, 28))let dataOffset = entry.localHeaderOffset + 30 + nameLength + extraLength- let raw = try await Self.range(of: url, from: dataOffset, count: entry.compressedSize)+ let raw = try await Self.range(of: url, from: dataOffset, count: entry.compressedSize, session: session)switch entry.compressionMethod {case 0: return rawcase 8: return try Self.inflate(raw, expecting: Int(entry.uncompressedSize))@@ -168,19 +174,37 @@ public struct VPhoneRemoteZip: Sendable {// MARK: - HTTP- private static func contentLength(of url: URL) async throws -> UInt64 {+ /// The archive's size, once the server is known to answer range requests.+ ///+ /// `Accept-Ranges: bytes` on the HEAD answer settles it. Without the header+ /// the server is asked for one byte: some CDN edges in front of+ /// updates.cdn-apple.com leave the header out of every HEAD answer and still+ /// serve ranges, as every cloudOS URL did from China in October 2026.+ static func contentLength(of url: URL, session: URLSession) async throws -> UInt64 {var request = URLRequest(url: url)request.httpMethod = "HEAD"let (_, response) = try await session.data(for: request)guard let http = response as? HTTPURLResponse else { throw Error.notSeekable(url) }guard http.statusCode == 200 else { throw Error.http(http.statusCode, url) }- guard http.expectedContentLength > 0,- (http.value(forHTTPHeaderField: "Accept-Ranges") ?? "").contains("bytes")- else { throw Error.notSeekable(url) }+ guard http.expectedContentLength > 0 else { throw Error.notSeekable(url) }+ if !(http.value(forHTTPHeaderField: "Accept-Ranges") ?? "").contains("bytes") {+ try await probeRange(of: url, session: session)+ }return UInt64(http.expectedContentLength)}- private static func range(of url: URL, from offset: UInt64, count: UInt64) async throws -> Data {+ /// Ask for the first byte and accept only a 206. The body is never read: a+ /// server that ignores `Range` answers 200 and starts sending the whole+ /// archive, so the request is cancelled as soon as its headers arrive.+ private static func probeRange(of url: URL, session: URLSession) async throws {+ var request = URLRequest(url: url)+ request.setValue("bytes=0-0", forHTTPHeaderField: "Range")+ let (bytes, response) = try await session.bytes(for: request)+ bytes.task.cancel()+ guard (response as? HTTPURLResponse)?.statusCode == 206 else { throw Error.notSeekable(url) }+ }++ private static func range(of url: URL, from offset: UInt64, count: UInt64, session: URLSession) async throws -> Data {guard count > 0 else { return Data() }var request = URLRequest(url: url)request.setValue("bytes=\(offset)-\(offset + count - 1)", forHTTPHeaderField: "Range")diff --git a/VPhoneKit/VPhoneCoreKitTests/Firmware/RemoteZipTests.swift b/VPhoneKit/VPhoneCoreKitTests/Firmware/RemoteZipTests.swiftnew file mode 100644index 00000000..d7279aa9--- /dev/null+++ b/VPhoneKit/VPhoneCoreKitTests/Firmware/RemoteZipTests.swift@@ -0,0 +1,124 @@+import Foundation+import Testing+@testable import VPhoneCoreKit++/// Serves `payload` the way a CDN edge would, with a switch for each of the+/// two things that differ between them: whether HEAD names `Accept-Ranges`,+/// and whether a `Range` request gets a 206 or the whole body.+private final class RemoteZipStubProtocol: URLProtocol {+ nonisolated(unsafe) static var payload = Data()+ nonisolated(unsafe) static var advertisesRanges = true+ nonisolated(unsafe) static var servesRanges = true+ nonisolated(unsafe) static var requests: [String] = []++ override class func canInit(with _: URLRequest) -> Bool {+ true+ }++ override class func canonicalRequest(for request: URLRequest) -> URLRequest {+ request+ }++ override func startLoading() {+ let method = request.httpMethod ?? "GET"+ let range = request.value(forHTTPHeaderField: "Range")+ Self.requests.append([method, range].compactMap(\.self).joined(separator: " "))++ var status = 200+ var body = Self.payload+ var headers = ["Content-Length": String(Self.payload.count)]+ if Self.advertisesRanges {+ headers["Accept-Ranges"] = "bytes"+ }+ if method != "HEAD", Self.servesRanges, let range,+ case let bounds = range.dropFirst("bytes=".count).split(separator: "-").compactMap({ Int($0) }),+ bounds.count == 2+ {+ status = 206+ body = Self.payload.subdata(in: bounds[0] ..< min(bounds[1] + 1, Self.payload.count))+ headers["Content-Length"] = String(body.count)+ headers["Content-Range"] = "bytes \(bounds[0])-\(bounds[0] + body.count - 1)/\(Self.payload.count)"+ }+ let response = HTTPURLResponse(url: request.url!, statusCode: status, httpVersion: "HTTP/1.1", headerFields: headers)!+ client?.urlProtocol(self, didReceive: response, cacheStoragePolicy: .notAllowed)+ if method != "HEAD" {+ client?.urlProtocol(self, didLoad: body)+ }+ client?.urlProtocolDidFinishLoading(self)+ }++ override func stopLoading() {}+}++@Suite("Remote zip", .serialized)+struct RemoteZipTests {+ private let url = URL(string: "https://updates.cdn-apple.com/private-cloud-compute/example")!++ private func session(advertisesRanges: Bool, servesRanges: Bool, payload: Data) -> URLSession {+ RemoteZipStubProtocol.payload = payload+ RemoteZipStubProtocol.advertisesRanges = advertisesRanges+ RemoteZipStubProtocol.servesRanges = servesRanges+ RemoteZipStubProtocol.requests = []+ let configuration = URLSessionConfiguration.ephemeral+ configuration.protocolClasses = [RemoteZipStubProtocol.self]+ return URLSession(configuration: configuration)+ }++ /// A one-member zip with the member stored, as an IPSW stores its+ /// BuildManifest. The reader checks no CRC, so the CRC fields stay zero.+ private func zip(name: String, contents: Data) -> Data {+ func le16(_ value: Int) -> [UInt8] {+ [UInt8(value & 0xFF), UInt8(value >> 8 & 0xFF)]+ }+ func le32(_ value: Int) -> [UInt8] {+ le16(value & 0xFFFF) + le16(value >> 16 & 0xFFFF)+ }+ let nameBytes = Array(name.utf8)+ var data: [UInt8] = [0x50, 0x4B, 0x03, 0x04] + le16(20) + le16(0) + le16(0) + le16(0) + le16(0)+ + le32(0) + le32(contents.count) + le32(contents.count) + le16(nameBytes.count) + le16(0)+ data += nameBytes + Array(contents)+ let directoryOffset = data.count+ data += [0x50, 0x4B, 0x01, 0x02] + le16(20) + le16(20) + le16(0) + le16(0) + le16(0) + le16(0)+ + le32(0) + le32(contents.count) + le32(contents.count) + le16(nameBytes.count) + le16(0) + le16(0)+ + le16(0) + le16(0) + le32(0) + le32(0)+ data += nameBytes+ let directorySize = data.count - directoryOffset+ data += [0x50, 0x4B, 0x05, 0x06] + le16(0) + le16(0) + le16(1) + le16(1)+ + le32(directorySize) + le32(directoryOffset) + le16(0)+ return Data(data)+ }++ @Test func `a server that names Accept-Ranges is not probed`() async throws {+ let session = session(advertisesRanges: true, servesRanges: true, payload: Data(count: 100))+ #expect(try await VPhoneRemoteZip.contentLength(of: url, session: session) == 100)+ #expect(RemoteZipStubProtocol.requests == ["HEAD"])+ }++ @Test func `a server that leaves out Accept-Ranges but answers 206 is seekable`() async throws {+ let session = session(advertisesRanges: false, servesRanges: true, payload: Data(count: 100))+ #expect(try await VPhoneRemoteZip.contentLength(of: url, session: session) == 100)+ #expect(RemoteZipStubProtocol.requests == ["HEAD", "GET bytes=0-0"])+ }++ @Test func `a server that ignores Range is refused`() async throws {+ let session = session(advertisesRanges: false, servesRanges: false, payload: Data(count: 100))+ await #expect {+ _ = try await VPhoneRemoteZip.contentLength(of: url, session: session)+ } throws: { error in+ if case .notSeekable? = error as? VPhoneRemoteZip.Error {+ true+ } else {+ false+ }+ }+ }++ @Test func `a member is read from a CDN that leaves out Accept-Ranges`() async throws {+ let manifest = Data("<plist><dict/></plist>".utf8)+ let session = session(advertisesRanges: false, servesRanges: true, payload: zip(name: "BuildManifest.plist", contents: manifest))+ let archive = try await VPhoneRemoteZip.open(url, session: session)+ #expect(try await archive.read(archive.entry(endingWith: "BuildManifest.plist")) == manifest)+ #expect(RemoteZipStubProtocol.requests.first == "HEAD")+ #expect(RemoteZipStubProtocol.requests.dropFirst().allSatisfy { $0.hasPrefix("GET bytes=") })+ }+}