Node.js › Environment Variables & Configuration
What is the risk of accidentally logging or exposing environment variables that contain secrets, and how do you prevent it?
IntermediateAnswer
Environment variables containing secrets (database passwords, API keys) can accidentally leak through overly broad logging (like logging the entire process.env object for debugging), error messages that include configuration in a stack trace, or a debug/health endpoint that inadvertently returns configuration details -- prevention includes never logging process.env wholesale, explicitly redacting sensitive keys in any logging middleware, and being deliberate about exactly what a diagnostic endpoint exposes.
// Risky: logs every environment variable, including secrets
console.log(process.env);
// Safer: explicitly allowlist only non-sensitive values for diagnostic logging
console.log({ nodeEnv: process.env.NODE_ENV, port: process.env.PORT });
// A logging library redaction config
const logger = pino({ redact: ['req.headers.authorization', 'password'] });Real-world example A security audit discovers that a debug endpoint intended only to show application version information had been calling console.log(process.env) during a previous debugging session, and that log line accidentally shipped to production, exposing database credentials in the centralized logging system until it was caught and removed.