Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ dependencies {

testImplementation 'junit:junit:4.13.2'
testImplementation 'org.json:json:20231013'
testImplementation 'org.mockito:mockito-core:5.14.2'

androidTestImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.2.1'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,13 @@ public void downloadPackage(JSONObject updatePackage, String expectedBundleFileN
}

connection.setRequestProperty("Accept-Encoding", "identity");

int responseCode = connection.getResponseCode();
if (responseCode < 200 || responseCode >= 300) {
throw new CodePushUnknownException("Error downloading update package. Response code: "
+ responseCode + ". Response body: " + NetworkUtils.readStreamToString(connection.getErrorStream()));
}

bin = new BufferedInputStream(connection.getInputStream());

long totalBytes = connection.getContentLength();
Expand Down Expand Up @@ -234,6 +241,15 @@ public void downloadPackage(JSONObject updatePackage, String expectedBundleFileN
}
}

installDownloadedUpdate(updatePackage, expectedBundleFileName, stringPublicKey,
downloadFile, isZip, newUpdateFolderPath, newUpdateMetadataPath);
}

void installDownloadedUpdate(JSONObject updatePackage, String expectedBundleFileName,
String stringPublicKey, File downloadFile, boolean isZip,
String newUpdateFolderPath, String newUpdateMetadataPath) throws IOException {
String newUpdateHash = updatePackage.optString(CodePushConstants.PACKAGE_HASH_KEY, null);

if (isZip) {
// Unzip the downloaded file and then delete the zip
String unzippedFolderPath = getUnzippedFolderPath();
Expand Down Expand Up @@ -301,30 +317,26 @@ public void downloadPackage(JSONObject updatePackage, String expectedBundleFileN
String signaturePath = CodePushUpdateUtils.getSignatureFilePath(newUpdateFolderPath);
boolean isSignatureAppearedInBundle = FileUtils.fileAtPathExists(signaturePath);

if (isSignatureVerificationEnabled && !isSignatureAppearedInBundle) {
throw new CodePushInvalidUpdateException(
"Error! Public key was provided but there is no JWT signature within app bundle to verify. " +
"Possible reasons, why that might happen: \n" +
"1. You've been released CodePush bundle update using version of CodePush CLI that is not support code signing.\n" +
"2. You've been released CodePush bundle update without providing --privateKeyPath option."
);
}

if (!isSignatureVerificationEnabled && isSignatureAppearedInBundle) {
CodePushUtils.log(
"Warning! JWT signature exists in codepush update but code integrity check couldn't be performed because there is no public key configured. " +
"Please ensure that public key is properly configured within your application."
);
}

CodePushUpdateUtils.verifyFolderHash(newUpdateFolderPath, newUpdateHash);

if (isSignatureVerificationEnabled) {
if (isSignatureAppearedInBundle) {
CodePushUpdateUtils.verifyFolderHash(newUpdateFolderPath, newUpdateHash);
CodePushUpdateUtils.verifyUpdateSignature(newUpdateFolderPath, newUpdateHash, stringPublicKey);
} else {
throw new CodePushInvalidUpdateException(
"Error! Public key was provided but there is no JWT signature within app bundle to verify. " +
"Possible reasons, why that might happen: \n" +
"1. You've been released CodePush bundle update using version of CodePush CLI that is not support code signing.\n" +
"2. You've been released CodePush bundle update without providing --privateKeyPath option."
);
}
} else {
if (isSignatureAppearedInBundle) {
CodePushUtils.log(
"Warning! JWT signature exists in codepush update but code integrity check couldn't be performed because there is no public key configured. " +
"Please ensure that public key is properly configured within your application."
);
CodePushUpdateUtils.verifyFolderHash(newUpdateFolderPath, newUpdateHash);
} else {
if (isDiffUpdate) {
CodePushUpdateUtils.verifyFolderHash(newUpdateFolderPath, newUpdateHash);
}
}
CodePushUpdateUtils.verifyUpdateSignature(newUpdateFolderPath, newUpdateHash, stringPublicKey);
}

CodePushUtils.setJSONValueForKey(updatePackage, CodePushConstants.RELATIVE_BUNDLE_PATH_KEY, relativeBundlePath);
Expand Down Expand Up @@ -384,6 +396,13 @@ public void downloadAndReplaceCurrentBundle(String remoteBundleUrl, String bundl
try {
downloadUrl = new URL(remoteBundleUrl);
connection = (HttpURLConnection) (downloadUrl.openConnection());

int responseCode = connection.getResponseCode();
if (responseCode < 200 || responseCode >= 300) {
throw new CodePushUnknownException("Error downloading update package. Response code: "
+ responseCode + ". Response body: " + NetworkUtils.readStreamToString(connection.getErrorStream()));
}

bin = new BufferedInputStream(connection.getInputStream());
File downloadFile = new File(getCurrentPackageBundlePath(bundleFileName));
downloadFile.delete();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
@file:JvmName("NetworkUtils")

package com.microsoft.codepush.react

import java.io.InputStream

fun readStreamToString(inputStream: InputStream?): String {
return inputStream?.bufferedReader()?.use { it.readText() } ?: ""
}
Loading