@capgo/capacitor-updater 8.53.1 → 8.54.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/README.md +55 -15
- package/android/build.gradle +1 -0
- package/android/src/main/java/ee/forgr/capacitor_updater/CapacitorUpdaterPlugin.java +187 -16
- package/android/src/main/java/ee/forgr/capacitor_updater/CapgoUpdater.java +278 -25
- package/android/src/main/java/ee/forgr/capacitor_updater/CryptoCipher.java +82 -19
- package/android/src/main/java/ee/forgr/capacitor_updater/DownloadService.java +314 -30
- package/android/src/main/java/ee/forgr/capacitor_updater/StatsBatchFailureDiagnostics.java +40 -0
- package/dist/docs.json +34 -2
- package/dist/esm/definitions.d.ts +46 -2
- package/dist/esm/definitions.js.map +1 -1
- package/ios/Sources/CapacitorUpdaterPlugin/CapacitorUpdaterPlugin.swift +44 -1
- package/ios/Sources/CapacitorUpdaterPlugin/CapgoUpdater.swift +368 -61
- package/ios/Sources/CapacitorUpdaterPlugin/CryptoCipher.swift +59 -33
- package/ios/Sources/CapacitorUpdaterPlugin/InternalUtils.swift +0 -19
- package/ios/Sources/CapacitorUpdaterPlugin/RedirectPolicyDelegate.swift +20 -0
- package/package.json +8 -4
- package/scripts/builtin-hashes-lib.mjs +148 -0
- package/scripts/capacitor-builtin-hashes-hook.mjs +65 -0
- package/scripts/resolve-real-path.mjs +25 -0
|
@@ -13,6 +13,7 @@ import android.content.pm.PackageManager;
|
|
|
13
13
|
import android.os.Build;
|
|
14
14
|
import android.os.Handler;
|
|
15
15
|
import android.os.Looper;
|
|
16
|
+
import android.os.SystemClock;
|
|
16
17
|
import androidx.annotation.NonNull;
|
|
17
18
|
import androidx.lifecycle.LiveData;
|
|
18
19
|
import androidx.lifecycle.Observer;
|
|
@@ -117,6 +118,13 @@ public class CapgoUpdater {
|
|
|
117
118
|
public String publicKey = "";
|
|
118
119
|
public String deviceID = "";
|
|
119
120
|
public int timeout = 20000;
|
|
121
|
+
/** Refuse bundles whose version is below the native app version (versionBuild). Config builtinMinimum, default false. */
|
|
122
|
+
public volatile boolean builtinMinimum = false;
|
|
123
|
+
/**
|
|
124
|
+
* Require https for every URL the updater downloads from. Config httpsOnly, default true. Mirrors
|
|
125
|
+
* DownloadService.httpsOnly (the OkHttp interceptor); this field gates URLs before any network IO.
|
|
126
|
+
*/
|
|
127
|
+
public volatile boolean httpsOnly = true;
|
|
120
128
|
|
|
121
129
|
// Cached key ID calculated once from publicKey
|
|
122
130
|
private String cachedKeyId = "";
|
|
@@ -144,6 +152,11 @@ public class CapgoUpdater {
|
|
|
144
152
|
private final AtomicBoolean statsFlushInFlight = new AtomicBoolean(false);
|
|
145
153
|
private final AtomicBoolean statsStopped = new AtomicBoolean(false);
|
|
146
154
|
private static final long STATS_FLUSH_INTERVAL_MS = 1000;
|
|
155
|
+
private static volatile Long statsFlushIntervalOverrideMs = null;
|
|
156
|
+
private static volatile boolean statsFlushSchedulingDisabledForTests = false;
|
|
157
|
+
private static volatile boolean statsFailureElapsedRealtimeStubForTests = false;
|
|
158
|
+
/** Unit tests only: runs after queue mutation and before the stats owner check. */
|
|
159
|
+
static volatile Runnable statsAdoptionBeforeOwnerCheckForTests = null;
|
|
147
160
|
private static final String PENDING_STATS_FILE = "capgo_pending_stats.json";
|
|
148
161
|
private static final int MAX_PENDING_STATS = 200;
|
|
149
162
|
// The live plugin instance owns the stats file. A headless worker hands its unsent events to it
|
|
@@ -640,6 +653,96 @@ public class CapgoUpdater {
|
|
|
640
653
|
}
|
|
641
654
|
}
|
|
642
655
|
|
|
656
|
+
static boolean isHttpsUrl(final String url) {
|
|
657
|
+
if (url == null) {
|
|
658
|
+
return false;
|
|
659
|
+
}
|
|
660
|
+
final String trimmed = url.trim();
|
|
661
|
+
return trimmed.regionMatches(true, 0, "https://", 0, "https://".length());
|
|
662
|
+
}
|
|
663
|
+
|
|
664
|
+
static String httpsOnlyMessage(final String url) {
|
|
665
|
+
return "httpsOnly is enabled and " + url + " is not https";
|
|
666
|
+
}
|
|
667
|
+
|
|
668
|
+
/** httpsOnly gate for the zip url and every manifest download_url, run before any network IO. */
|
|
669
|
+
void requireHttpsUrls(final String url, final JSONArray manifest) throws IOException {
|
|
670
|
+
if (!this.httpsOnly) {
|
|
671
|
+
return;
|
|
672
|
+
}
|
|
673
|
+
if (manifest == null || manifest.length() == 0) {
|
|
674
|
+
if (!isHttpsUrl(url)) {
|
|
675
|
+
logger.error(httpsOnlyMessage(url));
|
|
676
|
+
throw new IOException(httpsOnlyMessage(url));
|
|
677
|
+
}
|
|
678
|
+
return;
|
|
679
|
+
}
|
|
680
|
+
for (int i = 0; i < manifest.length(); i++) {
|
|
681
|
+
final JSONObject entry = manifest.optJSONObject(i);
|
|
682
|
+
final String downloadUrl = entry == null ? null : entry.optString("download_url", "");
|
|
683
|
+
if (!isHttpsUrl(downloadUrl)) {
|
|
684
|
+
logger.error(httpsOnlyMessage(downloadUrl));
|
|
685
|
+
throw new IOException(httpsOnlyMessage(downloadUrl));
|
|
686
|
+
}
|
|
687
|
+
}
|
|
688
|
+
}
|
|
689
|
+
|
|
690
|
+
static String stripBuildMetadata(final String version) {
|
|
691
|
+
if (version == null) {
|
|
692
|
+
return null;
|
|
693
|
+
}
|
|
694
|
+
final int plus = version.indexOf('+');
|
|
695
|
+
return plus >= 0 ? version.substring(0, plus) : version;
|
|
696
|
+
}
|
|
697
|
+
|
|
698
|
+
/** Compares major.minor.patch only (missing components count as 0); suffix and extra components are ignored. */
|
|
699
|
+
static int compareCoreVersion(final Version left, final Version right) {
|
|
700
|
+
for (int i = 0; i < 3; i++) {
|
|
701
|
+
final long a = i < left.getSubversionNumbers().size() ? left.getSubversionNumbers().get(i) : 0L;
|
|
702
|
+
final long b = i < right.getSubversionNumbers().size() ? right.getSubversionNumbers().get(i) : 0L;
|
|
703
|
+
if (a != b) {
|
|
704
|
+
return Long.compare(a, b);
|
|
705
|
+
}
|
|
706
|
+
}
|
|
707
|
+
return 0;
|
|
708
|
+
}
|
|
709
|
+
|
|
710
|
+
static boolean isParsableVersion(final Version version) {
|
|
711
|
+
return version != null && version.getOriginalString() != null && !version.getSubversionNumbers().isEmpty();
|
|
712
|
+
}
|
|
713
|
+
|
|
714
|
+
/**
|
|
715
|
+
* builtinMinimum gate: a bundle may never roll the app below its native version. Skipped (debug log) when
|
|
716
|
+
* either version string does not parse, so unversioned bundles keep working.
|
|
717
|
+
*/
|
|
718
|
+
void requireVersionNotBelowBuiltin(final String version) throws IOException {
|
|
719
|
+
if (!this.builtinMinimum) {
|
|
720
|
+
return;
|
|
721
|
+
}
|
|
722
|
+
// Build metadata (+...) never takes part in ordering.
|
|
723
|
+
final Version bundle = new Version(stripBuildMetadata(version));
|
|
724
|
+
final Version nativeVersion = new Version(stripBuildMetadata(this.versionBuild));
|
|
725
|
+
if (!isParsableVersion(bundle) || !isParsableVersion(nativeVersion)) {
|
|
726
|
+
logger.debug(
|
|
727
|
+
"builtinMinimum skipped: cannot parse bundle version '" + version + "' or native version '" + this.versionBuild + "'"
|
|
728
|
+
);
|
|
729
|
+
return;
|
|
730
|
+
}
|
|
731
|
+
// Only major.minor.patch matter: prerelease suffixes and a fourth component are not a rollback.
|
|
732
|
+
if (compareCoreVersion(bundle, nativeVersion) < 0) {
|
|
733
|
+
final String message = "Bundle version " + version + " is below native version " + this.versionBuild + " (builtinMinimum)";
|
|
734
|
+
logger.error(message);
|
|
735
|
+
this.sendStats("version_below_native", version);
|
|
736
|
+
throw new IOException(message);
|
|
737
|
+
}
|
|
738
|
+
}
|
|
739
|
+
|
|
740
|
+
/** Pre-network download gates shared by every entry point: httpsOnly and builtinMinimum. */
|
|
741
|
+
void runPreDownloadSecurityGates(final String url, final String version, final JSONArray manifest) throws IOException {
|
|
742
|
+
this.requireHttpsUrls(url, manifest);
|
|
743
|
+
this.requireVersionNotBelowBuiltin(version);
|
|
744
|
+
}
|
|
745
|
+
|
|
643
746
|
static boolean containsPathTraversalSegment(final String relativePath) {
|
|
644
747
|
for (final String segment : relativePath.split("/")) {
|
|
645
748
|
if ("..".equals(segment)) {
|
|
@@ -828,7 +931,7 @@ public class CapgoUpdater {
|
|
|
828
931
|
continue;
|
|
829
932
|
}
|
|
830
933
|
try {
|
|
831
|
-
|
|
934
|
+
DownloadService.linkOrCopyImmutableFile(file, cacheFile);
|
|
832
935
|
} catch (IOException e) {
|
|
833
936
|
logger.debug("Delta cache copy failed: " + file.getPath());
|
|
834
937
|
}
|
|
@@ -1854,6 +1957,7 @@ public class CapgoUpdater {
|
|
|
1854
1957
|
if (manifest == null) {
|
|
1855
1958
|
this.requireBundleChecksum(checksum);
|
|
1856
1959
|
}
|
|
1960
|
+
this.runPreDownloadSecurityGates(url, version, manifest);
|
|
1857
1961
|
} catch (final IOException e) {
|
|
1858
1962
|
logger.error("Download blocked: " + e.getMessage());
|
|
1859
1963
|
return null;
|
|
@@ -1910,6 +2014,7 @@ public class CapgoUpdater {
|
|
|
1910
2014
|
public BundleInfo download(final String url, final String version, final String sessionKey, final String checksum) throws IOException {
|
|
1911
2015
|
this.requireSessionKeyForEncryptedUpdate(sessionKey);
|
|
1912
2016
|
this.requireBundleChecksum(checksum);
|
|
2017
|
+
this.runPreDownloadSecurityGates(url, version, null);
|
|
1913
2018
|
this.runDownloadGate();
|
|
1914
2019
|
// Check for existing bundle with same version and clean up if in error state
|
|
1915
2020
|
BundleInfo existingBundle = this.getBundleInfoByName(version);
|
|
@@ -1967,6 +2072,7 @@ public class CapgoUpdater {
|
|
|
1967
2072
|
if (manifest == null) {
|
|
1968
2073
|
return download(url, version, sessionKey, checksum);
|
|
1969
2074
|
}
|
|
2075
|
+
this.runPreDownloadSecurityGates(url, version, manifest);
|
|
1970
2076
|
|
|
1971
2077
|
// Check for existing bundle with same version and clean up if in error state
|
|
1972
2078
|
BundleInfo existingBundle = this.getBundleInfoByName(version);
|
|
@@ -2274,6 +2380,13 @@ public class CapgoUpdater {
|
|
|
2274
2380
|
state.fallbackBundleId == null || state.fallbackBundleId.isEmpty() ? BundleInfo.ID_BUILTIN : state.fallbackBundleId;
|
|
2275
2381
|
|
|
2276
2382
|
this.editor.putString(this.CAP_SERVER_PATH, currentBundlePath);
|
|
2383
|
+
this.restoreResetStateRollbackTargets(state);
|
|
2384
|
+
}
|
|
2385
|
+
|
|
2386
|
+
void restoreResetStateRollbackTargets(final ResetState state) {
|
|
2387
|
+
final String fallbackBundleId =
|
|
2388
|
+
state.fallbackBundleId == null || state.fallbackBundleId.isEmpty() ? BundleInfo.ID_BUILTIN : state.fallbackBundleId;
|
|
2389
|
+
|
|
2277
2390
|
this.editor.putString(FALLBACK_VERSION, fallbackBundleId);
|
|
2278
2391
|
if (state.nextBundleId == null || state.nextBundleId.isEmpty()) {
|
|
2279
2392
|
this.editor.remove(NEXT_VERSION);
|
|
@@ -2283,6 +2396,13 @@ public class CapgoUpdater {
|
|
|
2283
2396
|
this.editor.commit();
|
|
2284
2397
|
}
|
|
2285
2398
|
|
|
2399
|
+
void preserveFallbackForWatchdog(final ResetState state) {
|
|
2400
|
+
final String fallbackBundleId =
|
|
2401
|
+
state.fallbackBundleId == null || state.fallbackBundleId.isEmpty() ? BundleInfo.ID_BUILTIN : state.fallbackBundleId;
|
|
2402
|
+
this.editor.putString(FALLBACK_VERSION, fallbackBundleId);
|
|
2403
|
+
this.editor.commit();
|
|
2404
|
+
}
|
|
2405
|
+
|
|
2286
2406
|
void prepareResetStateForTransition() {
|
|
2287
2407
|
this.setCurrentBundle(new File("public"));
|
|
2288
2408
|
this.setFallbackBundle(null);
|
|
@@ -2299,7 +2419,7 @@ public class CapgoUpdater {
|
|
|
2299
2419
|
}
|
|
2300
2420
|
|
|
2301
2421
|
boolean canSet(final BundleInfo bundle) {
|
|
2302
|
-
return bundle != null && (bundle.isBuiltin() || this.bundleExists(bundle.getId()));
|
|
2422
|
+
return bundle != null && !bundle.isErrorStatus() && (bundle.isBuiltin() || this.bundleExists(bundle.getId()));
|
|
2303
2423
|
}
|
|
2304
2424
|
|
|
2305
2425
|
public Boolean set(final BundleInfo bundle) {
|
|
@@ -3553,20 +3673,30 @@ public class CapgoUpdater {
|
|
|
3553
3673
|
return true;
|
|
3554
3674
|
}
|
|
3555
3675
|
final List<QueuedStatsEvent> displaced = new ArrayList<>();
|
|
3676
|
+
final List<QueuedStatsEvent> adopted = new ArrayList<>();
|
|
3556
3677
|
synchronized (statsQueue) {
|
|
3557
3678
|
if (statsStopped.get()) {
|
|
3558
3679
|
return false;
|
|
3559
3680
|
}
|
|
3560
3681
|
for (final JSONObject event : events) {
|
|
3561
3682
|
while (statsQueue.size() >= MAX_PENDING_STATS) {
|
|
3562
|
-
|
|
3683
|
+
final QueuedStatsEvent removed = statsQueue.remove(0);
|
|
3684
|
+
if (!adopted.contains(removed)) {
|
|
3685
|
+
displaced.add(removed);
|
|
3686
|
+
}
|
|
3563
3687
|
}
|
|
3564
|
-
|
|
3688
|
+
final QueuedStatsEvent queued = new QueuedStatsEvent(event, null);
|
|
3689
|
+
statsQueue.add(queued);
|
|
3690
|
+
adopted.add(queued);
|
|
3565
3691
|
}
|
|
3566
3692
|
}
|
|
3693
|
+
final Runnable beforeOwnerCheck = statsAdoptionBeforeOwnerCheckForTests;
|
|
3694
|
+
if (beforeOwnerCheck != null) {
|
|
3695
|
+
beforeOwnerCheck.run();
|
|
3696
|
+
}
|
|
3567
3697
|
synchronized (statsOwnerLock) {
|
|
3568
3698
|
if (statsOwner != null && statsOwner != this) {
|
|
3569
|
-
rollbackAdoptedEvents(
|
|
3699
|
+
rollbackAdoptedEvents(adopted, displaced);
|
|
3570
3700
|
return false;
|
|
3571
3701
|
}
|
|
3572
3702
|
}
|
|
@@ -3575,21 +3705,67 @@ public class CapgoUpdater {
|
|
|
3575
3705
|
return true;
|
|
3576
3706
|
}
|
|
3577
3707
|
|
|
3578
|
-
private void rollbackAdoptedEvents(final
|
|
3708
|
+
private void rollbackAdoptedEvents(final List<QueuedStatsEvent> adopted, final List<QueuedStatsEvent> displaced) {
|
|
3579
3709
|
synchronized (statsQueue) {
|
|
3580
|
-
|
|
3581
|
-
|
|
3582
|
-
|
|
3583
|
-
for (int i = 0; i < displaced.size(); i++) {
|
|
3584
|
-
statsQueue.add(i, displaced.get(i));
|
|
3710
|
+
statsQueue.removeAll(adopted);
|
|
3711
|
+
if (!displaced.isEmpty()) {
|
|
3712
|
+
statsQueue.addAll(0, displaced);
|
|
3585
3713
|
}
|
|
3586
3714
|
}
|
|
3587
3715
|
}
|
|
3588
3716
|
|
|
3717
|
+
/** Unit tests only: disable scheduling or override the flush interval. */
|
|
3718
|
+
static void configureStatsFlushForTests(final Long intervalMs, final boolean schedulingEnabled) {
|
|
3719
|
+
statsFlushIntervalOverrideMs = intervalMs;
|
|
3720
|
+
statsFlushSchedulingDisabledForTests = !schedulingEnabled;
|
|
3721
|
+
}
|
|
3722
|
+
|
|
3723
|
+
/** Unit tests only: restore default stats flush scheduling. */
|
|
3724
|
+
static void resetStatsFlushForTests() {
|
|
3725
|
+
statsFlushIntervalOverrideMs = null;
|
|
3726
|
+
statsFlushSchedulingDisabledForTests = false;
|
|
3727
|
+
statsFailureElapsedRealtimeStubForTests = false;
|
|
3728
|
+
}
|
|
3729
|
+
|
|
3730
|
+
static void stubStatsFailureElapsedRealtimeForTests(final boolean enabled) {
|
|
3731
|
+
statsFailureElapsedRealtimeStubForTests = enabled;
|
|
3732
|
+
}
|
|
3733
|
+
|
|
3589
3734
|
int pendingStatsCount() {
|
|
3590
3735
|
return statsQueue.size();
|
|
3591
3736
|
}
|
|
3592
3737
|
|
|
3738
|
+
void flushStatsQueueForTests() {
|
|
3739
|
+
flushStatsQueue();
|
|
3740
|
+
}
|
|
3741
|
+
|
|
3742
|
+
void enqueuePendingStatsEventForTests(final String action) {
|
|
3743
|
+
try {
|
|
3744
|
+
final JSONObject json = new JSONObject();
|
|
3745
|
+
json.put("action", action);
|
|
3746
|
+
json.put("timestamp", 1);
|
|
3747
|
+
synchronized (statsQueue) {
|
|
3748
|
+
statsQueue.add(new QueuedStatsEvent(json, null));
|
|
3749
|
+
}
|
|
3750
|
+
} catch (final JSONException e) {
|
|
3751
|
+
throw new IllegalStateException(e);
|
|
3752
|
+
}
|
|
3753
|
+
}
|
|
3754
|
+
|
|
3755
|
+
List<String> pendingStatsActionsForTests() {
|
|
3756
|
+
final List<String> actions = new ArrayList<>();
|
|
3757
|
+
synchronized (statsQueue) {
|
|
3758
|
+
for (final QueuedStatsEvent queued : statsQueue) {
|
|
3759
|
+
try {
|
|
3760
|
+
actions.add(queued.event.getString("action"));
|
|
3761
|
+
} catch (final JSONException ignored) {
|
|
3762
|
+
actions.add("");
|
|
3763
|
+
}
|
|
3764
|
+
}
|
|
3765
|
+
}
|
|
3766
|
+
return actions;
|
|
3767
|
+
}
|
|
3768
|
+
|
|
3593
3769
|
public void persistPendingStats() {
|
|
3594
3770
|
persistStatsQueue();
|
|
3595
3771
|
}
|
|
@@ -3708,16 +3884,12 @@ public class CapgoUpdater {
|
|
|
3708
3884
|
}
|
|
3709
3885
|
|
|
3710
3886
|
private synchronized void ensureStatsTimerStarted() {
|
|
3711
|
-
if (statsStopped.get()) {
|
|
3887
|
+
if (statsStopped.get() || statsFlushSchedulingDisabledForTests) {
|
|
3712
3888
|
return;
|
|
3713
3889
|
}
|
|
3890
|
+
final long intervalMs = statsFlushIntervalOverrideMs != null ? statsFlushIntervalOverrideMs : STATS_FLUSH_INTERVAL_MS;
|
|
3714
3891
|
if (statsFlushTask == null || statsFlushTask.isCancelled() || statsFlushTask.isDone()) {
|
|
3715
|
-
statsFlushTask = statsScheduler.scheduleAtFixedRate(
|
|
3716
|
-
this::flushStatsQueue,
|
|
3717
|
-
STATS_FLUSH_INTERVAL_MS,
|
|
3718
|
-
STATS_FLUSH_INTERVAL_MS,
|
|
3719
|
-
TimeUnit.MILLISECONDS
|
|
3720
|
-
);
|
|
3892
|
+
statsFlushTask = statsScheduler.scheduleAtFixedRate(this::flushStatsQueue, intervalMs, intervalMs, TimeUnit.MILLISECONDS);
|
|
3721
3893
|
}
|
|
3722
3894
|
}
|
|
3723
3895
|
|
|
@@ -3773,6 +3945,7 @@ public class CapgoUpdater {
|
|
|
3773
3945
|
.build();
|
|
3774
3946
|
|
|
3775
3947
|
final int eventCount = eventsToSend.size();
|
|
3948
|
+
final long requestStartElapsedRealtime = statsFailureElapsedRealtimeStubForTests ? 0L : SystemClock.elapsedRealtime();
|
|
3776
3949
|
DownloadService.sharedClient.newCall(request).enqueue(
|
|
3777
3950
|
new okhttp3.Callback() {
|
|
3778
3951
|
@Override
|
|
@@ -3780,10 +3953,19 @@ public class CapgoUpdater {
|
|
|
3780
3953
|
if (abandonStoppedStatsFlush()) {
|
|
3781
3954
|
return;
|
|
3782
3955
|
}
|
|
3956
|
+
final long elapsedMs = statsFailureElapsedRealtimeStubForTests
|
|
3957
|
+
? 0L
|
|
3958
|
+
: SystemClock.elapsedRealtime() - requestStartElapsedRealtime;
|
|
3783
3959
|
requeueStatsEvents(eventsToSend);
|
|
3784
3960
|
if (logger != null) {
|
|
3785
|
-
logger.error(
|
|
3786
|
-
|
|
3961
|
+
logger.error(
|
|
3962
|
+
StatsBatchFailureDiagnostics.formatFailureMessage(
|
|
3963
|
+
"Failed to send stats batch",
|
|
3964
|
+
StatsBatchFailureDiagnostics.safeDestinationUrl(call.request().url()),
|
|
3965
|
+
StatsBatchFailureDiagnostics.STATUS_UNAVAILABLE,
|
|
3966
|
+
elapsedMs
|
|
3967
|
+
)
|
|
3968
|
+
);
|
|
3787
3969
|
}
|
|
3788
3970
|
statsFlushInFlight.set(false);
|
|
3789
3971
|
}
|
|
@@ -3794,9 +3976,68 @@ public class CapgoUpdater {
|
|
|
3794
3976
|
if (abandonStoppedStatsFlush()) {
|
|
3795
3977
|
return;
|
|
3796
3978
|
}
|
|
3797
|
-
final String
|
|
3979
|
+
final String safeDestination = StatsBatchFailureDiagnostics.safeDestinationUrl(response.request().url());
|
|
3980
|
+
final String responseData;
|
|
3981
|
+
try {
|
|
3982
|
+
responseData = responseBody != null ? responseBody.string() : "";
|
|
3983
|
+
} catch (IOException readEx) {
|
|
3984
|
+
final long readFailureElapsedMs = statsFailureElapsedRealtimeStubForTests
|
|
3985
|
+
? 0L
|
|
3986
|
+
: SystemClock.elapsedRealtime() - requestStartElapsedRealtime;
|
|
3987
|
+
checkAndHandleRateLimitResponse(response, "");
|
|
3988
|
+
if (response.isSuccessful()) {
|
|
3989
|
+
synchronized (statsQueue) {
|
|
3990
|
+
statsInFlight.clear();
|
|
3991
|
+
}
|
|
3992
|
+
persistStatsQueue();
|
|
3993
|
+
runStatsCallbacks(eventsToSend);
|
|
3994
|
+
return;
|
|
3995
|
+
}
|
|
3996
|
+
if (!isTransientStatsFailure(response.code())) {
|
|
3997
|
+
synchronized (statsQueue) {
|
|
3998
|
+
statsInFlight.clear();
|
|
3999
|
+
}
|
|
4000
|
+
persistStatsQueue();
|
|
4001
|
+
if (logger != null) {
|
|
4002
|
+
logger.error(
|
|
4003
|
+
StatsBatchFailureDiagnostics.formatFailureMessage(
|
|
4004
|
+
"Dropping stats batch after permanent error",
|
|
4005
|
+
safeDestination,
|
|
4006
|
+
String.valueOf(response.code()),
|
|
4007
|
+
readFailureElapsedMs
|
|
4008
|
+
)
|
|
4009
|
+
);
|
|
4010
|
+
}
|
|
4011
|
+
return;
|
|
4012
|
+
}
|
|
4013
|
+
requeueStatsEvents(eventsToSend);
|
|
4014
|
+
if (logger != null) {
|
|
4015
|
+
logger.error(
|
|
4016
|
+
StatsBatchFailureDiagnostics.formatFailureMessage(
|
|
4017
|
+
"Error sending stats batch",
|
|
4018
|
+
safeDestination,
|
|
4019
|
+
String.valueOf(response.code()),
|
|
4020
|
+
readFailureElapsedMs
|
|
4021
|
+
)
|
|
4022
|
+
);
|
|
4023
|
+
}
|
|
4024
|
+
return;
|
|
4025
|
+
}
|
|
4026
|
+
final long normalElapsedMs = statsFailureElapsedRealtimeStubForTests
|
|
4027
|
+
? 0L
|
|
4028
|
+
: SystemClock.elapsedRealtime() - requestStartElapsedRealtime;
|
|
3798
4029
|
if (checkAndHandleRateLimitResponse(response, responseData).blocked) {
|
|
3799
4030
|
requeueStatsEvents(eventsToSend);
|
|
4031
|
+
if (logger != null) {
|
|
4032
|
+
logger.error(
|
|
4033
|
+
StatsBatchFailureDiagnostics.formatFailureMessage(
|
|
4034
|
+
"Error sending stats batch",
|
|
4035
|
+
safeDestination,
|
|
4036
|
+
String.valueOf(response.code()),
|
|
4037
|
+
normalElapsedMs
|
|
4038
|
+
)
|
|
4039
|
+
);
|
|
4040
|
+
}
|
|
3800
4041
|
return;
|
|
3801
4042
|
}
|
|
3802
4043
|
|
|
@@ -3813,8 +4054,14 @@ public class CapgoUpdater {
|
|
|
3813
4054
|
} else if (isTransientStatsFailure(response.code())) {
|
|
3814
4055
|
requeueStatsEvents(eventsToSend);
|
|
3815
4056
|
if (logger != null) {
|
|
3816
|
-
logger.error(
|
|
3817
|
-
|
|
4057
|
+
logger.error(
|
|
4058
|
+
StatsBatchFailureDiagnostics.formatFailureMessage(
|
|
4059
|
+
"Error sending stats batch",
|
|
4060
|
+
safeDestination,
|
|
4061
|
+
String.valueOf(response.code()),
|
|
4062
|
+
normalElapsedMs
|
|
4063
|
+
)
|
|
4064
|
+
);
|
|
3818
4065
|
}
|
|
3819
4066
|
} else {
|
|
3820
4067
|
synchronized (statsQueue) {
|
|
@@ -3822,8 +4069,14 @@ public class CapgoUpdater {
|
|
|
3822
4069
|
}
|
|
3823
4070
|
persistStatsQueue();
|
|
3824
4071
|
if (logger != null) {
|
|
3825
|
-
logger.error(
|
|
3826
|
-
|
|
4072
|
+
logger.error(
|
|
4073
|
+
StatsBatchFailureDiagnostics.formatFailureMessage(
|
|
4074
|
+
"Dropping stats batch after permanent error",
|
|
4075
|
+
safeDestination,
|
|
4076
|
+
String.valueOf(response.code()),
|
|
4077
|
+
normalElapsedMs
|
|
4078
|
+
)
|
|
4079
|
+
);
|
|
3827
4080
|
}
|
|
3828
4081
|
}
|
|
3829
4082
|
} finally {
|
|
@@ -145,11 +145,69 @@ public class CryptoCipher {
|
|
|
145
145
|
|
|
146
146
|
public static void decryptFile(final File file, final String publicKey, final String ivSessionKey) throws IOException {
|
|
147
147
|
if (publicKey.isEmpty() || !isValidSessionKey(ivSessionKey)) {
|
|
148
|
-
if (logger != null)
|
|
149
|
-
logger.info("Encryption not set, no public key or session, ignored");
|
|
150
|
-
}
|
|
148
|
+
if (logger != null) logger.info("Encryption not set, no public key or session, ignored");
|
|
151
149
|
return;
|
|
152
150
|
}
|
|
151
|
+
new DecryptionSession(publicKey, ivSessionKey).decryptFile(file);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
// A manifest shares key setup, while each file still creates its own CBC cipher.
|
|
155
|
+
static final class DecryptionSession {
|
|
156
|
+
|
|
157
|
+
private final String publicKey;
|
|
158
|
+
private final String ivSessionKey;
|
|
159
|
+
private PreparedKey prepared;
|
|
160
|
+
private IOException failure;
|
|
161
|
+
|
|
162
|
+
DecryptionSession(String publicKey, String ivSessionKey) {
|
|
163
|
+
this.publicKey = publicKey;
|
|
164
|
+
this.ivSessionKey = ivSessionKey;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
private synchronized PreparedKey key() throws IOException {
|
|
168
|
+
if (failure != null) throw new IOException(failure.getMessage(), failure);
|
|
169
|
+
if (prepared == null) {
|
|
170
|
+
try {
|
|
171
|
+
prepared = prepareDecryptionKey(publicKey, ivSessionKey);
|
|
172
|
+
} catch (IOException error) {
|
|
173
|
+
failure = error;
|
|
174
|
+
throw error;
|
|
175
|
+
} catch (RuntimeException error) {
|
|
176
|
+
// Malformed Base64 throws IllegalArgumentException: cache it like any other bad key.
|
|
177
|
+
failure = new IOException("AES file decryption failed: invalid session key", error);
|
|
178
|
+
throw failure;
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
return prepared;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
boolean isEncrypted() {
|
|
185
|
+
return publicKey != null && !publicKey.isEmpty();
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
void decryptFile(File file) throws IOException {
|
|
189
|
+
if (!isEncrypted()) {
|
|
190
|
+
if (logger != null) logger.info("Encryption not set, no public key or session, ignored");
|
|
191
|
+
return;
|
|
192
|
+
}
|
|
193
|
+
PreparedKey key = key();
|
|
194
|
+
decryptAesFile(file, key.key, key.iv);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
private static final class PreparedKey {
|
|
199
|
+
|
|
200
|
+
final SecretKey key;
|
|
201
|
+
final byte[] iv;
|
|
202
|
+
|
|
203
|
+
PreparedKey(SecretKey key, byte[] iv) {
|
|
204
|
+
this.key = key;
|
|
205
|
+
this.iv = iv;
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
private static PreparedKey prepareDecryptionKey(String publicKey, String ivSessionKey) throws IOException {
|
|
210
|
+
if (!isValidSessionKey(ivSessionKey)) throw new IOException("AES file decryption failed: session key is required");
|
|
153
211
|
// The bundle is encrypted from here on: an unusable key must fail, never leave it encrypted.
|
|
154
212
|
if (!publicKey.startsWith("-----BEGIN RSA PUBLIC KEY-----")) {
|
|
155
213
|
if (logger != null) {
|
|
@@ -173,7 +231,7 @@ public class CryptoCipher {
|
|
|
173
231
|
}
|
|
174
232
|
|
|
175
233
|
SecretKey sKey = CryptoCipher.byteToSessionKey(decryptedSessionKey);
|
|
176
|
-
|
|
234
|
+
return new PreparedKey(sKey, iv);
|
|
177
235
|
} catch (GeneralSecurityException e) {
|
|
178
236
|
if (logger != null) {
|
|
179
237
|
logger.info("decryptFile fail");
|
|
@@ -395,27 +453,32 @@ public class CryptoCipher {
|
|
|
395
453
|
}
|
|
396
454
|
|
|
397
455
|
public static String calcChecksum(InputStream inputStream) {
|
|
398
|
-
final int BUFFER_SIZE = checksumBufferBytes();
|
|
399
|
-
MessageDigest digest;
|
|
400
456
|
try {
|
|
401
|
-
|
|
402
|
-
} catch (
|
|
403
|
-
logger.error("
|
|
457
|
+
return calcChecksumOrThrow(inputStream);
|
|
458
|
+
} catch (IOException error) {
|
|
459
|
+
logger.error("Cannot calculate checksum");
|
|
460
|
+
logger.debug("Error: " + error.getMessage());
|
|
404
461
|
return "";
|
|
405
462
|
}
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
static String calcChecksumOrThrow(File file) throws IOException {
|
|
466
|
+
try (FileInputStream input = new FileInputStream(file)) {
|
|
467
|
+
return calcChecksumOrThrow(input);
|
|
468
|
+
}
|
|
469
|
+
}
|
|
406
470
|
|
|
471
|
+
static String calcChecksumOrThrow(InputStream input) throws IOException {
|
|
472
|
+
final MessageDigest digest;
|
|
407
473
|
try {
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
digest.update(buffer, 0, length);
|
|
412
|
-
}
|
|
413
|
-
return digestToHex(digest);
|
|
414
|
-
} catch (IOException e) {
|
|
415
|
-
logger.error("Cannot calculate checksum");
|
|
416
|
-
logger.debug("Error: " + e.getMessage());
|
|
417
|
-
return "";
|
|
474
|
+
digest = MessageDigest.getInstance("SHA-256");
|
|
475
|
+
} catch (java.security.NoSuchAlgorithmException error) {
|
|
476
|
+
throw new IOException("SHA-256 algorithm not available", error);
|
|
418
477
|
}
|
|
478
|
+
byte[] buffer = new byte[checksumBufferBytes()];
|
|
479
|
+
int length;
|
|
480
|
+
while ((length = input.read(buffer)) != -1) digest.update(buffer, 0, length);
|
|
481
|
+
return digestToHex(digest);
|
|
419
482
|
}
|
|
420
483
|
|
|
421
484
|
static String digestToHex(MessageDigest digest) {
|