World's most powerful open data catalog for building a high-performance, geo-distributed and federated metadata lake.
Java ai-catalogdata-catalogdatalakefederated-querylakehousemetadatametalakemodel-catalogopendatacatalogskycomputingstratosphere
[#13218] fix(common): exclude credentials from OAuth2TokenResponse and SecretsResponse toString (#13221)
YangJie37343112c487
diff --git a/common/src/main/java/org/apache/gravitino/dto/responses/OAuth2TokenResponse.java b/common/src/main/java/org/apache/gravitino/dto/responses/OAuth2TokenResponse.javaindex 796295e205..5d5eba9de5 100644--- a/common/src/main/java/org/apache/gravitino/dto/responses/OAuth2TokenResponse.java+++ b/common/src/main/java/org/apache/gravitino/dto/responses/OAuth2TokenResponse.java@@ -35,6 +35,9 @@ import org.apache.commons.lang3.StringUtils;@EqualsAndHashCode(callSuper = true)@ToStringpublic class OAuth2TokenResponse extends BaseResponse {+ // Excluded from toString: these are bearer credentials, and the response object ending up in+ // a log line must not leak them.+ @ToString.Exclude@JsonProperty("access_token")private final String accessToken;@@ -51,6 +54,7 @@ public class OAuth2TokenResponse extends BaseResponse {@JsonProperty("scope")private final String scope;+ @ToString.Exclude@Nullable@JsonProperty("refresh_token")private final String refreshToken;diff --git a/common/src/main/java/org/apache/gravitino/dto/responses/SecretsResponse.java b/common/src/main/java/org/apache/gravitino/dto/responses/SecretsResponse.javaindex ebef221da0..a2a31e0322 100644--- a/common/src/main/java/org/apache/gravitino/dto/responses/SecretsResponse.java+++ b/common/src/main/java/org/apache/gravitino/dto/responses/SecretsResponse.java@@ -31,6 +31,9 @@ import lombok.ToString;@EqualsAndHashCode(callSuper = true)public class SecretsResponse extends BaseResponse {+ // Excluded from toString: the map holds secret material, and the response object ending up in+ // a log line must not leak it.+ @ToString.Exclude@JsonProperty("secrets")private final Map<String, String> secrets;diff --git a/common/src/test/java/org/apache/gravitino/dto/responses/TestSecretBearingResponsesToString.java b/common/src/test/java/org/apache/gravitino/dto/responses/TestSecretBearingResponsesToString.javanew file mode 100644index 0000000000..039a7b21ca--- /dev/null+++ b/common/src/test/java/org/apache/gravitino/dto/responses/TestSecretBearingResponsesToString.java@@ -0,0 +1,54 @@+/*+ * 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.dto.responses;++import com.google.common.collect.ImmutableMap;+import org.junit.jupiter.api.Assertions;+import org.junit.jupiter.api.Test;++public class TestSecretBearingResponsesToString {++ @Test+ public void testOAuth2TokenResponseDoesNotLeakTokens() {+ OAuth2TokenResponse response =+ new OAuth2TokenResponse(+ "secret-access-token-123",+ "access_token",+ "bearer",+ 3600,+ "read write",+ "secret-refresh-token-456");++ // Before the fix, Lombok @ToString rendered the bearer credentials in plaintext, so logging+ // the response leaked them.+ Assertions.assertFalse(response.toString().contains("secret-access-token-123"));+ Assertions.assertFalse(response.toString().contains("secret-refresh-token-456"));+ // Non-secret fields stay visible so the string remains useful for debugging.+ Assertions.assertTrue(response.toString().contains("3600"));+ Assertions.assertTrue(response.toString().contains("read write"));+ }++ @Test+ public void testSecretsResponseDoesNotLeakSecretValues() {+ SecretsResponse response =+ new SecretsResponse(ImmutableMap.of("kms.key", "super-secret-material"));++ Assertions.assertFalse(response.toString().contains("super-secret-material"));+ }+}