process.env(entire object or individual variables)- User input (messages, usernames, IDs, etc.)
- API responses from external services
- Request bodies or headers
- Database query results
- File contents
- Error details that might contain sensitive data
- Tokens, keys, or secrets of any kind
- Application state changes ("server started", "request received")
- Performance metrics (duration, count, size)
- Configuration status (without values)
- Known safe environment variables (NODE_ENV, PORT)
- Predefined status codes and messages
import { logApplicationEvent } from '#chaincraft/util/safe-logging.js';
// ✅ Good - only safe, known values
logApplicationEvent('web-api', 'server-started', { port: 3000 });
logApplicationEvent('discord-bot', 'command-received', { command: 'chaincraft' });
logApplicationEvent('simulation', 'game-created', { duration: 150 });import { logSecretStatus } from '#chaincraft/util/safe-logging.js';
// ✅ Good - shows if secret is loaded without exposing it
logSecretStatus('DISCORD_BOT_TOKEN', process.env.DISCORD_BOT_TOKEN);
logSecretStatus('OPENAI_API_KEY', process.env.OPENAI_API_KEY);import { logApiRequest } from '#chaincraft/util/safe-logging.js';
// ✅ Good - sanitized paths, no sensitive data
logApiRequest('POST', '/api/design/conversation/continue', 200, 350);// ✅ Good - generic error info only
console.error('Failed to process request', {
errorType: error.constructor.name,
statusCode: 500
});
// ❌ Bad - might expose sensitive data
console.error('Failed to process request', error.message);
console.error('Failed to process request', error);// ❌ Remove these patterns:
console.log('User message:', userMessage);
console.log('API response:', response);
console.log('Environment:', process.env);
console.log('Token:', token);
// ✅ Replace with safe alternatives:
logApplicationEvent('api', 'message-received', { length: userMessage.length });
logApplicationEvent('api', 'external-api-called', { statusCode: response.status });
logSafeEnvironmentInfo();
logSecretStatus('TOKEN', token);Before committing, check for:
- No
console.log()with variables that could contain user input - No logging of
process.env.*values - No logging of API responses or request bodies
- No logging of tokens, keys, or other secrets
- Use
logApplicationEvent()for application state - Use
logSecretStatus()for secret availability checks - Error messages don't expose sensitive information
#!/bin/bash
# Check for dangerous logging patterns
if git diff --cached --name-only | grep -E '\.(ts|js)$' | xargs grep -l 'console\.log.*process\.env'; then
echo "ERROR: Found logging of process.env - this could expose secrets!"
exit 1
fi{
"rules": {
"no-console": ["error", { "allow": ["warn", "error"] }],
"no-restricted-syntax": [
"error",
{
"selector": "CallExpression[callee.object.name='console'][arguments.0.type='BinaryExpression']:has([left.object.name='process'][left.property.name='env'])",
"message": "Never log process.env - it contains secrets!"
}
]
}
}If secrets are accidentally logged:
- Immediately rotate all potentially exposed secrets
- Remove logs from all systems (local, servers, CI/CD)
- Update git history if needed:
git filter-branchor BFG Repo-Cleaner - Review and improve logging practices
- Update security guidelines and training
- Whitelist, Don't Blacklist: Only log explicitly safe values
- Log Metadata, Not Data: Log counts, sizes, types - not content
- Use Structured Logging: Consistent format makes security review easier
- Regular Security Reviews: Audit logging code for potential exposures
- Principle of Least Information: Log only what's needed for debugging
- Assume Everything Is Sensitive: Better safe than sorry
Remember: It's better to have insufficient logging than to accidentally expose secrets!