native-update 3.4.2 → 4.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AI-INTEGRATION-GUIDE.md +26 -16
- package/NativeUpdate.podspec +2 -2
- package/README.md +24 -20
- package/android/build.gradle +37 -33
- package/android/gradle/wrapper/gradle-wrapper.properties +2 -2
- package/android/proguard-rules.pro +0 -3
- package/android/src/main/java/com/aoneahsan/nativeupdate/BackgroundNotificationManager.kt +45 -49
- package/android/src/main/java/com/aoneahsan/nativeupdate/BackgroundUpdateManager.kt +12 -10
- package/android/src/main/java/com/aoneahsan/nativeupdate/BackgroundUpdatePlugin.kt +12 -21
- package/android/src/main/java/com/aoneahsan/nativeupdate/BackgroundUpdateWorker.kt +3 -34
- package/android/src/main/java/com/aoneahsan/nativeupdate/LiveUpdatePlugin.kt +206 -72
- package/android/src/main/java/com/aoneahsan/nativeupdate/NativeUpdatePlugin.kt +74 -37
- package/android/src/main/java/com/aoneahsan/nativeupdate/SecurityManager.kt +169 -179
- package/android/variables.gradle +2 -2
- package/cli/commands/bundle-verify.js +4 -1
- package/cli/commands/keys-generate.js +12 -46
- package/cli/index.js +4 -7
- package/cli/package.json +2 -1
- package/dist/esm/core/config.d.ts +3 -0
- package/dist/esm/core/config.js +17 -0
- package/dist/esm/core/plugin-manager.js +4 -2
- package/dist/esm/core/security.js +2 -5
- package/dist/esm/core/update-check.d.ts +2 -0
- package/dist/esm/core/update-check.js +5 -3
- package/dist/esm/definitions.d.ts +43 -2
- package/dist/esm/live-update/delta-processor.d.ts +6 -20
- package/dist/esm/live-update/delta-processor.js +75 -119
- package/dist/esm/plugin.d.ts +0 -3
- package/dist/esm/plugin.js +172 -12
- package/dist/esm/security/crypto.js +2 -5
- package/dist/esm/web.d.ts +1 -9
- package/dist/esm/web.js +4 -56
- package/dist/plugin.cjs +1 -1
- package/dist/plugin.esm.js +1 -1
- package/dist/plugin.js +2 -2
- package/docs/CHANGELOG.md +49 -0
- package/docs/KNOWN_LIMITATIONS.md +4 -3
- package/docs/MIGRATION.md +32 -3
- package/docs/server-requirements.md +40 -29
- package/ios/Plugin/BackgroundUpdate/BackgroundNotificationManager.swift +6 -2
- package/ios/Plugin/BackgroundUpdate/BackgroundUpdatePlugin.swift +21 -30
- package/ios/Plugin/LiveUpdate/LiveUpdatePlugin.swift +269 -61
- package/ios/Plugin/NativeUpdatePlugin.m +8 -1
- package/ios/Plugin/NativeUpdatePlugin.swift +71 -34
- package/ios/Plugin/Security/SecurityManager.swift +100 -99
- package/package.json +25 -13
|
@@ -4,13 +4,14 @@ import CommonCrypto
|
|
|
4
4
|
import UIKit
|
|
5
5
|
import ZIPFoundation
|
|
6
6
|
|
|
7
|
-
class LiveUpdatePlugin {
|
|
7
|
+
class LiveUpdatePlugin: @unchecked Sendable {
|
|
8
8
|
private weak var plugin: CAPPlugin?
|
|
9
9
|
private var config: [String: Any]?
|
|
10
10
|
private var progressListener: (([String: Any]) -> Void)?
|
|
11
11
|
private var stateChangeListener: (([String: Any]) -> Void)?
|
|
12
12
|
private var session: URLSession!
|
|
13
13
|
private var downloadTask: URLSessionDownloadTask?
|
|
14
|
+
private var activeDownloadBundleId: String?
|
|
14
15
|
private let securityManager = SecurityManager()
|
|
15
16
|
|
|
16
17
|
// UserDefaults keys for the A2 (boot-time re-verify) and A3
|
|
@@ -57,13 +58,22 @@ class LiveUpdatePlugin {
|
|
|
57
58
|
|
|
58
59
|
// Validate configuration
|
|
59
60
|
if let serverUrl = config["serverUrl"] as? String,
|
|
60
|
-
!
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
61
|
+
!securityManager.validateUrl(serverUrl) {
|
|
62
|
+
throw NSError(domain: "LiveUpdatePlugin", code: 1, userInfo: [
|
|
63
|
+
NSLocalizedDescriptionKey: "Server URL must be an allowed HTTPS URL"
|
|
64
|
+
])
|
|
65
|
+
}
|
|
66
|
+
let requireSignature = config["requireSignature"] as? Bool ?? true
|
|
67
|
+
let publicKey = config["publicKey"] as? String
|
|
68
|
+
if requireSignature && (publicKey?.isEmpty != false) {
|
|
69
|
+
throw NSError(domain: "LiveUpdatePlugin", code: 2, userInfo: [
|
|
70
|
+
NSLocalizedDescriptionKey: "publicKey is required when requireSignature is enabled"
|
|
71
|
+
])
|
|
72
|
+
}
|
|
73
|
+
if !requireSignature && !unsignedDevelopmentUpdatesAllowed() {
|
|
74
|
+
throw NSError(domain: "LiveUpdatePlugin", code: 3, userInfo: [
|
|
75
|
+
NSLocalizedDescriptionKey: "Unsigned updates are allowed only in debug builds with allowUnsignedDevelopmentUpdates enabled"
|
|
76
|
+
])
|
|
67
77
|
}
|
|
68
78
|
}
|
|
69
79
|
|
|
@@ -128,12 +138,9 @@ class LiveUpdatePlugin {
|
|
|
128
138
|
}
|
|
129
139
|
|
|
130
140
|
// Validate URL
|
|
131
|
-
if !
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
call.reject("INSECURE_URL", "Download URL must use HTTPS")
|
|
135
|
-
return
|
|
136
|
-
}
|
|
141
|
+
if !securityManager.validateUrl(url) {
|
|
142
|
+
call.reject("INSECURE_URL", "Download URL must be an allowed HTTPS URL")
|
|
143
|
+
return
|
|
137
144
|
}
|
|
138
145
|
|
|
139
146
|
Task {
|
|
@@ -152,22 +159,38 @@ class LiveUpdatePlugin {
|
|
|
152
159
|
do {
|
|
153
160
|
try FileManager.default.createDirectory(at: downloadDir, withIntermediateDirectories: true)
|
|
154
161
|
|
|
155
|
-
let downloadedFile
|
|
162
|
+
let downloadedFile: URL
|
|
163
|
+
if let deltaFile = try await applyDeltaIfAvailable(call, to: downloadDir, bundleId: bundleId) {
|
|
164
|
+
downloadedFile = deltaFile
|
|
165
|
+
} else {
|
|
166
|
+
downloadedFile = try await downloadBundle(from: url, to: downloadDir, bundleId: bundleId)
|
|
167
|
+
}
|
|
156
168
|
|
|
157
169
|
let calculatedChecksum = try calculateChecksum(for: downloadedFile)
|
|
158
|
-
if
|
|
170
|
+
if checksum.range(of: "^[a-fA-F0-9]{64}$", options: .regularExpression) == nil ||
|
|
171
|
+
calculatedChecksum.caseInsensitiveCompare(checksum) != .orderedSame {
|
|
159
172
|
call.reject("CHECKSUM_ERROR", "Bundle checksum validation failed")
|
|
160
173
|
return
|
|
161
174
|
}
|
|
162
175
|
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
176
|
+
let signature = call.getString("signature")
|
|
177
|
+
let publicKey = config?["publicKey"] as? String
|
|
178
|
+
let requireSignature = config?["requireSignature"] as? Bool ?? true
|
|
179
|
+
let signatureAlgorithm = call.getString("signatureAlgorithm") ?? "RSA-SHA256"
|
|
180
|
+
if signatureAlgorithm != "RSA-SHA256" {
|
|
181
|
+
call.reject("SIGNATURE_ERROR", "Unsupported signature algorithm")
|
|
182
|
+
return
|
|
183
|
+
}
|
|
184
|
+
if requireSignature && (signature?.isEmpty != false) {
|
|
185
|
+
call.reject("SIGNATURE_ERROR", "Bundle signature is required")
|
|
186
|
+
return
|
|
187
|
+
}
|
|
188
|
+
if let signature = signature, !signature.isEmpty {
|
|
189
|
+
guard let publicKey = publicKey, !publicKey.isEmpty else {
|
|
190
|
+
call.reject("SIGNATURE_ERROR", "Public key is not configured")
|
|
191
|
+
return
|
|
192
|
+
}
|
|
193
|
+
if !securityManager.verifySignature(fileURL: downloadedFile, signature: signature, publicKeyString: publicKey) {
|
|
171
194
|
call.reject("SIGNATURE_ERROR", "Bundle signature validation failed")
|
|
172
195
|
return
|
|
173
196
|
}
|
|
@@ -197,6 +220,7 @@ class LiveUpdatePlugin {
|
|
|
197
220
|
]
|
|
198
221
|
if let signature = call.getString("signature") {
|
|
199
222
|
bundleInfo["signature"] = signature
|
|
223
|
+
bundleInfo["signatureAlgorithm"] = "RSA-SHA256"
|
|
200
224
|
}
|
|
201
225
|
saveBundleInfo(bundleInfo)
|
|
202
226
|
|
|
@@ -309,6 +333,9 @@ class LiveUpdatePlugin {
|
|
|
309
333
|
"url": latestVersion["downloadUrl"] ?? "",
|
|
310
334
|
"notes": latestVersion["releaseNotes"] ?? "",
|
|
311
335
|
"checksum": latestVersion["checksum"] ?? "",
|
|
336
|
+
"signature": latestVersion["signature"] ?? NSNull(),
|
|
337
|
+
"signatureAlgorithm": latestVersion["signatureAlgorithm"] ?? "RSA-SHA256",
|
|
338
|
+
"delta": latestVersion["delta"] ?? NSNull(),
|
|
312
339
|
"size": latestVersion["size"] as? Int ?? 0,
|
|
313
340
|
"mandatory": latestVersion["mandatory"] as? Bool ?? false
|
|
314
341
|
])
|
|
@@ -332,20 +359,12 @@ class LiveUpdatePlugin {
|
|
|
332
359
|
}
|
|
333
360
|
|
|
334
361
|
func setUpdateUrl(_ call: CAPPluginCall) {
|
|
335
|
-
guard
|
|
362
|
+
guard call.getString("url") != nil else {
|
|
336
363
|
call.reject("URL is required")
|
|
337
364
|
return
|
|
338
365
|
}
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
let enforceHttps = (config?["security"] as? [String: Any])?["enforceHttps"] as? Bool ?? true
|
|
342
|
-
if enforceHttps {
|
|
343
|
-
call.reject("INSECURE_URL", "Update URL must use HTTPS")
|
|
344
|
-
return
|
|
345
|
-
}
|
|
346
|
-
}
|
|
347
|
-
|
|
348
|
-
config?["serverUrl"] = url
|
|
366
|
+
|
|
367
|
+
CAPLog.print("[NativeUpdate] setUpdateUrl() is deprecated and ignored; the update URL is locked after initialize()")
|
|
349
368
|
call.resolve()
|
|
350
369
|
}
|
|
351
370
|
|
|
@@ -375,6 +394,38 @@ class LiveUpdatePlugin {
|
|
|
375
394
|
call.reject("VALIDATION_ERROR", error.localizedDescription)
|
|
376
395
|
}
|
|
377
396
|
}
|
|
397
|
+
|
|
398
|
+
func cancelDownload(_ call: CAPPluginCall) {
|
|
399
|
+
guard let bundleId = call.getString("bundleId") else {
|
|
400
|
+
call.reject("Bundle ID is required")
|
|
401
|
+
return
|
|
402
|
+
}
|
|
403
|
+
if activeDownloadBundleId == bundleId {
|
|
404
|
+
downloadTask?.cancel()
|
|
405
|
+
downloadTask = nil
|
|
406
|
+
activeDownloadBundleId = nil
|
|
407
|
+
}
|
|
408
|
+
call.resolve()
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
func cancelAllDownloads(_ call: CAPPluginCall) {
|
|
412
|
+
downloadTask?.cancel()
|
|
413
|
+
downloadTask = nil
|
|
414
|
+
activeDownloadBundleId = nil
|
|
415
|
+
call.resolve()
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
func isDownloading(_ call: CAPPluginCall) {
|
|
419
|
+
guard let bundleId = call.getString("bundleId") else {
|
|
420
|
+
call.reject("Bundle ID is required")
|
|
421
|
+
return
|
|
422
|
+
}
|
|
423
|
+
call.resolve(["isDownloading": activeDownloadBundleId == bundleId && downloadTask != nil])
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
func getActiveDownloadCount(_ call: CAPPluginCall) {
|
|
427
|
+
call.resolve(["count": downloadTask == nil ? 0 : 1])
|
|
428
|
+
}
|
|
378
429
|
|
|
379
430
|
// MARK: - Async Methods for Background Updates
|
|
380
431
|
|
|
@@ -490,17 +541,26 @@ class LiveUpdatePlugin {
|
|
|
490
541
|
return deviceId
|
|
491
542
|
}
|
|
492
543
|
|
|
493
|
-
private func downloadBundle(
|
|
544
|
+
private func downloadBundle(
|
|
545
|
+
from urlString: String,
|
|
546
|
+
to directory: URL,
|
|
547
|
+
bundleId: String,
|
|
548
|
+
filename: String = "bundle.zip"
|
|
549
|
+
) async throws -> URL {
|
|
494
550
|
guard let url = URL(string: urlString) else {
|
|
495
551
|
throw NSError(domain: "LiveUpdatePlugin", code: 3, userInfo: [
|
|
496
552
|
NSLocalizedDescriptionKey: "Invalid download URL"
|
|
497
553
|
])
|
|
498
554
|
}
|
|
499
555
|
|
|
500
|
-
let destinationURL = directory.appendingPathComponent(
|
|
556
|
+
let destinationURL = directory.appendingPathComponent(filename)
|
|
501
557
|
|
|
502
558
|
return try await withCheckedThrowingContinuation { continuation in
|
|
503
559
|
let task = session.downloadTask(with: url) { [weak self] tempURL, response, error in
|
|
560
|
+
defer {
|
|
561
|
+
self?.downloadTask = nil
|
|
562
|
+
self?.activeDownloadBundleId = nil
|
|
563
|
+
}
|
|
504
564
|
if let error = error {
|
|
505
565
|
continuation.resume(throwing: error)
|
|
506
566
|
return
|
|
@@ -514,7 +574,21 @@ class LiveUpdatePlugin {
|
|
|
514
574
|
}
|
|
515
575
|
|
|
516
576
|
do {
|
|
577
|
+
let maxBytes = (self?.config?["maxBundleSize"] as? NSNumber)?.int64Value ?? 100 * 1024 * 1024
|
|
578
|
+
let responseSize = response?.expectedContentLength ?? NSURLSessionTransferSizeUnknown
|
|
579
|
+
if responseSize > maxBytes {
|
|
580
|
+
throw NSError(domain: "LiveUpdatePlugin", code: 12, userInfo: [
|
|
581
|
+
NSLocalizedDescriptionKey: "Bundle exceeds maximum download size"
|
|
582
|
+
])
|
|
583
|
+
}
|
|
517
584
|
try FileManager.default.moveItem(at: tempURL, to: destinationURL)
|
|
585
|
+
let actualSize = (try FileManager.default.attributesOfItem(atPath: destinationURL.path)[.size] as? NSNumber)?.int64Value ?? 0
|
|
586
|
+
if actualSize > maxBytes {
|
|
587
|
+
try? FileManager.default.removeItem(at: destinationURL)
|
|
588
|
+
throw NSError(domain: "LiveUpdatePlugin", code: 13, userInfo: [
|
|
589
|
+
NSLocalizedDescriptionKey: "Bundle exceeds maximum download size"
|
|
590
|
+
])
|
|
591
|
+
}
|
|
518
592
|
continuation.resume(returning: destinationURL)
|
|
519
593
|
} catch {
|
|
520
594
|
continuation.resume(throwing: error)
|
|
@@ -522,7 +596,7 @@ class LiveUpdatePlugin {
|
|
|
522
596
|
}
|
|
523
597
|
|
|
524
598
|
// Track progress
|
|
525
|
-
|
|
599
|
+
_ = task.progress.observe(\.fractionCompleted) { [weak self] progress, _ in
|
|
526
600
|
let percent = Int(progress.fractionCompleted * 100)
|
|
527
601
|
self?.progressListener?([
|
|
528
602
|
"percent": percent,
|
|
@@ -534,17 +608,103 @@ class LiveUpdatePlugin {
|
|
|
534
608
|
|
|
535
609
|
task.resume()
|
|
536
610
|
self.downloadTask = task
|
|
611
|
+
self.activeDownloadBundleId = bundleId
|
|
612
|
+
}
|
|
613
|
+
}
|
|
614
|
+
|
|
615
|
+
private func applyDeltaIfAvailable(
|
|
616
|
+
_ call: CAPPluginCall,
|
|
617
|
+
to directory: URL,
|
|
618
|
+
bundleId: String
|
|
619
|
+
) async throws -> URL? {
|
|
620
|
+
guard (config?["enableDeltaUpdates"] as? Bool) != false else { return nil }
|
|
621
|
+
guard let delta = call.getObject("delta"),
|
|
622
|
+
delta["format"] as? String == "NUDELTA/1",
|
|
623
|
+
delta["fromVersion"] as? String == getCurrentVersion(),
|
|
624
|
+
let activeId = UserDefaults.standard.string(forKey: Self.activeBundleKey),
|
|
625
|
+
let basePath = getBundleInfoById(activeId)?["path"] as? String,
|
|
626
|
+
FileManager.default.fileExists(atPath: basePath),
|
|
627
|
+
let patchUrl = delta["patchUrl"] as? String,
|
|
628
|
+
securityManager.validateUrl(patchUrl),
|
|
629
|
+
let patchChecksum = delta["patchChecksum"] as? String,
|
|
630
|
+
let targetChecksum = delta["targetChecksum"] as? String else { return nil }
|
|
631
|
+
|
|
632
|
+
do {
|
|
633
|
+
let patchURL = try await downloadBundle(
|
|
634
|
+
from: patchUrl,
|
|
635
|
+
to: directory,
|
|
636
|
+
bundleId: bundleId,
|
|
637
|
+
filename: "patch.nudelta.json"
|
|
638
|
+
)
|
|
639
|
+
guard try calculateChecksum(for: patchURL).caseInsensitiveCompare(patchChecksum) == .orderedSame else {
|
|
640
|
+
return nil
|
|
641
|
+
}
|
|
642
|
+
let data = try Data(contentsOf: patchURL)
|
|
643
|
+
guard let document = try JSONSerialization.jsonObject(with: data) as? [String: Any],
|
|
644
|
+
document["format"] as? String == "NUDELTA/1",
|
|
645
|
+
document["fromVersion"] as? String == delta["fromVersion"] as? String,
|
|
646
|
+
document["targetChecksum"] as? String == targetChecksum,
|
|
647
|
+
let baseChecksum = document["baseChecksum"] as? String,
|
|
648
|
+
let targetSize = (document["targetSize"] as? NSNumber)?.intValue,
|
|
649
|
+
targetSize > 0,
|
|
650
|
+
targetSize <= ((config?["maxBundleSize"] as? NSNumber)?.intValue ?? 100 * 1024 * 1024),
|
|
651
|
+
let operations = document["operations"] as? [[String: Any]],
|
|
652
|
+
!operations.isEmpty, operations.count <= 100_000 else { return nil }
|
|
653
|
+
let baseURL = URL(fileURLWithPath: basePath)
|
|
654
|
+
guard try calculateChecksum(for: baseURL).caseInsensitiveCompare(baseChecksum) == .orderedSame else { return nil }
|
|
655
|
+
let base = try Data(contentsOf: baseURL, options: [.mappedIfSafe])
|
|
656
|
+
var result = Data()
|
|
657
|
+
result.reserveCapacity(targetSize)
|
|
658
|
+
for operation in operations {
|
|
659
|
+
switch operation["op"] as? String {
|
|
660
|
+
case "copy":
|
|
661
|
+
guard let offset = (operation["offset"] as? NSNumber)?.intValue,
|
|
662
|
+
let length = (operation["length"] as? NSNumber)?.intValue,
|
|
663
|
+
offset >= 0, length > 0, length <= base.count,
|
|
664
|
+
offset <= base.count - length else { return nil }
|
|
665
|
+
result.append(base.subdata(in: offset..<(offset + length)))
|
|
666
|
+
case "literal":
|
|
667
|
+
guard let encoded = operation["data"] as? String,
|
|
668
|
+
let literal = Data(base64Encoded: encoded) else { return nil }
|
|
669
|
+
result.append(literal)
|
|
670
|
+
default: return nil
|
|
671
|
+
}
|
|
672
|
+
if result.count > targetSize { return nil }
|
|
673
|
+
}
|
|
674
|
+
guard result.count == targetSize else { return nil }
|
|
675
|
+
let output = directory.appendingPathComponent("bundle.zip")
|
|
676
|
+
try result.write(to: output, options: .atomic)
|
|
677
|
+
guard try calculateChecksum(for: output).caseInsensitiveCompare(targetChecksum) == .orderedSame else {
|
|
678
|
+
try? FileManager.default.removeItem(at: output)
|
|
679
|
+
return nil
|
|
680
|
+
}
|
|
681
|
+
try? FileManager.default.removeItem(at: patchURL)
|
|
682
|
+
return output
|
|
683
|
+
} catch {
|
|
684
|
+
try? FileManager.default.removeItem(at: directory.appendingPathComponent("patch.nudelta.json"))
|
|
685
|
+
try? FileManager.default.removeItem(at: directory.appendingPathComponent("bundle.zip"))
|
|
686
|
+
return nil
|
|
537
687
|
}
|
|
538
688
|
}
|
|
539
689
|
|
|
540
690
|
private func calculateChecksum(for url: URL) throws -> String {
|
|
541
|
-
let
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
data.withUnsafeBytes { bytes in
|
|
545
|
-
_ = CC_SHA256(bytes.baseAddress, CC_LONG(data.count), &digest)
|
|
691
|
+
guard let stream = InputStream(url: url) else {
|
|
692
|
+
throw NSError(domain: "LiveUpdatePlugin", code: 14, userInfo: [NSLocalizedDescriptionKey: "Unable to read bundle"])
|
|
546
693
|
}
|
|
547
|
-
|
|
694
|
+
stream.open()
|
|
695
|
+
defer { stream.close() }
|
|
696
|
+
var context = CC_SHA256_CTX()
|
|
697
|
+
CC_SHA256_Init(&context)
|
|
698
|
+
let buffer = UnsafeMutablePointer<UInt8>.allocate(capacity: 64 * 1024)
|
|
699
|
+
defer { buffer.deallocate() }
|
|
700
|
+
while stream.hasBytesAvailable {
|
|
701
|
+
let read = stream.read(buffer, maxLength: 64 * 1024)
|
|
702
|
+
if read < 0 { throw stream.streamError ?? NSError(domain: "LiveUpdatePlugin", code: 15) }
|
|
703
|
+
if read == 0 { break }
|
|
704
|
+
CC_SHA256_Update(&context, buffer, CC_LONG(read))
|
|
705
|
+
}
|
|
706
|
+
var digest = [UInt8](repeating: 0, count: Int(CC_SHA256_DIGEST_LENGTH))
|
|
707
|
+
CC_SHA256_Final(&digest, &context)
|
|
548
708
|
return digest.map { String(format: "%02x", $0) }.joined()
|
|
549
709
|
}
|
|
550
710
|
|
|
@@ -757,10 +917,10 @@ class LiveUpdatePlugin {
|
|
|
757
917
|
// available. Absence of either is treated as "not configured" and
|
|
758
918
|
// skipped — Phase A1 already enforces fail-closed at download time.
|
|
759
919
|
if let signature = bundleInfo["signature"] as? String, !signature.isEmpty,
|
|
760
|
-
let publicKey =
|
|
920
|
+
let publicKey = config?["publicKey"] as? String,
|
|
761
921
|
!publicKey.isEmpty {
|
|
762
922
|
do {
|
|
763
|
-
let bytes = try Data(contentsOf: bundleURL)
|
|
923
|
+
let bytes = try Data(contentsOf: bundleURL, options: [.mappedIfSafe])
|
|
764
924
|
if !securityManager.verifySignature(
|
|
765
925
|
data: bytes,
|
|
766
926
|
signature: signature,
|
|
@@ -840,7 +1000,10 @@ class LiveUpdatePlugin {
|
|
|
840
1000
|
/// the device). Chosen as 5x the default 100MB downloaded-bundle limit
|
|
841
1001
|
/// — enough headroom for typical web-app bundles after minification
|
|
842
1002
|
/// reverses, below a threshold that would be hostile on mobile.
|
|
843
|
-
private
|
|
1003
|
+
private var maxUncompressedBundleBytes: UInt64 {
|
|
1004
|
+
let configured = (config?["maxUncompressedBundleSize"] as? NSNumber)?.uint64Value
|
|
1005
|
+
return min(max(configured ?? 500 * 1024 * 1024, 1), 2 * 1024 * 1024 * 1024)
|
|
1006
|
+
}
|
|
844
1007
|
|
|
845
1008
|
private func extractZipBundle(from zipUrl: URL, to destinationUrl: URL) throws {
|
|
846
1009
|
// Ensure destination directory exists.
|
|
@@ -856,11 +1019,7 @@ class LiveUpdatePlugin {
|
|
|
856
1019
|
// itself resolves cleanly.
|
|
857
1020
|
// 3. zip-bomb — sum of uncompressed sizes capped; refuses to
|
|
858
1021
|
// extract an archive that would exhaust device storage.
|
|
859
|
-
|
|
860
|
-
throw NSError(domain: "LiveUpdatePlugin", code: 7, userInfo: [
|
|
861
|
-
NSLocalizedDescriptionKey: "Could not open bundle archive for inspection"
|
|
862
|
-
])
|
|
863
|
-
}
|
|
1022
|
+
let archive = try Archive(url: zipUrl, accessMode: .read)
|
|
864
1023
|
|
|
865
1024
|
let canonicalDestination = destinationUrl
|
|
866
1025
|
.standardizedFileURL
|
|
@@ -897,9 +1056,9 @@ class LiveUpdatePlugin {
|
|
|
897
1056
|
}
|
|
898
1057
|
|
|
899
1058
|
let (newTotal, overflow) = totalUncompressed.addingReportingOverflow(entry.uncompressedSize)
|
|
900
|
-
if overflow || newTotal >
|
|
1059
|
+
if overflow || newTotal > maxUncompressedBundleBytes {
|
|
901
1060
|
throw NSError(domain: "LiveUpdatePlugin", code: 11, userInfo: [
|
|
902
|
-
NSLocalizedDescriptionKey: "Bundle exceeds maximum uncompressed size (\(
|
|
1061
|
+
NSLocalizedDescriptionKey: "Bundle exceeds maximum uncompressed size (\(maxUncompressedBundleBytes) bytes) — possible zip-bomb"
|
|
903
1062
|
])
|
|
904
1063
|
}
|
|
905
1064
|
totalUncompressed = newTotal
|
|
@@ -916,8 +1075,8 @@ class LiveUpdatePlugin {
|
|
|
916
1075
|
])
|
|
917
1076
|
}
|
|
918
1077
|
|
|
919
|
-
//
|
|
920
|
-
|
|
1078
|
+
// Keep the verified ZIP so cold-start integrity/signature checks can
|
|
1079
|
+
// re-verify the exact signed bytes. It is removed with the bundle.
|
|
921
1080
|
}
|
|
922
1081
|
|
|
923
1082
|
private func configureWebViewPath(_ path: String) {
|
|
@@ -952,6 +1111,11 @@ class LiveUpdatePlugin {
|
|
|
952
1111
|
// Update stored path
|
|
953
1112
|
UserDefaults.standard.set(safePath, forKey: "native_update_webview_path")
|
|
954
1113
|
}
|
|
1114
|
+
|
|
1115
|
+
private func unsignedDevelopmentUpdatesAllowed() -> Bool {
|
|
1116
|
+
_isDebugAssertConfiguration() &&
|
|
1117
|
+
(config?["allowUnsignedDevelopmentUpdates"] as? Bool == true)
|
|
1118
|
+
}
|
|
955
1119
|
}
|
|
956
1120
|
|
|
957
1121
|
// MARK: - Data Models
|
|
@@ -1036,10 +1200,11 @@ class CertificatePinningDelegate: NSObject, URLSessionDelegate {
|
|
|
1036
1200
|
let certificateCount = SecTrustGetCertificateCount(serverTrust)
|
|
1037
1201
|
|
|
1038
1202
|
for i in 0..<certificateCount {
|
|
1039
|
-
guard let certificate = SecTrustGetCertificateAtIndex(serverTrust, i)
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1203
|
+
guard let certificate = SecTrustGetCertificateAtIndex(serverTrust, i),
|
|
1204
|
+
let key = SecCertificateCopyKey(certificate),
|
|
1205
|
+
let spki = subjectPublicKeyInfo(for: key) else { continue }
|
|
1206
|
+
|
|
1207
|
+
let hash = spki.sha256()
|
|
1043
1208
|
let pin = "sha256/" + hash.base64EncodedString()
|
|
1044
1209
|
|
|
1045
1210
|
if expectedPins.contains(pin) {
|
|
@@ -1049,6 +1214,49 @@ class CertificatePinningDelegate: NSObject, URLSessionDelegate {
|
|
|
1049
1214
|
|
|
1050
1215
|
return false
|
|
1051
1216
|
}
|
|
1217
|
+
|
|
1218
|
+
/** Build DER SubjectPublicKeyInfo so pins match OkHttp/curl/browser semantics. */
|
|
1219
|
+
private func subjectPublicKeyInfo(for key: SecKey) -> Data? {
|
|
1220
|
+
guard let attributes = SecKeyCopyAttributes(key) as? [CFString: Any],
|
|
1221
|
+
let keyType = attributes[kSecAttrKeyType],
|
|
1222
|
+
let bits = attributes[kSecAttrKeySizeInBits] as? Int,
|
|
1223
|
+
let rawKey = SecKeyCopyExternalRepresentation(key, nil) as Data? else { return nil }
|
|
1224
|
+
|
|
1225
|
+
let algorithm: Data
|
|
1226
|
+
if CFEqual(keyType as CFTypeRef, kSecAttrKeyTypeRSA) {
|
|
1227
|
+
algorithm = Data([0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86,
|
|
1228
|
+
0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00])
|
|
1229
|
+
} else if CFEqual(keyType as CFTypeRef, kSecAttrKeyTypeECSECPrimeRandom) {
|
|
1230
|
+
let curveOID: [UInt8]
|
|
1231
|
+
switch bits {
|
|
1232
|
+
case 256: curveOID = [0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07]
|
|
1233
|
+
case 384: curveOID = [0x06, 0x05, 0x2b, 0x81, 0x04, 0x00, 0x22]
|
|
1234
|
+
case 521: curveOID = [0x06, 0x05, 0x2b, 0x81, 0x04, 0x00, 0x23]
|
|
1235
|
+
default: return nil
|
|
1236
|
+
}
|
|
1237
|
+
algorithm = derSequence(Data([0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01]) + Data(curveOID))
|
|
1238
|
+
} else {
|
|
1239
|
+
return nil
|
|
1240
|
+
}
|
|
1241
|
+
|
|
1242
|
+
let bitString = Data([0x03]) + derLength(rawKey.count + 1) + Data([0x00]) + rawKey
|
|
1243
|
+
return derSequence(algorithm + bitString)
|
|
1244
|
+
}
|
|
1245
|
+
|
|
1246
|
+
private func derSequence(_ content: Data) -> Data {
|
|
1247
|
+
Data([0x30]) + derLength(content.count) + content
|
|
1248
|
+
}
|
|
1249
|
+
|
|
1250
|
+
private func derLength(_ length: Int) -> Data {
|
|
1251
|
+
if length < 128 { return Data([UInt8(length)]) }
|
|
1252
|
+
var value = length
|
|
1253
|
+
var bytes: [UInt8] = []
|
|
1254
|
+
while value > 0 {
|
|
1255
|
+
bytes.insert(UInt8(value & 0xff), at: 0)
|
|
1256
|
+
value >>= 8
|
|
1257
|
+
}
|
|
1258
|
+
return Data([0x80 | UInt8(bytes.count)] + bytes)
|
|
1259
|
+
}
|
|
1052
1260
|
}
|
|
1053
1261
|
|
|
1054
1262
|
// MARK: - Data Extension for SHA256
|
|
@@ -1061,4 +1269,4 @@ extension Data {
|
|
|
1061
1269
|
}
|
|
1062
1270
|
return Data(hash)
|
|
1063
1271
|
}
|
|
1064
|
-
}
|
|
1272
|
+
}
|
|
@@ -4,6 +4,9 @@
|
|
|
4
4
|
// Define the plugin using the CAP_PLUGIN Macro, and
|
|
5
5
|
// each method the plugin supports using the CAP_PLUGIN_METHOD macro.
|
|
6
6
|
CAP_PLUGIN(NativeUpdatePlugin, "NativeUpdate",
|
|
7
|
+
CAP_PLUGIN_METHOD(initialize, CAPPluginReturnPromise);
|
|
8
|
+
CAP_PLUGIN_METHOD(isInitialized, CAPPluginReturnPromise);
|
|
9
|
+
CAP_PLUGIN_METHOD(cleanup, CAPPluginReturnPromise);
|
|
7
10
|
CAP_PLUGIN_METHOD(configure, CAPPluginReturnPromise);
|
|
8
11
|
CAP_PLUGIN_METHOD(getSecurityInfo, CAPPluginReturnPromise);
|
|
9
12
|
|
|
@@ -22,6 +25,10 @@ CAP_PLUGIN(NativeUpdatePlugin, "NativeUpdate",
|
|
|
22
25
|
CAP_PLUGIN_METHOD(setChannel, CAPPluginReturnPromise);
|
|
23
26
|
CAP_PLUGIN_METHOD(setUpdateUrl, CAPPluginReturnPromise);
|
|
24
27
|
CAP_PLUGIN_METHOD(validateUpdate, CAPPluginReturnPromise);
|
|
28
|
+
CAP_PLUGIN_METHOD(cancelDownload, CAPPluginReturnPromise);
|
|
29
|
+
CAP_PLUGIN_METHOD(cancelAllDownloads, CAPPluginReturnPromise);
|
|
30
|
+
CAP_PLUGIN_METHOD(isDownloading, CAPPluginReturnPromise);
|
|
31
|
+
CAP_PLUGIN_METHOD(getActiveDownloadCount, CAPPluginReturnPromise);
|
|
25
32
|
|
|
26
33
|
// App Update Methods
|
|
27
34
|
CAP_PLUGIN_METHOD(getAppUpdateInfo, CAPPluginReturnPromise);
|
|
@@ -43,4 +50,4 @@ CAP_PLUGIN(NativeUpdatePlugin, "NativeUpdate",
|
|
|
43
50
|
CAP_PLUGIN_METHOD(setNotificationPreferences, CAPPluginReturnPromise);
|
|
44
51
|
CAP_PLUGIN_METHOD(getNotificationPermissions, CAPPluginReturnPromise);
|
|
45
52
|
CAP_PLUGIN_METHOD(requestNotificationPermissions, CAPPluginReturnPromise);
|
|
46
|
-
)
|
|
53
|
+
)
|