@Test public void shouldMaskStrange() { List<Pattern> maskPatterns = ImmutableList.of(Pattern.compile(".*password.*")); String requestQueryString = "test=one&&&===&=&xpasswordy=mask=me&&&==&test=two"; String maskRequestQueryString = ServletMessageSupplier.maskRequestQueryString(requestQueryString, maskPatterns); assertThat(maskRequestQueryString) .isEqualTo("test=one&&&===&=&xpasswordy=****&&&==&test=two"); }
static @Nullable String maskRequestQueryString(@Nullable String requestQueryString, List<Pattern> maskPatterns) { if (requestQueryString == null) { return null; } if (maskPatterns.isEmpty()) { return requestQueryString; } StringBuilder sb = new StringBuilder(requestQueryString.length()); boolean existMaskedParameters = false; int keyStartIndex = 0; boolean inMaskedValue = false; for (int i = 0; i < requestQueryString.length(); i++) { char c = requestQueryString.charAt(i); switch (c) { case '&': sb.append('&'); keyStartIndex = sb.length(); inMaskedValue = false; break; case '=': if (keyStartIndex == -1) { if (!inMaskedValue) { sb.append(c); } } else { String key = sb.substring(keyStartIndex, sb.length()); sb.append('='); String keyLowerCase = key.toLowerCase(Locale.ENGLISH); if (DetailCapture.matchesOneOf(keyLowerCase, maskPatterns)) { inMaskedValue = true; sb.append(MASK_TEXT); existMaskedParameters = true; } keyStartIndex = -1; } break; default: if (!inMaskedValue) { sb.append(c); } } } if (existMaskedParameters) { return sb.toString(); } else { return requestQueryString; } }
ServletMessageSupplier extends MessageSupplier implements ServletRequestInfo { static @Nullable String maskRequestQueryString(@Nullable String requestQueryString, List<Pattern> maskPatterns) { if (requestQueryString == null) { return null; } if (maskPatterns.isEmpty()) { return requestQueryString; } StringBuilder sb = new StringBuilder(requestQueryString.length()); boolean existMaskedParameters = false; int keyStartIndex = 0; boolean inMaskedValue = false; for (int i = 0; i < requestQueryString.length(); i++) { char c = requestQueryString.charAt(i); switch (c) { case '&': sb.append('&'); keyStartIndex = sb.length(); inMaskedValue = false; break; case '=': if (keyStartIndex == -1) { if (!inMaskedValue) { sb.append(c); } } else { String key = sb.substring(keyStartIndex, sb.length()); sb.append('='); String keyLowerCase = key.toLowerCase(Locale.ENGLISH); if (DetailCapture.matchesOneOf(keyLowerCase, maskPatterns)) { inMaskedValue = true; sb.append(MASK_TEXT); existMaskedParameters = true; } keyStartIndex = -1; } break; default: if (!inMaskedValue) { sb.append(c); } } } if (existMaskedParameters) { return sb.toString(); } else { return requestQueryString; } } }
ServletMessageSupplier extends MessageSupplier implements ServletRequestInfo { static @Nullable String maskRequestQueryString(@Nullable String requestQueryString, List<Pattern> maskPatterns) { if (requestQueryString == null) { return null; } if (maskPatterns.isEmpty()) { return requestQueryString; } StringBuilder sb = new StringBuilder(requestQueryString.length()); boolean existMaskedParameters = false; int keyStartIndex = 0; boolean inMaskedValue = false; for (int i = 0; i < requestQueryString.length(); i++) { char c = requestQueryString.charAt(i); switch (c) { case '&': sb.append('&'); keyStartIndex = sb.length(); inMaskedValue = false; break; case '=': if (keyStartIndex == -1) { if (!inMaskedValue) { sb.append(c); } } else { String key = sb.substring(keyStartIndex, sb.length()); sb.append('='); String keyLowerCase = key.toLowerCase(Locale.ENGLISH); if (DetailCapture.matchesOneOf(keyLowerCase, maskPatterns)) { inMaskedValue = true; sb.append(MASK_TEXT); existMaskedParameters = true; } keyStartIndex = -1; } break; default: if (!inMaskedValue) { sb.append(c); } } } if (existMaskedParameters) { return sb.toString(); } else { return requestQueryString; } } ServletMessageSupplier(String requestMethod, String requestContextPath,
            String requestServletPath, @Nullable String requestPathInfo, String requestUri,
            @Nullable String requestQueryString, Map<String, Object> requestHeaders,
            @Nullable String requestRemoteAddr, @Nullable String requestRemoteHost,
            Map<String, String> sessionAttributeMap); }
