mchades commented on code in PR #14669: URL: https://github.com/apache/gravitino/pull/14669#discussion_r4488507729
########## clients/client-java/src/test/java/org/apache/gravitino/client/integration/test/authorization/InvalidCatalogNameAuthorizationIT.java: ########## @@ -0,0 +1,72 @@ +/* + * 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.client.integration.test.authorization; + +import java.net.URI; +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; +import java.nio.charset.StandardCharsets; +import java.util.Base64; +import org.apache.gravitino.auth.AuthConstants; +import org.apache.gravitino.dto.responses.ErrorConstants; +import org.apache.gravitino.dto.responses.ErrorResponse; +import org.apache.gravitino.server.web.ObjectMapperProvider; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +/** Integration tests for invalid Catalog names with authorization enabled. */ +public class InvalidCatalogNameAuthorizationIT extends BaseRestApiAuthorizationIT { + + /** + * Verifies that URL-encoded whitespace in a Catalog name returns a JSON bad-request response. + * + * @param encodedName the percent-encoded invalid Catalog name + */ + @ParameterizedTest + @ValueSource(strings = {"%20", "%09", "%0A", "%20%09%0A"}) + public void testBlankCatalogNameReturnsBadRequest(String encodedName) throws Exception { + String authorization = + AuthConstants.AUTHORIZATION_BASIC_HEADER + + Base64.getEncoder() + .encodeToString((USER + ":dummy").getBytes(StandardCharsets.UTF_8)); + HttpRequest request = + HttpRequest.newBuilder() + .uri(new URI(serverUri + "/api/metalakes/" + METALAKE + "/catalogs/" + encodedName)) + .header(AuthConstants.HTTP_HEADER_AUTHORIZATION, authorization) + .header("Accept", "application/vnd.gravitino.v1+json") + .GET() + .build(); + + HttpResponse<String> response = + HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString()); + + Assertions.assertEquals(400, response.statusCode(), "Unexpected body: " + response.body()); + Assertions.assertTrue( + response.headers().firstValue("Content-Type").orElse("").startsWith("application/json")); Review Comment: `Utils.illegalArguments()` explicitly returns `application/json`. This test already sends the vendor `Accept` header, and `startsWith` allows parameters such as `charset`, so I’m keeping the assertion aligned with this error path. ########## server/src/main/java/org/apache/gravitino/server/web/filter/GravitinoInterceptionService.java: ########## @@ -302,6 +302,9 @@ public Object invoke(MethodInvocation methodInvocation) throws Throwable { } catch (IllegalNameIdentifierException ex) { LOG.warn("Invalid metadata object identifier during authorization", ex); return Utils.illegalArguments(ex.getMessage(), ex); + } catch (IllegalArgumentException ex) { + LOG.warn("Invalid request arguments during authorization", ex); + return Utils.illegalArguments(ex.getMessage(), ex); Review Comment: This matches the adjacent validation-error handlers. The same failure already logged a full stack trace at ERROR before this change. I’m keeping the existing logging style here; reducing noise across 400 handlers can be considered separately. ########## server/src/test/java/org/apache/gravitino/server/web/filter/TestGravitinoInterceptionService.java: ########## @@ -672,10 +675,50 @@ public void testUnexpectedIllegalArgumentExceptionRemainsInternalError() throws Response response = (Response) methodInterceptor.invoke(methodInvocation); ErrorResponse errorResponse = (ErrorResponse) response.getEntity(); - assertEquals( - "Authorization failed due to system internal error. Please contact administrator.", - errorResponse.getMessage()); - assertEquals(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), response.getStatus()); + assertEquals("Invalid authorizer configuration", errorResponse.getMessage()); + assertEquals(IllegalArgumentException.class.getSimpleName(), errorResponse.getType()); + assertEquals(ErrorConstants.ILLEGAL_ARGUMENTS_CODE, errorResponse.getCode()); + assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatus()); + verify(methodInvocation, never()).proceed(); + } + } + + /** + * Verifies that blank Catalog names are rejected before the resource method is invoked. + * + * @param catalogName the whitespace-only Catalog name + */ + @ParameterizedTest + @ValueSource(strings = {" ", "\t", "\n", " \t\n"}) + public void testBlankCatalogNameReturnsBadRequest(String catalogName) throws Throwable { + try (MockedStatic<PrincipalUtils> principals = mockStatic(PrincipalUtils.class); + MockedStatic<GravitinoAuthorizerProvider> providers = + mockStatic(GravitinoAuthorizerProvider.class); + MockedStatic<AuthorizationUtils> authUtils = mockStatic(AuthorizationUtils.class)) { Review Comment: `AuthorizationUtils.checkCurrentUser()` reads `GravitinoEnv` before catalog-name validation. The static mock bypasses that lookup to isolate the test; added a comment explaining this in 2e2d29fa60. -- 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]
