Skip to content

Commit

Permalink
Fix null pointer exception (microsoft#2761)
Browse files Browse the repository at this point in the history
* Fix  npe for copyNecessaryFilesFromCurrentPackage

* Reformat code
  • Loading branch information
DordeDimitrijev authored Sep 23, 2024
1 parent 94bcd15 commit b6a5bea
Showing 1 changed file with 15 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

import java.security.interfaces.*;


import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
Expand Down Expand Up @@ -86,8 +85,12 @@ private static String computeHash(InputStream dataStream) {
throw new CodePushUnknownException("Unable to compute hash of update contents.", e);
} finally {
try {
if (digestInputStream != null) digestInputStream.close();
if (dataStream != null) dataStream.close();
if (digestInputStream != null) {
digestInputStream.close();
}
if (dataStream != null) {
dataStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
Expand All @@ -98,7 +101,11 @@ private static String computeHash(InputStream dataStream) {
}

public static void copyNecessaryFilesFromCurrentPackage(String diffManifestFilePath, String currentPackageFolderPath, String newPackageFolderPath) throws IOException {
FileUtils.copyDirectoryContents(currentPackageFolderPath, newPackageFolderPath);
if (currentPackageFolderPath == null || !new File(currentPackageFolderPath).exists()) {
CodePushUtils.log("Unable to copy files from current package during diff update, because currentPackageFolderPath is invalid.");
return;
}
FileUtils.copyDirectoryContents(currentPackageFolderPath, newPackageFolderPath);
JSONObject diffManifest = CodePushUtils.getJsonObjectFromFile(diffManifestFilePath);
try {
JSONArray deletedFiles = diffManifest.getJSONArray("deletedFiles");
Expand Down Expand Up @@ -184,7 +191,7 @@ public static void verifyFolderHash(String folderPath, String expectedHash) {
public static Map<String, Object> verifyAndDecodeJWT(String jwt, PublicKey publicKey) {
try {
SignedJWT signedJWT = SignedJWT.parse(jwt);
JWSVerifier verifier = new RSASSAVerifier((RSAPublicKey)publicKey);
JWSVerifier verifier = new RSASSAVerifier((RSAPublicKey) publicKey);
if (signedJWT.verify(verifier)) {
Map<String, Object> claims = signedJWT.getJWTClaimsSet().getClaims();
CodePushUtils.log("JWT verification succeeded, payload content: " + claims.toString());
Expand Down Expand Up @@ -217,7 +224,7 @@ public static PublicKey parsePublicKey(String stringPublicKey) {
}
}

public static String getSignatureFilePath(String updateFolderPath){
public static String getSignatureFilePath(String updateFolderPath) {
return CodePushUtils.appendPathComponent(
CodePushUtils.appendPathComponent(updateFolderPath, CodePushConstants.CODE_PUSH_FOLDER_PREFIX),
CodePushConstants.BUNDLE_JWT_FILE
Expand Down Expand Up @@ -254,7 +261,7 @@ public static void verifyUpdateSignature(String folderPath, String packageHash,
throw new CodePushInvalidUpdateException("The update could not be verified because it was not signed by a trusted party.");
}

final String contentHash = (String)claims.get("contentHash");
final String contentHash = (String) claims.get("contentHash");
if (contentHash == null) {
throw new CodePushInvalidUpdateException("The update could not be verified because the signature did not specify a content hash.");
}
Expand All @@ -265,4 +272,4 @@ public static void verifyUpdateSignature(String folderPath, String packageHash,

CodePushUtils.log("The update contents succeeded the code signing check.");
}
}
}

0 comments on commit b6a5bea

Please sign in to comment.