This repository was archived by the owner on Jan 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 128
Go: Add Log Injection query (CWE-117) #609
Merged
Merged
Changes from 4 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
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,2 @@ | ||
| lgtm,codescanning | ||
| * A new query "Log entries created from user input" (`go/log-injection`) has been added. The query reports user-provided data reaching calls to logging methods. |
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
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,33 @@ | ||
| /** | ||
| * Provides a taint tracking configuration for reasoning about log injection vulnerabilities. | ||
| * | ||
| * Note: for performance reasons, only import this file if `LogInjection::Configuration` is needed, | ||
| * otherwise `LogInjectionCustomizations` should be imported instead. | ||
| */ | ||
|
|
||
| import go | ||
|
|
||
| /** | ||
| * Provides a taint-tracking configuration for reasoning about | ||
| * log injection vulnerabilities. | ||
| */ | ||
| module LogInjection { | ||
| import LogInjectionCustomizations::LogInjection | ||
|
|
||
| /** | ||
| * A taint-tracking configuration for reasoning about log injection vulnerabilities. | ||
| */ | ||
| class Configuration extends TaintTracking::Configuration { | ||
| Configuration() { this = "LogInjection" } | ||
|
|
||
| override predicate isSource(DataFlow::Node source) { source instanceof Source } | ||
|
|
||
| override predicate isSink(DataFlow::Node sink) { sink instanceof Sink } | ||
|
|
||
| override predicate isSanitizer(DataFlow::Node sanitizer) { sanitizer instanceof Sanitizer } | ||
|
|
||
| override predicate isSanitizerGuard(DataFlow::BarrierGuard guard) { | ||
| guard instanceof SanitizerGuard | ||
| } | ||
| } | ||
| } |
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,42 @@ | ||
| /** | ||
| * Provides default sources, sinks, and sanitizers for reasoning about | ||
| * log injection vulnerabilities, as well as extension points for adding your own. | ||
| */ | ||
|
|
||
| import go | ||
|
|
||
| /** | ||
| * Provides extension points for customizing the data-flow tracking configuration for reasoning | ||
| * about log injection. | ||
| */ | ||
| module LogInjection { | ||
| /** | ||
| * A data flow source for log injection vulnerabilities. | ||
| */ | ||
| abstract class Source extends DataFlow::Node { } | ||
|
|
||
| /** | ||
| * A data flow sink for log injection vulnerabilities. | ||
| */ | ||
| abstract class Sink extends DataFlow::Node { } | ||
|
|
||
| /** | ||
| * A sanitizer for log injection vulnerabilities. | ||
| */ | ||
| abstract class Sanitizer extends DataFlow::Node { } | ||
|
|
||
| /** | ||
| * A sanitizer guard for log injection vulnerabilities. | ||
| */ | ||
| abstract class SanitizerGuard extends DataFlow::BarrierGuard { } | ||
|
|
||
| /** A source of untrusted data, considered as a taint source for log injection. */ | ||
| class UntrustedFlowAsSource extends Source { | ||
| UntrustedFlowAsSource() { this instanceof UntrustedFlowSource } | ||
| } | ||
|
|
||
| /** An argument to a logging mechanism. */ | ||
| class LoggerSink extends Sink { | ||
| LoggerSink() { this = any(LoggerCall log).getAMessageComponent() } | ||
| } | ||
| } |
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,12 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "log" | ||
| "net/http" | ||
| ) | ||
|
|
||
| // BAD: A user-provided value is written directly to a log. | ||
| func handler(req *http.Request) { | ||
| username := req.URL.Query()["username"][0] | ||
| log.Printf("user %s logged in.\n", 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| <!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 with characters that are interpreted | ||
| when the log output is displayed. If the log is displayed as a plain text file, then new | ||
| line characters can be used by a malicious user. If the log is displayed as HTML, then | ||
| arbitrary HTML may be include to spoof log entries.</p> | ||
| </overview> | ||
|
|
||
| <recommendation> | ||
| <p> | ||
| User input should be suitably encoded before it is logged. | ||
| </p> | ||
| <p> | ||
| If the log entries are plain text then line breaks should be removed from user input, using | ||
| <code>strings.Replace</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 using | ||
| <code>html.EscapeString</code> or similar before being logged, to prevent forgery and | ||
| other forms of HTML injection. | ||
| </p> | ||
|
|
||
| </recommendation> | ||
|
|
||
| <example> | ||
| <p> | ||
| In the following example, a user name, provided by the user, is logged using a logging framework without any sanitization. | ||
| </p> | ||
| <sample src="LogInjection.go" /> | ||
| <p> | ||
| In the next example, <code>strings.Replace</code> is used to ensure no line endings are present in the user input. | ||
| </p> | ||
| <sample src="LogInjectionGood.go" /> | ||
| </example> | ||
|
|
||
| <references> | ||
| <li>OWASP: <a href="https://www.owasp.org/index.php/Log_Injection">Log Injection</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,21 @@ | ||
| /** | ||
| * @name Log entries created from user input | ||
| * @description Building log entries from user-controlled sources is vulnerable to | ||
| * insertion of forged log entries by a malicious user. | ||
| * @kind path-problem | ||
| * @problem.severity error | ||
| * @security-severity 7.8 | ||
| * @precision high | ||
| * @id go/log-injection | ||
| * @tags security | ||
| * external/cwe/cwe-117 | ||
| */ | ||
|
|
||
| import go | ||
| import semmle.go.security.LogInjection | ||
| import DataFlow::PathGraph | ||
|
|
||
| from LogInjection::Configuration c, DataFlow::PathNode source, DataFlow::PathNode sink | ||
| where c.hasFlowPath(source, sink) | ||
| select sink, source, sink, "This log write receives unsanitized user input from $@.", | ||
| source.getNode(), "here" |
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,15 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "log" | ||
| "net/http" | ||
| "strings" | ||
| ) | ||
|
|
||
| // GOOD: The user-provided value is escaped before being written to the log. | ||
| func handlerGood(req *http.Request) { | ||
| username := req.URL.Query()["username"][0] | ||
| escapedUsername := strings.Replace(username, "\n", "", -1) | ||
| escapedUsername = strings.Replace(escapedUsername, "\r", "", -1) | ||
| log.Printf("user %s logged in.\n", escapedUsername) | ||
| } |
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
Oops, something went wrong.
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.