native-update-capacitor 0.0.0-stage → 4.3.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.
Files changed (139) hide show
  1. package/AI-INTEGRATION-GUIDE.md +578 -0
  2. package/LICENSE +25 -0
  3. package/NativeUpdate.podspec +19 -0
  4. package/README.md +574 -2
  5. package/android/build.gradle +125 -0
  6. package/android/consumer-rules.pro +9 -0
  7. package/android/gradle/wrapper/gradle-wrapper.properties +8 -0
  8. package/android/gradle.properties +17 -0
  9. package/android/manifest-additions.xml +28 -0
  10. package/android/proguard-rules.pro +26 -0
  11. package/android/settings.gradle +2 -0
  12. package/android/src/main/AndroidManifest.xml +13 -0
  13. package/android/src/main/java/com/aoneahsan/nativeupdate/AppReviewPlugin.kt +159 -0
  14. package/android/src/main/java/com/aoneahsan/nativeupdate/AppUpdatePlugin.kt +314 -0
  15. package/android/src/main/java/com/aoneahsan/nativeupdate/BackgroundNotificationManager.kt +406 -0
  16. package/android/src/main/java/com/aoneahsan/nativeupdate/BackgroundUpdateManager.kt +48 -0
  17. package/android/src/main/java/com/aoneahsan/nativeupdate/BackgroundUpdatePlugin.kt +343 -0
  18. package/android/src/main/java/com/aoneahsan/nativeupdate/BackgroundUpdateWorker.kt +236 -0
  19. package/android/src/main/java/com/aoneahsan/nativeupdate/JsonOptional.kt +26 -0
  20. package/android/src/main/java/com/aoneahsan/nativeupdate/LiveUpdatePlugin.kt +1249 -0
  21. package/android/src/main/java/com/aoneahsan/nativeupdate/NativeUpdatePlugin.kt +404 -0
  22. package/android/src/main/java/com/aoneahsan/nativeupdate/NotificationActionReceiver.kt +107 -0
  23. package/android/src/main/java/com/aoneahsan/nativeupdate/SecurityManager.kt +240 -0
  24. package/android/variables.gradle +5 -0
  25. package/cli/commands/backend-create.js +623 -0
  26. package/cli/commands/bundle-create.js +145 -0
  27. package/cli/commands/bundle-sign.js +64 -0
  28. package/cli/commands/bundle-verify.js +58 -0
  29. package/cli/commands/init.js +146 -0
  30. package/cli/commands/keys-generate.js +58 -0
  31. package/cli/commands/monitor.js +89 -0
  32. package/cli/commands/remote-shared.js +262 -0
  33. package/cli/commands/remote.js +622 -0
  34. package/cli/commands/server-start.js +96 -0
  35. package/cli/index.js +605 -0
  36. package/cli/package.json +21 -0
  37. package/dist/esm/app-review/app-review-manager.d.ts +24 -0
  38. package/dist/esm/app-review/app-review-manager.js +194 -0
  39. package/dist/esm/app-review/index.d.ts +5 -0
  40. package/dist/esm/app-review/index.js +5 -0
  41. package/dist/esm/app-review/platform-review-handler.d.ts +20 -0
  42. package/dist/esm/app-review/platform-review-handler.js +137 -0
  43. package/dist/esm/app-review/review-conditions-checker.d.ts +22 -0
  44. package/dist/esm/app-review/review-conditions-checker.js +154 -0
  45. package/dist/esm/app-review/review-rate-limiter.d.ts +23 -0
  46. package/dist/esm/app-review/review-rate-limiter.js +163 -0
  47. package/dist/esm/app-review/types.d.ts +41 -0
  48. package/dist/esm/app-review/types.js +1 -0
  49. package/dist/esm/app-update/app-update-checker.d.ts +58 -0
  50. package/dist/esm/app-update/app-update-checker.js +227 -0
  51. package/dist/esm/app-update/app-update-installer.d.ts +20 -0
  52. package/dist/esm/app-update/app-update-installer.js +134 -0
  53. package/dist/esm/app-update/app-update-manager.d.ts +28 -0
  54. package/dist/esm/app-update/app-update-manager.js +198 -0
  55. package/dist/esm/app-update/app-update-notifier.d.ts +14 -0
  56. package/dist/esm/app-update/app-update-notifier.js +99 -0
  57. package/dist/esm/app-update/index.d.ts +6 -0
  58. package/dist/esm/app-update/index.js +6 -0
  59. package/dist/esm/app-update/platform-app-update.d.ts +19 -0
  60. package/dist/esm/app-update/platform-app-update.js +138 -0
  61. package/dist/esm/app-update/types.d.ts +58 -0
  62. package/dist/esm/app-update/types.js +11 -0
  63. package/dist/esm/background-update/background-scheduler.d.ts +63 -0
  64. package/dist/esm/background-update/background-scheduler.js +332 -0
  65. package/dist/esm/background-update/index.d.ts +3 -0
  66. package/dist/esm/background-update/index.js +2 -0
  67. package/dist/esm/background-update/notification-manager.d.ts +29 -0
  68. package/dist/esm/background-update/notification-manager.js +88 -0
  69. package/dist/esm/config/support.d.ts +9 -0
  70. package/dist/esm/config/support.js +9 -0
  71. package/dist/esm/core/analytics.d.ts +83 -0
  72. package/dist/esm/core/analytics.js +151 -0
  73. package/dist/esm/core/cache-manager.d.ts +72 -0
  74. package/dist/esm/core/cache-manager.js +274 -0
  75. package/dist/esm/core/config.d.ts +69 -0
  76. package/dist/esm/core/config.js +107 -0
  77. package/dist/esm/core/errors.d.ts +56 -0
  78. package/dist/esm/core/errors.js +85 -0
  79. package/dist/esm/core/event-emitter.d.ts +30 -0
  80. package/dist/esm/core/event-emitter.js +80 -0
  81. package/dist/esm/core/logger.d.ts +21 -0
  82. package/dist/esm/core/logger.js +108 -0
  83. package/dist/esm/core/performance.d.ts +53 -0
  84. package/dist/esm/core/performance.js +153 -0
  85. package/dist/esm/core/plugin-manager.d.ts +88 -0
  86. package/dist/esm/core/plugin-manager.js +224 -0
  87. package/dist/esm/core/security.d.ts +123 -0
  88. package/dist/esm/core/security.js +361 -0
  89. package/dist/esm/core/update-check.d.ts +121 -0
  90. package/dist/esm/core/update-check.js +127 -0
  91. package/dist/esm/definitions.d.ts +872 -0
  92. package/dist/esm/definitions.js +104 -0
  93. package/dist/esm/index.d.ts +14 -0
  94. package/dist/esm/index.js +18 -0
  95. package/dist/esm/live-update/bundle-manager.d.ts +102 -0
  96. package/dist/esm/live-update/bundle-manager.js +344 -0
  97. package/dist/esm/live-update/certificate-pinning.d.ts +38 -0
  98. package/dist/esm/live-update/certificate-pinning.js +77 -0
  99. package/dist/esm/live-update/delta-processor.d.ts +84 -0
  100. package/dist/esm/live-update/delta-processor.js +257 -0
  101. package/dist/esm/live-update/download-manager.d.ts +106 -0
  102. package/dist/esm/live-update/download-manager.js +412 -0
  103. package/dist/esm/live-update/rollout-checker.d.ts +86 -0
  104. package/dist/esm/live-update/rollout-checker.js +304 -0
  105. package/dist/esm/live-update/update-manager.d.ts +63 -0
  106. package/dist/esm/live-update/update-manager.js +321 -0
  107. package/dist/esm/live-update/version-manager.d.ts +93 -0
  108. package/dist/esm/live-update/version-manager.js +390 -0
  109. package/dist/esm/plugin.d.ts +3 -0
  110. package/dist/esm/plugin.js +848 -0
  111. package/dist/esm/security/crypto.d.ts +88 -0
  112. package/dist/esm/security/crypto.js +228 -0
  113. package/dist/esm/security/validator.d.ts +60 -0
  114. package/dist/esm/security/validator.js +142 -0
  115. package/dist/esm/types/app-identity.d.ts +36 -0
  116. package/dist/esm/types/app-identity.js +1 -0
  117. package/dist/esm/types/rollout.d.ts +91 -0
  118. package/dist/esm/types/rollout.js +25 -0
  119. package/dist/esm/web.d.ts +130 -0
  120. package/dist/esm/web.js +981 -0
  121. package/dist/plugin.cjs +1 -0
  122. package/dist/plugin.esm.js +1 -0
  123. package/dist/plugin.js +2 -0
  124. package/docs/CHANGELOG.md +993 -0
  125. package/docs/KNOWN_LIMITATIONS.md +235 -0
  126. package/docs/MIGRATION.md +391 -0
  127. package/docs/protocol/CLIENT-PROTOCOL.md +178 -0
  128. package/docs/server-requirements.md +211 -0
  129. package/ios/Plugin/AppReview/AppReviewPlugin.swift +158 -0
  130. package/ios/Plugin/AppUpdate/AppUpdatePlugin.swift +249 -0
  131. package/ios/Plugin/BackgroundUpdate/BackgroundNotificationManager.swift +333 -0
  132. package/ios/Plugin/BackgroundUpdate/BackgroundUpdatePlugin.swift +464 -0
  133. package/ios/Plugin/Info.plist +43 -0
  134. package/ios/Plugin/LiveUpdate/LiveUpdatePlugin.swift +1286 -0
  135. package/ios/Plugin/LiveUpdate/WebViewConfiguration.swift +45 -0
  136. package/ios/Plugin/NativeUpdatePlugin.m +53 -0
  137. package/ios/Plugin/NativeUpdatePlugin.swift +315 -0
  138. package/ios/Plugin/Security/SecurityManager.swift +289 -0
  139. package/package.json +135 -4
