Skip to content

Commit

Permalink
Bugfix/auth header log fix (#3674)
Browse files Browse the repository at this point in the history
* Fjerner bearer token som persisteres til logg gjennom instrumentering
  • Loading branch information
stigus authored Nov 25, 2024
1 parent 23828f6 commit a96c532
Showing 1 changed file with 10 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@
public class TestnavLogbackEncoder extends LogstashEncoder {

// matches exactly 11 digits (\\d{11}) that are not immediately preceded ((?<!\\d)) or followed ((?!\\d)) by another digit.
private final Pattern pattern = Pattern.compile("(?<!\\d)\\d{11}(?!\\d)");

private final Pattern identNummer = Pattern.compile("(?<!\\d)\\d{11}(?!\\d)");
private final Pattern bearer = Pattern.compile("Bearer [a-zA-Z0-9\\-_.]+");
@Setter
private int maxStackTraceLength = 480;

Expand Down Expand Up @@ -113,24 +113,15 @@ private void appendStackTraceCauses(ThrowableProxy exception, StringWriter write
}

private String formatMessage(String message) {
var matcher = pattern.matcher(message);

if (!matcher.find()) {
return message;
}

matcher.reset();
var result = new StringBuilder();

while (matcher.find()) {
var match = matcher.group();
if (match.charAt(2) == '0' || match.charAt(2) == '1') {
var replacement = match.substring(0, 6) + "xxxxx";
matcher.appendReplacement(result, replacement);
message = identNummer.matcher(message).replaceAll(match -> {
if (match.group().charAt(2) == '0' || match.group().charAt(2) == '1') {
return match.group().substring(0, 6) + "xxxxx";
}
}
matcher.appendTail(result);
return match.group();
});

message = bearer.matcher(message).replaceAll("REDACTED_BEARER");

return result.toString();
return message;
}
}

0 comments on commit a96c532

Please sign in to comment.