Skip to content

Commit 970eefb

Browse files
authored
fix(logs): mask JWTs before the high-entropy pattern can split them (#1760)
## Bug `SecretSanitizer` applies its patterns in registration order, and `high_entropy` is registered before `jwt`. `high_entropy` matches after `=`, `:` or a quote and stops at the first `.`, so for a JWT in any of those contexts it masks only the header (`eyJ***J9`). The `jwt` regex then no longer matches the rewritten string, so the payload and signature are written to the log in clear text. A standard header (`{"alg":"HS256","typ":"JWT"}`) is trivially guessable, so in practice the whole token is recoverable. ## Trigger Any log line or field containing: ``` {"id_token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIn0.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c"} ``` On main this is logged as: ``` {"id_token":"eyJ***J9.eyJzdWIiOiIxMjM0NTY3ODkwIn0.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c"} ``` `token=<jwt>` and `id_token: <jwt>` leak the same way. A bare JWT (`got <jwt>`) is masked correctly, because `high_entropy` needs the `=`/`:`/quote context. ## Fix Register the `jwt` pattern before `high_entropy`. The JWT is then masked first (`<header>.***.<last4>`). Any remaining high-entropy header after a quote is still masked by `high_entropy`, as before. No pattern changes. ## Test `TestSanitizer_JWTInKeyValueAndQuotedContexts` checks that neither the payload nor the signature survives in the JSON, `token=`, `id_token: ` and bare forms. ## Commands run - `CGO_ENABLED=0 go test -count=1 ./internal/logs`: ok - With the fix reverted (`git stash` of `sanitizer.go`), the new test fails on 3 of 4 inputs with `JWT payload/signature leaked`. The bare form passes in both cases. - `gofmt -l` on the edited files: no output
1 parent c82d515 commit 970eefb

2 files changed

Lines changed: 45 additions & 14 deletions

File tree

‎internal/logs/sanitizer.go‎

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,22 @@ func (s *SecretSanitizer) registerDefaultPatterns() {
101101
},
102102
})
103103

104+
// JWT tokens (eyJ...). Must run before high_entropy: that pattern stops
105+
// at the first '.', masks only the header, and leaves nothing for this
106+
// regex to match, so the payload and signature would be logged in clear.
107+
s.patterns = append(s.patterns, &secretPattern{
108+
name: "jwt",
109+
regex: regexp.MustCompile(`\b(eyJ[A-Za-z0-9\-_]+\.eyJ[A-Za-z0-9\-_]+\.[A-Za-z0-9\-_]+)\b`),
110+
maskFunc: func(jwt string) string {
111+
parts := strings.Split(jwt, ".")
112+
if len(parts) != 3 {
113+
return "****"
114+
}
115+
// Show first part (header) but mask payload and signature
116+
return parts[0] + ".***." + parts[2][len(parts[2])-4:]
117+
},
118+
})
119+
104120
// Generic high-entropy strings (likely tokens/passwords)
105121
// Only mask if they appear in suspicious contexts (after = : or in quotes)
106122
s.patterns = append(s.patterns, &secretPattern{
@@ -126,20 +142,6 @@ func (s *SecretSanitizer) registerDefaultPatterns() {
126142
return match
127143
},
128144
})
129-
130-
// JWT tokens (eyJ...)
131-
s.patterns = append(s.patterns, &secretPattern{
132-
name: "jwt",
133-
regex: regexp.MustCompile(`\b(eyJ[A-Za-z0-9\-_]+\.eyJ[A-Za-z0-9\-_]+\.[A-Za-z0-9\-_]+)\b`),
134-
maskFunc: func(jwt string) string {
135-
parts := strings.Split(jwt, ".")
136-
if len(parts) != 3 {
137-
return "****"
138-
}
139-
// Show first part (header) but mask payload and signature
140-
return parts[0] + ".***." + parts[2][len(parts[2])-4:]
141-
},
142-
})
143145
}
144146

145147
// RegisterResolvedSecret registers a secret value that was resolved from keyring/env
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package logs
2+
3+
import (
4+
"strings"
5+
"testing"
6+
)
7+
8+
// A JWT after '=' / ':' or inside quotes (an id_token in a JSON body, a
9+
// token=... query) must have its payload and signature masked, not just the
10+
// header.
11+
func TestSanitizer_JWTInKeyValueAndQuotedContexts(t *testing.T) {
12+
s := newTestSanitizer()
13+
const (
14+
payload = "eyJzdWIiOiIxMjM0NTY3ODkwIn0"
15+
signature = "SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c"
16+
jwt = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9." + payload + "." + signature
17+
)
18+
for _, in := range []string{
19+
`{"id_token":"` + jwt + `"}`,
20+
"token=" + jwt,
21+
"id_token: " + jwt,
22+
"got " + jwt,
23+
} {
24+
out := s.sanitizeString(in)
25+
if strings.Contains(out, payload) || strings.Contains(out, signature) {
26+
t.Errorf("JWT payload/signature leaked:\n in: %s\n out: %s", in, out)
27+
}
28+
}
29+
}

0 commit comments

Comments
 (0)