@@ -0,0 +1,240 @@
1
+ package com.aoneahsan.nativeupdate
2
+
3
+ import android.content.Context
4
+ import android.net.Uri
5
+ import android.security.keystore.KeyGenParameterSpec
6
+ import android.security.keystore.KeyProperties
7
+ import android.util.Base64
8
+ import com.getcapacitor.JSArray
9
+ import com.getcapacitor.JSObject
10
+ import okhttp3.CertificatePinner
11
+ import java.io.File
12
+ import java.security.KeyFactory
13
+ import java.security.KeyStore
14
+ import java.security.MessageDigest
15
+ import java.security.Signature
16
+ import java.security.spec.X509EncodedKeySpec
17
+ import javax.crypto.Cipher
18
+ import javax.crypto.KeyGenerator
19
+ import javax.crypto.SecretKey
20
+ import javax.crypto.spec.GCMParameterSpec
21
+
22
+ /** Security primitives shared by the Android update client. */
23
+ class SecurityManager(private val context: Context) {
24
+ private var config: JSObject? = null
25
+ private val securePrefs = context.getSharedPreferences(SECURE_PREFS, Context.MODE_PRIVATE)
26
+
27
+ companion object {
28
+ private const val SECURE_PREFS = "native_update_secure_v2"
29
+ private const val KEY_ALIAS = "native_update_aes_gcm_v2"
30
+ private const val ANDROID_KEYSTORE = "AndroidKeyStore"
31
+ private const val AES_TRANSFORMATION = "AES/GCM/NoPadding"
32
+ private const val GCM_IV_BYTES = 12
33
+ private const val GCM_TAG_BITS = 128
34
+ private val VALID_PIN = Regex("^sha256/[A-Za-z0-9+/]{43}=$")
35
+ }
36
+
37
+ fun configure(config: JSObject) {
38
+ this.config = config
39
+ val security = securityConfig()
40
+ require(security?.optBoolean("enforceHttps", true) != false) {
41
+ "HTTPS enforcement cannot be disabled"
42
+ }
43
+
44
+ val pinning = security?.takeIf { it.has("certificatePinning") }?.getJSObject("certificatePinning")
45
+ if (pinning?.getBool("enabled") == true) {
46
+ val pins = pinning.arrayOrNull("pins")
47
+ require(pins != null && pins.length() > 0) {
48
+ "Certificate pinning requires at least one host pin"
49
+ }
50
+ for (index in 0 until pins.length()) {
51
+ val pin = pins.getJSONObject(index)
52
+ require(!pin.optString("hostname").isNullOrBlank()) {
53
+ "Certificate pin hostname is required"
54
+ }
55
+ val hashes = pin.optJSONArray("sha256")
56
+ require(hashes != null && hashes.length() > 0) {
57
+ "Certificate pin requires at least one SHA-256 hash"
58
+ }
59
+ for (hashIndex in 0 until hashes.length()) {
60
+ require(VALID_PIN.matches(hashes.getString(hashIndex))) {
61
+ "Certificate pins must use sha256/<base64> format"
62
+ }
63
+ }
64
+ }
65
+ }
66
+ }
67
+
68
+ fun getSecurityInfo(): JSObject {
69
+ val security = securityConfig()
70
+ val result = JSObject()
71
+ result.put("enforceHttps", true)
72
+
73
+ val certificatePinning = JSObject()
74
+ val pinningConfig = security?.takeIf { it.has("certificatePinning") }?.getJSObject("certificatePinning")
75
+ certificatePinning.put("enabled", pinningConfig?.getBool("enabled") ?: false)
76
+ val pins = mutableListOf<JSObject>()
77
+ val pinsArray = pinningConfig?.arrayOrNull("pins")
78
+ if (pinsArray != null) {
79
+ for (index in 0 until pinsArray.length()) {
80
+ val pin = pinsArray.getJSONObject(index)
81
+ pins.add(JSObject().apply {
82
+ put("hostname", pin.getString("hostname"))
83
+ put("sha256", pin.arrayOrNull("sha256") ?: org.json.JSONArray())
84
+ })
85
+ }
86
+ }
87
+ certificatePinning.put("pins", JSArray(pins))
88
+ result.put("certificatePinning", certificatePinning)
89
+ result.put("validateInputs", security?.optBoolean("validateInputs", true) ?: true)
90
+ result.put("secureStorage", true)
91
+ result.put("signatureAlgorithm", "RSA-SHA256")
92
+ return result
93
+ }
94
+
95
+ fun validateUrl(url: String): Boolean {
96
+ val uri = try {
97
+ Uri.parse(url)
98
+ } catch (_: Exception) {
99
+ return false
100
+ }
101
+ if (!uri.scheme.equals("https", ignoreCase = true) || uri.host.isNullOrBlank() || uri.userInfo != null) {
102
+ return false
103
+ }
104
+ val allowedHosts = getAllowedHosts()
105
+ return allowedHosts.isEmpty() || allowedHosts.any { it.equals(uri.host, ignoreCase = true) }
106
+ }
107
+
108
+ fun getCertificatePinner(): CertificatePinner? {
109
+ val pinning = securityConfig()?.getJSObject("certificatePinning") ?: return null
110
+ if (pinning.getBool("enabled") != true) return null
111
+ val pins = pinning.arrayOrNull("pins") ?: return null
112
+ val builder = CertificatePinner.Builder()
113
+ for (index in 0 until pins.length()) {
114
+ val pin = pins.getJSONObject(index)
115
+ val hostname = pin.getString("hostname") ?: continue
116
+ val hashes = pin.arrayOrNull("sha256") ?: continue
117
+ for (hashIndex in 0 until hashes.length()) {
118
+ builder.add(hostname, hashes.getString(hashIndex))
119
+ }
120
+ }
121
+ return builder.build()
122
+ }
123
+
124
+ /** Verify the backend's RSA-SHA256 (PKCS#1 v1.5) signature without loading the bundle into memory. */
125
+ fun verifySignature(file: File, signature: String, publicKeyString: String): Boolean {
126
+ return try {
127
+ val publicKeyBase64 = publicKeyString
128
+ .replace("-----BEGIN PUBLIC KEY-----", "")
129
+ .replace("-----END PUBLIC KEY-----", "")
130
+ .replace(Regex("\\s"), "")
131
+ val publicKey = KeyFactory.getInstance("RSA").generatePublic(
132
+ X509EncodedKeySpec(Base64.decode(publicKeyBase64, Base64.DEFAULT))
133
+ )
134
+ val verifier = Signature.getInstance("SHA256withRSA")
135
+ verifier.initVerify(publicKey)
136
+ file.inputStream().use { input ->
137
+ val buffer = ByteArray(DEFAULT_BUFFER_SIZE)
138
+ while (true) {
139
+ val read = input.read(buffer)
140
+ if (read < 0) break
141
+ verifier.update(buffer, 0, read)
142
+ }
143
+ }
144
+ verifier.verify(Base64.decode(signature, Base64.DEFAULT))
145
+ } catch (_: Exception) {
146
+ false
147
+ }
148
+ }
149
+
150
+ fun calculateChecksum(file: File, algorithm: String = "SHA-256"): String {
151
+ val digest = MessageDigest.getInstance(algorithm)
152
+ file.inputStream().use { input ->
153
+ val buffer = ByteArray(DEFAULT_BUFFER_SIZE)
154
+ while (true) {
155
+ val read = input.read(buffer)
156
+ if (read < 0) break
157
+ digest.update(buffer, 0, read)
158
+ }
159
+ }
160
+ return digest.digest().joinToString("") { "%02x".format(it.toInt() and 0xff) }
161
+ }
162
+
163
+ /** Values are encrypted by a non-exportable AES-GCM key held in AndroidKeyStore. */
164
+ fun saveSecureData(key: String, value: String) {
165
+ val cipher = Cipher.getInstance(AES_TRANSFORMATION)
166
+ cipher.init(Cipher.ENCRYPT_MODE, getOrCreateSecretKey())
167
+ val encrypted = cipher.doFinal(value.toByteArray(Charsets.UTF_8))
168
+ require(cipher.iv.size == GCM_IV_BYTES)
169
+ val payload = cipher.iv + encrypted
170
+ securePrefs.edit().putString(key, Base64.encodeToString(payload, Base64.NO_WRAP)).apply()
171
+ }
172
+
173
+ fun getSecureData(key: String): String? {
174
+ val encoded = securePrefs.getString(key, null) ?: return null
175
+ return try {
176
+ val payload = Base64.decode(encoded, Base64.NO_WRAP)
177
+ if (payload.size <= GCM_IV_BYTES) return null
178
+ val iv = payload.copyOfRange(0, GCM_IV_BYTES)
179
+ val encrypted = payload.copyOfRange(GCM_IV_BYTES, payload.size)
180
+ val cipher = Cipher.getInstance(AES_TRANSFORMATION)
181
+ cipher.init(Cipher.DECRYPT_MODE, getOrCreateSecretKey(), GCMParameterSpec(GCM_TAG_BITS, iv))
182
+ String(cipher.doFinal(encrypted), Charsets.UTF_8)
183
+ } catch (_: Exception) {
184
+ null
185
+ }
186
+ }
187
+
188
+ fun validatePath(path: String): Boolean {
189
+ return try {
190
+ val candidate = File(path).canonicalFile
191
+ val root = context.filesDir.canonicalFile
192
+ candidate == root || candidate.path.startsWith(root.path + File.separator)
193
+ } catch (_: Exception) {
194
+ false
195
+ }
196
+ }
197
+
198
+ fun sanitizeInput(input: String): String = input.replace(Regex("[^a-zA-Z0-9._-]"), "")
199
+ fun isHttpsEnforced(): Boolean = true
200
+ fun isSecureStorageEnabled(): Boolean = true
201
+ fun isInputValidationEnabled(): Boolean = securityConfig()?.optBoolean("validateInputs", true) ?: true
202
+
203
+ fun logSecurityEvent(event: String, details: String? = null) {
204
+ if (securityConfig()?.optBoolean("logSecurityEvents", false) == true) {
205
+ android.util.Log.i("NativeUpdateSecurity", "$event ${details ?: ""}".trim())
206
+ }
207
+ }
208
+
209
+ private fun securityConfig(): JSObject? = config?.let {
210
+ if (it.has("security")) it.getJSObject("security") else it
211
+ }
212
+
213
+ private fun getAllowedHosts(): List<String> {
214
+ val hosts = mutableListOf<String>()
215
+ val allowed = config?.optJSONArray("allowedHosts")
216
+ ?: config?.optJSONObject("liveUpdate")?.optJSONArray("allowedHosts")
217
+ if (allowed != null) {
218
+ for (index in 0 until allowed.length()) hosts.add(allowed.getString(index))
219
+ }
220
+ return hosts.map { it.lowercase() }.distinct()
221
+ }
222
+
223
+ private fun getOrCreateSecretKey(): SecretKey {
224
+ val keyStore = KeyStore.getInstance(ANDROID_KEYSTORE).apply { load(null) }
225
+ (keyStore.getKey(KEY_ALIAS, null) as? SecretKey)?.let { return it }
226
+ val generator = KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES, ANDROID_KEYSTORE)
227
+ generator.init(
228
+ KeyGenParameterSpec.Builder(
229
+ KEY_ALIAS,
230
+ KeyProperties.PURPOSE_ENCRYPT or KeyProperties.PURPOSE_DECRYPT
231
+ )
232
+ .setBlockModes(KeyProperties.BLOCK_MODE_GCM)
233
+ .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_NONE)
234
+ .setKeySize(256)
235
+ .setRandomizedEncryptionRequired(true)
236
+ .build()
237
+ )
238
+ return generator.generateKey()
239
+ }
240
+ }
@@ -0,0 +1,5 @@
1
+ ext {
2
+ kotlinVersion = project.hasProperty('kotlinVersion') ? rootProject.ext.kotlinVersion : '2.2.20'
3
+ playAppUpdateVersion = '2.1.0'
4
+ playReviewVersion = '2.0.1'
5
+ }