-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Java: CWE-532 sensitive info logging #3090
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
aschackmull
merged 12 commits into
github:master
from
luchua-bc:java-insert-sensitive-info-into-log
May 14, 2020
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
dfb42ec
Address sensitive info logging
luchua-bc d932770
Fix the issue of mixed tabs and spaces
luchua-bc 048a33e
Remove user ids from the check since they get logged a lot and are le…
luchua-bc 000d894
Include Gradle Logging
luchua-bc a256065
Update java/ql/src/experimental/CWE-532/SensitiveInfoLog.qhelp
luchua-bc a6c9c51
Update java/ql/src/experimental/CWE-532/SensitiveInfoLog.ql
luchua-bc 5c803b7
The query help builder will interpret and automatically add this refe…
luchua-bc 491b67e
Change string concatenation in the source to TaintTracking::Configura…
luchua-bc ffd442a
Fine tuning criteria
luchua-bc d9cc3c6
Add a comment for reasoning in why debug and trace are included and o…
luchua-bc 632cb8b
Simplify CredentialExpr as the AddExpr step is included by TaintTrack…
luchua-bc 7b88988
Convert to path-problem query
luchua-bc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
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 | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<!DOCTYPE qhelp PUBLIC | ||
"-//Semmle//qhelp//EN" | ||
"qhelp.dtd"> | ||
<qhelp> | ||
|
||
<overview> | ||
<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 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.</p> | ||
</overview> | ||
|
||
<recommendation> | ||
<p>Do not write secrets into the log files and enforce proper logging level control.</p> | ||
</recommendation> | ||
|
||
<example> | ||
<p>The following example shows two ways of logging sensitive information. In the 'BAD' case, | ||
the credentials are simply written to a debug log. In the 'GOOD' case, the credentials are never written to debug logs.</p> | ||
<sample src="SensitiveInfoLog.java" /> | ||
</example> | ||
|
||
<references> | ||
<li> | ||
<a href="https://cheatsheetseries.owasp.org/cheatsheets/Logging_Cheat_Sheet.html">OWASP Logging Guide</a> | ||
</li> | ||
</references> | ||
</qhelp> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/** | ||
* @id java/sensitiveinfo-in-logfile | ||
* @name Insertion of sensitive information into log files | ||
* @description Writing sensitive information to log files can give valuable guidance to an attacker or expose sensitive user information. | ||
* @kind path-problem | ||
* @tags security | ||
* external/cwe-532 | ||
*/ | ||
|
||
import java | ||
import semmle.code.java.dataflow.TaintTracking | ||
import DataFlow | ||
import PathGraph | ||
luchua-bc marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/** | ||
* Gets a regular expression for matching names of variables that indicate the value being held is a credential | ||
*/ | ||
private string getACredentialRegex() { | ||
result = "(?i).*pass(wd|word|code|phrase)(?!.*question).*" or | ||
result = "(?i)(.*username|url).*" | ||
} | ||
|
||
/** Variable keeps sensitive information judging by its name * */ | ||
class CredentialExpr extends Expr { | ||
CredentialExpr() { | ||
exists(Variable v | this = v.getAnAccess() | v.getName().regexpMatch(getACredentialRegex())) | ||
} | ||
} | ||
|
||
/** Class of popular logging utilities * */ | ||
class LoggerType extends RefType { | ||
LoggerType() { | ||
this.hasQualifiedName("org.apache.log4j", "Category") or //Log4J | ||
this.hasQualifiedName("org.slf4j", "Logger") //SLF4j and Gradle Logging | ||
} | ||
} | ||
|
||
predicate isSensitiveLoggingSink(DataFlow::Node sink) { | ||
exists(MethodAccess ma | | ||
ma.getMethod().getDeclaringType() instanceof LoggerType and | ||
(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 | ||
sink.asExpr() = ma.getAnArgument() | ||
) | ||
} | ||
|
||
class LoggerConfiguration extends DataFlow::Configuration { | ||
LoggerConfiguration() { this = "Logger Configuration" } | ||
|
||
override predicate isSource(DataFlow::Node source) { source.asExpr() instanceof CredentialExpr } | ||
|
||
override predicate isSink(DataFlow::Node sink) { isSensitiveLoggingSink(sink) } | ||
|
||
override predicate isAdditionalFlowStep(DataFlow::Node node1, DataFlow::Node node2) { | ||
TaintTracking::localTaintStep(node1, node2) | ||
} | ||
} | ||
|
||
from LoggerConfiguration cfg, DataFlow::PathNode source, DataFlow::PathNode sink | ||
where cfg.hasFlowPath(source, sink) | ||
select sink.getNode(), source, sink, "Outputting $@ to log.", source.getNode(), | ||
"sensitive information" |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.