Skip to content

Commit 63115fa

Browse files
committed
Support overriding the default SanitizingFunction
Closes gh-29620
1 parent df54919 commit 63115fa

File tree

4 files changed

+47
-7
lines changed

4 files changed

+47
-7
lines changed

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/env/EnvironmentEndpointAutoConfigurationTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2021 the original author or authors.
2+
* Copyright 2012-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -82,7 +82,7 @@ void sanitizingFunctionsCanBeConfiguredViaTheEnvironment() {
8282
Map<String, PropertyValueDescriptor> systemProperties = getSource("systemProperties", env)
8383
.getProperties();
8484
assertThat(systemProperties.get("custom").getValue()).isEqualTo("$$$");
85-
assertThat(systemProperties.get("password").getValue()).isEqualTo("******");
85+
assertThat(systemProperties.get("password").getValue()).isEqualTo("$$$");
8686
});
8787
}
8888

spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/Sanitizer.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2021 the original author or authors.
2+
* Copyright 2012-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -184,13 +184,18 @@ public Object sanitize(String key, Object value) {
184184
* @since 2.6.0
185185
*/
186186
public Object sanitize(SanitizableData data) {
187-
if (data.getValue() == null) {
187+
Object value = data.getValue();
188+
if (value == null) {
188189
return null;
189190
}
190191
for (SanitizingFunction sanitizingFunction : this.sanitizingFunctions) {
191192
data = sanitizingFunction.apply(data);
193+
Object sanitizedValue = data.getValue();
194+
if (!value.equals(sanitizedValue)) {
195+
return sanitizedValue;
196+
}
192197
}
193-
return data.getValue();
198+
return value;
194199
}
195200

196201
private boolean keyIsUriWithUserInfo(Pattern pattern) {

spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/context/properties/ConfigurationPropertiesReportEndpointTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ void sanitizeWithCustomSanitizingFunction() {
300300
new ApplicationContextRunner().withUserConfiguration(CustomSanitizingEndpointConfig.class,
301301
SanitizingFunctionConfiguration.class, TestPropertiesConfiguration.class)
302302
.run(assertProperties("test", (properties) -> {
303-
assertThat(properties.get("dbPassword")).isEqualTo("******");
303+
assertThat(properties.get("dbPassword")).isEqualTo("$$$");
304304
assertThat(properties.get("myTestProperty")).isEqualTo("$$$");
305305
}));
306306
}

spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/SanitizerTests.java

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2021 the original author or authors.
2+
* Copyright 2012-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,7 +16,9 @@
1616

1717
package org.springframework.boot.actuate.endpoint;
1818

19+
import java.util.ArrayList;
1920
import java.util.Collections;
21+
import java.util.List;
2022
import java.util.stream.Stream;
2123

2224
import org.junit.jupiter.api.Test;
@@ -87,6 +89,39 @@ void whenCustomSanitizingFunctionPresentValueShouldBeSanitized() {
8789
assertThat(sanitizer.sanitize(hello)).isEqualTo("abc");
8890
}
8991

92+
@Test
93+
void overridingDefaultSanitizingFunction() {
94+
Sanitizer sanitizer = new Sanitizer(Collections.singletonList((data) -> {
95+
if (data.getKey().equals("password")) {
96+
return data.withValue("------");
97+
}
98+
return data;
99+
}));
100+
SanitizableData password = new SanitizableData(null, "password", "123456");
101+
assertThat(sanitizer.sanitize(password)).isEqualTo("------");
102+
}
103+
104+
@Test
105+
void whenValueSanitizedLaterSanitizingFunctionsShouldBeSkipped() {
106+
final String sameKey = "custom";
107+
List<SanitizingFunction> sanitizingFunctions = new ArrayList<>();
108+
sanitizingFunctions.add((data) -> {
109+
if (data.getKey().equals(sameKey)) {
110+
return data.withValue("------");
111+
}
112+
return data;
113+
});
114+
sanitizingFunctions.add((data) -> {
115+
if (data.getKey().equals(sameKey)) {
116+
return data.withValue("******");
117+
}
118+
return data;
119+
});
120+
Sanitizer sanitizer = new Sanitizer(sanitizingFunctions);
121+
SanitizableData custom = new SanitizableData(null, "custom", "123456");
122+
assertThat(sanitizer.sanitize(custom)).isEqualTo("------");
123+
}
124+
90125
@ParameterizedTest(name = "key = {0}")
91126
@MethodSource("matchingUriUserInfoKeys")
92127
void uriWithSingleValueWithPasswordShouldBeSanitized(String key) {

0 commit comments

Comments
 (0)