Copilot commented on code in PR #14669: URL: https://github.com/apache/gravitino/pull/14669#discussion_r4479929909
########## 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: The `Content-Type` assertion is too strict for APIs that may return vendor JSON media types (e.g., `application/vnd.gravitino.v1+json`) or `application/*+json`. Consider relaxing this assertion to accept `application/json` as well as `application/*+json` (or explicitly the vendor type) to avoid false negatives while still ensuring a JSON response. ########## 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: Logging client-caused 400s at WARN with full stack traces can be very noisy in production (especially if clients routinely send invalid inputs), increasing log volume and potentially obscuring real warnings. Consider logging without the stack trace at WARN (e.g., message + exception message), and/or downgrading to INFO/DEBUG with stack traces only at DEBUG. ########## 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: `authUtils` is created but not used/stubbed within this test method. If it's not needed, removing it will simplify the test. If it is needed to prevent real static calls, consider adding an explicit stub (or a brief comment) to document which call is being intercepted and why. -- 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]
