native-update 3.4.1 → 4.0.0
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 +554 -0
- package/android/build.gradle +43 -34
- 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/.yarn/install-state.gz +0 -0
- 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 +66 -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 +27 -15
- package/Readme.md +0 -565
|
@@ -2,18 +2,18 @@ package com.aoneahsan.nativeupdate
|
|
|
2
2
|
|
|
3
3
|
import android.app.Activity
|
|
4
4
|
import android.content.Context
|
|
5
|
+
import android.content.pm.ApplicationInfo
|
|
6
|
+
import android.util.Base64
|
|
5
7
|
import com.getcapacitor.JSObject
|
|
6
8
|
import com.getcapacitor.PluginCall
|
|
7
9
|
import kotlinx.coroutines.*
|
|
8
10
|
import okhttp3.*
|
|
9
|
-
import okhttp3.tls.HandshakeCertificates
|
|
10
11
|
import java.io.File
|
|
11
12
|
import java.io.FileOutputStream
|
|
12
13
|
import java.io.IOException
|
|
13
14
|
import java.security.MessageDigest
|
|
14
15
|
import java.util.concurrent.TimeUnit
|
|
15
16
|
import java.util.zip.ZipFile
|
|
16
|
-
import javax.net.ssl.X509TrustManager
|
|
17
17
|
|
|
18
18
|
class LiveUpdatePlugin(
|
|
19
19
|
private val activity: Activity,
|
|
@@ -31,11 +31,18 @@ class LiveUpdatePlugin(
|
|
|
31
31
|
private val scope = CoroutineScope(Dispatchers.IO + SupervisorJob())
|
|
32
32
|
private lateinit var okHttpClient: OkHttpClient
|
|
33
33
|
private val securityManager = SecurityManager(context)
|
|
34
|
-
private val activeDownloads =
|
|
35
|
-
private var downloadManager: android.app.DownloadManager? = null
|
|
34
|
+
private val activeDownloads = java.util.concurrent.ConcurrentHashMap<String, Call>()
|
|
36
35
|
|
|
37
36
|
/** Hard cap on uncompressed bundle size (zip-bomb defense). */
|
|
38
|
-
private val maxUncompressedBundleBytes: Long
|
|
37
|
+
private val maxUncompressedBundleBytes: Long
|
|
38
|
+
get() = config?.optLong("maxUncompressedBundleSize", 500L * 1024 * 1024)
|
|
39
|
+
?.coerceIn(1L, 2L * 1024 * 1024 * 1024)
|
|
40
|
+
?: 500L * 1024 * 1024
|
|
41
|
+
|
|
42
|
+
private val maxDownloadBytes: Long
|
|
43
|
+
get() = config?.optLong("maxBundleSize", 100L * 1024 * 1024)
|
|
44
|
+
?.coerceIn(1L, 2L * 1024 * 1024 * 1024)
|
|
45
|
+
?: 100L * 1024 * 1024
|
|
39
46
|
|
|
40
47
|
/**
|
|
41
48
|
* The app's signing-certificate fingerprints (SHA-256, SHA-1) as lowercase
|
|
@@ -68,7 +75,6 @@ class LiveUpdatePlugin(
|
|
|
68
75
|
init {
|
|
69
76
|
// Initialize OkHttp with default settings
|
|
70
77
|
okHttpClient = createOkHttpClient()
|
|
71
|
-
downloadManager = context.getSystemService(Context.DOWNLOAD_SERVICE) as? android.app.DownloadManager
|
|
72
78
|
}
|
|
73
79
|
|
|
74
80
|
private fun createOkHttpClient(): OkHttpClient {
|
|
@@ -98,12 +104,19 @@ class LiveUpdatePlugin(
|
|
|
98
104
|
okHttpClient = createOkHttpClient()
|
|
99
105
|
|
|
100
106
|
// Validate configuration
|
|
101
|
-
val serverUrl = config.
|
|
102
|
-
if (serverUrl != null && !
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
+
val serverUrl = config.optString("serverUrl").takeIf { it.isNotBlank() }
|
|
108
|
+
if (serverUrl != null && !securityManager.validateUrl(serverUrl)) {
|
|
109
|
+
throw IllegalArgumentException("Server URL must be an allowed HTTPS URL")
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
val requireSignature = config.optBoolean("requireSignature", true)
|
|
113
|
+
if (requireSignature && config.optString("publicKey").isBlank()) {
|
|
114
|
+
throw IllegalArgumentException("publicKey is required when requireSignature is enabled")
|
|
115
|
+
}
|
|
116
|
+
if (!requireSignature && !unsignedDevelopmentUpdatesAllowed()) {
|
|
117
|
+
throw IllegalArgumentException(
|
|
118
|
+
"Unsigned updates are allowed only in debuggable builds with allowUnsignedDevelopmentUpdates enabled"
|
|
119
|
+
)
|
|
107
120
|
}
|
|
108
121
|
}
|
|
109
122
|
|
|
@@ -179,14 +192,15 @@ class LiveUpdatePlugin(
|
|
|
179
192
|
return@launch
|
|
180
193
|
}
|
|
181
194
|
|
|
182
|
-
if (!
|
|
183
|
-
call.reject("INSECURE_URL", "Download URL must
|
|
195
|
+
if (!securityManager.validateUrl(url)) {
|
|
196
|
+
call.reject("INSECURE_URL", "Download URL must be an allowed HTTPS URL")
|
|
184
197
|
return@launch
|
|
185
198
|
}
|
|
186
199
|
|
|
187
200
|
downloadDir.mkdirs()
|
|
188
201
|
|
|
189
|
-
val downloadedFile =
|
|
202
|
+
val downloadedFile = tryApplyDelta(call, downloadDir, bundleId)
|
|
203
|
+
?: downloadBundle(url, downloadDir, bundleId)
|
|
190
204
|
|
|
191
205
|
if (!verifyChecksum(downloadedFile, checksum)) {
|
|
192
206
|
call.reject("CHECKSUM_ERROR", "Bundle checksum validation failed")
|
|
@@ -194,17 +208,21 @@ class LiveUpdatePlugin(
|
|
|
194
208
|
}
|
|
195
209
|
|
|
196
210
|
val signature = call.getString("signature")
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
211
|
+
val publicKey = config?.getString("publicKey")
|
|
212
|
+
val requireSignature = config?.getBool("requireSignature") ?: true
|
|
213
|
+
val signatureAlgorithm = call.getString("signatureAlgorithm") ?: "RSA-SHA256"
|
|
214
|
+
if (signatureAlgorithm != "RSA-SHA256") {
|
|
215
|
+
call.reject("SIGNATURE_ERROR", "Unsupported signature algorithm")
|
|
216
|
+
return@launch
|
|
217
|
+
}
|
|
218
|
+
if (requireSignature && signature.isNullOrBlank()) {
|
|
219
|
+
call.reject("SIGNATURE_ERROR", "Bundle signature is required")
|
|
220
|
+
return@launch
|
|
221
|
+
}
|
|
222
|
+
if (!signature.isNullOrBlank()) {
|
|
223
|
+
if (publicKey.isNullOrBlank() || !securityManager.verifySignature(downloadedFile, signature, publicKey)) {
|
|
224
|
+
call.reject("SIGNATURE_ERROR", "Bundle signature validation failed")
|
|
225
|
+
return@launch
|
|
208
226
|
}
|
|
209
227
|
}
|
|
210
228
|
|
|
@@ -231,6 +249,7 @@ class LiveUpdatePlugin(
|
|
|
231
249
|
bundleInfo.put("verified", true)
|
|
232
250
|
if (signature != null) {
|
|
233
251
|
bundleInfo.put("signature", signature)
|
|
252
|
+
bundleInfo.put("signatureAlgorithm", "RSA-SHA256")
|
|
234
253
|
}
|
|
235
254
|
|
|
236
255
|
saveBundleInfo(bundleInfo)
|
|
@@ -365,6 +384,11 @@ class LiveUpdatePlugin(
|
|
|
365
384
|
result.put("url", latestVersion.getString("downloadUrl"))
|
|
366
385
|
result.put("notes", latestVersion.getString("releaseNotes"))
|
|
367
386
|
result.put("checksum", latestVersion.getString("checksum"))
|
|
387
|
+
latestVersion.optString("signature", "")
|
|
388
|
+
.takeIf { it.isNotEmpty() }
|
|
389
|
+
?.let { result.put("signature", it) }
|
|
390
|
+
result.put("signatureAlgorithm", latestVersion.optString("signatureAlgorithm", "RSA-SHA256"))
|
|
391
|
+
latestVersion.optJSONObject("delta")?.let { result.put("delta", it) }
|
|
368
392
|
result.put("size", latestVersion.optLong("size", 0))
|
|
369
393
|
result.put("mandatory", latestVersion.optBoolean("mandatory", false))
|
|
370
394
|
call.resolve(result)
|
|
@@ -390,17 +414,12 @@ class LiveUpdatePlugin(
|
|
|
390
414
|
}
|
|
391
415
|
|
|
392
416
|
fun setUpdateUrl(call: PluginCall) {
|
|
393
|
-
|
|
417
|
+
call.getString("url") ?: run {
|
|
394
418
|
call.reject("URL is required")
|
|
395
419
|
return
|
|
396
420
|
}
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
call.reject("INSECURE_URL", "Update URL must use HTTPS")
|
|
400
|
-
return
|
|
401
|
-
}
|
|
402
|
-
|
|
403
|
-
config?.put("serverUrl", url)
|
|
421
|
+
|
|
422
|
+
android.util.Log.w("NativeUpdate", "setUpdateUrl() is deprecated and ignored; the update URL is locked after initialize()")
|
|
404
423
|
call.resolve()
|
|
405
424
|
}
|
|
406
425
|
|
|
@@ -433,6 +452,33 @@ class LiveUpdatePlugin(
|
|
|
433
452
|
}
|
|
434
453
|
}
|
|
435
454
|
}
|
|
455
|
+
|
|
456
|
+
fun cancelDownload(call: PluginCall) {
|
|
457
|
+
val bundleId = call.getString("bundleId") ?: run {
|
|
458
|
+
call.reject("Bundle ID is required")
|
|
459
|
+
return
|
|
460
|
+
}
|
|
461
|
+
activeDownloads.remove(bundleId)?.cancel()
|
|
462
|
+
call.resolve()
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
fun cancelAllDownloads(call: PluginCall) {
|
|
466
|
+
activeDownloads.values.forEach(Call::cancel)
|
|
467
|
+
activeDownloads.clear()
|
|
468
|
+
call.resolve()
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
fun isDownloading(call: PluginCall) {
|
|
472
|
+
val bundleId = call.getString("bundleId") ?: run {
|
|
473
|
+
call.reject("Bundle ID is required")
|
|
474
|
+
return
|
|
475
|
+
}
|
|
476
|
+
call.resolve(JSObject().put("isDownloading", activeDownloads.containsKey(bundleId)))
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
fun getActiveDownloadCount(call: PluginCall) {
|
|
480
|
+
call.resolve(JSObject().put("count", activeDownloads.size))
|
|
481
|
+
}
|
|
436
482
|
|
|
437
483
|
/**
|
|
438
484
|
* The app's client identity for API-key client restrictions: applicationId
|
|
@@ -542,45 +588,131 @@ class LiveUpdatePlugin(
|
|
|
542
588
|
}
|
|
543
589
|
}
|
|
544
590
|
|
|
545
|
-
private suspend fun downloadBundle(
|
|
591
|
+
private suspend fun downloadBundle(
|
|
592
|
+
url: String,
|
|
593
|
+
downloadDir: File,
|
|
594
|
+
bundleId: String,
|
|
595
|
+
filename: String = "bundle.zip"
|
|
596
|
+
): File {
|
|
546
597
|
return withContext(Dispatchers.IO) {
|
|
547
598
|
val request = Request.Builder()
|
|
548
599
|
.url(url)
|
|
549
600
|
.build()
|
|
550
601
|
|
|
551
|
-
val
|
|
552
|
-
|
|
553
|
-
|
|
602
|
+
val networkCall = okHttpClient.newCall(request)
|
|
603
|
+
activeDownloads[bundleId] = networkCall
|
|
604
|
+
try {
|
|
605
|
+
networkCall.execute().use { response ->
|
|
606
|
+
if (!response.isSuccessful) throw IOException("Download failed: ${response.code}")
|
|
607
|
+
|
|
608
|
+
val file = File(downloadDir, filename)
|
|
609
|
+
val totalBytes = response.body.contentLength()
|
|
610
|
+
if (totalBytes > maxDownloadBytes) throw IOException("Bundle exceeds maximum download size")
|
|
611
|
+
var downloadedBytes = 0L
|
|
612
|
+
|
|
613
|
+
response.body.byteStream().use { input ->
|
|
614
|
+
FileOutputStream(file).use { output ->
|
|
615
|
+
val buffer = ByteArray(8192)
|
|
616
|
+
while (true) {
|
|
617
|
+
val read = input.read(buffer)
|
|
618
|
+
if (read < 0) break
|
|
619
|
+
output.write(buffer, 0, read)
|
|
620
|
+
downloadedBytes += read
|
|
621
|
+
if (downloadedBytes > maxDownloadBytes) {
|
|
622
|
+
throw IOException("Bundle exceeds maximum download size")
|
|
623
|
+
}
|
|
624
|
+
if (totalBytes > 0) {
|
|
625
|
+
progressListener?.invoke(JSObject().apply {
|
|
626
|
+
put("percent", ((downloadedBytes * 100) / totalBytes).toInt())
|
|
627
|
+
put("bytesDownloaded", downloadedBytes)
|
|
628
|
+
put("totalBytes", totalBytes)
|
|
629
|
+
put("bundleId", bundleId)
|
|
630
|
+
})
|
|
631
|
+
}
|
|
632
|
+
}
|
|
633
|
+
}
|
|
634
|
+
}
|
|
635
|
+
file
|
|
636
|
+
}
|
|
637
|
+
} finally {
|
|
638
|
+
activeDownloads.remove(bundleId)
|
|
554
639
|
}
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
640
|
+
}
|
|
641
|
+
}
|
|
642
|
+
|
|
643
|
+
private suspend fun tryApplyDelta(call: PluginCall, directory: File, bundleId: String): File? {
|
|
644
|
+
if (config?.optBoolean("enableDeltaUpdates", true) == false) return null
|
|
645
|
+
val delta = call.getObject("delta") ?: return null
|
|
646
|
+
if (delta.optString("format") != "NUDELTA/1" ||
|
|
647
|
+
delta.optString("fromVersion") != getCurrentVersion()) return null
|
|
648
|
+
return try {
|
|
649
|
+
val activeId = context.getSharedPreferences(PREF_FILE, Context.MODE_PRIVATE)
|
|
650
|
+
.getString(ACTIVE_BUNDLE_KEY, null) ?: return null
|
|
651
|
+
val basePath = getBundleInfoById(activeId)?.optString("path", "") ?: return null
|
|
652
|
+
val base = File(basePath)
|
|
653
|
+
if (!base.isFile) return null
|
|
654
|
+
val patchUrl = delta.getString("patchUrl") ?: return null
|
|
655
|
+
if (!securityManager.validateUrl(patchUrl)) return null
|
|
656
|
+
val patchChecksum = delta.getString("patchChecksum") ?: return null
|
|
657
|
+
val targetChecksum = delta.getString("targetChecksum") ?: return null
|
|
658
|
+
val patch = downloadBundle(patchUrl, directory, bundleId, "patch.nudelta.json")
|
|
659
|
+
if (!verifyChecksum(patch, patchChecksum)) return null
|
|
660
|
+
val document = JSObject(patch.readText(Charsets.UTF_8))
|
|
661
|
+
if (document.optString("format") != "NUDELTA/1" ||
|
|
662
|
+
document.optString("fromVersion") != delta.optString("fromVersion") ||
|
|
663
|
+
document.optString("targetChecksum") != targetChecksum ||
|
|
664
|
+
document.optString("baseChecksum").isBlank() ||
|
|
665
|
+
!verifyChecksum(base, document.optString("baseChecksum"))) return null
|
|
666
|
+
val targetSize = document.getLong("targetSize") ?: return null
|
|
667
|
+
if (targetSize <= 0 || targetSize > maxDownloadBytes) return null
|
|
668
|
+
val output = File(directory, "bundle.zip")
|
|
669
|
+
java.io.RandomAccessFile(base, "r").use { source ->
|
|
670
|
+
FileOutputStream(output).use { destination ->
|
|
671
|
+
var written = 0L
|
|
672
|
+
val operations = document.getJSONArray("operations") ?: return null
|
|
673
|
+
if (operations.length() == 0 || operations.length() > 100_000) return null
|
|
674
|
+
for (index in 0 until operations.length()) {
|
|
675
|
+
val operation = operations.getJSONObject(index)
|
|
676
|
+
when (operation.getString("op")) {
|
|
677
|
+
"copy" -> {
|
|
678
|
+
val offset = operation.getLong("offset")
|
|
679
|
+
val length = operation.getLong("length")
|
|
680
|
+
if (offset < 0 || length <= 0 || length > source.length() ||
|
|
681
|
+
offset > source.length() - length) return null
|
|
682
|
+
source.seek(offset)
|
|
683
|
+
var remaining = length
|
|
684
|
+
val buffer = ByteArray(DEFAULT_BUFFER_SIZE)
|
|
685
|
+
while (remaining > 0) {
|
|
686
|
+
val read = source.read(buffer, 0, minOf(buffer.size.toLong(), remaining).toInt())
|
|
687
|
+
if (read <= 0) return null
|
|
688
|
+
destination.write(buffer, 0, read)
|
|
689
|
+
remaining -= read
|
|
690
|
+
}
|
|
691
|
+
written += length
|
|
692
|
+
}
|
|
693
|
+
"literal" -> {
|
|
694
|
+
val bytes = Base64.decode(operation.getString("data"), Base64.DEFAULT)
|
|
695
|
+
destination.write(bytes)
|
|
696
|
+
written += bytes.size
|
|
697
|
+
}
|
|
698
|
+
else -> return null
|
|
578
699
|
}
|
|
700
|
+
if (written > targetSize) return null
|
|
579
701
|
}
|
|
702
|
+
if (written != targetSize) return null
|
|
580
703
|
}
|
|
581
704
|
}
|
|
582
|
-
|
|
583
|
-
|
|
705
|
+
if (!verifyChecksum(output, targetChecksum)) {
|
|
706
|
+
output.delete()
|
|
707
|
+
null
|
|
708
|
+
} else {
|
|
709
|
+
patch.delete()
|
|
710
|
+
output
|
|
711
|
+
}
|
|
712
|
+
} catch (_: Exception) {
|
|
713
|
+
File(directory, "patch.nudelta.json").delete()
|
|
714
|
+
File(directory, "bundle.zip").delete()
|
|
715
|
+
null
|
|
584
716
|
}
|
|
585
717
|
}
|
|
586
718
|
|
|
@@ -671,7 +803,8 @@ class LiveUpdatePlugin(
|
|
|
671
803
|
}
|
|
672
804
|
|
|
673
805
|
val calculatedChecksum = digest.digest().joinToString("") { "%02x".format(it) }
|
|
674
|
-
return
|
|
806
|
+
return expectedChecksum.matches(Regex("^[a-fA-F0-9]{64}$")) &&
|
|
807
|
+
calculatedChecksum.equals(expectedChecksum, ignoreCase = true)
|
|
675
808
|
}
|
|
676
809
|
|
|
677
810
|
/**
|
|
@@ -816,9 +949,7 @@ class LiveUpdatePlugin(
|
|
|
816
949
|
fun cleanup() {
|
|
817
950
|
// Clean up any resources
|
|
818
951
|
// Cancel any ongoing downloads
|
|
819
|
-
activeDownloads.values.forEach
|
|
820
|
-
downloadManager?.remove(downloadId)
|
|
821
|
-
}
|
|
952
|
+
activeDownloads.values.forEach(Call::cancel)
|
|
822
953
|
activeDownloads.clear()
|
|
823
954
|
}
|
|
824
955
|
|
|
@@ -914,11 +1045,9 @@ class LiveUpdatePlugin(
|
|
|
914
1045
|
|
|
915
1046
|
val signature = bundleInfo.optString("signature", "")
|
|
916
1047
|
if (signature.isNotEmpty()) {
|
|
917
|
-
val publicKey = config?.
|
|
918
|
-
?: config?.getString("publicKey")
|
|
1048
|
+
val publicKey = config?.getString("publicKey")
|
|
919
1049
|
if (!publicKey.isNullOrEmpty()) {
|
|
920
|
-
|
|
921
|
-
if (!securityManager.verifySignature(bytes, signature, publicKey)) {
|
|
1050
|
+
if (!securityManager.verifySignature(bundleFile, signature, publicKey)) {
|
|
922
1051
|
rollbackToPrevious("bundle signature mismatch on boot", activeBundleId)
|
|
923
1052
|
return
|
|
924
1053
|
}
|
|
@@ -926,6 +1055,11 @@ class LiveUpdatePlugin(
|
|
|
926
1055
|
}
|
|
927
1056
|
}
|
|
928
1057
|
|
|
1058
|
+
private fun unsignedDevelopmentUpdatesAllowed(): Boolean {
|
|
1059
|
+
val debuggable = context.applicationInfo.flags and ApplicationInfo.FLAG_DEBUGGABLE != 0
|
|
1060
|
+
return debuggable && config?.optBoolean("allowUnsignedDevelopmentUpdates", false) == true
|
|
1061
|
+
}
|
|
1062
|
+
|
|
929
1063
|
private fun rollbackToPrevious(reason: String, fromBundleId: String) {
|
|
930
1064
|
val prefs = context.getSharedPreferences(PREF_FILE, Context.MODE_PRIVATE)
|
|
931
1065
|
val previous = prefs.getString(PREVIOUS_BUNDLE_KEY, null)
|
|
@@ -992,4 +1126,4 @@ class LiveUpdatePlugin(
|
|
|
992
1126
|
null
|
|
993
1127
|
}
|
|
994
1128
|
}
|
|
995
|
-
}
|
|
1129
|
+
}
|
|
@@ -26,7 +26,7 @@ class NativeUpdatePlugin : Plugin() {
|
|
|
26
26
|
liveUpdatePlugin = LiveUpdatePlugin(activity, context)
|
|
27
27
|
appUpdatePlugin = AppUpdatePlugin(activity, context)
|
|
28
28
|
appReviewPlugin = AppReviewPlugin(activity, context)
|
|
29
|
-
backgroundUpdatePlugin = BackgroundUpdatePlugin()
|
|
29
|
+
backgroundUpdatePlugin = BackgroundUpdatePlugin(context)
|
|
30
30
|
securityManager = SecurityManager(context)
|
|
31
31
|
|
|
32
32
|
// Set up app update event listener
|
|
@@ -46,6 +46,9 @@ class NativeUpdatePlugin : Plugin() {
|
|
|
46
46
|
liveUpdatePlugin.setStateChangeListener { state ->
|
|
47
47
|
notifyListeners("updateStateChanged", state)
|
|
48
48
|
}
|
|
49
|
+
backgroundUpdatePlugin.setEventListener { event, data ->
|
|
50
|
+
notifyListeners(event, data)
|
|
51
|
+
}
|
|
49
52
|
|
|
50
53
|
// Wire the WebView retargeting hook. set() calls this with the
|
|
51
54
|
// extracted bundle's www/ path; we pass it to the Capacitor
|
|
@@ -77,15 +80,12 @@ class NativeUpdatePlugin : Plugin() {
|
|
|
77
80
|
@PluginMethod
|
|
78
81
|
fun initialize(call: PluginCall) {
|
|
79
82
|
try {
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
// rejected here, so native never received serverUrl/apiKey).
|
|
87
|
-
configure(call)
|
|
88
|
-
|
|
83
|
+
if (isInitialized) {
|
|
84
|
+
call.reject("Configuration is locked after initialization; call reset() first")
|
|
85
|
+
return
|
|
86
|
+
}
|
|
87
|
+
val config = call.getObject("config") ?: call.data
|
|
88
|
+
applyConfiguration(config)
|
|
89
89
|
isInitialized = true
|
|
90
90
|
call.resolve()
|
|
91
91
|
} catch (e: Exception) {
|
|
@@ -118,42 +118,28 @@ class NativeUpdatePlugin : Plugin() {
|
|
|
118
118
|
// configure(UpdateConfig) — nested sections
|
|
119
119
|
// Before this fix, only the wrapped shape reached native, so the
|
|
120
120
|
// flat initialize() every consumer uses configured NOTHING here.
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
if (config == null) {
|
|
124
|
-
call.reject("Configuration object is required")
|
|
121
|
+
if (isInitialized) {
|
|
122
|
+
call.reject("Configuration is locked after initialization; call reset() first")
|
|
125
123
|
return
|
|
126
124
|
}
|
|
125
|
+
val config = call.getObject("config") ?: call.data
|
|
127
126
|
|
|
128
127
|
try {
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
if (securityConfig != null) {
|
|
132
|
-
securityManager.configure(securityConfig)
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
// Configure sub-plugins
|
|
136
|
-
val liveUpdateConfig = config.getJSObject("liveUpdate") ?: buildFlatLiveUpdateConfig(config)
|
|
137
|
-
liveUpdateConfig?.let {
|
|
138
|
-
liveUpdatePlugin.configure(it)
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
config.getJSObject("appUpdate")?.let {
|
|
142
|
-
appUpdatePlugin.configure(it)
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
config.getJSObject("appReview")?.let {
|
|
146
|
-
appReviewPlugin.configure(it)
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
// Background update configuration is handled separately via enableBackgroundUpdates
|
|
150
|
-
|
|
128
|
+
applyConfiguration(config)
|
|
129
|
+
isInitialized = true
|
|
151
130
|
call.resolve()
|
|
152
131
|
} catch (e: Exception) {
|
|
153
132
|
call.reject("Configuration failed", e)
|
|
154
133
|
}
|
|
155
134
|
}
|
|
156
135
|
|
|
136
|
+
private fun applyConfiguration(config: JSObject) {
|
|
137
|
+
securityManager.configure(config)
|
|
138
|
+
normalizeLiveUpdateConfig(config)?.let(liveUpdatePlugin::configure)
|
|
139
|
+
config.getJSObject("appUpdate")?.let(appUpdatePlugin::configure)
|
|
140
|
+
config.getJSObject("appReview")?.let(appReviewPlugin::configure)
|
|
141
|
+
}
|
|
142
|
+
|
|
157
143
|
/**
|
|
158
144
|
* Normalize a flat PluginInitConfig (serverUrl/baseUrl, apiKey, appId,
|
|
159
145
|
* channel, publicKey at the top level) into the liveUpdate config shape
|
|
@@ -173,9 +159,48 @@ class NativeUpdatePlugin : Plugin() {
|
|
|
173
159
|
config.getString("appId")?.let { liveUpdate.put("appId", it) }
|
|
174
160
|
config.getString("channel")?.let { liveUpdate.put("channel", it) }
|
|
175
161
|
config.getString("publicKey")?.let { liveUpdate.put("publicKey", it) }
|
|
162
|
+
config.getBool("requireSignature")?.let { liveUpdate.put("requireSignature", it) }
|
|
163
|
+
config.getBool("allowUnsignedDevelopmentUpdates")?.let {
|
|
164
|
+
liveUpdate.put("allowUnsignedDevelopmentUpdates", it)
|
|
165
|
+
}
|
|
166
|
+
config.getString("checksumAlgorithm")?.let { liveUpdate.put("checksumAlgorithm", it) }
|
|
167
|
+
config.getLong("maxBundleSize")?.let { liveUpdate.put("maxBundleSize", it) }
|
|
168
|
+
config.getLong("maxUncompressedBundleSize")?.let {
|
|
169
|
+
liveUpdate.put("maxUncompressedBundleSize", it)
|
|
170
|
+
}
|
|
171
|
+
config.getBool("enableDeltaUpdates")?.let { liveUpdate.put("enableDeltaUpdates", it) }
|
|
172
|
+
config.getJSONArray("allowedHosts")?.let { liveUpdate.put("allowedHosts", it) }
|
|
176
173
|
config.getJSObject("security")?.let { liveUpdate.put("security", it) }
|
|
177
174
|
return liveUpdate
|
|
178
175
|
}
|
|
176
|
+
|
|
177
|
+
/** Merge root security controls into both documented configuration shapes. */
|
|
178
|
+
private fun normalizeLiveUpdateConfig(config: JSObject): JSObject? {
|
|
179
|
+
val liveUpdate = config.getJSObject("liveUpdate") ?: buildFlatLiveUpdateConfig(config) ?: return null
|
|
180
|
+
if (!liveUpdate.has("security")) {
|
|
181
|
+
config.getJSObject("security")?.let { liveUpdate.put("security", it) }
|
|
182
|
+
}
|
|
183
|
+
if (!liveUpdate.has("publicKey")) {
|
|
184
|
+
config.getString("publicKey")?.let { liveUpdate.put("publicKey", it) }
|
|
185
|
+
}
|
|
186
|
+
if (!liveUpdate.has("requireSignature")) {
|
|
187
|
+
config.getBool("requireSignature")?.let { liveUpdate.put("requireSignature", it) }
|
|
188
|
+
}
|
|
189
|
+
if (!liveUpdate.has("allowUnsignedDevelopmentUpdates")) {
|
|
190
|
+
config.getBool("allowUnsignedDevelopmentUpdates")?.let {
|
|
191
|
+
liveUpdate.put("allowUnsignedDevelopmentUpdates", it)
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
if (!liveUpdate.has("maxUncompressedBundleSize")) {
|
|
195
|
+
config.getLong("maxUncompressedBundleSize")?.let {
|
|
196
|
+
liveUpdate.put("maxUncompressedBundleSize", it)
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
if (!liveUpdate.has("enableDeltaUpdates")) {
|
|
200
|
+
config.getBool("enableDeltaUpdates")?.let { liveUpdate.put("enableDeltaUpdates", it) }
|
|
201
|
+
}
|
|
202
|
+
return liveUpdate
|
|
203
|
+
}
|
|
179
204
|
|
|
180
205
|
@PluginMethod
|
|
181
206
|
fun getSecurityInfo(call: PluginCall) {
|
|
@@ -260,6 +285,18 @@ class NativeUpdatePlugin : Plugin() {
|
|
|
260
285
|
fun validateUpdate(call: PluginCall) {
|
|
261
286
|
liveUpdatePlugin.validateUpdate(call)
|
|
262
287
|
}
|
|
288
|
+
|
|
289
|
+
@PluginMethod
|
|
290
|
+
fun cancelDownload(call: PluginCall) = liveUpdatePlugin.cancelDownload(call)
|
|
291
|
+
|
|
292
|
+
@PluginMethod
|
|
293
|
+
fun cancelAllDownloads(call: PluginCall) = liveUpdatePlugin.cancelAllDownloads(call)
|
|
294
|
+
|
|
295
|
+
@PluginMethod
|
|
296
|
+
fun isDownloading(call: PluginCall) = liveUpdatePlugin.isDownloading(call)
|
|
297
|
+
|
|
298
|
+
@PluginMethod
|
|
299
|
+
fun getActiveDownloadCount(call: PluginCall) = liveUpdatePlugin.getActiveDownloadCount(call)
|
|
263
300
|
|
|
264
301
|
// App Update Methods
|
|
265
302
|
|
|
@@ -342,4 +379,4 @@ class NativeUpdatePlugin : Plugin() {
|
|
|
342
379
|
backgroundUpdatePlugin.requestNotificationPermissions(call)
|
|
343
380
|
}
|
|
344
381
|
|
|
345
|
-
}
|
|
382
|
+
}
|