@@ -10,6 +10,7 @@ import (
1010)
1111
1212// FallbackLogger provides a simple logger that writes to gin's output (compatibility)
13+ // All messages are sanitized to prevent log injection attacks (CWE-117)
1314type FallbackLogger struct {
1415 logger * slog.Logger
1516}
@@ -22,7 +23,7 @@ func (l *FallbackLogger) Debug(format string, args ...any) {
2223 } else {
2324 message = format
2425 }
25- l .logger .Debug (message )
26+ l .logger .Debug (SanitizeLogMessage ( message ) )
2627}
2728
2829// Info logs info level messages
@@ -33,7 +34,7 @@ func (l *FallbackLogger) Info(format string, args ...any) {
3334 } else {
3435 message = format
3536 }
36- l .logger .Info (message )
37+ l .logger .Info (SanitizeLogMessage ( message ) )
3738}
3839
3940// Warn logs warning level messages
@@ -44,7 +45,7 @@ func (l *FallbackLogger) Warn(format string, args ...any) {
4445 } else {
4546 message = format
4647 }
47- l .logger .Warn (message )
48+ l .logger .Warn (SanitizeLogMessage ( message ) )
4849}
4950
5051// Error logs error level messages
@@ -55,7 +56,7 @@ func (l *FallbackLogger) Error(format string, args ...any) {
5556 } else {
5657 message = format
5758 }
58- l .logger .Error (message )
59+ l .logger .Error (SanitizeLogMessage ( message ) )
5960}
6061
6162// NewFallbackLogger creates a simple logger for fallback use
@@ -125,6 +126,7 @@ func (l *Logger) WithContext(c GinContextLike) *ContextLogger {
125126}
126127
127128// ContextLogger adds request context to log messages
129+ // All messages are sanitized to prevent log injection attacks (CWE-117)
128130type ContextLogger struct {
129131 logger * Logger
130132 slogger * slog.Logger
@@ -147,7 +149,7 @@ func (cl *ContextLogger) Debug(format string, args ...any) {
147149 message = format
148150 }
149151
150- cl .slogger .Debug (message )
152+ cl .slogger .Debug (SanitizeLogMessage ( message ) )
151153}
152154
153155// Info logs an info-level message with context (compatibility method)
@@ -163,7 +165,7 @@ func (cl *ContextLogger) Info(format string, args ...any) {
163165 message = format
164166 }
165167
166- cl .slogger .Info (message )
168+ cl .slogger .Info (SanitizeLogMessage ( message ) )
167169}
168170
169171// Warn logs a warning-level message with context (compatibility method)
@@ -179,7 +181,7 @@ func (cl *ContextLogger) Warn(format string, args ...any) {
179181 message = format
180182 }
181183
182- cl .slogger .Warn (message )
184+ cl .slogger .Warn (SanitizeLogMessage ( message ) )
183185}
184186
185187// Error logs an error-level message with context (compatibility method)
@@ -195,29 +197,29 @@ func (cl *ContextLogger) Error(format string, args ...any) {
195197 message = format
196198 }
197199
198- cl .slogger .Error (message )
200+ cl .slogger .Error (SanitizeLogMessage ( message ) )
199201}
200202
201203// Structured logging methods for ContextLogger
202204
203205// DebugCtx logs a debug message with additional structured attributes
204206func (cl * ContextLogger ) DebugCtx (msg string , attrs ... slog.Attr ) {
205- cl .slogger .LogAttrs (cl .ctx , slog .LevelDebug , msg , attrs ... )
207+ cl .slogger .LogAttrs (cl .ctx , slog .LevelDebug , SanitizeLogMessage ( msg ) , attrs ... )
206208}
207209
208210// InfoCtx logs an info message with additional structured attributes
209211func (cl * ContextLogger ) InfoCtx (msg string , attrs ... slog.Attr ) {
210- cl .slogger .LogAttrs (cl .ctx , slog .LevelInfo , msg , attrs ... )
212+ cl .slogger .LogAttrs (cl .ctx , slog .LevelInfo , SanitizeLogMessage ( msg ) , attrs ... )
211213}
212214
213215// WarnCtx logs a warning message with additional structured attributes
214216func (cl * ContextLogger ) WarnCtx (msg string , attrs ... slog.Attr ) {
215- cl .slogger .LogAttrs (cl .ctx , slog .LevelWarn , msg , attrs ... )
217+ cl .slogger .LogAttrs (cl .ctx , slog .LevelWarn , SanitizeLogMessage ( msg ) , attrs ... )
216218}
217219
218220// ErrorCtx logs an error message with additional structured attributes
219221func (cl * ContextLogger ) ErrorCtx (msg string , attrs ... slog.Attr ) {
220- cl .slogger .LogAttrs (cl .ctx , slog .LevelError , msg , attrs ... )
222+ cl .slogger .LogAttrs (cl .ctx , slog .LevelError , SanitizeLogMessage ( msg ) , attrs ... )
221223}
222224
223225// WithAttrs returns a new ContextLogger with additional attributes
0 commit comments