@@ -45,16 +45,19 @@ public final class CredentialMasker {
4545 private static final String MASKED_VALUE = "***MASKED***" ;
4646
4747 // Regex patterns for common credential patterns
48+ // Group 1 captures the prefix (key + delimiter) to preserve original format in replacements
4849 private static final Pattern PASSWORD_PATTERN =
49- Pattern .compile ("password[\" ']?\\ s*[:=]\\ s*[\" ']?([^\" '\\ s,}]+)" , Pattern .CASE_INSENSITIVE );
50+ Pattern .compile (
51+ "(password[\" ']?\\ s*[:=]\\ s*[\" ']?)([^\" '\\ s,}]+)" , Pattern .CASE_INSENSITIVE );
5052 private static final Pattern TOKEN_PATTERN =
51- Pattern .compile ("token[\" ']?\\ s*[:=]\\ s*[\" ']?([^\" '\\ s,}]+)" , Pattern .CASE_INSENSITIVE );
53+ Pattern .compile ("( token[\" ']?\\ s*[:=]\\ s*[\" ']?) ([^\" '\\ s,}]+)" , Pattern .CASE_INSENSITIVE );
5254 private static final Pattern API_KEY_PATTERN =
5355 Pattern .compile (
54- "api[_\\ s-]?key[\" ']?\\ s*[:=]\\ s*[\" ']?([^\" '\\ s,}]+)" , Pattern .CASE_INSENSITIVE );
56+ "( api[_\\ s-]?key[\" ']?\\ s*[:=]\\ s*[\" ']?) ([^\" '\\ s,}]+)" , Pattern .CASE_INSENSITIVE );
5557 private static final Pattern BEARER_PATTERN =
5658 Pattern .compile ("Bearer\\ s+([^\\ s]+)" , Pattern .CASE_INSENSITIVE );
57- private static final Pattern BASIC_AUTH_PATTERN = Pattern .compile ("Basic\\ s+([^\\ s]+)" );
59+ private static final Pattern BASIC_AUTH_PATTERN =
60+ Pattern .compile ("Basic\\ s+([^\\ s]+)" , Pattern .CASE_INSENSITIVE );
5861 private static final Pattern ENV_VAR_PATTERN = Pattern .compile ("\\ $\\ {([A-Z0-9_]+)\\ }" );
5962
6063 private CredentialMasker () {
@@ -74,23 +77,23 @@ public static String mask(String message) {
7477
7578 String masked = message ;
7679
77- // Mask password values
78- masked = PASSWORD_PATTERN .matcher (masked ).replaceAll ("password= " + MASKED_VALUE );
80+ // Mask password values (preserve original prefix format via $1 backreference)
81+ masked = PASSWORD_PATTERN .matcher (masked ).replaceAll ("$1 " + MASKED_VALUE );
7982
80- // Mask token values
81- masked = TOKEN_PATTERN .matcher (masked ).replaceAll ("token= " + MASKED_VALUE );
83+ // Mask token values (preserve original prefix format via $1 backreference)
84+ masked = TOKEN_PATTERN .matcher (masked ).replaceAll ("$1 " + MASKED_VALUE );
8285
83- // Mask API key values
84- masked = API_KEY_PATTERN .matcher (masked ).replaceAll ("api_key= " + MASKED_VALUE );
86+ // Mask API key values (preserve original prefix format via $1 backreference)
87+ masked = API_KEY_PATTERN .matcher (masked ).replaceAll ("$1 " + MASKED_VALUE );
8588
8689 // Mask Bearer tokens
8790 masked = BEARER_PATTERN .matcher (masked ).replaceAll ("Bearer " + MASKED_VALUE );
8891
8992 // Mask Basic auth
9093 masked = BASIC_AUTH_PATTERN .matcher (masked ).replaceAll ("Basic " + MASKED_VALUE );
9194
92- // Mask environment variable references (but not the variable name)
93- masked = ENV_VAR_PATTERN .matcher (masked ).replaceAll ("\\ $\\ {$1 \\ }" );
95+ // Mask environment variable references (hide variable name)
96+ masked = ENV_VAR_PATTERN .matcher (masked ).replaceAll ("\\ ${" + MASKED_VALUE + " }" );
9497
9598 return masked ;
9699 }
0 commit comments