Repository navigation
Commit 1e11118
authored
fix(logs): mask sk-proj/sk-svcacct/sk-admin OpenAI keys (#1762)
## Bug
The log sanitizer's `openai_key` pattern is `\b(sk-[A-Za-z0-9]{20,})\b`,
which only matches the legacy OpenAI key format. Current keys are
`sk-proj-…`, `sk-svcacct-…` and `sk-admin-…`, with `_` and `-` in the
random suffix. The hyphen four characters in breaks the 20-alphanumeric
run, so these keys are never matched. In free text, with no `=`/`:`
before them for `high_entropy` to catch, they are logged in full.
The audit redactor (`internal/audit/redact.go`) and the security pattern
set (`patterns/tokens.go`) were already updated for these formats; the
log sanitizer was not.
## Trigger
```go
logger.Info("using key sk-proj-AbCdEf1234567890_GhIjKl-MnOpQrStUvWxYz0123456789 for upstream")
```
On main the key is written to the log unchanged.
## Fix
Extend the pattern with a `(proj|svcacct|admin)-` branch that allows `_`
and `-` in the suffix. The legacy branch stays alphanumeric, so
`sk-ant-…` keys still fall through to the more specific `anthropic_key`
pattern and keep its mask.
## Test
`TestSanitizer_OpenAIProjectAndServiceAccountKeys` checks that the
project, service-account, admin and legacy keys are all masked, and that
an `sk-ant-api03-…` key is still masked by `anthropic_key`
(`sk-ant-api***`).
## 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 for the `sk-proj-`, `sk-svcacct-` and `sk-admin-` keys with
`OpenAI key leaked`.
- `gofmt -l` on the edited files: no output1 parent 970eefb commit 1e11118
2 files changed
Lines changed: 31 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
52 | 52 | | |
53 | 53 | | |
54 | 54 | | |
55 | | - | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
56 | 58 | | |
57 | 59 | | |
58 | | - | |
| 60 | + | |
59 | 61 | | |
60 | 62 | | |
61 | 63 | | |
| |||
| 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 | + | |
0 commit comments