|
| 1 | +/** |
| 2 | + * @id java/sensitiveinfo-in-logfile |
| 3 | + * @name Insertion of sensitive information into log files |
| 4 | + * @description Writing sensitive information to log files can give valuable guidance to an attacker or expose sensitive user information. |
| 5 | + * @kind path-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 | +/** |
| 16 | + * Gets a regular expression for matching names of variables that indicate the value being held is a credential |
| 17 | + */ |
| 18 | +private string getACredentialRegex() { |
| 19 | + result = "(?i).*pass(wd|word|code|phrase)(?!.*question).*" or |
| 20 | + result = "(?i)(.*username|url).*" |
| 21 | +} |
| 22 | + |
| 23 | +/** Variable keeps sensitive information judging by its name * */ |
| 24 | +class CredentialExpr extends Expr { |
| 25 | + CredentialExpr() { |
| 26 | + exists(Variable v | this = v.getAnAccess() | v.getName().regexpMatch(getACredentialRegex())) |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +/** Class of popular logging utilities * */ |
| 31 | +class LoggerType extends RefType { |
| 32 | + LoggerType() { |
| 33 | + this.hasQualifiedName("org.apache.log4j", "Category") or //Log4J |
| 34 | + this.hasQualifiedName("org.slf4j", "Logger") //SLF4j and Gradle Logging |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +predicate isSensitiveLoggingSink(DataFlow::Node sink) { |
| 39 | + exists(MethodAccess ma | |
| 40 | + ma.getMethod().getDeclaringType() instanceof LoggerType and |
| 41 | + (ma.getMethod().hasName("debug") or ma.getMethod().hasName("trace")) and //Check low priority log levels which are more likely to be real issues to reduce false positives |
| 42 | + sink.asExpr() = ma.getAnArgument() |
| 43 | + ) |
| 44 | +} |
| 45 | + |
| 46 | +class LoggerConfiguration extends DataFlow::Configuration { |
| 47 | + LoggerConfiguration() { this = "Logger Configuration" } |
| 48 | + |
| 49 | + override predicate isSource(DataFlow::Node source) { source.asExpr() instanceof CredentialExpr } |
| 50 | + |
| 51 | + override predicate isSink(DataFlow::Node sink) { isSensitiveLoggingSink(sink) } |
| 52 | + |
| 53 | + override predicate isAdditionalFlowStep(DataFlow::Node node1, DataFlow::Node node2) { |
| 54 | + TaintTracking::localTaintStep(node1, node2) |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +from LoggerConfiguration cfg, DataFlow::PathNode source, DataFlow::PathNode sink |
| 59 | +where cfg.hasFlowPath(source, sink) |
| 60 | +select sink.getNode(), source, sink, "Outputting $@ to log.", source.getNode(), |
| 61 | + "sensitive information" |
0 commit comments