Skip to content

Commit f148137

Browse files
committed
* Avoid to compile bad or false regex
1 parent 03f74e9 commit f148137

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/mvc/NamePatternFilter.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,12 @@
3131
* @author Phillip Webb
3232
* @author Sergei Egorov
3333
* @author Andy Wilkinson
34+
* @author Dylian Bego
3435
* @since 1.3.0
3536
*/
3637
abstract class NamePatternFilter<T> {
3738

38-
private static final String[] REGEX_PARTS = { "*", "$", "^", "+", "[" };
39+
private static final String[] REGEX_PARTS = { "*", "$", "^", "+", "["};
3940

4041
private final T source;
4142

@@ -44,27 +45,31 @@ abstract class NamePatternFilter<T> {
4445
}
4546

4647
public Map<String, Object> getResults(String name) {
47-
if (!isRegex(name)) {
48+
Pattern pattern = getRegexPattern(name);
49+
if (pattern == null) { // this is not a regex
4850
Object value = getValue(this.source, name);
4951
Map<String, Object> result = new HashMap<>();
5052
result.put(name, value);
5153
return result;
5254
}
53-
Pattern pattern = Pattern.compile(name);
5455
ResultCollectingNameCallback resultCollector = new ResultCollectingNameCallback(
5556
pattern);
5657
getNames(this.source, resultCollector);
5758
return resultCollector.getResults();
5859

5960
}
6061

61-
private boolean isRegex(String name) {
62+
private Pattern getRegexPattern(String name) {
6263
for (String part : REGEX_PARTS) {
6364
if (name.contains(part)) {
64-
return true;
65+
try {
66+
return Pattern.compile(name);
67+
} catch(RuntimeException re){
68+
return null;
69+
}
6570
}
6671
}
67-
return false;
72+
return null;
6873
}
6974

7075
protected abstract void getNames(T source, NameCallback callback);

spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/mvc/NamePatternFilterTests.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
*
2828
* @author Phillip Webb
2929
* @author Andy Wilkinson
30+
* @author Dylian Bego
3031
*/
3132
public class NamePatternFilterTests {
3233

@@ -37,6 +38,13 @@ public void nonRegex() throws Exception {
3738
"not.a.regex");
3839
assertThat(filter.isGetNamesCalled()).isFalse();
3940
}
41+
42+
@Test
43+
public void nonRegexThatContainsRegexPart() throws Exception {
44+
MockNamePatternFilter filter = new MockNamePatternFilter();
45+
assertThat(filter.getResults("*")).containsEntry("*", "*");
46+
assertThat(filter.isGetNamesCalled()).isFalse();
47+
}
4048

4149
@Test
4250
public void regexRepetitionZeroOrMore() {

0 commit comments

Comments
 (0)