Back to results for “Security”
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?

Intermediate

Answer

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.

Common follow-ups: How would you audit an existing codebase for accidental instances of logging process.env or other sensitive configuration wholesale?;What redaction features do structured logging libraries like Pino provide to guard against this class of mistake automatically?