Skip to content

Commit

Permalink
Simplified regex
Browse files Browse the repository at this point in the history
  • Loading branch information
eshryane committed Jan 14, 2025
1 parent 19a5d01 commit 805448f
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class PasswordFilter {

//from logsearch tweaked
private static final Pattern PASSWORD_PATTERN_FOR_CONTENT = Pattern.compile("(?im)^(override|password)(:|%3A)\\s*(.+)\\s*$");
private static final Pattern BASIC_AUTH_HEADER_PATTERN_FOR_CONTENT = Pattern.compile("(?im)^Header: Authorization=Basic *([^ ]+) *$", Pattern.CASE_INSENSITIVE);
private static final Pattern BASIC_AUTH_HEADER_PATTERN_FOR_CONTENT = Pattern.compile("(?im)^(Header: Authorization=Basic)\\s*(.*)\\s*$", Pattern.CASE_INSENSITIVE);

private static final Pattern URI_PASSWORD_PATTERN_PASSWORD_FOR_URL = Pattern.compile("(?<=)(password|override)(:|=|%3A)([^&^\\s]*)", Pattern.CASE_INSENSITIVE);

Expand All @@ -37,7 +37,7 @@ public static String filterPasswordsInContents(final String contents) {
private static String replaceBasicAuthHeader(final Matcher matcher) {
final StringBuilder result = new StringBuilder();
while (matcher.find()) {
matcher.appendReplacement(result, String.format("%s%s", StringUtils.substringBefore(matcher.group(0), matcher.group(1)),"FILTERED"));
matcher.appendReplacement(result, String.format("%s FILTERED", matcher.group(1)));
}
matcher.appendTail(result);
return result.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public void testFilterPasswordsInMessage() {
}

@Test
public void testFilterBasicAuthHeaderInMessage() {
public void testFilterBasicAuthHeadersInMessage() {
final String input = "" +
"Header: Authorization=Basic dDZsUlpndk9GSXBoamlHd3RDR3VMd3F3OjJDVEdQeDVhbFVFVzRwa1Rrd2FRdGRPNg==\n" +
"blue: asdfasdfasdf\n" +
Expand All @@ -58,6 +58,7 @@ public void testFilterBasicAuthHeaderInMessage() {
"password: test3\n" +
"password%3A++test4\n" +
"password%3A++test5\n" +
"Header: Authorization=Basic dDZsUlpndk9GSXBoamlHd3RDR3VMd3F3OjJDVEdQeDVhbFVFVzRwa1Rrd2FRdGRPNg==\n" +
"delete: adsf\n";

assertThat(PasswordFilter.filterPasswordsInContents(input), containsString("" +
Expand All @@ -71,6 +72,7 @@ public void testFilterBasicAuthHeaderInMessage() {
"password:FILTERED\n" +
"password%3AFILTERED\n" +
"password%3AFILTERED\n" +
"Header: Authorization=Basic FILTERED\n" +
"delete: adsf\n"));
}

Expand Down

0 comments on commit 805448f

Please sign in to comment.