rich7420 commented on code in PR #11387:
URL: https://github.com/apache/ozone/pull/11387#discussion_r4704537799


##########
hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/endpoint/EndpointBase.java:
##########
@@ -489,6 +492,29 @@ protected Map<String, String> getCustomMetadataFromHeaders(
     return customMetadata;
   }
 
+  private static String decodeRfc2047MetadataValue(String value) {
+    try {
+      return MimeUtility.decodeText(value);
+    } catch (UnsupportedEncodingException ex) {
+      return value;
+    }
+  }
+
+  private static String encodeRfc2047MetadataValue(String value) {
+    if (value.chars().allMatch(EndpointBase::isSafeAsciiMetadataCharacter)) {

Review Comment:
   `encodeText` leaves CR/LF alone, so `a\r\nb` came back with spaces for me. 
Force the encoding when this check fails?



##########
hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/endpoint/EndpointBase.java:
##########
@@ -470,7 +471,9 @@ protected Map<String, String> getCustomMetadataFromHeaders(
           throw ex;
         }
         List<String> values = requestHeaders.get(key);
-        String value = StringUtils.join(values, ",");
+        String value = values.stream()
+            .map(EndpointBase::decodeRfc2047MetadataValue)

Review Comment:
   Maybe a test for the 2 KB limit on the decoded value? Measuring the raw 
header still passed everything.



##########
hadoop-ozone/s3gateway/pom.xml:
##########
@@ -167,6 +167,11 @@
       <groupId>org.apache.ratis</groupId>
       <artifactId>ratis-common</artifactId>
     </dependency>
+    <dependency>
+      <groupId>org.eclipse.angus</groupId>
+      <artifactId>jakarta.mail</artifactId>
+      <version>2.0.4</version>

Review Comment:
   Version in the root pom like the others? Also `angus-activation` can't load 
here (no `jakarta.activation`), maybe exclude it.



##########
hadoop-ozone/integration-test-s3/src/test/java/org/apache/hadoop/ozone/s3/awssdk/v2/AbstractS3SDKV2Tests.java:
##########
@@ -538,6 +538,22 @@ public void testPutObject() {
     assertEquals("\"37b51d194a7513e45b56f6524f2d51f2\"", 
getObjectResponse.eTag());
   }
 
+  @Test
+  public void testPutObjectWithRfc2047Metadata() {
+    final String bucketName = getBucketName();
+    final String keyName = getKeyName();
+    final String metadataValue = "café";

Review Comment:
   The SDK sends `café` raw, so this never hits the decode path. Send 
`=?UTF-8?B?Y2Fmw6k=?=` instead?



##########
hadoop-ozone/s3gateway/src/test/java/org/apache/hadoop/ozone/s3/endpoint/TestObjectGet.java:
##########
@@ -282,6 +282,91 @@ public void getKeyWithCustomMetadata() throws IOException, 
OS3Exception {
         response.getHeaderString(CUSTOM_METADATA_HEADER_PREFIX + "meta1"));
   }
 
+  @Test
+  public void getKeyWithQEncodedRfc2047CustomMetadata()
+      throws IOException, OS3Exception {
+    final String keyName = "key-with-rfc2047-meta";
+    final String metaValue = "Hello Worldé";
+    final String encodedMetaValue = "=?UTF-8?Q?Hello_World=C3=A9?=";
+    MultivaluedMap<String, String> requestHeaders = new MultivaluedHashMap<>();
+    requestHeaders.putSingle(CUSTOM_METADATA_HEADER_PREFIX + "meta1",
+        encodedMetaValue);
+    when(headers.getRequestHeaders()).thenReturn(requestHeaders);
+
+    assertSucceeds(() -> put(rest, BUCKET_NAME, keyName, CONTENT));
+
+    assertEquals(metaValue, bucket.getKey(keyName).getMetadata().get("meta1"));
+    Response response = get(rest, BUCKET_NAME, keyName);
+    assertEquals(encodedMetaValue,
+        response.getHeaderString(CUSTOM_METADATA_HEADER_PREFIX + "meta1"));
+  }
+
+  @Test
+  public void getKeyWithBase64Rfc2047CustomMetadata()
+      throws IOException, OS3Exception {
+    final String keyName = "key-with-base64-rfc2047-meta";
+    final String metaValue = "café";
+    final String encodedMetaValue = "=?UTF-8?B?Y2Fmw6k=?=";
+    final String responseMetaValue = "=?UTF-8?Q?caf=C3=A9?=";
+    MultivaluedMap<String, String> requestHeaders = new MultivaluedHashMap<>();
+    requestHeaders.putSingle(CUSTOM_METADATA_HEADER_PREFIX + "meta1",
+        encodedMetaValue);
+    when(headers.getRequestHeaders()).thenReturn(requestHeaders);
+
+    assertSucceeds(() -> put(rest, BUCKET_NAME, keyName, CONTENT));
+
+    assertEquals(metaValue, bucket.getKey(keyName).getMetadata().get("meta1"));
+    Response response = get(rest, BUCKET_NAME, keyName);
+    assertEquals(responseMetaValue,
+        response.getHeaderString(CUSTOM_METADATA_HEADER_PREFIX + "meta1"));
+  }
+
+  @Test
+  public void getKeyDecodesMultipleRfc2047EncodedWords()
+      throws IOException, OS3Exception {
+    final String keyName = "key-with-multiple-rfc2047-words";
+    final String metaValue = "=?UTF-8?Q?a?= =?UTF-8?Q?b?=";
+    MultivaluedMap<String, String> requestHeaders = new MultivaluedHashMap<>();
+    requestHeaders.putSingle(CUSTOM_METADATA_HEADER_PREFIX + "meta1",
+        metaValue);
+    when(headers.getRequestHeaders()).thenReturn(requestHeaders);
+
+    assertSucceeds(() -> put(rest, BUCKET_NAME, keyName, CONTENT));
+
+    assertEquals("ab", bucket.getKey(keyName).getMetadata().get("meta1"));
+  }
+
+  @ParameterizedTest
+  @CsvSource({

Review Comment:
   Could the tests right above and below be rows here too? Maybe plus an 
unknown-charset row for the fallback.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to