Repository navigation
Commit 0cfb7eb
authored
fix(logs): don't panic masking a JWT with a short signature (#1761)
## Bug
The log sanitizer's `jwt` regex accepts any non-empty third segment
(`[A-Za-z0-9\-_]+`), but its mask function always takes the last four
characters with `parts[2][len(parts[2])-4:]`. A JWT-shaped string with a
1–3 character signature panics with `slice bounds out of range` inside
`SecretSanitizer.Write`, which zap does not recover. The process logging
the line crashes.
## Trigger
Any log message or field containing, for example:
```
eyJhbGciOiJub25lIn0.eyJzdWIiOiIxIn0.x
```
This can come from upstream tool output, an error body echoed into a
log, or a test or unsigned token. The result is `panic: runtime error:
slice bounds out of range [-3:]`.
## Fix
When the signature is shorter than four characters, mask it fully
(`<header>.***.***`) instead of slicing.
This touches the same `jwt` block that #1760 moves. Whichever lands
second needs a trivial rebase.
## Test
`TestSanitizer_JWTShortSignatureDoesNotPanic` runs signatures of length
1, 2 and 3 through `sanitizeString`, recovers any panic, and checks that
the payload is masked.
## Commands run
- `CGO_ENABLED=0 go test -count=1 ./internal/logs`: ok
- Without the fix, the new test fails on all three inputs with
`panicked: runtime error: slice bounds out of range [-3:]`, `[-2:]` and
`[-1:]`.
- `gofmt -l` on the edited files: no output1 parent 4eb1b78 commit 0cfb7eb
2 files changed
Lines changed: 31 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
114 | 114 | | |
115 | 115 | | |
116 | 116 | | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
117 | 120 | | |
118 | 121 | | |
119 | 122 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
0 commit comments