native-update-capacitor 0.0.0-stage → 4.4.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 +578 -0
- package/LICENSE +25 -0
- package/NativeUpdate.podspec +19 -0
- package/README.md +574 -2
- package/android/build.gradle +125 -0
- package/android/consumer-rules.pro +9 -0
- package/android/gradle/wrapper/gradle-wrapper.properties +8 -0
- package/android/gradle.properties +17 -0
- package/android/manifest-additions.xml +28 -0
- package/android/proguard-rules.pro +26 -0
- package/android/settings.gradle +2 -0
- package/android/src/main/AndroidManifest.xml +13 -0
- package/android/src/main/java/com/aoneahsan/nativeupdate/AppReviewPlugin.kt +159 -0
- package/android/src/main/java/com/aoneahsan/nativeupdate/AppUpdatePlugin.kt +314 -0
- package/android/src/main/java/com/aoneahsan/nativeupdate/BackgroundNotificationManager.kt +406 -0
- package/android/src/main/java/com/aoneahsan/nativeupdate/BackgroundUpdateManager.kt +48 -0
- package/android/src/main/java/com/aoneahsan/nativeupdate/BackgroundUpdatePlugin.kt +343 -0
- package/android/src/main/java/com/aoneahsan/nativeupdate/BackgroundUpdateWorker.kt +236 -0
- package/android/src/main/java/com/aoneahsan/nativeupdate/JsonOptional.kt +26 -0
- package/android/src/main/java/com/aoneahsan/nativeupdate/LiveUpdatePlugin.kt +1249 -0
- package/android/src/main/java/com/aoneahsan/nativeupdate/NativeUpdatePlugin.kt +404 -0
- package/android/src/main/java/com/aoneahsan/nativeupdate/NotificationActionReceiver.kt +107 -0
- package/android/src/main/java/com/aoneahsan/nativeupdate/SecurityManager.kt +240 -0
- package/android/variables.gradle +5 -0
- package/cli/commands/backend-create.js +623 -0
- package/cli/commands/bundle-create.js +145 -0
- package/cli/commands/bundle-sign.js +64 -0
- package/cli/commands/bundle-verify.js +58 -0
- package/cli/commands/flutter-patch.js +107 -0
- package/cli/commands/init.js +146 -0
- package/cli/commands/keys-generate.js +58 -0
- package/cli/commands/monitor.js +89 -0
- package/cli/commands/remote-shared.js +264 -0
- package/cli/commands/remote.js +634 -0
- package/cli/commands/server-start.js +96 -0
- package/cli/index.js +625 -0
- package/cli/package.json +21 -0
- package/dist/esm/app-review/app-review-manager.d.ts +24 -0
- package/dist/esm/app-review/app-review-manager.js +194 -0
- package/dist/esm/app-review/index.d.ts +5 -0
- package/dist/esm/app-review/index.js +5 -0
- package/dist/esm/app-review/platform-review-handler.d.ts +20 -0
- package/dist/esm/app-review/platform-review-handler.js +137 -0
- package/dist/esm/app-review/review-conditions-checker.d.ts +22 -0
- package/dist/esm/app-review/review-conditions-checker.js +154 -0
- package/dist/esm/app-review/review-rate-limiter.d.ts +23 -0
- package/dist/esm/app-review/review-rate-limiter.js +163 -0
- package/dist/esm/app-review/types.d.ts +41 -0
- package/dist/esm/app-review/types.js +1 -0
- package/dist/esm/app-update/app-update-checker.d.ts +58 -0
- package/dist/esm/app-update/app-update-checker.js +227 -0
- package/dist/esm/app-update/app-update-installer.d.ts +20 -0
- package/dist/esm/app-update/app-update-installer.js +134 -0
- package/dist/esm/app-update/app-update-manager.d.ts +28 -0
- package/dist/esm/app-update/app-update-manager.js +198 -0
- package/dist/esm/app-update/app-update-notifier.d.ts +14 -0
- package/dist/esm/app-update/app-update-notifier.js +99 -0
- package/dist/esm/app-update/index.d.ts +6 -0
- package/dist/esm/app-update/index.js +6 -0
- package/dist/esm/app-update/platform-app-update.d.ts +19 -0
- package/dist/esm/app-update/platform-app-update.js +138 -0
- package/dist/esm/app-update/types.d.ts +58 -0
- package/dist/esm/app-update/types.js +11 -0
- package/dist/esm/background-update/background-scheduler.d.ts +63 -0
- package/dist/esm/background-update/background-scheduler.js +332 -0
- package/dist/esm/background-update/index.d.ts +3 -0
- package/dist/esm/background-update/index.js +2 -0
- package/dist/esm/background-update/notification-manager.d.ts +29 -0
- package/dist/esm/background-update/notification-manager.js +88 -0
- package/dist/esm/config/support.d.ts +9 -0
- package/dist/esm/config/support.js +9 -0
- package/dist/esm/core/analytics.d.ts +83 -0
- package/dist/esm/core/analytics.js +151 -0
- package/dist/esm/core/cache-manager.d.ts +72 -0
- package/dist/esm/core/cache-manager.js +274 -0
- package/dist/esm/core/config.d.ts +69 -0
- package/dist/esm/core/config.js +107 -0
- package/dist/esm/core/errors.d.ts +56 -0
- package/dist/esm/core/errors.js +85 -0
- package/dist/esm/core/event-emitter.d.ts +30 -0
- package/dist/esm/core/event-emitter.js +80 -0
- package/dist/esm/core/logger.d.ts +21 -0
- package/dist/esm/core/logger.js +108 -0
- package/dist/esm/core/performance.d.ts +53 -0
- package/dist/esm/core/performance.js +153 -0
- package/dist/esm/core/plugin-manager.d.ts +88 -0
- package/dist/esm/core/plugin-manager.js +224 -0
- package/dist/esm/core/security.d.ts +123 -0
- package/dist/esm/core/security.js +361 -0
- package/dist/esm/core/update-check.d.ts +121 -0
- package/dist/esm/core/update-check.js +127 -0
- package/dist/esm/definitions.d.ts +872 -0
- package/dist/esm/definitions.js +104 -0
- package/dist/esm/index.d.ts +14 -0
- package/dist/esm/index.js +18 -0
- package/dist/esm/live-update/bundle-manager.d.ts +102 -0
- package/dist/esm/live-update/bundle-manager.js +344 -0
- package/dist/esm/live-update/certificate-pinning.d.ts +38 -0
- package/dist/esm/live-update/certificate-pinning.js +77 -0
- package/dist/esm/live-update/delta-processor.d.ts +84 -0
- package/dist/esm/live-update/delta-processor.js +257 -0
- package/dist/esm/live-update/download-manager.d.ts +106 -0
- package/dist/esm/live-update/download-manager.js +412 -0
- package/dist/esm/live-update/rollout-checker.d.ts +86 -0
- package/dist/esm/live-update/rollout-checker.js +304 -0
- package/dist/esm/live-update/update-manager.d.ts +63 -0
- package/dist/esm/live-update/update-manager.js +321 -0
- package/dist/esm/live-update/version-manager.d.ts +93 -0
- package/dist/esm/live-update/version-manager.js +390 -0
- package/dist/esm/plugin.d.ts +3 -0
- package/dist/esm/plugin.js +848 -0
- package/dist/esm/security/crypto.d.ts +88 -0
- package/dist/esm/security/crypto.js +228 -0
- package/dist/esm/security/validator.d.ts +60 -0
- package/dist/esm/security/validator.js +142 -0
- package/dist/esm/types/app-identity.d.ts +36 -0
- package/dist/esm/types/app-identity.js +1 -0
- package/dist/esm/types/rollout.d.ts +91 -0
- package/dist/esm/types/rollout.js +25 -0
- package/dist/esm/web.d.ts +130 -0
- package/dist/esm/web.js +981 -0
- package/dist/plugin.cjs +1 -0
- package/dist/plugin.esm.js +1 -0
- package/dist/plugin.js +2 -0
- package/docs/CHANGELOG.md +1016 -0
- package/docs/KNOWN_LIMITATIONS.md +240 -0
- package/docs/MIGRATION.md +391 -0
- package/docs/protocol/CLIENT-PROTOCOL.md +199 -0
- package/docs/server-requirements.md +211 -0
- package/ios/Plugin/AppReview/AppReviewPlugin.swift +158 -0
- package/ios/Plugin/AppUpdate/AppUpdatePlugin.swift +249 -0
- package/ios/Plugin/BackgroundUpdate/BackgroundNotificationManager.swift +333 -0
- package/ios/Plugin/BackgroundUpdate/BackgroundUpdatePlugin.swift +464 -0
- package/ios/Plugin/Info.plist +43 -0
- package/ios/Plugin/LiveUpdate/LiveUpdatePlugin.swift +1286 -0
- package/ios/Plugin/LiveUpdate/WebViewConfiguration.swift +45 -0
- package/ios/Plugin/NativeUpdatePlugin.m +53 -0
- package/ios/Plugin/NativeUpdatePlugin.swift +315 -0
- package/ios/Plugin/Security/SecurityManager.swift +289 -0
- package/package.json +133 -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
|
+
}
|