@capgo/capacitor-updater 7.51.21 → 7.53.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 (29) hide show
  1. package/CapgoCapacitorUpdater.podspec +0 -3
  2. package/Package.swift +3 -11
  3. package/README.md +202 -67
  4. package/android/build.gradle +2 -3
  5. package/android/src/main/java/ee/forgr/capacitor_updater/CapacitorUpdaterPlugin.java +610 -177
  6. package/android/src/main/java/ee/forgr/capacitor_updater/CapgoUpdater.java +909 -196
  7. package/android/src/main/java/ee/forgr/capacitor_updater/CryptoCipher.java +53 -19
  8. package/android/src/main/java/ee/forgr/capacitor_updater/DelayUpdateUtils.java +226 -106
  9. package/android/src/main/java/ee/forgr/capacitor_updater/DownloadService.java +210 -58
  10. package/android/src/main/java/ee/forgr/capacitor_updater/HeadlessUpdateWorker.java +325 -0
  11. package/android/src/main/java/ee/forgr/capacitor_updater/Logger.java +63 -4
  12. package/android/src/main/java/ee/forgr/capacitor_updater/NativeUpdatePrompt.java +303 -0
  13. package/android/src/main/java/ee/forgr/capacitor_updater/Version.java +306 -0
  14. package/dist/docs.json +394 -27
  15. package/dist/esm/definitions.d.ts +220 -7
  16. package/dist/esm/definitions.js.map +1 -1
  17. package/ios/Sources/CapacitorUpdaterPlugin/AES.swift +104 -52
  18. package/ios/Sources/CapacitorUpdaterPlugin/CapacitorUpdaterPlugin.swift +562 -187
  19. package/ios/Sources/CapacitorUpdaterPlugin/CapgoSemanticVersion.swift +162 -0
  20. package/ios/Sources/CapacitorUpdaterPlugin/CapgoUpdater.swift +564 -109
  21. package/ios/Sources/CapacitorUpdaterPlugin/CryptoCipher.swift +11 -2
  22. package/ios/Sources/CapacitorUpdaterPlugin/DelayUpdateUtils.swift +204 -93
  23. package/ios/Sources/CapacitorUpdaterPlugin/Logger.swift +75 -5
  24. package/ios/Sources/CapacitorUpdaterPlugin/NativeUpdatePrompt.swift +183 -0
  25. package/ios/Sources/CapacitorUpdaterPlugin/RedirectPolicyDelegate.swift +53 -0
  26. package/ios/Sources/CapacitorUpdaterPlugin/ShakeMenu.swift +9 -3
  27. package/ios/Sources/CapacitorUpdaterPlugin/ZipArchiveReader.swift +247 -0
  28. package/ios/Sources/CapacitorUpdaterPlugin/ZipCentralDirectory.swift +280 -0
  29. package/package.json +7 -3
@@ -50,17 +50,15 @@ public class CryptoCipher {
50
50
  return decryptedBytes;
51
51
  }
52
52
 
