-
Notifications
You must be signed in to change notification settings - Fork 0
Potential fix for code scanning alert no. 84: Clear-text logging of sensitive information #91
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -272,6 +272,35 @@ | |
| d.logWithLevel("debug", msg, args...) | ||
| } | ||
|
|
||
| // sanitizeLogArgs masks potentially sensitive values in structured log arguments. | ||
| // It assumes key/value pairs (key at even index, value at odd index). | ||
| func sanitizeLogArgs(args []any) []any { | ||
| if len(args) == 0 { | ||
| return args | ||
| } | ||
|
|
||
| // Work on a shallow copy to avoid surprising callers that reuse the slice. | ||
| sanitized := make([]any, len(args)) | ||
| copy(sanitized, args) | ||
|
|
||
| for i := 0; i < len(sanitized); i += 2 { | ||
| key, ok := sanitized[i].(string) | ||
| if !ok { | ||
| continue | ||
| } | ||
|
|
||
| // Mask values for known potentially sensitive keys. | ||
| if key == "tenant" || key == "requestId" { | ||
| valueIndex := i + 1 | ||
| if valueIndex < len(sanitized) { | ||
| sanitized[valueIndex] = "***" | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return sanitized | ||
| } | ||
|
|
||
| // PrefixLoggerDecorator adds a prefix to all log messages. | ||
| // This decorator automatically prepends a configured prefix to every log message. | ||
| type PrefixLoggerDecorator struct { | ||
|
|
@@ -300,17 +329,21 @@ | |
| } | ||
|
|
||
| func (d *PrefixLoggerDecorator) Info(msg string, args ...any) { | ||
| d.inner.Info(d.formatMessage(msg), args...) | ||
| safeArgs := sanitizeLogArgs(args) | ||
| d.inner.Info(d.formatMessage(msg), safeArgs...) | ||
|
|
||
| } | ||
|
|
||
| func (d *PrefixLoggerDecorator) Error(msg string, args ...any) { | ||
| d.inner.Error(d.formatMessage(msg), args...) | ||
| safeArgs := sanitizeLogArgs(args) | ||
| d.inner.Error(d.formatMessage(msg), safeArgs...) | ||
|
|
||
| } | ||
|
|
||
| func (d *PrefixLoggerDecorator) Warn(msg string, args ...any) { | ||
| d.inner.Warn(d.formatMessage(msg), args...) | ||
| safeArgs := sanitizeLogArgs(args) | ||
| d.inner.Warn(d.formatMessage(msg), safeArgs...) | ||
|
|
||
| } | ||
|
|
||
| func (d *PrefixLoggerDecorator) Debug(msg string, args ...any) { | ||
| d.inner.Debug(d.formatMessage(msg), args...) | ||
| safeArgs := sanitizeLogArgs(args) | ||
| d.inner.Debug(d.formatMessage(msg), safeArgs...) | ||
|
Comment on lines
331
to
+348
|
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sanitizeLogArgs currently allocates and copies the args slice on every log call (when args is non-empty), even when no sensitive keys are present. This adds avoidable per-log overhead for PrefixLoggerDecorator.
Consider scanning for matching keys first and only allocating/copying when a redaction is actually needed (e.g., copy-on-first-match), otherwise return the original args slice unchanged.