Skip to content

Commit 7829593

Browse files
committed
Polish "Sanitize password in URI properties"
See gh-17939
1 parent d49a2ec commit 7829593

File tree

3 files changed

+21
-13
lines changed

3 files changed

+21
-13
lines changed

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

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

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

19-
import java.net.URI;
19+
import java.util.regex.Matcher;
2020
import java.util.regex.Pattern;
2121

2222
import org.springframework.util.Assert;
@@ -38,6 +38,8 @@ public class Sanitizer {
3838

3939
private static final String[] REGEX_PARTS = { "*", "$", "^", "+" };
4040

41+
private static final Pattern URI_USERINFO_PATTERN = Pattern.compile("[A-Za-z]+://.+:(.*)@.+$");
42+
4143
private Pattern[] keysToSanitize;
4244

4345
public Sanitizer() {
@@ -99,17 +101,10 @@ public Object sanitize(String key, Object value) {
99101
}
100102

101103
private Object sanitizeUri(Object value) {
102-
URI uri = URI.create(value.toString());
103-
String userInfo = uri.getUserInfo();
104-
if (!StringUtils.hasText(userInfo) || userInfo.split(":").length == 0) {
105-
return value;
106-
}
107-
String[] parts = userInfo.split(":");
108-
String userName = parts[0];
109-
if (StringUtils.hasText(userName)) {
110-
String sanitizedPassword = "******";
111-
return uri.getScheme() + "://" + userName + ":" + sanitizedPassword + "@" + uri.getHost() + ":"
112-
+ uri.getPort() + uri.getPath();
104+
Matcher matcher = URI_USERINFO_PATTERN.matcher(value.toString());
105+
String password = matcher.matches() ? matcher.group(1) : null;
106+
if (password != null) {
107+
return StringUtils.replace(value.toString(), ":" + password + "@", ":******@");
113108
}
114109
return value;
115110
}

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
@@ -286,7 +286,7 @@ public static class TestProperties {
286286

287287
private URI sensitiveUri = URI.create("http://user:password@localhost:8080");
288288

289-
private URI noPasswordUri = URI.create("http://user:p@localhost:8080");
289+
private URI noPasswordUri = URI.create("http://user:@localhost:8080");
290290

291291
TestProperties() {
292292
this.secrets.put("mine", "myPrivateThing");

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,19 @@ void defaults() {
4444
.isEqualTo("http://user:******@localhost:8080");
4545
}
4646

47+
@Test
48+
void uriWithNoPasswordShouldNotBeSanitized() {
49+
Sanitizer sanitizer = new Sanitizer();
50+
assertThat(sanitizer.sanitize("my.uri", "http://localhost:8080")).isEqualTo("http://localhost:8080");
51+
}
52+
53+
@Test
54+
void uriWithPasswordMatchingOtherPartsOfString() {
55+
Sanitizer sanitizer = new Sanitizer();
56+
assertThat(sanitizer.sanitize("my.uri", "http://user://@localhost:8080"))
57+
.isEqualTo("http://user:******@localhost:8080");
58+
}
59+
4760
@Test
4861
void regex() {
4962
Sanitizer sanitizer = new Sanitizer(".*lock.*");

0 commit comments

Comments
 (0)