53
- public static byte[] decryptAES(byte[] cipherText, SecretKey key, byte[] iv) {
54
- try {
55
- IvParameterSpec ivParameterSpec = new IvParameterSpec(iv);
56
- Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
57
- SecretKeySpec keySpec = new SecretKeySpec(key.getEncoded(), "AES");
58
- cipher.init(Cipher.DECRYPT_MODE, keySpec, ivParameterSpec);
59
- return cipher.doFinal(cipherText);
60
- } catch (Exception e) {
61
- e.printStackTrace();
53
+ public static byte[] decryptAES(byte[] cipherText, SecretKey key, byte[] iv) throws GeneralSecurityException {
54
+ if (key == null || iv == null) {
55
+ throw new IllegalArgumentException("AES key and IV must not be null");
62
56
  }
63
- return null;
57
+ IvParameterSpec ivParameterSpec = new IvParameterSpec(iv);
58
+ Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
59
+ SecretKeySpec keySpec = new SecretKeySpec(key.getEncoded(), "AES");
60
+ cipher.init(Cipher.DECRYPT_MODE, keySpec, ivParameterSpec);
61
+ return cipher.doFinal(cipherText);
64
62
  }
65
63
 
66
64
  public static SecretKey byteToSessionKey(byte[] sessionKey) {
@@ -137,14 +135,27 @@ public class CryptoCipher {
137
135
  return buf;
138
136
  }
139
137
 
138
+ public static boolean isValidSessionKey(final String sessionKey) {
139
+ if (sessionKey == null || sessionKey.isEmpty()) {
140
+ return false;
141
+ }
142
+ String[] sessionKeyParts = sessionKey.split(":", -1);
143
+ return sessionKeyParts.length == 2 && !sessionKeyParts[0].isEmpty() && !sessionKeyParts[1].isEmpty();
144
+ }
145
+
140
146
  public static void decryptFile(final File file, final String publicKey, final String ivSessionKey) throws IOException {
141
- if (publicKey.isEmpty() || ivSessionKey == null || ivSessionKey.isEmpty() || ivSessionKey.split(":").length != 2) {
142
- logger.info("Encryption not set, no public key or session, ignored");
147
+ if (publicKey.isEmpty() || !isValidSessionKey(ivSessionKey)) {
148
+ if (logger != null) {
149
+ logger.info("Encryption not set, no public key or session, ignored");
150
+ }
143
151
  return;
144
152
  }
153
+ // The bundle is encrypted from here on: an unusable key must fail, never leave it encrypted.
145
154
  if (!publicKey.startsWith("-----BEGIN RSA PUBLIC KEY-----")) {
146
- logger.error("The public key is not a valid RSA Public key");
147
- return;
155
+ if (logger != null) {
156
+ logger.error("The public key is not a valid RSA Public key");
157
+ }
158
+ throw new IOException("AES file decryption failed: the public key is not a valid RSA public key");
148
159
  }
149
160
 
150
161
  try {
@@ -152,29 +163,50 @@ public class CryptoCipher {
152
163
  String sessionKeyB64 = ivSessionKey.split(":")[1];
153
164
  byte[] iv = Base64.decode(ivB64.getBytes(), Base64.DEFAULT);
154
165
  byte[] sessionKey = Base64.decode(sessionKeyB64.getBytes(), Base64.DEFAULT);
166
+ if (iv.length != 16) {
167
+ throw new IOException("AES file decryption failed: IV must be 16 bytes");
168
+ }
155
169
  PublicKey pKey = CryptoCipher.stringToPublicKey(publicKey);
156
170
  byte[] decryptedSessionKey = CryptoCipher.decryptRSA(sessionKey, pKey);
171
+ if (decryptedSessionKey == null || decryptedSessionKey.length != 16) {
172
+ throw new IOException("AES file decryption failed: decrypted session key must be 16 bytes");
173
+ }
157
174
 
158
175
  SecretKey sKey = CryptoCipher.byteToSessionKey(decryptedSessionKey);
159
176
  decryptAesFile(file, sKey, iv);
160
177
  } catch (GeneralSecurityException e) {
161
- logger.info("decryptFile fail");
162
- throw new IOException("GeneralSecurityException", e);
178
+ if (logger != null) {
179
+ logger.info("decryptFile fail");
180
+ }
181
+ throw new IOException("AES file decryption failed: " + e.getMessage(), e);
163
182
  }
164
183
  }
165
184
 
166
- static void decryptAesFile(File file, SecretKey key, byte[] iv) throws IOException, GeneralSecurityException {
185
+ static void decryptAesFile(File file, SecretKey key, byte[] iv) throws IOException {
186
+ if (key == null) {
187
+ throw new IOException("AES file decryption failed: missing session key");
188
+ }
189
+ if (iv == null) {
190
+ throw new IOException("AES file decryption failed: missing IV");
191
+ }
192
+ if (iv.length != 16) {
193
+ throw new IOException("AES file decryption failed: IV must be 16 bytes");
194
+ }
195
+ byte[] keyBytes = key.getEncoded();
196
+ if (keyBytes == null || keyBytes.length != 16) {
197
+ throw new IOException("AES file decryption failed: session key must be 16 bytes");
198
+ }
167
199
  if (file.length() == 0) {
168
200
  throw new IOException("Empty encrypted data");
169
201
  }
170
- Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
171
- cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key.getEncoded(), "AES"), new IvParameterSpec(iv));
172
202
  File parent = file.getAbsoluteFile().getParentFile();
173
203
  if (parent == null) {
174
204
  throw new IOException("Cannot create temp file for " + file.getAbsolutePath());
175
205
  }
176
206
  File tempFile = File.createTempFile("capgo-aes-", ".tmp", parent);
177
207
  try {
208
+ Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
209
+ cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(keyBytes, "AES"), new IvParameterSpec(iv));
178
210
  byte[] inBuf = new byte[ioBufferBytes()];
179
211
  // Reuse one output buffer. cipher.update(in) allocates a new byte[] per chunk.
180
212
  byte[] outBuf = new byte[inBuf.length + 16];
@@ -196,6 +228,8 @@ public class CryptoCipher {
196
228
  }
197
229
  replaceFile(tempFile, file);
198
230
  tempFile = null;
231
+ } catch (GeneralSecurityException e) {
232
+ throw new IOException("AES file decryption failed: " + e.getMessage(), e);
199
233
  } finally {
200
234
  if (tempFile != null && tempFile.exists()) {
201
235
  tempFile.delete();
@@ -1,7 +1,6 @@
1
1
  package ee.forgr.capacitor_updater;
2
2
 
3
3
  import android.content.SharedPreferences;
4
- import io.github.g00fy2.versioncompare.Version;
5
4
  import java.text.ParsePosition;
6
5
  import java.text.SimpleDateFormat;
7
6
  import java.util.ArrayList;
@@ -17,6 +16,9 @@ public class DelayUpdateUtils {
17
16
  private final Logger logger;
18
17
 
19
18
  public static final String DELAY_CONDITION_PREFERENCES = "DELAY_CONDITION_PREFERENCES_CAPGO";
19
+ public static final String DELAY_CONDITION_MODE_PREFERENCES = "DELAY_CONDITION_MODE_PREFERENCES_CAPGO";
20
+ public static final String DELAY_CONDITION_MODE_AND = "and";
21
+ public static final String DELAY_CONDITION_MODE_OR = "or";
20
22
  public static final String BACKGROUND_TIMESTAMP_KEY = "BACKGROUND_TIMESTAMP_KEY_CAPGO";
21
23
 
22
24
  private final SharedPreferences prefs;
@@ -39,128 +41,242 @@ public class DelayUpdateUtils {
39
41
  public void checkCancelDelay(CancelDelaySource source) {
40
42
  String delayUpdatePreferences = prefs.getString(DELAY_CONDITION_PREFERENCES, "[]");
41
43
  ArrayList<DelayCondition> delayConditionList = parseDelayConditions(delayUpdatePreferences);
44
+ if (delayConditionList.isEmpty()) {
45
+ return;
46
+ }
47
+
48
+ final boolean useOrMode = isOrConditionMode();
42
49
  ArrayList<DelayCondition> delayConditionListToKeep = new ArrayList<>(delayConditionList.size());
43
50
  int index = 0;
51
+ boolean anyConditionMet = false;
44
52
 
45
53
  for (DelayCondition condition : delayConditionList) {
46
- DelayUntilNext kind = condition.getKind();
47
- String value = condition.getValue();
48
- switch (kind) {
49
- case DelayUntilNext.background:
50
- if (source == CancelDelaySource.FOREGROUND) {
51
- long backgroundedAt = getBackgroundTimestamp();
52
- long now = System.currentTimeMillis();
53
- long delta = Math.max(0, now - backgroundedAt);
54
- long longValue = 0L;
55
- try {
56
- longValue = Long.parseLong(value);
57
- } catch (NumberFormatException e) {
58
- logger.error(
59
- "Background condition (value: " +
60
- value +
61
- ") had an invalid value at index " +
62
- index +
63
- ". We will likely remove it."
64
- );
65
- }
66
-
67
- if (delta > longValue) {
68
- logger.info(
69
- "Background condition (value: " +
70
- value +
71
- ") deleted at index " +
72
- index +
73
- ". Delta: " +
74
- delta +
75
- ", longValue: " +
76
- longValue
77
- );
78
- } else {
79
- delayConditionListToKeep.add(condition);
80
- logger.info(
81
- "Background delay (value: " +
82
- value +
83
- ") condition kept at index " +
84
- index +
85
- " (source: " +
86
- source.toString() +
87
- ")"
88
- );
89
- }
54
+ ConditionCheckResult result = evaluateCondition(condition, source, index);
55
+ if (result.met) {
56
+ anyConditionMet = true;
57
+ if (!useOrMode) {
58
+ logger.info(result.logMessage);
59
+ }
60
+ } else if (!useOrMode) {
61
+ if (result.keep) {
62
+ delayConditionListToKeep.add(condition);
63
+ }
64
+ if (result.logMessage != null) {
65
+ if (result.error) {
66
+ logger.error(result.logMessage);
90
67
  } else {
91
- delayConditionListToKeep.add(condition);
92
- logger.info(
93
- "Background delay (value: " +
68
+ logger.info(result.logMessage);
69
+ }
70
+ }
71
+ } else if (result.keep) {
72
+ delayConditionListToKeep.add(condition);
73
+ }
74
+ index++;
75
+ }
76
+
77
+ if (useOrMode) {
78
+ if (anyConditionMet || delayConditionListToKeep.isEmpty()) {
79
+ logger.info("Delay condition met in OR mode (source: " + source + "), canceling delay");
80
+ this.cancelDelay("checkCancelDelay");
81
+ } else if (delayConditionListToKeep.size() != delayConditionList.size()) {
82
+ this.setMultiDelay(convertDelayConditionsToJson(delayConditionListToKeep));
83
+ }
84
+ return;
85
+ }
86
+
87
+ if (delayConditionListToKeep.isEmpty()) {
88
+ this.cancelDelay("checkCancelDelay");
89
+ } else {
90
+ this.setMultiDelay(convertDelayConditionsToJson(delayConditionListToKeep));
91
+ }
92
+ }
93
+
94
+ private static final class ConditionCheckResult {
95
+
96
+ final boolean met;
97
+ final boolean keep;
98
+ final boolean error;
99
+ final String logMessage;
100
+
101
+ ConditionCheckResult(boolean met, boolean keep, boolean error, String logMessage) {
102
+ this.met = met;
103
+ this.keep = keep;
104
+ this.error = error;
105
+ this.logMessage = logMessage;
106
+ }
107
+ }
108
+
109
+ private ConditionCheckResult evaluateCondition(DelayCondition condition, CancelDelaySource source, int index) {
110
+ DelayUntilNext kind = condition.getKind();
111
+ String value = condition.getValue();
112
+ switch (kind) {
113
+ case background:
114
+ if (source == CancelDelaySource.FOREGROUND) {
115
+ long backgroundedAt = getBackgroundTimestamp();
116
+ long now = System.currentTimeMillis();
117
+ long delta = Math.max(0, now - backgroundedAt);
118
+ long longValue = 0L;
119
+ try {
120
+ longValue = Long.parseLong(value);
121
+ } catch (NumberFormatException e) {
122
+ return new ConditionCheckResult(
123
+ false,
124
+ false,
125
+ true,
126
+ "Background condition (value: " +
94
127
  value +
95
- ") condition kept at index " +
128
+ ") had an invalid value at index " +
96
129
  index +
97
- " (source: " +
98
- source.toString() +
99
- ")"
130
+ ". We will likely remove it."
100
131
  );
101
132
  }
102
- break;
103
- case DelayUntilNext.kill:
104
- if (source == CancelDelaySource.KILLED) {
105
- logger.info("Kill delay (value: " + value + ") condition removed at index " + index + " after app kill");
106
- } else {
107
- delayConditionListToKeep.add(condition);
108
- logger.info(
109
- "Kill delay (value: " + value + ") condition kept at index " + index + " (source: " + source.toString() + ")"
133
+
134
+ if (delta > longValue) {
135
+ return new ConditionCheckResult(
136
+ true,
137
+ false,
138
+ false,
139
+ "Background condition (value: " +
140
+ value +
141
+ ") deleted at index " +
142
+ index +
143
+ ". Delta: " +
144
+ delta +
145
+ ", longValue: " +
146
+ longValue
110
147
  );
111
148
  }
112
- break;
113
- case DelayUntilNext.date:
114
- if (!"".equals(value)) {
115
- Date date = parseDateCondition(value);
116
- if (date != null) {
117
- if (new Date().compareTo(date) > 0) {
118
- logger.info("Date delay (value: " + value + ") condition removed due to expired date at index " + index);
119
- } else {
120
- delayConditionListToKeep.add(condition);
121
- logger.info("Date delay (value: " + value + ") condition kept at index " + index);
122
- }
123
- } else {
124
- logger.error("Date delay (value: " + value + ") condition removed due to parsing issue at index " + index);
149
+ return new ConditionCheckResult(
150
+ false,
151
+ true,
152
+ false,
153
+ "Background delay (value: " + value + ") condition kept at index " + index + " (source: " + source + ")"
154
+ );
155
+ }
156
+ return new ConditionCheckResult(
157
+ false,
158
+ true,
159
+ false,
160
+ "Background delay (value: " + value + ") condition kept at index " + index + " (source: " + source + ")"
161
+ );
162
+ case kill:
163
+ if (source == CancelDelaySource.KILLED) {
164
+ return new ConditionCheckResult(
165
+ true,
166
+ false,
167
+ false,
168
+ "Kill delay (value: " + value + ") condition removed at index " + index + " after app kill"
169
+ );
170
+ }
171
+ return new ConditionCheckResult(
172
+ false,
173
+ true,
174
+ false,
175
+ "Kill delay (value: " + value + ") condition kept at index " + index + " (source: " + source + ")"
176
+ );
177
+ case date:
178
+ if (!"".equals(value)) {
179
+ Date date = parseDateCondition(value);
180
+ if (date != null) {
181
+ if (new Date().compareTo(date) > 0) {
182
+ return new ConditionCheckResult(
183
+ true,
184
+ false,
185
+ false,
186
+ "Date delay (value: " + value + ") condition removed due to expired date at index " + index
187
+ );
125
188
  }
126
- } else {
127
- logger.debug("Date delay (value: " + value + ") condition removed due to empty value at index " + index);
189
+ return new ConditionCheckResult(
190
+ false,
191
+ true,
192
+ false,
193
+ "Date delay (value: " + value + ") condition kept at index " + index
194
+ );
128
195
  }
129
- break;
130
- case DelayUntilNext.nativeVersion:
131
- if (!"".equals(value)) {
132
- try {
133
- final Version versionLimit = new Version(value);
134
- if (this.currentVersionNative.isAtLeast(versionLimit)) {
135
- logger.info(
136
- "Native version delay (value: " + value + ") condition removed due to above limit at index " + index
137
- );
138
- } else {
139
- delayConditionListToKeep.add(condition);
140
- logger.info("Native version delay (value: " + value + ") condition kept at index " + index);
141
- }
142
- } catch (final Exception e) {
143
- logger.error(
144
- "Native version delay (value: " +
145
- value +
146
- ") condition removed due to parsing issue at index " +
147
- index +
148
- " " +
149
- e.getMessage()
196
+ return new ConditionCheckResult(
197
+ false,
198
+ false,
199
+ true,
200
+ "Date delay (value: " + value + ") condition removed due to parsing issue at index " + index
201
+ );
202
+ }
203
+ return new ConditionCheckResult(
204
+ false,
205
+ false,
206
+ false,
207
+ "Date delay (value: " + value + ") condition removed due to empty value at index " + index
208
+ );
209
+ case nativeVersion:
210
+ if (!"".equals(value)) {
211
+ try {
212
+ final Version versionLimit = new Version(value);
213
+ if (this.currentVersionNative.isAtLeast(versionLimit)) {
214
+ return new ConditionCheckResult(
215
+ true,
216
+ false,
217
+ false,
218
+ "Native version delay (value: " + value + ") condition removed due to above limit at index " + index
150
219
  );
151
220
  }
152
- } else {
153
- logger.debug("Native version delay (value: " + value + ") condition removed due to empty value at index " + index);
221
+ return new ConditionCheckResult(
222
+ false,
223
+ true,
224
+ false,
225
+ "Native version delay (value: " + value + ") condition kept at index " + index
226
+ );
227
+ } catch (final Exception e) {
228
+ return new ConditionCheckResult(
229
+ false,
230
+ false,
231
+ true,
232
+ "Native version delay (value: " +
233
+ value +
234
+ ") condition removed due to parsing issue at index " +
235
+ index +
236
+ " " +
237
+ e.getMessage()
238
+ );
154
239
  }
155
- break;
156
- }
157
- index++;
240
+ }
241
+ return new ConditionCheckResult(
242
+ false,
243
+ false,
244
+ false,
245
+ "Native version delay (value: " + value + ") condition removed due to empty value at index " + index
246
+ );
247
+ default:
248
+ return new ConditionCheckResult(false, false, true, "Unknown delay condition kind at index " + index);
158
249
  }
250
+ }
159
251
 
160
- if (delayConditionListToKeep.isEmpty()) {
161
- this.cancelDelay("checkCancelDelay");
162
- } else {
163
- this.setMultiDelay(convertDelayConditionsToJson(delayConditionListToKeep));
252
+ public boolean isOrConditionMode() {
253
+ return DELAY_CONDITION_MODE_OR.equals(getConditionMode());
254
+ }
255
+
256
+ public String getConditionMode() {
257
+ String mode = prefs.getString(DELAY_CONDITION_MODE_PREFERENCES, DELAY_CONDITION_MODE_AND);
258
+ if (DELAY_CONDITION_MODE_OR.equals(mode)) {
259
+ return DELAY_CONDITION_MODE_OR;
260
+ }
261
+ return DELAY_CONDITION_MODE_AND;
262
+ }
263
+
264
+ public Boolean setConditionMode(String conditionMode) {
265
+ try {
266
+ String normalized = DELAY_CONDITION_MODE_OR.equals(conditionMode) ? DELAY_CONDITION_MODE_OR : DELAY_CONDITION_MODE_AND;
267
+ if (!DELAY_CONDITION_MODE_AND.equals(conditionMode) && !DELAY_CONDITION_MODE_OR.equals(conditionMode)) {
268
+ logger.warn("Unknown delay condition mode '" + conditionMode + "', defaulting to '" + DELAY_CONDITION_MODE_AND + "'");
269
+ }
270
+ this.editor.putString(DELAY_CONDITION_MODE_PREFERENCES, normalized);
271
+ if (!this.editor.commit()) {
272
+ logger.error("Failed to save delay condition mode: commit returned false");
273
+ return false;
274
+ }
275
+ logger.info("Delay condition mode saved: " + normalized);
276
+ return true;
277
+ } catch (final Exception e) {
278
+ logger.error("Failed to save delay condition mode: " + e.getMessage());
279
+ return false;
164
280
  }
165
281
  }
166
282
 
@@ -255,7 +371,10 @@ public class DelayUpdateUtils {
255
371
  public Boolean setMultiDelay(String delayConditions) {
256
372
  try {
257
373
  this.editor.putString(DELAY_CONDITION_PREFERENCES, delayConditions);
258
- this.editor.commit();
374
+ if (!this.editor.commit()) {
375
+ logger.error("Failed to delay update: commit returned false");
376
+ return false;
377
+ }
259
378
  logger.info("Delay update saved");
260
379
  return true;
261
380
  } catch (final Exception e) {
@@ -296,6 +415,7 @@ public class DelayUpdateUtils {
296
415
  public boolean cancelDelay(String source) {
297
416
  try {
298
417
  this.editor.remove(DELAY_CONDITION_PREFERENCES);
418
+ this.editor.remove(DELAY_CONDITION_MODE_PREFERENCES);
299
419
  this.editor.commit();
300
420
  logger.info("All delays canceled from " + source);
301
421
  return true;