Skip to content

Commit 383765c

Browse files
authored
mod(Runtime): prevent corrupted updates by verifying package hash (#86)
1 parent 99702d9 commit 383765c

File tree

2 files changed

+19
-41
lines changed

2 files changed

+19
-41
lines changed

android/app/src/main/java/com/microsoft/codepush/react/CodePushUpdateManager.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -265,14 +265,15 @@ public void downloadPackage(JSONObject updatePackage, String expectedBundleFileN
265265
CodePushUtils.log("Applying full update.");
266266
}
267267

268+
CodePushUpdateUtils.verifyFolderHash(newUpdateFolderPath, newUpdateHash);
269+
268270
boolean isSignatureVerificationEnabled = (stringPublicKey != null);
269271

270272
String signaturePath = CodePushUpdateUtils.getSignatureFilePath(newUpdateFolderPath);
271273
boolean isSignatureAppearedInBundle = FileUtils.fileAtPathExists(signaturePath);
272274

273275
if (isSignatureVerificationEnabled) {
274276
if (isSignatureAppearedInBundle) {
275-
CodePushUpdateUtils.verifyFolderHash(newUpdateFolderPath, newUpdateHash);
276277
CodePushUpdateUtils.verifyUpdateSignature(newUpdateFolderPath, newUpdateHash, stringPublicKey);
277278
} else {
278279
throw new CodePushInvalidUpdateException(
@@ -288,11 +289,6 @@ public void downloadPackage(JSONObject updatePackage, String expectedBundleFileN
288289
"Warning! JWT signature exists in codepush update but code integrity check couldn't be performed because there is no public key configured. " +
289290
"Please ensure that public key is properly configured within your application."
290291
);
291-
CodePushUpdateUtils.verifyFolderHash(newUpdateFolderPath, newUpdateHash);
292-
} else {
293-
if (isDiffUpdate) {
294-
CodePushUpdateUtils.verifyFolderHash(newUpdateFolderPath, newUpdateHash);
295-
}
296292
}
297293
}
298294

ios/CodePush/CodePushPackage.m

Lines changed: 17 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -237,27 +237,28 @@ + (void)downloadPackage:(NSDictionary *)updatePackage
237237
}
238238

239239
CPLog((isDiffUpdate) ? @"Applying diff update." : @"Applying full update.");
240-
240+
241+
if (![CodePushUpdateUtils verifyFolderHash:newUpdateFolderPath
242+
expectedHash:newUpdateHash
243+
error:&error]) {
244+
CPLog(@"The update contents failed the data integrity check.");
245+
if (!error) {
246+
error = [CodePushErrorUtils errorWithMessage:@"The update contents failed the data integrity check."];
247+
}
248+
249+
failCallback(error);
250+
return;
251+
} else {
252+
CPLog(@"The update contents succeeded the data integrity check.");
253+
}
254+
241255
BOOL isSignatureVerificationEnabled = (publicKey != nil);
242-
256+
243257
NSString *signatureFilePath = [CodePushUpdateUtils getSignatureFilePath:newUpdateFolderPath];
244258
BOOL isSignatureAppearedInBundle = [[NSFileManager defaultManager] fileExistsAtPath:signatureFilePath];
245-
259+
246260
if (isSignatureVerificationEnabled) {
247261
if (isSignatureAppearedInBundle) {
248-
if (![CodePushUpdateUtils verifyFolderHash:newUpdateFolderPath
249-
expectedHash:newUpdateHash
250-
error:&error]) {
251-
CPLog(@"The update contents failed the data integrity check.");
252-
if (!error) {
253-
error = [CodePushErrorUtils errorWithMessage:@"The update contents failed the data integrity check."];
254-
}
255-
256-
failCallback(error);
257-
return;
258-
} else {
259-
CPLog(@"The update contents succeeded the data integrity check.");
260-
}
261262
BOOL isSignatureValid = [CodePushUpdateUtils verifyUpdateSignatureFor:newUpdateFolderPath
262263
expectedHash:newUpdateHash
263264
withPublicKey:publicKey
@@ -283,29 +284,10 @@ + (void)downloadPackage:(NSDictionary *)updatePackage
283284
}
284285

285286
} else {
286-
BOOL needToVerifyHash;
287287
if (isSignatureAppearedInBundle) {
288288
CPLog(@"Warning! JWT signature exists in codepush update but code integrity check couldn't be performed" \
289289
" because there is no public key configured. " \
290290
"Please ensure that public key is properly configured within your application.");
291-
needToVerifyHash = true;
292-
} else {
293-
needToVerifyHash = isDiffUpdate;
294-
}
295-
if(needToVerifyHash){
296-
if (![CodePushUpdateUtils verifyFolderHash:newUpdateFolderPath
297-
expectedHash:newUpdateHash
298-
error:&error]) {
299-
CPLog(@"The update contents failed the data integrity check.");
300-
if (!error) {
301-
error = [CodePushErrorUtils errorWithMessage:@"The update contents failed the data integrity check."];
302-
}
303-
304-
failCallback(error);
305-
return;
306-
} else {
307-
CPLog(@"The update contents succeeded the data integrity check.");
308-
}
309291
}
310292
}
311293
} else {

0 commit comments

Comments
 (0)