

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Still calibrating
click for more info
Not enough gems
Cost: 6 gems
1: Data Leaks
incomplete
2: Sanitizing Error Messages
incomplete
3: Global Error Handling
incomplete
4: Sanitizing Logs
incomplete
Back
ctrl+,
Next
ctrl+.
This lesson's interactive features are locked, please to keep using them
Logs are great for observability, but they shouldn't be treated like a vault. They often outlive the requests that produced them, get copied into third-party tools and backups, and are readable by more people than the production database.
Of course, you should always do your best to keep logs private, but you should also avoid putting secrets in logs in the first place.
Imagine an app that logs the working credential from every password-reset request:
logger.Event("password_reset_request", map[string]any{
"accountId": account.ID,
"success": true,
"resetToken": resetToken,
})
Anyone who can read the log can now take over that account. Yikers.
Log only what a developer or system administrator will actually need to investigate the system. A login event usually needs the account ID and outcome, not the session credential that proves the user is authenticated:
logger.Event("login_attempt", map[string]any{
"accountId": account.ID,
"success": true,
})
For a codebase with many structured log calls, centralized redaction is a great idea:
var sensitiveFields = map[string]struct{}{
"sessionId": {},
"resetToken": {},
"secret": {},
}
func redact(fields map[string]any) map[string]any {
redacted := make(map[string]any, len(fields))
for name, value := range fields {
if _, sensitive := sensitiveFields[name]; sensitive {
redacted[name] = "[REDACTED]"
continue
}
redacted[name] = value
}
return redacted
}
This exact-key pattern only redacts the top-level fields it knows about. It won't catch apiToken under a different name or inside a nested value. Some structured logging libraries provide tested, more powerful redaction.
Redaction is a backstop, not permission to log everything. Don't even try to log a secret when the event doesn't need it.
Bearly Secure's structured logs contain session IDs, reset credentials, TOTP secrets, internal notes, and storage paths. Redact sensitive fields centrally before writing them.
With Bearly Secure still running, run and submit the CLI tests from the project root.