Skip to content

Commit bcd711e

Browse files
authored
fix: treat null matdesc as empty (#448)
* fix: treat null matdesc as empty
1 parent 034bb89 commit bcd711e

File tree

2 files changed

+62
-1
lines changed

2 files changed

+62
-1
lines changed

src/main/java/software/amazon/encryption/s3/internal/ContentMetadataDecodingStrategy.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,8 @@ private ContentMetadata readFromMap(Map<String, String> metadata, GetObjectRespo
138138

139139
// Get encrypted data key encryption context
140140
final Map<String, String> encryptionContext = new HashMap<>();
141-
final String jsonEncryptionContext = metadata.get(MetadataKeyConstants.ENCRYPTED_DATA_KEY_CONTEXT);
141+
// The V2 client treats null value here as empty, do the same to avoid incompatibility
142+
String jsonEncryptionContext = metadata.getOrDefault(MetadataKeyConstants.ENCRYPTED_DATA_KEY_CONTEXT, "{}");
142143
// When the encryption context contains non-US-ASCII characters,
143144
// the S3 server applies an esoteric encoding to the object metadata.
144145
// Reverse that, to allow decryption.

src/test/java/software/amazon/encryption/s3/S3EncryptionClientCompatibilityTest.java

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import software.amazon.awssdk.services.s3.S3Client;
2626
import software.amazon.awssdk.services.s3.model.GetObjectRequest;
2727
import software.amazon.awssdk.services.s3.model.GetObjectResponse;
28+
import software.amazon.awssdk.services.s3.model.MetadataDirective;
2829
import software.amazon.awssdk.services.s3.model.PutObjectRequest;
2930
import software.amazon.encryption.s3.internal.InstructionFileConfig;
3031

@@ -168,6 +169,7 @@ public void AesGcmV2toV3() {
168169
// Cleanup
169170
deleteObject(BUCKET, objectKey, v3Client);
170171
v3Client.close();
172+
171173
}
172174

173175
@Test
@@ -902,4 +904,62 @@ public void AesWrapV1toV3FailsWhenLegacyModeDisabled() {
902904
deleteObject(BUCKET, objectKey, v3Client);
903905
v3Client.close();
904906
}
907+
908+
@Test
909+
public void nullMaterialDescriptionV3() {
910+
final String objectKey = appendTestSuffix("null-matdesc-v3");
911+
912+
// V2 Client
913+
EncryptionMaterialsProvider materialsProvider =
914+
new StaticEncryptionMaterialsProvider(new EncryptionMaterials(AES_KEY));
915+
AmazonS3EncryptionV2 v2Client = AmazonS3EncryptionClientV2.encryptionBuilder()
916+
.withEncryptionMaterialsProvider(materialsProvider)
917+
.build();
918+
919+
// V3 Client
920+
S3Client v3Client = S3EncryptionClient.builder()
921+
.aesKey(AES_KEY)
922+
.build();
923+
924+
// Asserts
925+
final String input = "AesGcmWithNullMatDesc";
926+
v2Client.putObject(BUCKET, objectKey, input);
927+
928+
ResponseBytes<GetObjectResponse> objectResponse = v3Client.getObjectAsBytes(builder -> builder
929+
.bucket(BUCKET)
930+
.key(objectKey));
931+
String output = objectResponse.asUtf8String();
932+
assertEquals(input, output);
933+
934+
// Now remove MatDesc - this must be done via CopyObject
935+
final String copyKey = objectKey + "copied";
936+
Map<String, String> modMd = new HashMap<>(objectResponse.response().metadata());
937+
modMd.remove("x-amz-meta-x-amz-matdesc");
938+
modMd.remove("x-amz-matdesc");
939+
v3Client.copyObject(builder -> builder
940+
.sourceBucket(BUCKET)
941+
.destinationBucket(BUCKET)
942+
.sourceKey(objectKey)
943+
.destinationKey(copyKey)
944+
.metadataDirective(MetadataDirective.REPLACE)
945+
.metadata(modMd)
946+
.build());
947+
948+
// V2
949+
String v2CopyOut = v2Client.getObjectAsString(BUCKET, copyKey);
950+
assertEquals(input, v2CopyOut);
951+
952+
// V3
953+
ResponseBytes<GetObjectResponse> objectResponseCopy = v3Client.getObjectAsBytes(builder -> builder
954+
.bucket(BUCKET)
955+
.key(copyKey));
956+
String outputCopy = objectResponseCopy.asUtf8String();
957+
assertEquals(input, outputCopy);
958+
959+
// Cleanup
960+
deleteObject(BUCKET, objectKey, v3Client);
961+
deleteObject(BUCKET, copyKey, v3Client);
962+
v3Client.close();
963+
964+
}
905965
}

0 commit comments

Comments
 (0)