Skip to content

Commit 20a417f

Browse files
committed
Merge pull request #9730 from Dylian Bego
* gh-9730: Polish "Handle possible regexes defensively in NamePatternFilter" Handle possible regexes defensively in NamePatternFilter
2 parents c70cc55 + bbc34a6 commit 20a417f

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

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

+13-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2016 the original author or authors.
2+
* Copyright 2012-2017 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.
@@ -20,6 +20,7 @@
2020
import java.util.LinkedHashMap;
2121
import java.util.Map;
2222
import java.util.regex.Pattern;
23+
import java.util.regex.PatternSyntaxException;
2324

2425
/**
2526
* Utility class that can be used to filter source data using a name regular expression.
@@ -31,6 +32,7 @@
3132
* @author Phillip Webb
3233
* @author Sergei Egorov
3334
* @author Andy Wilkinson
35+
* @author Dylian Bego
3436
* @since 1.3.0
3537
*/
3638
abstract class NamePatternFilter<T> {
@@ -44,27 +46,32 @@ abstract class NamePatternFilter<T> {
4446
}
4547

4648
public Map<String, Object> getResults(String name) {
47-
if (!isRegex(name)) {
49+
Pattern pattern = compilePatternIfNecessary(name);
50+
if (pattern == null) {
4851
Object value = getValue(this.source, name);
4952
Map<String, Object> result = new HashMap<String, Object>();
5053
result.put(name, value);
5154
return result;
5255
}
53-
Pattern pattern = Pattern.compile(name);
5456
ResultCollectingNameCallback resultCollector = new ResultCollectingNameCallback(
5557
pattern);
5658
getNames(this.source, resultCollector);
5759
return resultCollector.getResults();
5860

5961
}
6062

61-
private boolean isRegex(String name) {
63+
private Pattern compilePatternIfNecessary(String name) {
6264
for (String part : REGEX_PARTS) {
6365
if (name.contains(part)) {
64-
return true;
66+
try {
67+
return Pattern.compile(name);
68+
}
69+
catch (PatternSyntaxException ex) {
70+
return null;
71+
}
6572
}
6673
}
67-
return false;
74+
return null;
6875
}
6976

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

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

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2016 the original author or authors.
2+
* Copyright 2012-2017 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.
@@ -27,6 +27,7 @@
2727
*
2828
* @author Phillip Webb
2929
* @author Andy Wilkinson
30+
* @author Dylian Bego
3031
*/
3132
public class NamePatternFilterTests {
3233

@@ -38,6 +39,13 @@ public void nonRegex() throws Exception {
3839
assertThat(filter.isGetNamesCalled()).isFalse();
3940
}
4041

42+
@Test
43+
public void nonRegexThatContainsRegexPart() throws Exception {
44+
MockNamePatternFilter filter = new MockNamePatternFilter();
45+
assertThat(filter.getResults("*")).containsEntry("*", "*");
46+
assertThat(filter.isGetNamesCalled()).isFalse();
47+
}
48+
4149
@Test
4250
public void regexRepetitionZeroOrMore() {
4351
MockNamePatternFilter filter = new MockNamePatternFilter();

0 commit comments

Comments
 (0)