This is an automated email from the ASF dual-hosted git repository.

ChenSammi pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/ozone.git


The following commit(s) were added to refs/heads/master by this push:
     new 2a9f38eb3c0 HDDS-17703. Reject lifecycle filter tags with missing 
fields (#11975)
2a9f38eb3c0 is described below

commit 2a9f38eb3c034d10e50792d1144b8a52f2b01c44
Author: Billy Shen <[email protected]>
AuthorDate: Tue Nov 17 12:12:48 2026 +0800

    HDDS-17703. Reject lifecycle filter tags with missing fields (#11975)
---
 .../apache/hadoop/ozone/om/helpers/OmLCFilter.java |  8 ++++
 .../hadoop/ozone/om/helpers/OmLifecycleUtils.java  |  4 ++
 .../om/helpers/TestOmLifeCycleConfiguration.java   | 44 +++++++++++++++++++++
 .../s3/endpoint/S3LifecycleConfiguration.java      |  9 +++++
 .../endpoint/TestS3LifecycleConfigurationPut.java  | 45 ++++++++++++++++++++++
 5 files changed, 110 insertions(+)

diff --git 
a/hadoop-ozone/common/src/main/java/org/apache/hadoop/ozone/om/helpers/OmLCFilter.java
 
b/hadoop-ozone/common/src/main/java/org/apache/hadoop/ozone/om/helpers/OmLCFilter.java
index b06ffd67a96..771bd4fdf0e 100644
--- 
a/hadoop-ozone/common/src/main/java/org/apache/hadoop/ozone/om/helpers/OmLCFilter.java
+++ 
b/hadoop-ozone/common/src/main/java/org/apache/hadoop/ozone/om/helpers/OmLCFilter.java
@@ -41,6 +41,7 @@ public final class OmLCFilter {
   private final boolean directoryStylePrefix;
   private final String tagKey;
   private final String tagValue;
+  private final boolean tagSet;
   private final OmLifecycleRuleAndOperator andOperator;
 
   private OmLCFilter() {
@@ -57,6 +58,7 @@ private OmLCFilter(Builder builder) {
     this.andOperator = builder.andOperator;
     this.tagKey = builder.tagKey;
     this.tagValue = builder.tagValue;
+    this.tagSet = builder.tagSet;
   }
 
   /**
@@ -75,6 +77,10 @@ private OmLCFilter(Builder builder) {
    * @throws OMException if the filter is invalid.
    */
   public void valid(BucketLayout layout) throws OMException {
+    if (tagSet && (tagKey == null || tagValue == null)) {
+      throw new OMException("A Tag must have a Key and a Value.", 
OMException.ResultCodes.INVALID_REQUEST);
+    }
+
     boolean hasPrefix = prefix != null;
     boolean hasTag = hasTag();
     boolean hasAndOperator = andOperator != null;
@@ -209,6 +215,7 @@ public static class Builder {
     private String prefix = null;
     private String tagKey = null;
     private String tagValue = null;
+    private boolean tagSet = false;
     private OmLifecycleRuleAndOperator andOperator = null;
 
     public Builder setPrefix(String lcPrefix) {
@@ -219,6 +226,7 @@ public Builder setPrefix(String lcPrefix) {
     public Builder setTag(String key, String value) {
       this.tagKey = key;
       this.tagValue = value;
+      this.tagSet = true;
       return this;
     }
 
diff --git 
a/hadoop-ozone/common/src/main/java/org/apache/hadoop/ozone/om/helpers/OmLifecycleUtils.java
 
b/hadoop-ozone/common/src/main/java/org/apache/hadoop/ozone/om/helpers/OmLifecycleUtils.java
index 32781280919..827b8929f6d 100644
--- 
a/hadoop-ozone/common/src/main/java/org/apache/hadoop/ozone/om/helpers/OmLifecycleUtils.java
+++ 
b/hadoop-ozone/common/src/main/java/org/apache/hadoop/ozone/om/helpers/OmLifecycleUtils.java
@@ -116,6 +116,10 @@ public static void validateTagUniqAndLength(Map<String, 
String> tags) throws OME
             MAX_TAG_KEY_LENGTH, OMException.ResultCodes.INVALID_REQUEST);
       }
 
+      if (value == null) {
+        throw new OMException("A Tag must have a Value.", 
OMException.ResultCodes.INVALID_REQUEST);
+      }
+
       if (!StringUtils.isEmpty(value) && 
value.getBytes(StandardCharsets.UTF_8).length > MAX_TAG_VALUE_LENGTH) {
         throw new OMException("A Tag's Value must be a length between 0 and " +
             MAX_TAG_VALUE_LENGTH, OMException.ResultCodes.INVALID_REQUEST);
diff --git 
a/hadoop-ozone/common/src/test/java/org/apache/hadoop/ozone/om/helpers/TestOmLifeCycleConfiguration.java
 
b/hadoop-ozone/common/src/test/java/org/apache/hadoop/ozone/om/helpers/TestOmLifeCycleConfiguration.java
index 4a04c9d2ca4..a73654d3ad1 100644
--- 
a/hadoop-ozone/common/src/test/java/org/apache/hadoop/ozone/om/helpers/TestOmLifeCycleConfiguration.java
+++ 
b/hadoop-ozone/common/src/test/java/org/apache/hadoop/ozone/om/helpers/TestOmLifeCycleConfiguration.java
@@ -39,6 +39,8 @@
 import org.apache.hadoop.ozone.om.helpers.OmLCFilter.Tag;
 import 
org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.LifecycleConfiguration;
 import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.CsvSource;
 
 /**
  * Test lifecycle configuration related entities.
@@ -263,4 +265,46 @@ public void testOMStartupWithPastExpirationDate() throws 
OMException {
     assertDoesNotThrow(configFromProto::valid);
   }
 
+  @ParameterizedTest
+  @CsvSource({"false, , value", "false, key,", "false, ,", "true, , value", 
"true, key,"})
+  public void testTagPresenceValidation(boolean useAnd, String key, String 
value) {
+    OmLCFilter filter;
+    String expectedMessage;
+    if (useAnd) {
+      OmLifecycleRuleAndOperator and = new OmLifecycleRuleAndOperator.Builder()
+          .setPrefix("prefix")
+          .addTag(key, value)
+          .build();
+      filter = new OmLCFilter.Builder().setAndOperator(and).build();
+      expectedMessage = key == null ? "A Tag's Key must be a length between 1 
and" : "A Tag must have a Value.";
+    } else {
+      filter = new OmLCFilter.Builder().setTag(key, value).build();
+      expectedMessage = "A Tag must have a Key and a Value.";
+    }
+    assertOMException(() -> configurationWithFilter(filter).build(), 
INVALID_REQUEST, expectedMessage);
+  }
+
+  @Test
+  public void testValidTagPresence() {
+    OmLCFilter emptyFilter = new OmLCFilter.Builder().build();
+    OmLCFilter emptyValue = new OmLCFilter.Builder()
+        .setTag("key", "")
+        .build();
+    OmLifecycleRuleAndOperator and = new OmLifecycleRuleAndOperator.Builder()
+        .setPrefix("prefix")
+        .addTag("key", "")
+        .build();
+    assertDoesNotThrow(() -> configurationWithFilter(emptyFilter).build());
+    assertDoesNotThrow(() -> configurationWithFilter(emptyValue).build());
+    assertDoesNotThrow(() -> configurationWithFilter(new 
OmLCFilter.Builder().setAndOperator(and).build()).build());
+  }
+
+  private OmLifecycleConfiguration.Builder configurationWithFilter(OmLCFilter 
filter) {
+    OmLCRule rule = new OmLCRule.Builder()
+        .setId("rule")
+        .setFilter(filter)
+        .setAction(new OmLCExpiration.Builder().setDays(1).build())
+        .build();
+    return getOmLifecycleConfiguration("volume", "bucket", 
Collections.singletonList(rule));
+  }
 }
diff --git 
a/hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/endpoint/S3LifecycleConfiguration.java
 
b/hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/endpoint/S3LifecycleConfiguration.java
index f7b628f23a6..d39ab7f0a09 100644
--- 
a/hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/endpoint/S3LifecycleConfiguration.java
+++ 
b/hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/endpoint/S3LifecycleConfiguration.java
@@ -475,6 +475,9 @@ private OmLCFilter convertToOmFilter(Filter filter) throws 
OMException {
       builder.setPrefix(filter.getPrefix());
     }
     if (filter.getTag() != null) {
+      if (filter.getTag().getKey() == null || filter.getTag().getValue() == 
null) {
+        throw S3ErrorTable.newError(S3ErrorTable.MALFORMED_XML);
+      }
       builder.setTag(filter.getTag().getKey(), filter.getTag().getValue());
     }
     if (filter.getAndOperator() != null) {
@@ -498,6 +501,12 @@ private OmLifecycleRuleAndOperator 
convertToOmAndOperator(AndOperator andOperato
     }
     if (andOperator.getTags() != null) {
       Map<String, String> tags = andOperator.getTags().stream()
+          .map(tag -> {
+            if (tag.getKey() == null || tag.getValue() == null) {
+              throw S3ErrorTable.newError(S3ErrorTable.MALFORMED_XML);
+            }
+            return tag;
+          })
           .collect(Collectors.toMap(Tag::getKey, Tag::getValue));
       builder.setTags(tags);
     }
diff --git 
a/hadoop-ozone/s3gateway/src/test/java/org/apache/hadoop/ozone/s3/endpoint/TestS3LifecycleConfigurationPut.java
 
b/hadoop-ozone/s3gateway/src/test/java/org/apache/hadoop/ozone/s3/endpoint/TestS3LifecycleConfigurationPut.java
index e9c23a835c6..ba2db1bd38c 100644
--- 
a/hadoop-ozone/s3gateway/src/test/java/org/apache/hadoop/ozone/s3/endpoint/TestS3LifecycleConfigurationPut.java
+++ 
b/hadoop-ozone/s3gateway/src/test/java/org/apache/hadoop/ozone/s3/endpoint/TestS3LifecycleConfigurationPut.java
@@ -116,6 +116,51 @@ public void testLifecycleConfigurationFailWithEmptyBody() 
throws Exception {
     }
   }
 
+  @ParameterizedTest
+  @ValueSource(strings = {"<Key>a</Key>", "<Value>b</Value>", ""})
+  public void testMissingTagFieldsPreserveConfiguration(String fields) throws 
Exception {
+    bucketEndpoint.put("bucket1", onePrefix());
+    for (String filter : new String[] {"<Tag>" + fields + "</Tag>",
+        "<And><Prefix>prefix/</Prefix><Tag>" + fields + "</Tag></And>"}) {
+      OS3Exception exception = assertErrorResponse(MALFORMED_XML,
+          () -> bucketEndpoint.put("bucket1", lifecycleWithFilter(filter)));
+      assertEquals(HTTP_BAD_REQUEST, exception.getHttpCode());
+
+      // make sure the bucket keeps its current configuration
+      try (Response response = bucketEndpoint.get("bucket1")) {
+        S3LifecycleConfiguration configuration = (S3LifecycleConfiguration) 
response.getEntity();
+        assertThat(configuration.getRules()).hasSize(1);
+        assertThat(configuration.getRules().get(0).getId()).isEqualTo("remove 
logs after 30 days");
+        
assertThat(configuration.getRules().get(0).getPrefix()).isEqualTo("prefix/");
+      }
+    }
+  }
+
+  @ParameterizedTest
+  @ValueSource(booleans = {false, true})
+  public void testEmptyTagValue(boolean and) throws Exception {
+    String tagString = "<Tag><Key>a</Key><Value/></Tag>";
+    String filterString = and ? "<And><Prefix>prefix/</Prefix>" + tagString + 
"</And>" : tagString;
+    try (Response response = bucketEndpoint.put("bucket1", 
lifecycleWithFilter(filterString))) {
+      assertThat(response.getStatus()).isEqualTo(HTTP_OK);
+    }
+
+    // Verify the empty tag value is preserved.
+    try (Response response = bucketEndpoint.get("bucket1")) {
+      S3LifecycleConfiguration configuration = (S3LifecycleConfiguration) 
response.getEntity();
+      S3LifecycleConfiguration.Filter filter = 
configuration.getRules().get(0).getFilter();
+      S3LifecycleConfiguration.Tag tag = and ? 
filter.getAndOperator().getTags().get(0) : filter.getTag();
+      assertThat(tag.getKey()).isEqualTo("a");
+      assertThat(tag.getValue()).isEmpty();
+    }
+  }
+
+  private static InputStream lifecycleWithFilter(String filter) {
+    String xml = "<LifecycleConfiguration><Rule><ID>rule</ID><Filter>" + filter
+        + 
"</Filter><Status>Enabled</Status><Expiration><Days>1</Days></Expiration></Rule></LifecycleConfiguration>";
+    return new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8));
+  }
+
   @Test
   public void testLifecycleConfigurationFailWithNonExistentBucket()
       throws Exception {


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

Reply via email to