ServletMessageSupplier extends MessageSupplier implements ServletRequestInfo { static @Nullable String maskRequestQueryString(@Nullable String requestQueryString, List<Pattern> maskPatterns) { if (requestQueryString == null) { return null; } if (maskPatterns.isEmpty()) { return requestQueryString; } StringBuilder sb = new StringBuilder(requestQueryString.length()); boolean existMaskedParameters = false; int keyStartIndex = 0; boolean inMaskedValue = false; for (int i = 0; i < requestQueryString.length(); i++) { char c = requestQueryString.charAt(i); switch (c) { case '&': sb.append('&'); keyStartIndex = sb.length(); inMaskedValue = false; break; case '=': if (keyStartIndex == -1) { if (!inMaskedValue) { sb.append(c); } } else { String key = sb.substring(keyStartIndex, sb.length()); sb.append('='); String keyLowerCase = key.toLowerCase(Locale.ENGLISH); if (DetailCapture.matchesOneOf(keyLowerCase, maskPatterns)) { inMaskedValue = true; sb.append(MASK_TEXT); existMaskedParameters = true; } keyStartIndex = -1; } break; default: if (!inMaskedValue) { sb.append(c); } } } if (existMaskedParameters) { return sb.toString(); } else { return requestQueryString; } } ServletMessageSupplier(String requestMethod, String requestContextPath,
            String requestServletPath, @Nullable String requestPathInfo, String requestUri,
            @Nullable String requestQueryString, Map<String, Object> requestHeaders,
            @Nullable String requestRemoteAddr, @Nullable String requestRemoteHost,
            Map<String, String> sessionAttributeMap); @Override Message get(); @Override String getMethod(); @Override String getContextPath(); @Override String getServletPath(); @Override @Nullable String getPathInfo(); @Override String getUri(); }
ServletMessageSupplier extends MessageSupplier implements ServletRequestInfo { static @Nullable String maskRequestQueryString(@Nullable String requestQueryString, List<Pattern> maskPatterns) { if (requestQueryString == null) { return null; } if (maskPatterns.isEmpty()) { return requestQueryString; } StringBuilder sb = new StringBuilder(requestQueryString.length()); boolean existMaskedParameters = false; int keyStartIndex = 0; boolean inMaskedValue = false; for (int i = 0; i < requestQueryString.length(); i++) { char c = requestQueryString.charAt(i); switch (c) { case '&': sb.append('&'); keyStartIndex = sb.length(); inMaskedValue = false; break; case '=': if (keyStartIndex == -1) { if (!inMaskedValue) { sb.append(c); } } else { String key = sb.substring(keyStartIndex, sb.length()); sb.append('='); String keyLowerCase = key.toLowerCase(Locale.ENGLISH); if (DetailCapture.matchesOneOf(keyLowerCase, maskPatterns)) { inMaskedValue = true; sb.append(MASK_TEXT); existMaskedParameters = true; } keyStartIndex = -1; } break; default: if (!inMaskedValue) { sb.append(c); } } } if (existMaskedParameters) { return sb.toString(); } else { return requestQueryString; } } ServletMessageSupplier(String requestMethod, String requestContextPath,
            String requestServletPath, @Nullable String requestPathInfo, String requestUri,
            @Nullable String requestQueryString, Map<String, Object> requestHeaders,
            @Nullable String requestRemoteAddr, @Nullable String requestRemoteHost,
            Map<String, String> sessionAttributeMap); @Override Message get(); @Override String getMethod(); @Override String getContextPath(); @Override String getServletPath(); @Override @Nullable String getPathInfo(); @Override String getUri();  }