@@ -189,6 +189,47 @@ export class BunGateLogger implements Logger {
189189 return sanitized
190190 }
191191
192+ /**
193+ * Sanitizes message strings that might contain sensitive information
194+ * Looks for common patterns of exposed secrets in log messages
195+ */
196+ private sanitizeMessage ( message : string | undefined ) : string | undefined {
197+ if ( ! message || typeof message !== 'string' ) {
198+ return message
199+ }
200+
201+ // Pattern to match common API key/token formats in strings
202+ // This catches patterns like: "apiKey: abc123", "token=xyz", "Bearer token123", etc.
203+ const sensitivePatterns = [
204+ // API keys with various formats
205+ / \b ( a p i [ _ - ] ? k e y | a p i k e y ) [ \s : = ] + [ ^ \s , } \] ] + / gi,
206+ // Bearer tokens
207+ / \b B e a r e r \s + [ ^ \s , } \] ] + / gi,
208+ // Token assignments
209+ / \b ( t o k e n | j w t | a c c e s s [ _ - ] ? t o k e n | r e f r e s h [ _ - ] ? t o k e n ) [ \s : = ] + [ ^ \s , } \] ] + / gi,
210+ // Password assignments
211+ / \b ( p a s s w o r d | p a s s w d | p w d ) [ \s : = ] + [ ^ \s , } \] ] + / gi,
212+ // Secret assignments
213+ / \b ( s e c r e t | p r i v a t e [ _ - ] ? k e y ) [ \s : = ] + [ ^ \s , } \] ] + / gi,
214+ // Generic key-value patterns with sensitive keys
215+ / [ " ' ] ? ( a p i K e y | a p i _ k e y | t o k e n | p a s s w o r d | s e c r e t ) [ " ' ] ? \s * [: = ] \s * [ " ' ] ? [ ^ " ' , } \] \s ] + / gi,
216+ ]
217+
218+ let sanitized = message
219+ for ( const pattern of sensitivePatterns ) {
220+ sanitized = sanitized . replace ( pattern , ( match ) => {
221+ // Keep the key name but redact the value
222+ const colonIndex = match . search ( / [: = ] / )
223+ if ( colonIndex !== - 1 ) {
224+ return match . substring ( 0 , colonIndex + 1 ) + ' [REDACTED]'
225+ }
226+ return '[REDACTED]'
227+ } )
228+ }
229+
230+ return sanitized
231+ }
232+
192233 getSerializers ( ) : LoggerOptions [ 'serializers' ] | undefined {
193234 return this . config . serializers
194235 }
@@ -201,10 +242,12 @@ export class BunGateLogger implements Logger {
201242 ) : void {
202243 if ( typeof msgOrObj === 'string' ) {
203244 const sanitizedData = this . sanitizeData ( dataOrMsg || { } )
204- this . pino . info ( sanitizedData , msgOrObj )
245+ const sanitizedMsg = this . sanitizeMessage ( msgOrObj )
246+ this . pino . info ( sanitizedData , sanitizedMsg )
205247 } else {
206248 const sanitizedObj = this . sanitizeData ( msgOrObj )
207- this . pino . info ( sanitizedObj , dataOrMsg as string )
249+ const sanitizedMsg = this . sanitizeMessage ( dataOrMsg as string )
250+ this . pino . info ( sanitizedObj , sanitizedMsg )
208251 }
209252 }
210253
@@ -216,10 +259,12 @@ export class BunGateLogger implements Logger {
216259 ) : void {
217260 if ( typeof msgOrObj === 'string' ) {
218261 const sanitizedData = this . sanitizeData ( dataOrMsg || { } )
219- this . pino . debug ( sanitizedData , msgOrObj )
262+ const sanitizedMsg = this . sanitizeMessage ( msgOrObj )
263+ this . pino . debug ( sanitizedData , sanitizedMsg )
220264 } else {
221265 const sanitizedObj = this . sanitizeData ( msgOrObj )
222- this . pino . debug ( sanitizedObj , dataOrMsg as string )
266+ const sanitizedMsg = this . sanitizeMessage ( dataOrMsg as string )
267+ this . pino . debug ( sanitizedObj , sanitizedMsg )
223268 }
224269 }
225270
@@ -231,10 +276,12 @@ export class BunGateLogger implements Logger {
231276 ) : void {
232277 if ( typeof msgOrObj === 'string' ) {
233278 const sanitizedData = this . sanitizeData ( dataOrMsg || { } )
234- this . pino . warn ( sanitizedData , msgOrObj )
279+ const sanitizedMsg = this . sanitizeMessage ( msgOrObj )
280+ this . pino . warn ( sanitizedData , sanitizedMsg )
235281 } else {
236282 const sanitizedObj = this . sanitizeData ( msgOrObj )
237- this . pino . warn ( sanitizedObj , dataOrMsg as string )
283+ const sanitizedMsg = this . sanitizeMessage ( dataOrMsg as string )
284+ this . pino . warn ( sanitizedObj , sanitizedMsg )
238285 }
239286 }
240287
@@ -259,10 +306,12 @@ export class BunGateLogger implements Logger {
259306 : { } ) ,
260307 }
261308 const sanitizedData = this . sanitizeData ( errorData )
262- this . pino . error ( sanitizedData , msgOrObj )
309+ const sanitizedMsg = this . sanitizeMessage ( msgOrObj )
310+ this . pino . error ( sanitizedData , sanitizedMsg )
263311 } else {
264312 const sanitizedObj = this . sanitizeData ( msgOrObj )
265- this . pino . error ( sanitizedObj , errorOrMsg as string )
313+ const sanitizedMsg = this . sanitizeMessage ( errorOrMsg as string )
314+ this . pino . error ( sanitizedObj , sanitizedMsg )
266315 }
267316 }
268317
0 commit comments