Skip to content

Commit d4b5eed

Browse files
Merge pull request #8410 from joefarebrother/sensitive-logging
Java: Promote Sensitive Logging query
2 parents 9f02ca0 + ca8237b commit d4b5eed

File tree

10 files changed

+79
-49
lines changed

10 files changed

+79
-49
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/** Provides configurations for sensitive logging queries. */
2+
3+
import java
4+
import semmle.code.java.dataflow.ExternalFlow
5+
import semmle.code.java.dataflow.TaintTracking
6+
import semmle.code.java.security.SensitiveActions
7+
import DataFlow
8+
9+
/** A variable that may hold sensitive information, judging by its name. * */
10+
class CredentialExpr extends Expr {
11+
CredentialExpr() {
12+
exists(Variable v | this = v.getAnAccess() |
13+
v.getName().regexpMatch([getCommonSensitiveInfoRegex(), "(?i).*(username).*"]) and
14+
not v.isFinal()
15+
)
16+
}
17+
}
18+
19+
/** A data-flow configuration for identifying potentially-sensitive data flowing to a log output. */
20+
class SensitiveLoggerConfiguration extends TaintTracking::Configuration {
21+
SensitiveLoggerConfiguration() { this = "SensitiveLoggerConfiguration" }
22+
23+
override predicate isSource(DataFlow::Node source) { source.asExpr() instanceof CredentialExpr }
24+
25+
override predicate isSink(DataFlow::Node sink) { sinkNode(sink, "logging") }
26+
}

java/ql/src/experimental/Security/CWE/CWE-532/SensitiveInfoLog.java renamed to java/ql/src/Security/CWE/CWE-532/SensitiveInfoLog.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,6 @@ public static void main(String[] args) {
1414
String password = "Pass@0rd";
1515

1616
// GOOD: user password is never written to debug log
17+
logger.debug("User password changed")
1718
}
1819
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* @name Insertion of sensitive information into log files
3+
* @description Writing sensitive information to log files can allow that
4+
* information to be leaked to an attacker more easily.
5+
* @kind path-problem
6+
* @problem.severity warning
7+
* @precision medium
8+
* @id java/sensitive-log
9+
* @tags security
10+
* external/cwe/cwe-532
11+
*/
12+
13+
import java
14+
import semmle.code.java.security.SensitiveLoggingQuery
15+
import PathGraph
16+
17+
from SensitiveLoggerConfiguration cfg, DataFlow::PathNode source, DataFlow::PathNode sink
18+
where cfg.hasFlowPath(source, sink)
19+
select sink.getNode(), source, sink, "This $@ is written to a log file.", source.getNode(),
20+
"potentially sensitive information"
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
category: newQuery
3+
---
4+
* The query "Insertion of sensitive information into log files" (`java/sensitive-logging`) has been promoted from experimental to the main query pack. This query was originally [submitted as an experimental query by @luchua-bc](https://github.com/github/codeql/pull/3090).

java/ql/src/experimental/Security/CWE/CWE-532/SensitiveInfoLog.ql

Lines changed: 0 additions & 49 deletions
This file was deleted.

java/ql/test/query-tests/security/CWE-532/SensitiveLogInfo.expected

Whitespace-only changes.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import java
2+
import TestUtilities.InlineFlowTest
3+
import semmle.code.java.security.SensitiveLoggingQuery
4+
5+
class HasFlowTest extends InlineFlowTest {
6+
override DataFlow::Configuration getTaintFlowConfig() {
7+
result instanceof SensitiveLoggerConfiguration
8+
}
9+
10+
override DataFlow::Configuration getValueFlowConfig() { none() }
11+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import org.apache.logging.log4j.Logger;
2+
3+
class Test {
4+
void test(String password) {
5+
Logger logger = null;
6+
7+
logger.info("User's password is: " + password); // $ hasTaintFlow
8+
}
9+
10+
void test2(String authToken) {
11+
Logger logger = null;
12+
13+
logger.error("Auth failed for: " + authToken); // $ hasTaintFlow
14+
}
15+
16+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
//semmle-extractor-options: --javac-args -cp ${testdir}/../../../stubs/apache-log4j-1.2.17:${testdir}/../../../stubs/apache-log4j-2.14.1:${testdir}/../../../stubs/apache-commons-logging-1.2:${testdir}/../../../stubs/jboss-logging-3.4.2:${testdir}/../../../stubs/slf4j-2.0.0:${testdir}/../../../stubs/scijava-common-2.87.1:${testdir}/../../../stubs/flogger-0.7.1:${testdir}/../../../stubs/google-android-9.0.0

0 commit comments

Comments
 (0)