diff --git a/internal/logs/sanitizer.go b/internal/logs/sanitizer.go index f450866e8..fcc4494d2 100644 --- a/internal/logs/sanitizer.go +++ b/internal/logs/sanitizer.go @@ -101,6 +101,22 @@ func (s *SecretSanitizer) registerDefaultPatterns() { }, }) + // JWT tokens (eyJ...). Must run before high_entropy: that pattern stops + // at the first '.', masks only the header, and leaves nothing for this + // regex to match, so the payload and signature would be logged in clear. + s.patterns = append(s.patterns, &secretPattern{ + name: "jwt", + regex: regexp.MustCompile(`\b(eyJ[A-Za-z0-9\-_]+\.eyJ[A-Za-z0-9\-_]+\.[A-Za-z0-9\-_]+)\b`), + maskFunc: func(jwt string) string { + parts := strings.Split(jwt, ".") + if len(parts) != 3 { + return "****" + } + // Show first part (header) but mask payload and signature + return parts[0] + ".***." + parts[2][len(parts[2])-4:] + }, + }) + // Generic high-entropy strings (likely tokens/passwords) // Only mask if they appear in suspicious contexts (after = : or in quotes) s.patterns = append(s.patterns, &secretPattern{ @@ -126,20 +142,6 @@ func (s *SecretSanitizer) registerDefaultPatterns() { return match }, }) - - // JWT tokens (eyJ...) - s.patterns = append(s.patterns, &secretPattern{ - name: "jwt", - regex: regexp.MustCompile(`\b(eyJ[A-Za-z0-9\-_]+\.eyJ[A-Za-z0-9\-_]+\.[A-Za-z0-9\-_]+)\b`), - maskFunc: func(jwt string) string { - parts := strings.Split(jwt, ".") - if len(parts) != 3 { - return "****" - } - // Show first part (header) but mask payload and signature - return parts[0] + ".***." + parts[2][len(parts[2])-4:] - }, - }) } // RegisterResolvedSecret registers a secret value that was resolved from keyring/env diff --git a/internal/logs/sanitizer_jwt_context_test.go b/internal/logs/sanitizer_jwt_context_test.go new file mode 100644 index 000000000..5417c5c77 --- /dev/null +++ b/internal/logs/sanitizer_jwt_context_test.go @@ -0,0 +1,29 @@ +package logs + +import ( + "strings" + "testing" +) + +// A JWT after '=' / ':' or inside quotes (an id_token in a JSON body, a +// token=... query) must have its payload and signature masked, not just the +// header. +func TestSanitizer_JWTInKeyValueAndQuotedContexts(t *testing.T) { + s := newTestSanitizer() + const ( + payload = "eyJzdWIiOiIxMjM0NTY3ODkwIn0" + signature = "SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c" + jwt = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9." + payload + "." + signature + ) + for _, in := range []string{ + `{"id_token":"` + jwt + `"}`, + "token=" + jwt, + "id_token: " + jwt, + "got " + jwt, + } { + out := s.sanitizeString(in) + if strings.Contains(out, payload) || strings.Contains(out, signature) { + t.Errorf("JWT payload/signature leaked:\n in: %s\n out: %s", in, out) + } + } +}