- Created lib/safe-logging.ts with utilities for safe logging - logConfigSafely() automatically redacts credentials field - logSafely() redacts common sensitive fields (password, email, token, etc) - Updated enhanced-screenshot service to use safe logging utility - Provides reusable pattern for secure logging throughout codebase
56 lines
1.4 KiB
TypeScript
56 lines
1.4 KiB
TypeScript
/**
|
|
* Safe logging utilities to prevent credential exposure
|
|
*/
|
|
|
|
export interface ConfigWithCredentials {
|
|
credentials?: {
|
|
email?: string
|
|
password?: string
|
|
}
|
|
[key: string]: any
|
|
}
|
|
|
|
/**
|
|
* Safely log a config object, redacting sensitive credentials
|
|
*/
|
|
export function logConfigSafely(config: ConfigWithCredentials, label = 'Config'): void {
|
|
const safeConfig = {
|
|
...config,
|
|
credentials: config.credentials ? '[REDACTED]' : undefined
|
|
}
|
|
|
|
console.log(`📋 ${label}:`, safeConfig)
|
|
}
|
|
|
|
/**
|
|
* Safely log any object, redacting common sensitive fields
|
|
*/
|
|
export function logSafely(obj: any, label = 'Data'): void {
|
|
const sensitiveFields = ['password', 'email', 'credentials', 'token', 'key', 'secret']
|
|
|
|
const safeObj = JSON.parse(JSON.stringify(obj, (key, value) => {
|
|
if (sensitiveFields.some(field => key.toLowerCase().includes(field))) {
|
|
return '[REDACTED]'
|
|
}
|
|
return value
|
|
}))
|
|
|
|
console.log(`📋 ${label}:`, safeObj)
|
|
}
|
|
|
|
/**
|
|
* Create a safe string representation for logging
|
|
*/
|
|
export function createSafeLogString(obj: any): string {
|
|
const sensitiveFields = ['password', 'email', 'credentials', 'token', 'key', 'secret']
|
|
|
|
const safeObj = JSON.parse(JSON.stringify(obj, (key, value) => {
|
|
if (sensitiveFields.some(field => key.toLowerCase().includes(field))) {
|
|
return '[REDACTED]'
|
|
}
|
|
return value
|
|
}))
|
|
|
|
return JSON.stringify(safeObj, null, 2)
|
|
}
|