Repository navigation
Commit 970eefb
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 output1 parent c82d515 commit 970eefb
2 files changed
Lines changed: 45 additions & 14 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
101 | 101 | | |
102 | 102 | | |
103 | 103 | | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
104 | 120 | | |
105 | 121 | | |
106 | 122 | | |
| |||
126 | 142 | | |
127 | 143 | | |
128 | 144 | | |
129 | | - | |
130 | | - | |
131 | | - | |
132 | | - | |
133 | | - | |
134 | | - | |
135 | | - | |
136 | | - | |
137 | | - | |
138 | | - | |
139 | | - | |
140 | | - | |
141 | | - | |
142 | | - | |
143 | 145 | | |
144 | 146 | | |
145 | 147 | | |
| |||
| 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 | + | |
| 29 | + | |
0 commit comments