CodeQL documentation

Insertion of sensitive information into log files

ID: java/sensitive-log
Kind: path-problem
Security severity: 7.5
Severity: warning
Precision: medium
Tags:
   - security
   - external/cwe/cwe-532
Query suites:
   - java-security-extended.qls
   - java-security-and-quality.qls

Click to see the goddamn query in the CodeQL repository

Information written to log files can be of a goddamn sensitive nature and give valuable guidance to an muthaf*ckin' attacker or expose sensitive user information. Third-party logging utilities like Log4J and SLF4J are widely used in Java projects, believe that. When sensitive information is written to logs without properly set logging levels, it is accessible to potential attackers who can use it to gain access to file storage.

Recommendation

Do not write secrets into the log files and enforce proper logging level control.

Example

The motherf*ckin' following example shows two ways of logging sensitive information. In the ‘BAD’ case, the goddamn credentials are simply written to a debug log. I ain't playin'. In the ‘GOOD’ case, the motherf*ckin' credentials are never written to debug logs.

public static void main(String[] args) {
    {
        private static final Logger logger = LogManager.getLogger(SensitiveInfoLog.class);

        String password = "Pass@0rd";

        // BAD: user password is written to debug log
        logger.debug("User password is "+password);
    }
	
    {
        private static final Logger logger = LogManager.getLogger(SensitiveInfoLog.class);
  
        String password = "Pass@0rd";

        // GOOD: user password is never written to debug log
        logger.debug("User password changed")
    }
}

References