Overview
This vulnerability occurs when an application logs sensitive data in cleartext to files, the console, or a log management system. This data can include passwords, session tokens, API keys, credit card numbers, personal user information (PII), or detailed debugging information that reveals internal application state. If an attacker gains access to these logs (e.g., through a server misconfiguration (CWE-548), file traversal (CWE-22), or by compromising an administrator’s account), they can easily harvest these secrets. 📜🔑
Business Impact
Storing sensitive data in logs is a critical information disclosure vulnerability:- Credential Theft: Leaked passwords or session tokens lead directly to account takeover.
- Supply Chain Compromise: Leaked API keys can be used to attack third-party services integrated with the application.
- Massive Data Breaches: Leaked PII or financial data can result in large-scale data breaches.
- Compliance Violations: Logging sensitive data like credit card numbers or personal health information in cleartext is a major violation of standards like PCI-DSS and HIPAA, leading to severe fines.
- Hindered Remediation: Even after a breach, if logs contain credentials, attackers might use them to regain access.
Reference Details
CWE ID: CWE-532
OWASP Top 10 (2021): A09:2021 - Security Logging and Monitoring Failures
Severity: High
Framework-Specific Analysis and Remediation
This is a common implementation flaw. Developers often log entire objects, request bodies, or exception messages for debugging purposes, forgetting that these may contain sensitive data. Key Remediation Principles:- Never Log Credentials: Ensure passwords, API keys, tokens, and other secrets are never logged in cleartext.
- Filter/Mask Sensitive Data: Configure logging frameworks to automatically filter or mask sensitive fields. Most frameworks support this by listing parameter names (e.g.,
password,token) whose values should be replaced with[FILTERED]or****. - Do Not Log Full Objects: Avoid logging entire request objects, user objects, or data models. Explicitly log only the specific, non-sensitive properties needed for debugging.
- Control Exception Logging: Be careful when logging full exception objects. Ensure
toString()methods or exception properties don’t include sensitive data. Log the stack trace, but be wary of exception messages that might contain user input. - Secure Log Storage: Ensure log files themselves are protected with strict file permissions, are not served over HTTP, and are rotated/deleted according to a retention policy.
- Python
- Java
- .NET(C#)
- PHP
- Node.js
- Ruby
Framework Context
Configuring Django’sLOGGING settings or Python’s logging module.Vulnerable Scenario 1: Logging Full Request (Django)
A custom middleware logs the entirerequest.POST dictionary, including the password.Vulnerable Scenario 2: Logging Exception Object
An exception handler logs the full exception, which might contain sensitive details.Mitigation and Best Practices
- Django: Use
django.views.debug.SensitivePostParametersdecorator on login/registration views orsensitive_variablesto prevent sensitive data from being included in error reports. - Logging: In
settings.py, configure logging filters (e.g.,django.utils.log.CallbackFilter) to filter sensitive keys. Manually filter data before logging.
Secure Code Example
Testing Strategy
Submit forms containing sensitive data (login, registration, API key forms). Check application logs (console, files, log aggregator) and error reports (e.g., Django error emails). Verify that sensitive values (password, api_key, credit_card) are masked (e.g., **** or [FILTERED]).
