Allow additional keys to be configured for value sanitization
Closes gh-25384
This commit is contained in:
@@ -115,6 +115,10 @@ public class ConfigurationPropertiesReportEndpoint implements ApplicationContext
|
||||
this.sanitizer.setKeysToSanitize(keysToSanitize);
|
||||
}
|
||||
|
||||
public void keysToSanitize(String... keysToSanitize) {
|
||||
this.sanitizer.keysToSanitize(keysToSanitize);
|
||||
}
|
||||
|
||||
@ReadOperation
|
||||
public ApplicationConfigurationProperties configurationProperties() {
|
||||
return extract(this.context, (bean) -> true);
|
||||
|
||||
@@ -67,8 +67,8 @@ public class Sanitizer {
|
||||
}
|
||||
|
||||
/**
|
||||
* Keys that should be sanitized. Keys can be simple strings that the property ends
|
||||
* with or regular expressions.
|
||||
* Set the keys that should be sanitized, overwriting any existing configuration. Keys
|
||||
* can be simple strings that the property ends with or regular expressions.
|
||||
* @param keysToSanitize the keys to sanitize
|
||||
*/
|
||||
public void setKeysToSanitize(String... keysToSanitize) {
|
||||
@@ -79,6 +79,21 @@ public class Sanitizer {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds keys that should be sanitized. Keys can be simple strings that the property
|
||||
* ends with or regular expressions.
|
||||
* @param keysToSanitize the keys to sanitize
|
||||
* @since 2.5.0
|
||||
*/
|
||||
public void keysToSanitize(String... keysToSanitize) {
|
||||
Assert.notNull(keysToSanitize, "KeysToSanitize must not be null");
|
||||
int existingKeys = this.keysToSanitize.length;
|
||||
this.keysToSanitize = Arrays.copyOf(this.keysToSanitize, this.keysToSanitize.length + keysToSanitize.length);
|
||||
for (int i = 0; i < keysToSanitize.length; i++) {
|
||||
this.keysToSanitize[i + existingKeys] = getPattern(keysToSanitize[i]);
|
||||
}
|
||||
}
|
||||
|
||||
private Pattern getPattern(String value) {
|
||||
if (isRegex(value)) {
|
||||
return Pattern.compile(value, Pattern.CASE_INSENSITIVE);
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2020 the original author or authors.
|
||||
* Copyright 2012-2021 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -76,6 +76,10 @@ public class EnvironmentEndpoint {
|
||||
this.sanitizer.setKeysToSanitize(keysToSanitize);
|
||||
}
|
||||
|
||||
public void keysToSanitize(String... keysToSanitize) {
|
||||
this.sanitizer.keysToSanitize(keysToSanitize);
|
||||
}
|
||||
|
||||
@ReadOperation
|
||||
public EnvironmentDescriptor environment(@Nullable String pattern) {
|
||||
if (StringUtils.hasText(pattern)) {
|
||||
|
||||
@@ -48,6 +48,23 @@ class SanitizerTests {
|
||||
assertThat(sanitizer.sanitize("sun.java.command", "--spring.redis.password=pa55w0rd")).isEqualTo("******");
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenAdditionalKeysAreAddedValuesOfBothThemAndTheDefaultKeysAreSanitized() {
|
||||
Sanitizer sanitizer = new Sanitizer();
|
||||
sanitizer.keysToSanitize("find", "confidential");
|
||||
assertThat(sanitizer.sanitize("password", "secret")).isEqualTo("******");
|
||||
assertThat(sanitizer.sanitize("my-password", "secret")).isEqualTo("******");
|
||||
assertThat(sanitizer.sanitize("my-OTHER.paSSword", "secret")).isEqualTo("******");
|
||||
assertThat(sanitizer.sanitize("somesecret", "secret")).isEqualTo("******");
|
||||
assertThat(sanitizer.sanitize("somekey", "secret")).isEqualTo("******");
|
||||
assertThat(sanitizer.sanitize("token", "secret")).isEqualTo("******");
|
||||
assertThat(sanitizer.sanitize("sometoken", "secret")).isEqualTo("******");
|
||||
assertThat(sanitizer.sanitize("find", "secret")).isEqualTo("******");
|
||||
assertThat(sanitizer.sanitize("sun.java.command", "--spring.redis.password=pa55w0rd")).isEqualTo("******");
|
||||
assertThat(sanitizer.sanitize("confidential", "secret")).isEqualTo("******");
|
||||
assertThat(sanitizer.sanitize("private", "secret")).isEqualTo("secret");
|
||||
}
|
||||
|
||||
@ParameterizedTest(name = "key = {0}")
|
||||
@MethodSource("matchingUriUserInfoKeys")
|
||||
void uriWithSingleValueWithPasswordShouldBeSanitized(String key) {
|
||||
|
||||
Reference in New Issue
Block a user