Skip to content

Commit 775f0fa

Browse files
helloworldlessbclozel
authored andcommitted
Improve sanitization for list of URI types
Prior to this commit, Actuator would sanitize properties values when serializing them on the dedicated endpoint. Keys like "password" or "secret" are entirely sanitized, but other keys like "uri" or "address" are considered as URI types and only the password part of the user info is sanitized. This commit fixes the sanitization process where lists of such URI types would not match the first entries of the list since they're starting with `'['`. This commit improves the regexp matching process to sanitize all URIs within a collection. The documentation is also updated to better underline the processing difference between complete sanitization and selective sanitization for URIs. Fixes gh-23037
1 parent e4691a4 commit 775f0fa

File tree

3 files changed

+24
-3
lines changed
  • spring-boot-project

3 files changed

+24
-3
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
* @author Stephane Nicoll
3838
* @author HaiTao Zhang
3939
* @author Chris Bono
40+
* @author David Good
4041
* @since 2.0.0
4142
*/
4243
public class Sanitizer {
@@ -49,7 +50,7 @@ public class Sanitizer {
4950
private static final Set<String> URI_USERINFO_KEYS = new LinkedHashSet<>(
5051
Arrays.asList("uri", "uris", "address", "addresses"));
5152

52-
private static final Pattern URI_USERINFO_PATTERN = Pattern.compile("[A-Za-z]+://.+:(.*)@.+$");
53+
private static final Pattern URI_USERINFO_PATTERN = Pattern.compile("\\[?[A-Za-z]+://.+:(.*)@.+$");
5354

5455
private Pattern[] keysToSanitize;
5556

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
* @author Phillip Webb
3131
* @author Stephane Nicoll
3232
* @author Chris Bono
33+
* @author David Good
3334
*/
3435
class SanitizerTests {
3536

@@ -105,6 +106,22 @@ void uriWithMultipleValuesWithPasswordMatchingOtherPartsOfStringShouldBeSanitize
105106
.isEqualTo("http://user1:******@localhost:8080,http://user2:******@localhost:8082");
106107
}
107108

109+
@ParameterizedTest(name = "key = {0}")
110+
@MethodSource("matchingUriUserInfoKeys")
111+
void uriKeyWithUserProvidedListLiteralShouldBeSanitized(String key) {
112+
Sanitizer sanitizer = new Sanitizer();
113+
assertThat(sanitizer.sanitize(key, "[amqp://username:password@host/]"))
114+
.isEqualTo("[amqp://username:******@host/]");
115+
assertThat(sanitizer.sanitize(key,
116+
"[http://user1:password1@localhost:8080,http://user2@localhost:8082,http://localhost:8083]")).isEqualTo(
117+
"[http://user1:******@localhost:8080,http://user2@localhost:8082,http://localhost:8083]");
118+
assertThat(sanitizer.sanitize(key,
119+
"[http://user1:password1@localhost:8080,http://user2:password2@localhost:8082]"))
120+
.isEqualTo("[http://user1:******@localhost:8080,http://user2:******@localhost:8082]");
121+
assertThat(sanitizer.sanitize(key, "[http://user1@localhost:8080,http://user2@localhost:8082]"))
122+
.isEqualTo("[http://user1@localhost:8080,http://user2@localhost:8082]");
123+
}
124+
108125
private static Stream<String> matchingUriUserInfoKeys() {
109126
return Stream.of("uri", "my.uri", "myuri", "uris", "my.uris", "myuris", "address", "my.address", "myaddress",
110127
"addresses", "my.addresses", "myaddresses");

spring-boot-project/spring-boot-docs/src/main/asciidoc/howto.adoc

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2227,10 +2227,13 @@ Information returned by the `env` and `configprops` endpoints can be somewhat se
22272227

22282228
The patterns to use can be customized using the `management.endpoint.env.keys-to-sanitize` and `management.endpoint.configprops.keys-to-sanitize` respectively.
22292229

2230-
Spring Boot uses sensible defaults for such keys: any key ending with the word "password", "secret", "key", "token", "vcap_services", "sun.java.command", "uri", "uris", "address" or "addresses" is sanitized.
2230+
Spring Boot uses sensible defaults for such keys: any key ending with the word "password", "secret", "key", "token", "vcap_services", "sun.java.command" is entirely sanitized.
22312231
Additionally, any key that holds the word `credentials` as part of the key is sanitized (configured as a regular expression, i.e. `+*credentials.*+`).
22322232

2233-
If any of the keys to sanitize are URI format (i.e. `<scheme>://<username>:<password>@<host>:<port>/`), only the password part is sanitized.
2233+
Furthermore, Spring Boot only sanitizes the sensitive portion of URIs for keys which end with "uri", "uris", "address", or "addresses".
2234+
The sensitive portion of the URI is identified using the format `<scheme>://<username>:<password>@<host>:<port>/`.
2235+
For example, for the property `myclient.uri=http://user1:password1@localhost:8081`, the resulting sanitized value is
2236+
`++http://user1:******@localhost:8081++`.
22342237

22352238

22362239

0 commit comments

Comments
 (0)