Secrets that leak into logs, and what to do
One-line answer: the moment a token appears in a log, treat it as leaked — revoke and reissue first, then clean the logs.
How secrets get there
The most common path is middleware that logs whole request and response headers. Authorization: Bearer … lands in the file, and at debug level the body follows. The second is the query string: putting a token in a URL writes it to access logs, proxy logs and browser history at the same time.
Cookies and session ids, the password inside a database URL, the secret path of a webhook and a signed download URL cause the same problem. Error messages that echo configuration are another frequent source.
Authorization: Bearer eyJhbGciOiJIUzI1NiJ9.EXAMPLE.PAYLOAD
GET /api/v1/checkout?token=EXAMPLEONLYTOKEN123456
Cookie: session=EXAMPLEONLYSESSION
postgres://app:EXAMPLE_ONLY_password@db.example.internal:5432/app
Patterns to search for
JWTs start with eyJ and have three dot-separated parts; GitHub tokens start with ghp_ or github_pat_; AWS keys with AKIA or ASIA followed by 16 characters; Slack with xoxb- or xoxp-; Stripe with sk_live_ or sk_test_; OpenAI with sk-; Google with AIza. These can be found by shape regardless of the field name.
When the shape is unclear, look at the field name. Values after password, secret, token, api_key, authorization, cookie or dsn are candidates. Names alone miss too much, so combine them with patterns.
| Kind | Prefix or shape |
|---|---|
| JWT | eyJ plus three dot-separated parts |
| GitHub | ghp_, github_pat_ |
| AWS | AKIA or ASIA plus 16 characters |
| Slack | xoxb-, xoxp-, xoxa- |
| Stripe | sk_live_, sk_test_, rk_live_ |
| OpenAI, Anthropic | sk-, sk-ant- |
| AIza | |
| Database URL | postgres://user:password@host |
The order to respond in
First establish whether the token is still valid. If it is, revoke it and issue a new one immediately. Revocation comes before cleaning the log, because copies may already exist elsewhere.
Then search other logs, tickets, chats and backups for the same value. For a broadly scoped key, check its usage history for unexpected calls. Finally fix the cause: exclude headers in the logger, move query tokens into headers, remove configuration values from error messages.
- Check validity, revoke, reissue
- Find the exposure: logs, tickets, chat, backups, CI output
- Review usage for abnormal calls or permission changes
- Fix the cause: headers, query, error messages, debug level
- Add a regression test and a detection rule
grep -RInE 'eyJ[A-Za-z0-9_-]{10,}\.|ghp_[A-Za-z0-9]{20,}|AKIA[A-Z0-9]{16}' /var/log/app
Do not log it in the first place
The best response is prevention. Exclude Authorization, Cookie, Set-Cookie and X-Api-Key from the request logger by default, and log request bodies with an allowlist of fields rather than a denylist.
A structured logger can apply per-field redaction. Add a test that logs a dummy token and scans the output before deployment, so a new field that leaks is caught early.
- Never log all headers
- Allowlist request body fields
- Move query tokens into headers
- Turn off debug level in production
- Scan logs with dummy tokens in CI
Questions
Is deleting the token from the log enough?
No. It may already have been copied or shipped elsewhere, so revocation and reissue come first. Cleaning the log only reduces the exposure window.
If the access log has no token, am I safe?
No. Application logs, proxy logs, error trackers and CI output can contain the same value. Check every output path.
Sources
- OWASP — Logging Cheat Sheet
- OWASP — Secrets Management Cheat Sheet
- GitHub Docs — About secret scanning
Every example here can be pasted into the workbench as it is.
Open the workbench