|
17 | 17 | import java.util.ArrayList;
|
18 | 18 | import java.util.List;
|
19 | 19 | import java.util.Properties;
|
20 |
| - |
| 20 | +import java.util.regex.Matcher; |
| 21 | +import java.util.regex.Pattern; |
| 22 | +import java.util.logging.Level; |
| 23 | +import java.util.logging.Logger; |
21 | 24 | /**
|
22 | 25 | * The MetricsFilter class provides a high-level filter that enables collection of (latency, amount and response
|
23 | 26 | * size metrics) for Servlet performance, based on schema, status code, HTTP method and URI path.
|
@@ -57,7 +60,14 @@ public class MetricsCollectorFilter implements Filter {
|
57 | 60 | private static final String ERROR_MESSAGE_PARAM = "error-message";
|
58 | 61 | private static final String DEBUG = "debug";
|
59 | 62 | private static final String APPLICATION_VERSION = "application-version";
|
| 63 | + private static final String DEFAULT_FILTER_REGEX = "[^A-zÀ-ú .,]+"; |
| 64 | + private static final String FILTER_REGEX_PARAM = "error-info-regex"; |
| 65 | + private static final String FILTER_MAX_SIZE_PARAM = "error-info-max-size"; |
| 66 | + private static final Logger LOGGER = Logger.getLogger(MetricsCollectorFilter.class.getName()); |
60 | 67 | private final List<String> exclusions = new ArrayList<String>();
|
| 68 | + private int filter_max_size = 50; |
| 69 | + private String filter_regex = ""; |
| 70 | + |
61 | 71 |
|
62 | 72 | private int pathDepth = 0;
|
63 | 73 | private String errorMessageParam = "";
|
@@ -109,6 +119,11 @@ public void init(FilterConfig filterConfig) {
|
109 | 119 | exportJvmMetrics = Boolean.parseBoolean(exportJvmMetricsStr);
|
110 | 120 | }
|
111 | 121 | exportApplicationVersion = filterConfig.getInitParameter(APPLICATION_VERSION);
|
| 122 | + |
| 123 | + filter_max_size = filterConfig.getInitParameter(FILTER_MAX_SIZE_PARAM) != null ? |
| 124 | + Integer.valueOf(filterConfig.getInitParameter(FILTER_MAX_SIZE_PARAM)) : filter_max_size; |
| 125 | + filter_regex = filterConfig.getInitParameter(FILTER_REGEX_PARAM) != null ? |
| 126 | + filterConfig.getInitParameter(FILTER_REGEX_PARAM) : DEFAULT_FILTER_REGEX; |
112 | 127 | }
|
113 | 128 | String version = isNotEmpty(exportApplicationVersion) ? exportApplicationVersion : getApplicationVersionFromPropertiesFile();
|
114 | 129 | // Allow users to capture error messages
|
@@ -209,20 +224,37 @@ private boolean isErrorStatus(int status) {
|
209 | 224 | /**
|
210 | 225 | * Get the error message from a request.
|
211 | 226 | * If error message is null, sets the string to empty string.
|
| 227 | + * If a regex is defined, use it to filter message |
| 228 | + * |
| 229 | + * Default regex: [^A-zÀ-ú .,]+ |
| 230 | + * Default max size: 50 |
212 | 231 | *
|
213 | 232 | * @param httpRequest request
|
214 | 233 | * @return string with the error message or empty string if error message not found.
|
215 | 234 | */
|
216 | 235 | private String getErrorMessage(HttpServletRequest httpRequest) {
|
217 |
| - if (errorMessageParam == null) { |
218 |
| - return ""; |
219 |
| - } |
220 |
| - String errorMessage = (String) httpRequest.getAttribute(errorMessageParam); |
221 |
| - if (errorMessage == null) { |
222 |
| - return ""; |
223 |
| - } |
224 | 236 |
|
225 |
| - return errorMessage; |
| 237 | + if (errorMessageParam == null) { |
| 238 | + return ""; |
| 239 | + } |
| 240 | + String errorMessage = (String) httpRequest.getAttribute(errorMessageParam); |
| 241 | + String result = ""; |
| 242 | + if (errorMessage == null) { |
| 243 | + return result; |
| 244 | + } |
| 245 | + |
| 246 | + try { |
| 247 | + // apply whitelist filter |
| 248 | + result = errorMessage.replaceAll(filter_regex, ""); |
| 249 | + if (result.length() > filter_max_size) { |
| 250 | + result = result.substring(0, filter_max_size); |
| 251 | + } |
| 252 | + } catch (Exception e) { |
| 253 | + // avoid invalid regex or invalid matcher group index |
| 254 | + result = ""; |
| 255 | + LOGGER.warning("Invalid regex: " + e.getMessage()); |
| 256 | + } |
| 257 | + return result; |
226 | 258 | }
|
227 | 259 |
|
228 | 260 | /**
|
|
0 commit comments