jerryshao commented on code in PR #14881:
URL: https://github.com/apache/gravitino/pull/14881#discussion_r4547745799


##########
clients/client-java/src/main/java/org/apache/gravitino/client/GenericDomain.java:
##########
@@ -293,18 +291,10 @@ public int hashCode() {
   }
 
   static String domainPath(String metalake, String name) {
-    validateName(name);
     return "api/metalakes/"
         + RESTUtils.encodeString(metalake)
         + "/domains/"
-        + RESTUtils.encodeString(name);
-  }
-
-  static void validateName(String name) {
-    Preconditions.checkArgument(StringUtils.isNotBlank(name), "domain name 
must not be blank");
-    // A proxy can normalize a dot segment and route the request to a 
different resource.
-    Preconditions.checkArgument(
-        !".".equals(name) && !"..".equals(name), "Unsafe domain path segment: 
%s", name);
+        + RESTUtils.encodeDomainPathSegment(name);

Review Comment:
   Updated both SDK comments in 35ac637f62. They now explain that each domain 
scope is sent as a separate query parameter.



##########
common/src/main/java/org/apache/gravitino/rest/RESTUtils.java:
##########
@@ -110,6 +112,32 @@ public static String encodeString(String toEncode) {
     return formEncode(toEncode).replace(FORM_ENCODED_SPACE, 
PERCENT_ENCODED_SPACE);
   }
 
+  /**
+   * Encodes a domain name as a single path segment, guarding against path 
normalization.
+   *
+   * @param name The domain name.
+   * @return The encoded path segment.
+   */
+  public static String encodeDomainPathSegment(String name) {
+    Preconditions.checkArgument(
+        StringUtils.isNotBlank(name) && !".".equals(name) && 
!"..".equals(name),
+        "Unsafe domain path segment: %s",
+        name);
+    return encodeString(name);
+  }
+
+  /**
+   * Encodes the full name used to address a metadata object in a REST path.
+   *
+   * @param object The metadata object.
+   * @return The encoded path segment.
+   */
+  public static String encodeMetadataObjectName(MetadataObject object) {

Review Comment:
   Fixed in 35ac637f62. All seven Java path builders and the five Python 
counterparts now use the shared metadata-object encoder. For Domain targets it 
guards blank names and dot segments before constructing the URL. Other object 
types keep their existing encoding. Both SDKs now exercise this through a 
tag-operation path as well.



##########
server/src/main/java/org/apache/gravitino/server/web/filter/GravitinoInterceptionService.java:
##########
@@ -182,10 +183,21 @@ public Object invoke(MethodInvocation methodInvocation) 
throws Throwable {
           String expression = expressionAnnotation.expression();
           Object[] args = methodInvocation.getArguments();
           Optional<String> entityType = 
extractMetadataObjectTypeFromParameters(parameters, args);
+          Map<String, Object> pathParams = 
Utils.extractPathParamsFromParameters(parameters, args);
+          // Grant and owner expressions may resolve this path before the 
resource method runs.
+          Object objectType =
+              pathParams.getOrDefault("p_metadataObjectType", 
pathParams.get("p_type"));
+          if ("domain".equalsIgnoreCase(String.valueOf(objectType))
+              && pathParams.containsKey("p_fullName")) {
+            try {
+              NameValidationUtils.validateName("domain", (String) 
pathParams.get("p_fullName"));

Review Comment:
   I checked the REST resource signatures. Every current fullName path 
parameter is declared as String, so I kept the cast. String.valueOf(null) would 
turn a missing value into the valid domain name `null`. The current code passes 
null through to name validation and rejects it.



##########
core/src/main/java/org/apache/gravitino/utils/NameValidationUtils.java:
##########
@@ -0,0 +1,59 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.gravitino.utils;
+
+import java.util.regex.Pattern;
+import org.apache.gravitino.MetadataObjects;
+
+/** Naming rules shared by catalogs and domains. */
+public final class NameValidationUtils {
+  /** Reserved name of the system catalog. */
+  public static final String SYSTEM_CATALOG_RESERVED_NAME = "system";
+
+  private static final Pattern NAME_PATTERN = 
Pattern.compile("^\\w[\\w-]{0,63}$");
+
+  private NameValidationUtils() {}
+
+  /**
+   * Validates a catalog or domain name.
+   *
+   * @param type The entity type used in validation messages.
+   * @param name The proposed name.
+   * @throws IllegalArgumentException If the name is reserved or illegal.
+   */
+  public static void validateName(String type, String name) {
+    if (SYSTEM_CATALOG_RESERVED_NAME.equalsIgnoreCase(name)
+        || MetadataObjects.METADATA_OBJECT_RESERVED_NAME.equals(name)) {
+      throw new IllegalArgumentException("The " + type + " name '" + name + "' 
is reserved.");
+    }
+    if (name == null || !NAME_PATTERN.matcher(name).matches()) {
+      throw new IllegalArgumentException("The " + type + " name '" + name + "' 
is illegal.");
+    }
+  }
+
+  /**
+   * Validates the domain names in a member filter.
+   *
+   * @param names The requested domain names.
+   * @throws IllegalArgumentException If any name is reserved or illegal.
+   */
+  public static void validateDomainNames(Iterable<String> names) {
+    names.forEach(name -> validateName("domain", name));

Review Comment:
   Added explicit argument checks for a null name and a null scope collection 
in 35ac637f62. Each has its own diagnostic and throws IllegalArgumentException, 
preserving HTTP 400 handling. A unit test covers both cases.



-- 
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]

Reply via email to