zhoujinsong commented on code in PR #4118:
URL: https://github.com/apache/amoro/pull/4118#discussion_r2993236448


##########
amoro-ams/src/main/java/org/apache/amoro/server/dashboard/DashboardServer.java:
##########
@@ -408,8 +419,30 @@ public void preHandleRequest(Context ctx) {
     boolean isWebRequest = 
X_REQUEST_SOURCE_WEB.equalsIgnoreCase(requestSource);
 
     if (isWebRequest) {
-      if (null == ctx.sessionAttribute("user")) {
-        throw new ForbiddenException("User session attribute is missed for 
url: " + uriPath);
+      LoginController.SessionInfo user = ctx.sessionAttribute("user");
+      if (user == null) {
+        throw new ForbiddenException(LOGIN_REQUIRED_MESSAGE);
+      }
+      if (authorizationManager.isAuthorizationEnabled()) {
+        AuthorizationRequest request =
+            privilegeMapper
+                .resolve(ctx)
+                .orElseThrow(
+                    () ->
+                        new ForbiddenException(
+                            "No authorization mapping for request: "
+                                + ctx.method()
+                                + " "

Review Comment:
   **[Bug] Unmapped request paths return 403 instead of 404**
   
   When `privilegeMapper.resolve(ctx)` returns `Optional.empty()` (i.e., the 
request path is not covered by `privilege_mapping.yaml`), the code throws 
`ForbiddenException` with the message `"No authorization mapping for request: 
..."`. This means any new or typo-ed API endpoint silently returns 403 rather 
than 404, which is confusing to callers and leaks information about the 
authorization layer.
   
   A missing mapping means the resource doesn't exist or wasn't declared, not 
that the user lacks permission. Consider returning 404 for unmapped paths, or 
fall back to allowing the request (to avoid breaking undeclared internal 
endpoints).



##########
amoro-ams/src/main/java/org/apache/amoro/server/authorization/DashboardPrivilegeMapper.java:
##########
@@ -0,0 +1,159 @@
+/*
+ * 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.amoro.server.authorization;
+
+import io.javalin.http.Context;
+import org.apache.amoro.shade.guava32.com.google.common.base.Preconditions;
+import org.yaml.snakeyaml.Yaml;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.nio.charset.StandardCharsets;
+import java.util.Collections;
+import java.util.LinkedHashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Optional;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+public class DashboardPrivilegeMapper {
+  private static final String MAPPING_RESOURCE = 
"authorization/privilege_mapping.yaml";
+
+  private final List<DashboardPrivilegeMappingRule> mappingRules;
+
+  public DashboardPrivilegeMapper() {
+    this.mappingRules = loadRules();
+  }
+
+  public Optional<AuthorizationRequest> resolve(Context ctx) {
+    String method = ctx.method().toUpperCase();
+    String path = ctx.path();
+    return mappingRules.stream()
+        .filter(rule -> rule.matches(method, path))
+        .map(DashboardPrivilegeMappingRule::getAuthorizationRequest)
+        .findFirst();
+  }
+
+  private static List<DashboardPrivilegeMappingRule> loadRules() {
+    Map<String, Object> root = loadYaml(MAPPING_RESOURCE);
+    Object mappingsObject = root.get("mappings");
+    if (!(mappingsObject instanceof List)) {
+      throw new IllegalArgumentException(
+          "Invalid dashboard privilege mapping resource: missing mappings 
list");

Review Comment:
   **[Bug] Path-prefix matching can produce false positives**
   
   `prefixes.stream().anyMatch(path::startsWith)` will match 
`/api/ams/v1/catalogs-extra` against the prefix `/api/ams/v1/catalogs`, 
granting unintended privileges to paths that merely share a common string 
prefix.
   
   Add a separator boundary check: `path.startsWith(prefix + "/") || 
path.equals(prefix)`.



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