Skip to content

Commit

Permalink
Fix Adal decrypt crash (#1748)
Browse files Browse the repository at this point in the history
* Fix Adal decrypt crash

* change log and version

* Fix test
  • Loading branch information
mohitc1 authored Jul 26, 2023
1 parent ab1684f commit 723acf9
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import androidx.test.filters.Suppress;
import androidx.test.platform.app.InstrumentationRegistry;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand Down Expand Up @@ -128,15 +129,8 @@ public void testDecryptInvalidInput() throws
IOException, GeneralSecurityException {
final Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();
final StorageHelper storageHelper = new StorageHelper(context);
assertThrowsException(
IllegalArgumentException.class,
"is not valid, it must be greater of equal to 0",
new AndroidTestHelper.ThrowableRunnable() {
@Override
public void run() throws GeneralSecurityException, IOException, AuthenticationException {
storageHelper.decrypt("E1bad64");
}
});

Assert.assertEquals("E1bad64", storageHelper.decrypt("E1bad64"));

assertThrowsException(
IllegalArgumentException.class,
Expand Down
21 changes: 15 additions & 6 deletions adal/src/main/java/com/microsoft/aad/adal/StorageHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -254,14 +254,23 @@ public String decrypt(final String encryptedBlob)

int encodeVersionLength = encryptedBlob.charAt(0) - 'a';
if (encodeVersionLength <= 0) {
throw new IllegalArgumentException(String.format(
"Encode version length: '%s' is not valid, it must be greater of equal to 0",
encodeVersionLength));
final String message = String.format(
"Encode version length: '%s' is not valid, it must be greater of equal to 0. " +
"Assuming string is not encrypted. Returning input blob.",
encodeVersionLength
);
Logger.w(TAG + methodName, message);
return encryptedBlob;
}

if (!encryptedBlob.substring(1, 1 + encodeVersionLength).equals(ENCODE_VERSION)) {
throw new IllegalArgumentException(String.format(
"Encode version received was: '%s', Encode version supported is: '%s'", encryptedBlob,
ENCODE_VERSION));
final String message = String.format(
"Unsupported encode version received. Encode version supported is: %s. " +
"Assuming string is not encrypted. Returning input blob.",
ENCODE_VERSION
);
Logger.w(TAG + methodName, message);
return encryptedBlob;
}

final byte[] bytes = Base64
Expand Down
2 changes: 1 addition & 1 deletion adal/versioning/version.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#Wed Aug 01 15:24:11 PDT 2018
versionName=4.6.0
versionName=4.6.1
versionCode=1
4 changes: 4 additions & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
Version 4.6.1
-------------
- [PATCH] Fix crash due to IllegalArgumentException in StorageHelper.decrypt (#1748)

Version 4.6.0
-------------
- [MINOR] Remove dependency from common's storagehelper #1725
Expand Down

0 comments on commit 723acf9

Please sign in to comment.