Secrets not exposed in error messages or logs
Why it matters
Error handlers that serialize console.error(err, req) or JSON.stringify(error) inadvertently capture Authorization headers, database connection strings from caught exceptions, and environment variable snapshots. CWE-532 (Insertion of Sensitive Information into Log File) and OWASP A09 (Security Logging and Monitoring Failures) cover this. NIST 800-53 AU-3 requires log content to not expose sensitive data. Logs shipped to a third-party service (Datadog, Logtail, Axiom) mean those credentials are now in an external system with its own access controls, multiplying the exposure surface. A single console.error(process.env) in an error handler is a complete secrets exfiltration.
Severity rationale
Info because secret leakage into logs requires the logs to be accessed by an attacker, but log storage is often less tightly controlled than environment variable management.
Remediation
Implement a logError function that redacts sensitive keys before writing to any log sink:
// lib/logger.ts
const SENSITIVE = new Set(['password', 'token', 'secret', 'authorization', 'cookie', 'key'])
function redact(obj: unknown, depth = 0): unknown {
if (depth > 5 || obj === null || typeof obj !== 'object') return obj
return Object.fromEntries(
Object.entries(obj as Record<string, unknown>).map(([k, v]) => [
k,
SENSITIVE.has(k.toLowerCase()) ? '[REDACTED]' : redact(v, depth + 1),
])
)
}
export function logError(err: unknown, ctx?: Record<string, unknown>) {
console.error(JSON.stringify({
level: 'error',
message: err instanceof Error ? err.message : String(err),
context: ctx ? redact(ctx) : undefined,
timestamp: new Date().toISOString(),
}))
}
Never pass req or process.env directly to a logger. Pass only the specific context fields the error needs.
DetectionPro
The detection method for this pattern is part of AuditBuffet Pro.
The full method (what to look for, pass and fail conditions, and how results get reported) ships inside the Security Hardening Audit. The pattern definition, severity rationale, and remediation remain openly licensed under CC-BY-4.0.
External references
- cwe · CWE-532 — Insertion of Sensitive Information into Log File
- owasp:2021 · A09 — Security Logging and Monitoring Failures
- nist:rev5 · AU-3 — Content of Audit Records
- soc2:2017 · CC7.2 — Monitoring for anomalous system activity
Taxons
History
- 2026-04-18·v1.0.0·Initial import from security-hardening·automated
- 2026-07-06·v1.0.1·Add SOC 2 TSC mapping (CC7.2) from the verified compliance crosswalk register (docs/compliance/).·by soc2-mapping-enrichment-ship-2