-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Python: promote log injection #7735
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
18 commits
Select commit
Hold shift + click to select a range
20d5454
python: move log injection out of experimental
yoff 8b5114d
python: Add standard customization setup
yoff bf1145e
python: Add change note
yoff 9d41666
python: modern change note
yoff c03f89d
Apply suggestions from code review
yoff 7511b33
python: "command" -> "log"
yoff 26befeb
python: drop precision and add severity score
yoff ecea392
python: rewrite qhelp overview
yoff 119a7e4
python: provide links for Flask
yoff c587084
python: use standard `InstanceSource` construction
yoff bec8c0d
python: update change note
yoff 448e078
python: `logging.root` is not a call
yoff f21ac04
Update python/ql/lib/semmle/python/frameworks/Stdlib.qll
yoff bd14ade
python: add apologetic comment
yoff 62598c0
Update python/ql/lib/semmle/python/security/dataflow/LogInjectionCust…
yoff 3a995ec
Update python/ql/lib/semmle/python/security/dataflow/LogInjectionCust…
yoff 62d4bb5
Python: Autoformat
RasmusWL b59ab7f
Merge branch 'main' into python/promote-log-injection
RasmusWL 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
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
35 changes: 35 additions & 0 deletions
35
python/ql/lib/semmle/python/security/dataflow/LogInjection.qll
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 a taint-tracking configuration for tracking untrusted user input used in log entries. | ||
* | ||
* Note, for performance reasons: only import this file if | ||
* `LogInjection::Configuration` is needed, otherwise | ||
* `LogInjectionCustomizations` should be imported instead. | ||
*/ | ||
|
||
import python | ||
import semmle.python.dataflow.new.DataFlow | ||
import semmle.python.dataflow.new.TaintTracking | ||
|
||
/** | ||
* Provides a taint-tracking configuration for tracking untrusted user input used in log entries. | ||
*/ | ||
module LogInjection { | ||
import LogInjectionCustomizations::LogInjection | ||
|
||
/** | ||
* A taint-tracking configuration for tracking untrusted user input used in log entries. | ||
*/ | ||
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 node) { node instanceof Sanitizer } | ||
|
||
override predicate isSanitizerGuard(DataFlow::BarrierGuard guard) { | ||
guard instanceof SanitizerGuard | ||
} | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
python/ql/lib/semmle/python/security/dataflow/LogInjectionCustomizations.qll
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,73 @@ | ||
/** | ||
* Provides default sources, sinks and sanitizers for detecting | ||
* "log injection" | ||
* vulnerabilities, as well as extension points for adding your own. | ||
*/ | ||
|
||
private import python | ||
private import semmle.python.dataflow.new.DataFlow | ||
private import semmle.python.Concepts | ||
private import semmle.python.dataflow.new.RemoteFlowSources | ||
private import semmle.python.dataflow.new.BarrierGuards | ||
|
||
/** | ||
* Provides default sources, sinks and sanitizers for detecting | ||
* "log injection" | ||
* vulnerabilities, as well as extension points for adding your own. | ||
*/ | ||
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 remote user input, considered as a flow source. | ||
*/ | ||
class RemoteFlowSourceAsSource extends Source, RemoteFlowSource { } | ||
|
||
/** | ||
* A logging operation, considered as a flow sink. | ||
*/ | ||
class LoggingAsSink extends Sink { | ||
LoggingAsSink() { this = any(Logging write).getAnInput() } | ||
} | ||
|
||
/** | ||
* A comparison with a constant string, considered as a sanitizer-guard. | ||
*/ | ||
class StringConstCompareAsSanitizerGuard extends SanitizerGuard, StringConstCompare { } | ||
|
||
/** | ||
* A call to replace line breaks, considered as a sanitizer. | ||
*/ | ||
class ReplaceLineBreaksSanitizer extends Sanitizer, DataFlow::CallCfgNode { | ||
// Note: This sanitizer is not 100% accurate, since: | ||
// - we do not check that all kinds of line breaks are replaced | ||
// - we do not check that one kind of line breaks is not replaced by another | ||
// | ||
// However, we lack a simple way to do better, and the query would likely | ||
// be too noisy without this. | ||
// | ||
// TODO: Consider rewriting using flow states. | ||
ReplaceLineBreaksSanitizer() { | ||
this.getFunction().(DataFlow::AttrRead).getAttributeName() = "replace" and | ||
this.getArg(0).asExpr().(StrConst).getText() in ["\r\n", "\n"] | ||
} | ||
} | ||
} |
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
File renamed without changes.
File renamed without changes.
4 changes: 4 additions & 0 deletions
4
python/ql/src/change-notes/2022-02-25-promote-log-injection.md
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,4 @@ | ||
--- | ||
category: newQuery | ||
--- | ||
* The query "Log Injection" (`py/log-injection`) has been promoted from experimental to the main query pack. Its results will now appear when `security-extended` is used. This query was originally [submitted as an experimental query by @haby0](https://github.com/github/codeql/pull/6182). |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't agree about this removal. I see that
API::moduleImport("logging")
is now modeled as a LoggerInstance... but that is not true, these are helper methods defined on the module leve.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there value in modelling instances specifically? Or could we just rename
instance
to something likeloggingMethodProvider
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reverted to previous modelling approach as discussed offline.