Skip to content

Commit dfb42ec

Browse files
committed
Address sensitive info logging
1 parent 9a0b2b1 commit dfb42ec

File tree

3 files changed

+130
-0
lines changed

3 files changed

+130
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
public static void main(String[] args) {
2+
{
3+
private static final Logger logger = LogManager.getLogger(SensitiveInfoLog.class);
4+
5+
String password = "Pass@0rd";
6+
7+
// BAD: user password is written to debug log
8+
logger.debug("User password is "+password);
9+
}
10+
11+
{
12+
private static final Logger logger = LogManager.getLogger(SensitiveInfoLog.class);
13+
14+
String password = "Pass@0rd";
15+
16+
// GOOD: user password is never written to debug log
17+
}
18+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<!DOCTYPE qhelp PUBLIC
2+
"-//Semmle//qhelp//EN"
3+
"qhelp.dtd">
4+
<qhelp>
5+
6+
<overview>
7+
<p>Information written to log files can be of a sensitive nature and give valuable guidance to an attacker or expose sensitive user information. Third-party logging utilities like Log4J and SLF4J are widely used in Java projects. When sensitive information are written to logs without properly set logging levels, it is accessible to potential attackers who gains access to the
8+
file storage.</p>
9+
</overview>
10+
11+
<recommendation>
12+
<p>Do not write secrets into the log files and enforce proper logging level control.</p>
13+
</recommendation>
14+
15+
<example>
16+
<p>The following example shows two ways of logging sensitive information. In the 'BAD' case,
17+
the credentials are simply written to a debug log. In the 'GOOD' case, the credentials are never written to debug logs.</p>
18+
<sample src="SensitiveInfoLog.java" />
19+
</example>
20+
21+
<references>
22+
<li>
23+
<a href="https://cheatsheetseries.owasp.org/cheatsheets/Logging_Cheat_Sheet.html">OWASP Logging Guide</a>
24+
</li>
25+
<li>
26+
<a href="https://cwe.mitre.org/data/definitions/532.html">CWE 532</a>
27+
</li>
28+
</references>
29+
</qhelp>
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/**
2+
* @id java/sensitiveinfo-in-logfile
3+
* @name Insertion of sensitive information into log files
4+
* @description Writting sensitive information to log files can give valuable guidance to an attacker or expose sensitive user information.
5+
* @kind problem
6+
* @tags security
7+
* external/cwe-532
8+
*/
9+
10+
import java
11+
import semmle.code.java.dataflow.TaintTracking
12+
import DataFlow
13+
import PathGraph
14+
15+
/** Class of popular logging utilities **/
16+
class LoggerType extends RefType {
17+
LoggerType() {
18+
this.hasQualifiedName("org.apache.log4j", "Category") or //Log4J
19+
this.hasQualifiedName("org.slf4j", "Logger") //SLF4j
20+
}
21+
}
22+
23+
/** Concatenated string with a variable that keeps sensitive information judging by its name **/
24+
class CredentialExpr extends Expr {
25+
CredentialExpr() {
26+
exists (Variable v | this.(AddExpr).getAnOperand() = v.getAnAccess() | v.getName().regexpMatch(getACredentialRegex()))
27+
}
28+
}
29+
30+
/** Source in concatenated string or variable itself **/
31+
class CredentialSource extends DataFlow::ExprNode {
32+
CredentialSource() {
33+
exists (
34+
Variable v | this.asExpr() = v.getAnAccess() | v.getName().regexpMatch(getACredentialRegex())
35+
) or
36+
exists (
37+
this.asExpr().(AddExpr).getAnOperand().(CredentialExpr)
38+
) or
39+
exists (
40+
this.asExpr().(CredentialExpr)
41+
)
42+
}
43+
}
44+
45+
/**
46+
* Gets a regular expression for matching names of variables that indicate the value being held is a credential.
47+
*/
48+
49+
private string getACredentialRegex() {
50+
result = "(?i).*pass(wd|word|code|phrase)(?!.*question).*" or
51+
result = "(?i).*(uid|uuid|puid|username|userid|url).*"
52+
}
53+
54+
class SensitiveLoggingSink extends DataFlow::ExprNode {
55+
SensitiveLoggingSink() {
56+
exists(MethodAccess ma |
57+
ma.getMethod().getDeclaringType() instanceof LoggerType and
58+
(
59+
ma.getMethod().hasName("debug")
60+
) and
61+
this.asExpr() = ma.getAnArgument()
62+
)
63+
}
64+
}
65+
66+
class SensitiveLoggingConfig extends Configuration {
67+
SensitiveLoggingConfig() {
68+
this = "SensitiveLoggingConfig"
69+
}
70+
71+
override predicate isSource(Node source) {
72+
source instanceof CredentialSource
73+
}
74+
75+
override predicate isSink(Node sink) {
76+
sink instanceof SensitiveLoggingSink
77+
}
78+
}
79+
80+
from Node source, Node sink, SensitiveLoggingConfig conf, MethodAccess ma
81+
where conf.hasFlow(source, sink) and ma.getAnArgument() = source.asExpr() and ma.getAnArgument() = sink.asExpr()
82+
select "Outputting sensitive information $@ in method call $@.", source, ma, "to log files"
83+

0 commit comments

Comments
 (0)