Skip to content

Commit 28495d7

Browse files
committed
Polish SanitizingFunction
See gh-39243
1 parent 3eee1f1 commit 28495d7

File tree

2 files changed

+30
-9
lines changed

2 files changed

+30
-9
lines changed

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

Lines changed: 18 additions & 4 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-2025 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.
@@ -80,7 +80,7 @@ default SanitizableData applyUnlessFiltered(SanitizableData data) {
8080
* @see #sanitizeValue()
8181
*/
8282
default SanitizingFunction ifLikelySenstive() {
83-
return ifLikelyCredential().ifLikelyUri().ifLikelySenstiveEnvironmentVariable().ifVcapServices();
83+
return ifLikelyCredential().ifLikelyUri().ifLikelySenstiveProperty().ifVcapServices();
8484
}
8585

8686
/**
@@ -113,15 +113,15 @@ default SanitizingFunction ifLikelyUri() {
113113

114114
/**
115115
* Return a new function with a filter that <em>also</em> applies if the data is
116-
* likely to sensitive environment variable value. This method can help construct a
116+
* likely to contain a sensitive property value. This method can help construct a
117117
* useful sanitizing function, but may not catch all sensitive data so care should be
118118
* taken to test the results for your specific environment.
119119
* @return a new sanitizing function with an updated {@link #filter()}
120120
* @since 3.5.0
121121
* @see #filter()
122122
* @see #sanitizeValue()
123123
*/
124-
default SanitizingFunction ifLikelySenstiveEnvironmentVariable() {
124+
default SanitizingFunction ifLikelySenstiveProperty() {
125125
return ifKeyMatches("sun.java.command", "^spring[._]application[._]json$");
126126
}
127127

@@ -431,4 +431,18 @@ static SanitizingFunction sanitizeValue() {
431431
return SanitizableData::withSanitizedValue;
432432
}
433433

434+
/**
435+
* Helper method that can be used working with a sanitizingFunction as a lambda. For
436+
* example: <pre class="code">
437+
* SanitizingFunction.of((data) -> data.withValue("----")).ifKeyContains("password");
438+
* </pre>
439+
* @param sanitizingFunction the sanitizing function lambda
440+
* @return a {@link SanitizingFunction} for further method calls
441+
* @since 3.5.0
442+
*/
443+
static SanitizingFunction of(SanitizingFunction sanitizingFunction) {
444+
Assert.notNull(sanitizingFunction, "'sanitizingFunction' must not be null");
445+
return sanitizingFunction;
446+
}
447+
434448
}

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

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ void applyUnlessFilteredWhenHasFilterTestingFalseReturnsUnfiltered() {
6060
void ifLikelySenstiveFiltersExpected() {
6161
SanitizingFunction function = SanitizingFunction.sanitizeValue().ifLikelySenstive();
6262
assertThat(function).satisfies(this::likelyCredentialChecks, this::likelyUriChecks,
63-
this::likelySenstiveEnvironmentVariableChecks, this::vcapServicesChecks);
63+
this::likelySenstivePropertyChecks, this::vcapServicesChecks);
6464
}
6565

6666
@Test
@@ -101,12 +101,12 @@ private void likelyUriChecks(SanitizingFunction function) {
101101
}
102102

103103
@Test
104-
void ifLikelySenstiveEnvironmentVariableFiltersExpected() {
105-
SanitizingFunction function = SanitizingFunction.sanitizeValue().ifLikelySenstiveEnvironmentVariable();
106-
assertThat(function).satisfies(this::likelySenstiveEnvironmentVariableChecks);
104+
void ifLikelySenstivePropertyFiltersExpected() {
105+
SanitizingFunction function = SanitizingFunction.sanitizeValue().ifLikelySenstiveProperty();
106+
assertThat(function).satisfies(this::likelySenstivePropertyChecks);
107107
}
108108

109-
private void likelySenstiveEnvironmentVariableChecks(SanitizingFunction function) {
109+
private void likelySenstivePropertyChecks(SanitizingFunction function) {
110110
assertThatApplyingToKey(function, "sun.java.command").has(sanitizedValue());
111111
assertThatApplyingToKey(function, "spring.application.json").has(sanitizedValue());
112112
assertThatApplyingToKey(function, "SPRING_APPLICATION_JSON").has(sanitizedValue());
@@ -305,6 +305,13 @@ void ifMatchesPredicateFiltersExpected() {
305305
assertThatApplying(function, data("spring", null)).is(unsanitizedValue());
306306
}
307307

308+
@Test
309+
void ofAllowsChainingFromLambda() {
310+
SanitizingFunction function = SanitizingFunction.of((data) -> data.withValue("----")).ifKeyContains("password");
311+
assertThat(function.applyUnlessFiltered(data("username", "spring")).getValue()).isEqualTo("spring");
312+
assertThat(function.applyUnlessFiltered(data("password", "boot")).getValue()).isEqualTo("----");
313+
}
314+
308315
private ObjectAssert<SanitizableData> assertThatApplyingToKey(SanitizingFunction function, String key) {
309316
return assertThatApplying(function, data(key));
310317
}

0 commit comments

Comments
 (0)