-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Java : Add Log Injection Vulnerability #5099
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
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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
49 changes: 49 additions & 0 deletions
49
java/ql/src/experimental/Security/CWE/CWE-117/LogInjection.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,49 @@ | ||
|
||
<!DOCTYPE qhelp PUBLIC | ||
"-//Semmle//qhelp//EN" | ||
"qhelp.dtd"> | ||
<qhelp> | ||
|
||
<overview> | ||
|
||
<p>If unsanitized user input is written to a log entry, a malicious user may be able to forge new log entries.</p> | ||
|
||
<p>Forgery can occur if a user provides some input creating the appearance of multiple | ||
log entries. This can include unescaped new-line characters, or HTML or other markup.</p> | ||
</overview> | ||
|
||
<recommendation> | ||
<p> | ||
User input should be suitably sanitized before it is logged. | ||
</p> | ||
<p> | ||
If the log entries are plain text then line breaks should be removed from user input, using for example | ||
<code>String replace(char oldChar, char newChar)</code> or similar. Care should also be taken that user input is clearly marked | ||
in log entries, and that a malicious user cannot cause confusion in other ways. | ||
</p> | ||
<p> | ||
For log entries that will be displayed in HTML, user input should be HTML encoded before being logged, to prevent forgery and | ||
other forms of HTML injection. | ||
</p> | ||
|
||
</recommendation> | ||
|
||
<example> | ||
<p>In the example, a username, provided by the user, is logged using <code>logger.warn</code> (from <code>org.slf4j.Logger</code>). | ||
In the first case (<code>/bad</code> endpoint), the username is logged without any sanitization. | ||
If a malicious user provides <code>Guest'%0AUser:'Admin</code> as a username parameter, | ||
the log entry will be split into two separate lines, where the first line will be <code>User:'Guest'</code> and the second one will be <code>User:'Admin'</code>. | ||
</p> | ||
<sample src="LogInjectionBad.java" /> | ||
|
||
<p> In the second case (<code>/good</code> endpoint), <code>matches()</code> is used to ensure the user input only has alphanumeric characters. | ||
If a malicious user provides `Guest'%0AUser:'Admin` as a username parameter, | ||
the log entry will not be split into two separate lines, resulting in a single line <code>User:'Guest'User:'Admin'</code>.</p> | ||
|
||
<sample src="LogInjectionGood.java" /> | ||
</example> | ||
|
||
<references> | ||
<li>OWASP: <a href="https://owasp.org/www-community/attacks/Log_Injection">Log Injection</a>.</li> | ||
</references> | ||
</qhelp> |
38 changes: 38 additions & 0 deletions
38
java/ql/src/experimental/Security/CWE/CWE-117/LogInjection.ql
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,38 @@ | ||
/** | ||
* @name Log Injection | ||
* @description Building log entries from user-controlled data is vulnerable to | ||
* insertion of forged log entries by a malicious user. | ||
* @kind path-problem | ||
* @problem.severity error | ||
* @precision high | ||
* @id java/log-injection | ||
* @tags security | ||
* external/cwe/cwe-117 | ||
*/ | ||
|
||
import java | ||
import DataFlow::PathGraph | ||
import experimental.semmle.code.java.Logging | ||
import semmle.code.java.dataflow.FlowSources | ||
|
||
/** | ||
* A taint-tracking configuration for tracking untrusted user input used in log entries. | ||
*/ | ||
private class LogInjectionConfiguration extends TaintTracking::Configuration { | ||
LogInjectionConfiguration() { this = "Log Injection" } | ||
|
||
override predicate isSource(DataFlow::Node source) { source instanceof RemoteFlowSource } | ||
|
||
override predicate isSink(DataFlow::Node sink) { | ||
sink.asExpr() = any(LoggingCall c).getALogArgument() | ||
} | ||
|
||
override predicate isSanitizer(DataFlow::Node node) { | ||
node.getType() instanceof BoxedType or node.getType() instanceof PrimitiveType | ||
} | ||
} | ||
|
||
from LogInjectionConfiguration cfg, DataFlow::PathNode source, DataFlow::PathNode sink | ||
where cfg.hasFlowPath(source, sink) | ||
select sink.getNode(), source, sink, "$@ flows to log entry.", source.getNode(), | ||
"User-provided value" |
24 changes: 24 additions & 0 deletions
24
java/ql/src/experimental/Security/CWE/CWE-117/LogInjectionBad.java
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,24 @@ | ||
package com.example.restservice; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
public class LogInjection { | ||
|
||
private final Logger log = LoggerFactory.getLogger(LogInjection.class); | ||
|
||
// /bad?username=Guest'%0AUser:'Admin | ||
@GetMapping("/bad") | ||
public String bad(@RequestParam(value = "username", defaultValue = "name") String username) { | ||
log.warn("User:'{}'", username); | ||
// The logging call above would result in multiple log entries as shown below: | ||
// User:'Guest' | ||
// User:'Admin' | ||
return username; | ||
} | ||
} | ||
|
25 changes: 25 additions & 0 deletions
25
java/ql/src/experimental/Security/CWE/CWE-117/LogInjectionGood.java
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,25 @@ | ||
package com.example.restservice; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
public class LogInjection { | ||
|
||
private final Logger log = LoggerFactory.getLogger(LogInjection.class); | ||
|
||
// /good?username=Guest'%0AUser:'Admin | ||
@GetMapping("/good") | ||
public String good(@RequestParam(value = "username", defaultValue = "name") String username) { | ||
// The regex check here, allows only alphanumeric characters to pass. | ||
// Hence, does not result in log injection | ||
if (username.matches("\w*")) { | ||
log.warn("User:'{}'", username); | ||
|
||
return username; | ||
} | ||
} | ||
} |
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
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,35 @@ | ||
/** | ||
* Provides classes and predicates for working with loggers. | ||
*/ | ||
|
||
import java | ||
|
||
/** Models a call to a logging method. */ | ||
class LoggingCall extends MethodAccess { | ||
LoggingCall() { | ||
exists(RefType t, Method m | | ||
This conversation was marked as resolved.
Show resolved
Hide resolved
|
||
t.hasQualifiedName("org.apache.log4j", "Category") or // Log4j 1 | ||
t.hasQualifiedName("org.apache.logging.log4j", ["Logger", "LogBuilder"]) or // Log4j 2 | ||
t.hasQualifiedName("org.apache.commons.logging", "Log") or | ||
// JBoss Logging (`org.jboss.logging.Logger` in some implementations like JBoss Application Server 4.0.4 did not implement `BasicLogger`) | ||
t.hasQualifiedName("org.jboss.logging", ["BasicLogger", "Logger"]) or | ||
t.hasQualifiedName("org.slf4j.spi", "LoggingEventBuilder") or | ||
t.hasQualifiedName("org.slf4j", "Logger") or | ||
t.hasQualifiedName("org.scijava.log", "Logger") or | ||
t.hasQualifiedName("com.google.common.flogger", "LoggingApi") or | ||
t.hasQualifiedName("java.lang", "System$Logger") or | ||
t.hasQualifiedName("java.util.logging", "Logger") or | ||
t.hasQualifiedName("android.util", "Log") | ||
| | ||
( | ||
m.getDeclaringType().getASourceSupertype*() = t or | ||
m.getDeclaringType().extendsOrImplements*(t) | ||
) and | ||
m.getReturnType() instanceof VoidType and | ||
This conversation was marked as resolved.
Show resolved
Hide resolved
|
||
this = m.getAReference() | ||
) | ||
} | ||
|
||
/** Returns an argument which would be logged by this call. */ | ||
Argument getALogArgument() { result = this.getArgument(_) } | ||
} |
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.