17
17
package org .springframework .boot .actuate .endpoint ;
18
18
19
19
import org .junit .jupiter .api .Test ;
20
+ import org .junit .jupiter .params .ParameterizedTest ;
21
+ import org .junit .jupiter .params .provider .MethodSource ;
22
+
23
+ import java .util .stream .Stream ;
20
24
21
25
import static org .assertj .core .api .Assertions .assertThat ;
22
26
25
29
*
26
30
* @author Phillip Webb
27
31
* @author Stephane Nicoll
32
+ * @author Chris Bono
28
33
*/
29
34
class SanitizerTests {
30
35
31
36
@ Test
32
- void defaults () {
37
+ void defaultNonUriKeys () {
33
38
Sanitizer sanitizer = new Sanitizer ();
34
39
assertThat (sanitizer .sanitize ("password" , "secret" )).isEqualTo ("******" );
35
40
assertThat (sanitizer .sanitize ("my-password" , "secret" )).isEqualTo ("******" );
@@ -40,21 +45,64 @@ void defaults() {
40
45
assertThat (sanitizer .sanitize ("sometoken" , "secret" )).isEqualTo ("******" );
41
46
assertThat (sanitizer .sanitize ("find" , "secret" )).isEqualTo ("secret" );
42
47
assertThat (sanitizer .sanitize ("sun.java.command" , "--spring.redis.password=pa55w0rd" )).isEqualTo ("******" );
43
- assertThat (sanitizer .sanitize ("my.uri" , "http://user:password@localhost:8080" ))
44
- .isEqualTo ("http://user:******@localhost:8080" );
45
48
}
46
49
47
- @ Test
48
- void uriWithNoPasswordShouldNotBeSanitized () {
50
+ @ ParameterizedTest (name = "key = {0}" )
51
+ @ MethodSource ("matchingUriUserInfoKeys" )
52
+ void uriWithSingleEntryWithPasswordShouldBeSanitized (String key ) {
49
53
Sanitizer sanitizer = new Sanitizer ();
50
- assertThat (sanitizer .sanitize ("my.uri" , "http://localhost:8080" )).isEqualTo ("http://localhost:8080" );
54
+ assertThat (sanitizer .sanitize (key , "http://user:password@ localhost:8080" )).isEqualTo ("http://user:******@ localhost:8080" );
51
55
}
52
56
53
- @ Test
54
- void uriWithPasswordMatchingOtherPartsOfString () {
57
+ @ ParameterizedTest (name = "key = {0}" )
58
+ @ MethodSource ("matchingUriUserInfoKeys" )
59
+ void uriWithSingleEntryWithNoPasswordShouldNotBeSanitized (String key ) {
60
+ Sanitizer sanitizer = new Sanitizer ();
61
+ assertThat (sanitizer .sanitize (key , "http://localhost:8080" )).isEqualTo ("http://localhost:8080" );
62
+ assertThat (sanitizer .sanitize (key , "http://user@localhost:8080" )).isEqualTo ("http://user@localhost:8080" );
63
+ }
64
+
65
+ @ ParameterizedTest (name = "key = {0}" )
66
+ @ MethodSource ("matchingUriUserInfoKeys" )
67
+ void uriWithSingleEntryWithPasswordMatchingOtherPartsOfStringShouldBeSanitized (String key ) {
55
68
Sanitizer sanitizer = new Sanitizer ();
56
- assertThat (sanitizer .sanitize ("my.uri" , "http://user://@localhost:8080" ))
57
- .isEqualTo ("http://user:******@localhost:8080" );
69
+ assertThat (sanitizer .sanitize (key , "http://user://@localhost:8080" )).isEqualTo ("http://user:******@localhost:8080" );
70
+ }
71
+
72
+ @ ParameterizedTest (name = "key = {0}" )
73
+ @ MethodSource ("matchingUriUserInfoKeys" )
74
+ void uriWithMultipleEntriesEachWithPasswordShouldHaveAllSanitized (String key ) {
75
+ Sanitizer sanitizer = new Sanitizer ();
76
+ assertThat (sanitizer .sanitize (key , "http://user1:password1@localhost:8080,http://user2:password2@localhost:8082" ))
77
+ .isEqualTo ("http://user1:******@localhost:8080,http://user2:******@localhost:8082" );
78
+ }
79
+
80
+ @ ParameterizedTest (name = "key = {0}" )
81
+ @ MethodSource ("matchingUriUserInfoKeys" )
82
+ void uriWithMultipleEntriesNoneWithPasswordShouldHaveNoneSanitized (String key ) {
83
+ Sanitizer sanitizer = new Sanitizer ();
84
+ assertThat (sanitizer .sanitize (key , "http://user@localhost:8080,http://localhost:8082" ))
85
+ .isEqualTo ("http://user@localhost:8080,http://localhost:8082" );
86
+ }
87
+
88
+ @ ParameterizedTest (name = "key = {0}" )
89
+ @ MethodSource ("matchingUriUserInfoKeys" )
90
+ void uriWithMultipleEntriesSomeWithPasswordShouldHaveThoseSanitized (String key ) {
91
+ Sanitizer sanitizer = new Sanitizer ();
92
+ assertThat (sanitizer .sanitize (key , "http://user1:password1@localhost:8080,http://user2@localhost:8082,http://localhost:8083" ))
93
+ .isEqualTo ("http://user1:******@localhost:8080,http://user2@localhost:8082,http://localhost:8083" );
94
+ }
95
+
96
+ @ ParameterizedTest (name = "key = {0}" )
97
+ @ MethodSource ("matchingUriUserInfoKeys" )
98
+ void uriWithMultipleEntriesWithPasswordMatchingOtherPartsOfStringShouldBeSanitized (String key ) {
99
+ Sanitizer sanitizer = new Sanitizer ();
100
+ assertThat (sanitizer .sanitize (key , "http://user1://@localhost:8080,http://user2://@localhost:8082" ))
101
+ .isEqualTo ("http://user1:******@localhost:8080,http://user2:******@localhost:8082" );
102
+ }
103
+
104
+ static private Stream <String > matchingUriUserInfoKeys () {
105
+ return Stream .of ("uri" , "my.uri" , "myuri" , "uris" , "my.uris" , "myuris" , "address" , "my.address" , "myaddress" , "addresses" , "my.addresses" , "myaddresses" );
58
106
}
59
107
60
108
@ Test
@@ -63,5 +111,4 @@ void regex() {
63
111
assertThat (sanitizer .sanitize ("verylOCkish" , "secret" )).isEqualTo ("******" );
64
112
assertThat (sanitizer .sanitize ("veryokish" , "secret" )).isEqualTo ("secret" );
65
113
}
66
-
67
114
}
0 commit comments