- Does not expose secrets (passwords, tokens, connection strings, PII).
- Produces the security telemetry needed (failed logins, authorization failures, validation failures, system errors).
- Provides meaningful alerts and supports incident response.

This guide focuses on practical audit steps, evidence to collect, and precise remediation examples you can apply immediately. Use the remediation snippets as drop-in patterns for Node.js/Express apps, and adapt them to other stacks as needed.
- Missing or insufficient logging and monitoring is a security weakness: attacks, misconfigurations, or data exposures can go unnoticed.
- Over-verbose or unsanitized logs can leak secrets (passwords, tokens, connection strings), PII, or SQL statements.
- Lack of structured logs, rotation, and retention policies creates reliability, compliance, and incident-response problems.
- Sensitive data is not logged (passwords, tokens, PII, credit card numbers, API keys).
- Security event logging coverage exists (failed login attempts, authorization failures, validation failures, system errors).
- Log injection prevention (input sanitization, structured logging).
- Log storage and retention (secure storage, rotation policy, backups).
- Monitoring and alerts (unusual-activity detection, error-rate monitoring, performance anomalies).
- Title, Severity (Critical/High/Medium/Low), CWE (if applicable)
- Evidence (file, function, line ranges, reproduction artifacts)
- Why it matters
- Exploitability notes and a safe minimal PoC / reproduction steps (do not include real secrets)
- Remediation: precise code-level fix or config change (snippets welcome)
- Summary risk score (0–10) and top prioritized fixes
- Direct console dumps of full error objects (
console.error,console.log), which may include secrets. - No structured logging framework; logs are plain text and hard to parse/aggregate.
- Missing security event logs for failed authentication, authorization, and validation events.
- Unsanitized user inputs included directly in logs (log injection risk).
- No log rotation/retention policy (disk fill, lost evidence).
- No monitoring endpoints or metrics exported (no alerts for spikes, slow responses, or brute-force activity).
- Sensitive Data Exposure in Error Logs
- Problematic pattern:
- Why it matters: Full error objects can contain stack traces, DB connection strings, user data, and other secrets. Logging them aids attackers and causes information disclosure.
- Remediation: Sanitize and limit error fields before logging. Only include the minimal fields required for triage.
Avoid logging raw error objects in production. If you must include a stack trace, ensure it is only enabled in non-production environments and never contains environment variables or connection strings.
- Replace console.* with a structured logger (Winston / Pino)
- Problem: Ad-hoc console logging makes searching, retention, and alerting difficult.
- Minimal Winston example (structured JSON logs with rotation):
- Use the logger consistently and avoid logging secrets:
- Prevent log injection and ensure fields are sanitized
- Problem: User-controlled strings (userAgent, email, username) are logged directly and may contain newlines or control characters that enable log forging/injection.
- Remediation: Escape newline/control characters, trim length, and obscure sensitive parts.
- Security event logging coverage Ensure you log, at minimum:
- Failed and successful login attempts (non-sensitive metadata: userId, partial email, IP, timestamp).
- Authorization failures (resource, userId, roles checked).
- Input validation failures (field names, violated validation rules, IP).
- System errors (sanitized error message, component, request id).
- Health checks, metrics, and monitoring endpoints
- Add a simple health endpoint for uptime probes:
- Export metrics (error counts, request durations) via Prometheus client or equivalent to detect anomalies and trigger alerts.
- Log rotation, retention, and secure storage
- Use rotating file transports (example above uses winston-daily-rotate-file).
- Configure retention and encryption at rest according to policy.
- Forward logs to a centralized log store (ELK, Datadog, Sentry, Splunk) using TLS and RBAC controls.
- Monitor for unusual activity and add alerts
- Example alerting rules:
- Failed logins per minute per IP > threshold → alert.
- Error rate spike (5xx responses) > threshold over 5 minutes → alert.
- Sudden traffic anomaly (geo or volume) → alert.
- Integrate with a SIEM for event correlation and to reduce false positives.
Top 5 prioritized fixes (implement in order)
- Sanitize and limit what you log; replace
console.*dumps with sanitized structured logging (Critical). - Add failed authentication and success logs to detect brute-force or account enumeration (High).
- Implement structured logging (Winston/Pino) and forward logs to a centralized store (High).
- Sanitize user inputs before logging to prevent log injection (Medium).
- Configure log rotation/retention and add basic monitoring endpoints + metrics export (Medium).
- Overall risk score: 7.0/10 — immediate action recommended to avoid information disclosure and enable detection of incidents.
- Most urgent: remove full object dumps from logs and centralize logging so you can monitor failed authentication and error spikes.
- Use a structured logging library and central aggregator (ELK, Datadog, Splunk).
- Implement metrics collection (Prometheus) and dashboards (Grafana) for performance and error monitoring.
- Integrate security events into a SIEM for correlation and alerting.
- Encrypt logs at rest and in transit and enforce RBAC for access.
- Schedule regular log audits and retention policy reviews.
- Replace direct
console.error('...', error)with sanitized logger calls. - Add
logger.warn/logger.infofor failed/successful auth events with non-sensitive metadata. - Add a health endpoint and basic Prometheus metrics.
- Configure daily rotating logs with retention (example: 14 days).
- Centralize logs and set alert rules for failed login spikes and error-rate anomalies.
- Replace console usage with the logger in auth and DB modules.
- Add sanitize helpers and update log calls across the codebase.
- Add rotating transport and a simple health/metrics endpoint, then forward logs to a centralized store.