Skip to content

Commit e86efed

Browse files
committed
Protect code and rename regex's params
include try/catch to protect invalid regex rename regex's params to get align with `ERROR_MESSAGE_PARAM` Signed-off-by: Luiz Oliveira <luiz.oliveira@ibm.com>
1 parent d329437 commit e86efed

File tree

1 file changed

+18
-14
lines changed

1 file changed

+18
-14
lines changed

src/main/java/br/com/labbs/monitor/filter/MetricsCollectorFilter.java

+18-14
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,9 @@ public class MetricsCollectorFilter implements Filter {
5959
private static final String DEBUG = "debug";
6060
private static final String APPLICATION_VERSION = "application-version";
6161
private static final String DEFAULT_FILTER_REGEX = "^([a-zA-z0-9 ]{0,120})";
62-
private static final String FILTER_REGEX_PARAM = "filter-regex";
63-
private static final String FILTER_GROUP_INDEX_PARAM = "filter-index";
64-
private static final String FILTER_MAX_SIZE_PARAM = "filter-max-size";
62+
private static final String FILTER_REGEX_PARAM = "error-message-regex";
63+
private static final String FILTER_GROUP_INDEX_PARAM = "error-message-regex-index";
64+
private static final String FILTER_MAX_SIZE_PARAM = "error-message-max-size";
6565
private final List<String> exclusions = new ArrayList<String>();
6666
private int filter_group_index = 0;
6767
private int filter_max_size = 120;
@@ -244,18 +244,22 @@ private String getErrorMessage(HttpServletRequest httpRequest) {
244244
return result;
245245
}
246246

247-
// filter Error Message with regex
248-
if (filter_regex.length() > 0) {
249-
final Pattern pattern = Pattern.compile(filter_regex);
250-
final Matcher matcher = pattern.matcher(errorMessage);
251-
252-
if (matcher.find()) {
253-
result = matcher.group(filter_group_index);
247+
try {
248+
// filter Error Message with regex
249+
if (filter_regex.length() > 0) {
250+
final Pattern pattern = Pattern.compile(filter_regex);
251+
final Matcher matcher = pattern.matcher(errorMessage);
252+
if (matcher.find()) {
253+
result = matcher.group(filter_group_index);
254+
}
254255
}
255-
}
256-
// create limit size of the error message
257-
if (result.length() > filter_max_size) {
258-
result = result.substring(0, filter_max_size);
256+
// create limit size of the error message
257+
if (result.length() > filter_max_size) {
258+
result = result.substring(0, filter_max_size);
259+
}
260+
} catch (Exception e) {
261+
// avoid invalid regex or invalid matcher group index
262+
result = "";
259263
}
260264
return result;
261265
}

0 commit comments

Comments
 (0)