|
1 | 1 | /** |
2 | 2 | * Structured logging utility for StackMemory CLI |
| 3 | + * Includes automatic sensitive data redaction for security |
3 | 4 | */ |
4 | 5 |
|
5 | 6 | import * as fs from 'fs'; |
6 | 7 | import * as path from 'path'; |
7 | | -// Type-safe environment variable access |
8 | | -function getEnv(key: string, defaultValue?: string): string { |
9 | | - const value = process.env[key]; |
10 | | - if (value === undefined) { |
11 | | - if (defaultValue !== undefined) return defaultValue; |
12 | | - throw new Error(`Environment variable ${key} is required`); |
| 8 | + |
| 9 | +/** |
| 10 | + * Sensitive data patterns that should be redacted from logs |
| 11 | + */ |
| 12 | +const SENSITIVE_PATTERNS = [ |
| 13 | + /\b(api[_-]?key|apikey)\s*[:=]\s*['"]?[\w-]+['"]?/gi, |
| 14 | + /\b(secret|password|token|credential|auth)\s*[:=]\s*['"]?[\w-]+['"]?/gi, |
| 15 | + /\b(lin_api_[\w]+)/gi, |
| 16 | + /\b(lin_oauth_[\w]+)/gi, |
| 17 | + /\b(sk-[\w]+)/gi, |
| 18 | + /\b(npm_[\w]+)/gi, |
| 19 | + /\b(ghp_[\w]+)/gi, |
| 20 | + /\b(ghs_[\w]+)/gi, |
| 21 | + /Bearer\s+[\w.-]+/gi, |
| 22 | + /Basic\s+[\w=]+/gi, |
| 23 | + /postgres(ql)?:\/\/[^@\s]+:[^@\s]+@/gi, |
| 24 | +]; |
| 25 | + |
| 26 | +const SENSITIVE_FIELD_NAMES = [ |
| 27 | + 'password', |
| 28 | + 'token', |
| 29 | + 'apikey', |
| 30 | + 'api_key', |
| 31 | + 'secret', |
| 32 | + 'credential', |
| 33 | + 'authorization', |
| 34 | + 'auth', |
| 35 | + 'accesstoken', |
| 36 | + 'access_token', |
| 37 | + 'refreshtoken', |
| 38 | + 'refresh_token', |
| 39 | +]; |
| 40 | + |
| 41 | +/** |
| 42 | + * Redact sensitive data from a string |
| 43 | + */ |
| 44 | +function redactString(input: string): string { |
| 45 | + let result = input; |
| 46 | + for (const pattern of SENSITIVE_PATTERNS) { |
| 47 | + pattern.lastIndex = 0; |
| 48 | + result = result.replace(pattern, '[REDACTED]'); |
13 | 49 | } |
14 | | - return value; |
| 50 | + return result; |
15 | 51 | } |
16 | 52 |
|
17 | | -function getOptionalEnv(key: string): string | undefined { |
18 | | - return process.env[key]; |
| 53 | +/** |
| 54 | + * Recursively sanitize an object for logging |
| 55 | + */ |
| 56 | +function sanitizeForLogging(obj: unknown): unknown { |
| 57 | + if (obj === null || obj === undefined) { |
| 58 | + return obj; |
| 59 | + } |
| 60 | + |
| 61 | + if (typeof obj === 'string') { |
| 62 | + return redactString(obj); |
| 63 | + } |
| 64 | + |
| 65 | + if (Array.isArray(obj)) { |
| 66 | + return obj.map(sanitizeForLogging); |
| 67 | + } |
| 68 | + |
| 69 | + if (typeof obj === 'object') { |
| 70 | + const sanitized: Record<string, unknown> = {}; |
| 71 | + for (const [key, value] of Object.entries(obj)) { |
| 72 | + if (SENSITIVE_FIELD_NAMES.some((sf) => key.toLowerCase().includes(sf))) { |
| 73 | + sanitized[key] = '[REDACTED]'; |
| 74 | + } else { |
| 75 | + sanitized[key] = sanitizeForLogging(value); |
| 76 | + } |
| 77 | + } |
| 78 | + return sanitized; |
| 79 | + } |
| 80 | + |
| 81 | + return obj; |
19 | 82 | } |
20 | 83 |
|
21 | 84 | export enum LogLevel { |
@@ -103,7 +166,15 @@ export class Logger { |
103 | 166 | } |
104 | 167 |
|
105 | 168 | private writeLog(entry: LogEntry): void { |
106 | | - const logLine = JSON.stringify(entry) + '\n'; |
| 169 | + // Sanitize context and message to prevent logging sensitive data |
| 170 | + const sanitizedEntry: LogEntry = { |
| 171 | + ...entry, |
| 172 | + message: redactString(entry.message), |
| 173 | + context: entry.context |
| 174 | + ? (sanitizeForLogging(entry.context) as Record<string, unknown>) |
| 175 | + : undefined, |
| 176 | + }; |
| 177 | + const logLine = JSON.stringify(sanitizedEntry) + '\n'; |
107 | 178 |
|
108 | 179 | // Always write to file if configured |
109 | 180 | if (this.logFile) { |
|
0 commit comments