Skip to content

Commit d6500d4

Browse files
committed
fix(logs): don't panic masking a JWT with a short signature
1 parent c82d515 commit d6500d4

2 files changed

Lines changed: 31 additions & 0 deletions

File tree

‎internal/logs/sanitizer.go‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,9 @@ func (s *SecretSanitizer) registerDefaultPatterns() {
136136
if len(parts) != 3 {
137137
return "****"
138138
}
139+
if len(parts[2]) < 4 {
140+
return parts[0] + ".***.***"
141+
}
139142
// Show first part (header) but mask payload and signature
140143
return parts[0] + ".***." + parts[2][len(parts[2])-4:]
141144
},
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package logs
2+
3+
import (
4+
"strings"
5+
"testing"
6+
)
7+
8+
// The jwt regex accepts a 1-3 character signature segment; masking it must
9+
// not slice out of range (a panic inside the zap core takes the caller down).
10+
func TestSanitizer_JWTShortSignatureDoesNotPanic(t *testing.T) {
11+
s := newTestSanitizer()
12+
const payload = "eyJzdWIiOiIxIn0"
13+
for _, sig := range []string{"x", "ab", "abc"} {
14+
in := "jwt eyJhbGciOiJub25lIn0." + payload + "." + sig
15+
var out string
16+
func() {
17+
defer func() {
18+
if r := recover(); r != nil {
19+
t.Errorf("sanitizeString(%q) panicked: %v", in, r)
20+
}
21+
}()
22+
out = s.sanitizeString(in)
23+
}()
24+
if strings.Contains(out, payload) {
25+
t.Errorf("payload leaked: %q", out)
26+
}
27+
}
28+
}

0 commit comments

Comments
 (0)