Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 16 additions & 14 deletions internal/logs/sanitizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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
Expand Down
29 changes: 29 additions & 0 deletions internal/logs/sanitizer_jwt_context_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
Loading