Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion dubbo-common/src/main/java/org/apache/dubbo/common/URL.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.apache.dubbo.common;

import org.apache.dubbo.common.config.Configuration;
import org.apache.dubbo.common.config.ConfigurationUtils;
import org.apache.dubbo.common.config.InmemoryConfiguration;
import org.apache.dubbo.common.constants.CommonConstants;
import org.apache.dubbo.common.constants.RemotingConstants;
Expand Down Expand Up @@ -1204,7 +1205,10 @@ protected void buildParameters(StringBuilder buf, boolean concat, String[] param
List<String> includes = (ArrayUtils.isEmpty(parameters) ? null : Arrays.asList(parameters));
boolean first = true;
for (Map.Entry<String, String> entry : new TreeMap<>(getParameters()).entrySet()) {
if (StringUtils.isNotEmpty(entry.getKey()) && (includes == null || includes.contains(entry.getKey()))) {
String key = entry.getKey();
if (StringUtils.isNotEmpty(key)
&& (includes == null || includes.contains(entry.getKey()))
&& !ConfigurationUtils.isSensitiveParameter(this, key)) {
if (first) {
if (concat) {
buf.append('?');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
*/
package org.apache.dubbo.common.config;

import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.config.configcenter.DynamicConfigurationFactory;
import org.apache.dubbo.common.constants.CommonConstants;
import org.apache.dubbo.common.extension.ExtensionAccessor;
import org.apache.dubbo.common.extension.ExtensionLoader;
import org.apache.dubbo.common.logger.ErrorTypeAwareLogger;
Expand Down Expand Up @@ -60,6 +62,7 @@ private ConfigurationUtils() {
private static final Set<String> securityKey;

private static volatile long expectedShutdownTime = Long.MAX_VALUE;
private static volatile Set<String> SensitiveParameterNames;

static {
Set<String> keys = new HashSet<>();
Expand Down Expand Up @@ -398,6 +401,34 @@ public static DynamicConfigurationFactory getDynamicConfigurationFactory(
extensionAccessor.getExtensionLoader(DynamicConfigurationFactory.class);
return loader.getOrDefaultExtension(name);
}
/**
* Checks whether a parameter name is considered sensitive.
* <p>
* The set of sensitive parameter names is lazily initialized from configuration
* defined by {@link CommonConstants#SENSITIVE_PARAMETER_NAMES}. Initialization
* is thread-safe using double-checked locking.
*
* @param url the {@link URL} to get the application model from
* @param name the parameter name to check
* @return true if the parameter is sensitive, false otherwise
*/
public static boolean isSensitiveParameter(URL url, String name) {
if (SensitiveParameterNames == null) {
synchronized (ConfigurationUtils.class) {
if (SensitiveParameterNames == null) {
Set<String> names = new HashSet<>();
String value = ConfigurationUtils.getProperty(
url.getOrDefaultApplicationModel(), CommonConstants.SENSITIVE_PARAMETER_NAMES);
if (value != null) {
String[] customNames = StringUtils.tokenize(value);
Collections.addAll(names, customNames);
}
SensitiveParameterNames = Collections.unmodifiableSet(names);
}
}
}
return SensitiveParameterNames.contains(name);
}

/**
* For compact single instance
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,8 @@ public interface CommonConstants {

String PASSWORD_KEY = "password";

String SENSITIVE_PARAMETER_NAMES = "dubbo.url.sensitive-parameter-names";

String HOST_KEY = "host";

String PORT_KEY = "port";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@
*/
package org.apache.dubbo.common.config;

import org.apache.dubbo.common.URL;
import org.apache.dubbo.rpc.model.ApplicationModel;
import org.apache.dubbo.rpc.model.FrameworkModel;
import org.apache.dubbo.rpc.model.ModuleModel;

import java.lang.reflect.Field;
import java.util.Map;

import org.junit.jupiter.api.Assertions;
Expand Down Expand Up @@ -108,4 +110,20 @@ void testEscapedNewLine() throws Exception {
Assertions.assertEquals(
"zookeeper://127.0.0.1:2181\\ndubbo.protocol.port=20880", result.get("dubbo.registry.address"));
}

@Test
void testSensitiveParameterFromConfig() throws NoSuchFieldException, IllegalAccessException {
// Clear the static cache to ensure fresh load
Field field = ConfigurationUtils.class.getDeclaredField("SensitiveParameterNames");
field.setAccessible(true);
field.set(null, null);
// Set a system property to simulate a custom sensitive parameter for testing
System.setProperty("dubbo.url.sensitive-parameter-names", "token");
// Construct a URL; the default ApplicationModel will read the configuration
URL url = URL.valueOf("nacos://127.0.0.1:8848/registry?password=secret&secretKey=mysecret&timeout=5000");
// Assert that the custom sensitive parameter is recognized
Assertions.assertTrue(ConfigurationUtils.isSensitiveParameter(url, "token"));
// Assert that a non-sensitive parameter is not recognized
Assertions.assertFalse(ConfigurationUtils.isSensitiveParameter(url, "username"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@
*/
package org.apache.dubbo.common.url;

import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.config.ConfigurationUtils;
import org.apache.dubbo.common.url.component.URLParam;
import org.apache.dubbo.common.utils.CollectionUtils;

import java.lang.reflect.Field;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
Expand Down Expand Up @@ -308,4 +311,25 @@ void testMethodParameters() {
Assertions.assertEquals("aaa", urlParam2.getAnyMethodParameter("method1"));
Assertions.assertNull(urlParam2.getAnyMethodParameter("method2"));
}

@Test
void testBuildParametersFiltersSensitiveFields() throws Exception {
// Reset the static cache to ensure test isolation
Field field = ConfigurationUtils.class.getDeclaredField("SensitiveParameterNames");
field.setAccessible(true);
field.set(null, null);
// Set a system property to simulate custom sensitive parameters
System.setProperty("dubbo.url.sensitive-parameter-names", "token,password");
// Construct a URL
URL url = URL.valueOf(
"nacos://127.0.0.1:8848/registry?password=secret&secretKey=mysecret&token=mytoken&timeout=5000");
// Get the parameter string from the URL
String paramStr = url.toString();
// Verify that sensitive parameters are filtered out
Assertions.assertFalse(paramStr.contains("token="));
Assertions.assertFalse(paramStr.contains("password="));
// Verify that non-sensitive parameters are retained
Assertions.assertTrue(paramStr.contains("secretKey=mysecret"));
Assertions.assertTrue(paramStr.contains("timeout=5000"));
}
}
Loading