Skip to content

Commit 01e33d1

Browse files
Skip malformed arns in OnDecrypt instead of failing
1 parent 55c2f3c commit 01e33d1

File tree

2 files changed

+11
-9
lines changed

2 files changed

+11
-9
lines changed

src/main/java/com/amazonaws/encryptionsdk/keyrings/KmsKeyring.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -127,16 +127,9 @@ public void onDecrypt(DecryptionMaterials decryptionMaterials, List<? extends En
127127
return;
128128
}
129129

130-
if (!encryptedDataKeys.stream()
131-
.filter(edk -> edk.getProviderId().equals(KMS_PROVIDER_ID))
132-
.map(edk -> new String(edk.getProviderInformation(), PROVIDER_ENCODING))
133-
.allMatch(KmsUtils::isArnWellFormed)) {
134-
throw new MalformedArnException("encryptedDataKeys contains a malformed ARN");
135-
}
136-
137130
final Set<String> configuredKeyIds = new HashSet<>(keyIds);
138131

139-
if(generatorKeyId != null) {
132+
if (generatorKeyId != null) {
140133
configuredKeyIds.add(generatorKeyId);
141134
}
142135

@@ -163,6 +156,12 @@ private boolean okToDecrypt(EncryptedDataKey encryptedDataKey, Set<String> confi
163156
return false;
164157
}
165158

159+
// If the key ARN cannot be parsed, skip it
160+
if(!isArnWellFormed(new String(encryptedDataKey.getProviderInformation(), PROVIDER_ENCODING)))
161+
{
162+
return false;
163+
}
164+
166165
// If this keyring is a discovery keyring, OnDecrypt MUST attempt to
167166
// decrypt every encrypted data key in the input encrypted data key list
168167
if (isDiscovery) {

src/test/java/com/amazonaws/encryptionsdk/keyrings/KmsKeyringTest.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,10 @@ void testMalformedArns() {
104104

105105
List<EncryptedDataKey> encryptedDataKeys = new ArrayList<>();
106106
encryptedDataKeys.add(new KeyBlob(KMS_PROVIDER_ID, "badArn".getBytes(PROVIDER_ENCODING), new byte[]{}));
107-
assertThrows(MalformedArnException.class, () -> keyring.onDecrypt(decryptionMaterials, encryptedDataKeys));
107+
encryptedDataKeys.add(ENCRYPTED_KEY_1);
108+
109+
keyring.onDecrypt(decryptionMaterials, encryptedDataKeys);
110+
assertEquals(PLAINTEXT_DATA_KEY, decryptionMaterials.getPlaintextDataKey());
108111

109112
// Malformed Arn for a non KMS provider shouldn't fail
110113
encryptedDataKeys.clear();

0 commit comments

Comments
 (0)