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 125
Add missing string replacement sanitizers to log-injection and string-break #731
Open
owen-mc
wants to merge
5
commits into
github:main
Choose a base branch
from
owen-mc:log-injection-sanitizer-newreplacer-replace
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
81ccc40
Make `strings.Replacer.Replace` a sanitizer for log injection
owen-mc 3ab9bd3
Make `strings.Replacer.WriteString` a sanitizer for log injection
owen-mc 2a0fdd1
Add extra replace sanitizers to StringBreak
owen-mc 926136d
Add change note
owen-mc af3d6b0
Address review comments
owen-mc 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -103,4 +103,67 @@ module StringBreak { | |
|
||
override Quote getQuote() { result = quote } | ||
} | ||
|
||
class StringsNewReplacerCall extends DataFlow::CallNode { | ||
StringsNewReplacerCall() { this.getTarget().hasQualifiedName("strings", "NewReplacer") } | ||
|
||
DataFlow::Node getAReplacedArgument() { | ||
exists(int m, int n | m = 2 * n and n = m / 2 and result = getArgument(m)) | ||
} | ||
} | ||
|
||
class StringsNewReplacerConfiguration extends DataFlow2::Configuration { | ||
StringsNewReplacerConfiguration() { this = "StringsNewReplacerConfiguration" } | ||
|
||
override predicate isSource(DataFlow::Node source) { source instanceof StringsNewReplacerCall } | ||
|
||
override predicate isSink(DataFlow::Node sink) { | ||
exists(DataFlow::MethodCallNode call | | ||
sink = call.getReceiver() and | ||
call.getTarget().hasQualifiedName("strings", "Replacer", ["Replace", "WriteString"]) | ||
) | ||
} | ||
} | ||
|
||
/** | ||
* A call to `strings.Replacer.Replace`, considered as a sanitizer for unsafe | ||
* quoting. | ||
*/ | ||
class ReplacerReplaceSanitizer extends DataFlow::MethodCallNode, Sanitizer { | ||
Quote quote; | ||
|
||
ReplacerReplaceSanitizer() { | ||
exists(StringsNewReplacerConfiguration config, DataFlow::Node source, DataFlow::Node sink | | ||
config.hasFlow(source, sink) and | ||
this.getTarget().hasQualifiedName("strings", "Replacer", "Replace") and | ||
sink = this.getReceiver() and | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use |
||
quote = source.(StringsNewReplacerCall).getAReplacedArgument().getStringValue() | ||
) | ||
} | ||
|
||
override Quote getQuote() { result = quote } | ||
} | ||
|
||
/** | ||
* A call to `strings.Replacer.WriteString`, considered as a sanitizer for | ||
* unsafe quoting. | ||
*/ | ||
class ReplacerWriteStringSanitizer extends Sanitizer { | ||
Quote quote; | ||
|
||
ReplacerWriteStringSanitizer() { | ||
exists( | ||
StringsNewReplacerConfiguration config, DataFlow::Node source, DataFlow::Node sink, | ||
DataFlow::MethodCallNode call | ||
| | ||
config.hasFlow(source, sink) and | ||
call.getTarget().hasQualifiedName("strings", "Replacer", "WriteString") and | ||
sink = call.getReceiver() and | ||
this = call.getArgument(1) and | ||
quote = source.(StringsNewReplacerCall).getAReplacedArgument().getStringValue() | ||
) | ||
} | ||
|
||
override Quote getQuote() { result = quote } | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
ql/src/change-notes/2022-05-04-add-extra-string-replace-sanitizers.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: minorAnalysis | ||
--- | ||
* The functions `strings.Replacer.Replace` and `strings.Replacer.WriteString` have been added as sanitizers for the queries "Log entries created from user input" and "Potentially unsafe quoting". |
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
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.