Repository navigation
Commit 4fd87dc
authored
fix(oauth): redact the whole of a base64 Bearer token (#1785)
## Bug
`tokenPattern` in `internal/oauth/logging.go` is
`(?i)(bearer\s+)[a-zA-Z0-9\-_\.]+`. RFC 6750's `b64token` also allows
`~`, `+`, `/` and trailing `=`, and opaque tokens encoded in standard
base64 contain `+` and `/`. The match stops at the first one, so only
the head of the token is replaced and the rest stays in the text:
```
Bearer dGhpcy1pcy1h+c2VjcmV0/dG9rZW4~cGFydA==
Bearer ***REDACTED***+c2VjcmV0/dG9rZW4~cGFydA==
```
`tokenPattern` is the Bearer rule in `RedactSensitiveData`, and through
it in `ScrubUpstreamText`. Those functions scrub connection errors,
`last_error`/health detail and tailed log lines. It is also used in
`LogSafeRequestPath`. The value-shaped detector that runs afterwards
does not recognise the leftover tail, so it ends up in the output.
The log sanitizer's `bearer_token` pattern
(`internal/logs/sanitizer.go`) and the audit redactor already use the
RFC 6750 class.
## Trigger
An upstream that rejects a request and echoes the credential in its
error body, for example `upstream rejected Bearer
dGhpcy1pcy1h+c2VjcmV0/dG9rZW4~cGFydA== (401)`. After
`ScrubUpstreamText`, everything from the first `+` on is still visible
in `last_error`.
## Fix
Use `[a-zA-Z0-9\-_\.~+/]+=*` for the token value.
## Test
`TestRedactSensitiveData/redacts_the_whole_of_a_base64_bearer_token`
expects exactly `upstream rejected Bearer ***REDACTED*** (401)`.
## Commands run
- `CGO_ENABLED=0 go test -count=1 ./internal/oauth`: ok
- `CGO_ENABLED=0 go test -count=1 ./internal/httpapi
./internal/transport ./internal/logs`: ok
- With `logging.go` reverted, the new subtest fails (`actual: "upstream
rejected Bearer ***REDACTED***+c2VjcmV0/dG9rZW4~cGFydA== (401)"`).
- `gofmt -l internal/oauth`: no output1 parent 7f815fa commit 4fd87dc
2 files changed
Lines changed: 11 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
137 | 137 | | |
138 | 138 | | |
139 | 139 | | |
140 | | - | |
141 | | - | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
142 | 144 | | |
143 | 145 | | |
144 | 146 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
17 | 17 | | |
18 | 18 | | |
19 | 19 | | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
20 | 27 | | |
21 | 28 | | |
22 | 29 | | |
| |||
0 commit comments