-
Notifications
You must be signed in to change notification settings - Fork 1.8k
JS: Add query to detect bad code sanitizers #3680
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
erik-krogh
merged 6 commits into
github:js-team-sprint
from
erik-krogh:bad-code-sanitizer
Jun 12, 2020
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
373a437
add query to detect improperly sanitized code
erik-krogh 5142670
don't import AdditionalSinks, refactor sink out in new HeuristicSinks…
erik-krogh aa3482c
improve detection of duplicate results with `js/code-injection`
erik-krogh c375a0c
fix compilation and update expected output
erik-krogh adabd2d
add qldoc and customizations module
erik-krogh f0ec2eb
add missing qldoc
erik-krogh 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
37 changes: 37 additions & 0 deletions
37
javascript/ql/src/Security/CWE-094/ImproperCodeSanitization.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,37 @@ | ||
<!DOCTYPE qhelp PUBLIC | ||
"-//Semmle//qhelp//EN" | ||
"qhelp.dtd"> | ||
<qhelp> | ||
|
||
<overview> | ||
<p> | ||
Placeholder | ||
</p> | ||
</overview> | ||
|
||
<recommendation> | ||
<p> | ||
Placeholder | ||
</p> | ||
</recommendation> | ||
|
||
<example> | ||
<p> | ||
Placeholder | ||
</p> | ||
|
||
</example> | ||
|
||
<references> | ||
<li> | ||
OWASP: | ||
<a href="https://www.owasp.org/index.php/Code_Injection">Code Injection</a>. | ||
</li> | ||
<li> | ||
MDN: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects#Function_properties">Global functions</a>. | ||
</li> | ||
<li> | ||
MDN: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function">Function constructor</a>. | ||
</li> | ||
</references> | ||
</qhelp> |
66 changes: 66 additions & 0 deletions
66
javascript/ql/src/Security/CWE-094/ImproperCodeSanitization.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,66 @@ | ||
/** | ||
* @name Improper code sanitization | ||
* @description Escaping code as HTML does not provide protection against code-injection. | ||
* @kind path-problem | ||
* @problem.severity error | ||
* @precision high | ||
* @id js/bad-code-sanitization | ||
* @tags security | ||
* external/cwe/cwe-094 | ||
* external/cwe/cwe-079 | ||
* external/cwe/cwe-116 | ||
*/ | ||
|
||
import javascript | ||
import semmle.javascript.security.dataflow.ImproperCodeSanitization::ImproperCodeSanitization | ||
import DataFlow::PathGraph | ||
private import semmle.javascript.heuristics.HeuristicSinks | ||
private import semmle.javascript.security.dataflow.CodeInjectionCustomizations | ||
|
||
/** | ||
* Gets a type-tracked instance of `RemoteFlowSource` using type-tracker `t`. | ||
*/ | ||
private DataFlow::SourceNode remoteFlow(DataFlow::TypeTracker t) { | ||
t.start() and | ||
result instanceof RemoteFlowSource | ||
or | ||
exists(DataFlow::TypeTracker t2 | result = remoteFlow(t2).track(t2, t)) | ||
} | ||
|
||
/** | ||
* Gets a type-tracked reference to a `RemoteFlowSource`. | ||
*/ | ||
private DataFlow::SourceNode remoteFlow() { result = remoteFlow(DataFlow::TypeTracker::end()) } | ||
|
||
/** | ||
* Gets a type-back-tracked instance of a code-injection sink using type-tracker `t`. | ||
*/ | ||
private DataFlow::Node endsInCodeInjectionSink(DataFlow::TypeBackTracker t) { | ||
t.start() and | ||
( | ||
result instanceof CodeInjection::Sink | ||
or | ||
result instanceof HeuristicCodeInjectionSink and | ||
not result instanceof StringOps::ConcatenationRoot // the heuristic CodeInjection sink looks for string-concats, we are not interrested in those here. | ||
) | ||
or | ||
exists(DataFlow::TypeBackTracker t2 | t = t2.smallstep(result, endsInCodeInjectionSink(t2))) | ||
} | ||
|
||
/** | ||
* Gets a reference to to a data-flow node that ends in a code-injection sink. | ||
*/ | ||
private DataFlow::Node endsInCodeInjectionSink() { | ||
result = endsInCodeInjectionSink(DataFlow::TypeBackTracker::end()) | ||
} | ||
|
||
from Configuration cfg, DataFlow::PathNode source, DataFlow::PathNode sink | ||
where | ||
cfg.hasFlowPath(source, sink) and | ||
// Basic detection of duplicate results with `js/code-injection`. | ||
not ( | ||
sink.getNode().(StringOps::ConcatenationLeaf).getRoot() = endsInCodeInjectionSink() and | ||
remoteFlow().flowsTo(source.getNode().(DataFlow::InvokeNode).getAnArgument()) | ||
) | ||
select sink.getNode(), source, sink, "$@ flows to here and is used to construct code.", | ||
source.getNode(), "Improperly sanitized value" |
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
javascript/ql/src/semmle/javascript/heuristics/HeuristicSinks.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 heuristically recognized sinks for security queries. | ||
*/ | ||
|
||
import javascript | ||
private import SyntacticHeuristics | ||
|
||
/** | ||
* A heuristic sink for data flow in a security query. | ||
*/ | ||
abstract class HeuristicSink extends DataFlow::Node { } | ||
|
||
/** | ||
* A heuristically recognized sink for `js/code-injection` vulnerabilities. | ||
*/ | ||
class HeuristicCodeInjectionSink extends HeuristicSink { | ||
HeuristicCodeInjectionSink() { | ||
isAssignedTo(this, "$where") | ||
or | ||
isAssignedToOrConcatenatedWith(this, "(?i)(command|cmd|exec|code|script|program)") | ||
or | ||
isArgTo(this, "(?i)(eval|run)") // "exec" clashes too often with `RegExp.prototype.exec` | ||
or | ||
exists(string srcPattern | | ||
// function/lambda syntax anywhere | ||
srcPattern = "(?s).*function\\s*\\(.*\\).*" or | ||
srcPattern = "(?s).*(\\(.*\\)|[A-Za-z_]+)\\s?=>.*" | ||
| | ||
isConcatenatedWithString(this, srcPattern) | ||
) | ||
or | ||
// dynamic property name | ||
isConcatenatedWithStrings("(?is)[a-z]+\\[", this, "(?s)\\].*") | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
javascript/ql/src/semmle/javascript/security/dataflow/ImproperCodeSanitization.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,30 @@ | ||
/** | ||
* Provides a taint-tracking configuration for reasoning about improper code | ||
* sanitization. | ||
* | ||
* Note, for performance reasons: only import this file if | ||
* `ImproperCodeSanitization::Configuration` is needed, otherwise | ||
* `ImproperCodeSanitizationCustomizations` should be imported instead. | ||
*/ | ||
|
||
import javascript | ||
|
||
/** | ||
* Classes and predicates for reasoning about improper code sanitization. | ||
*/ | ||
module ImproperCodeSanitization { | ||
import ImproperCodeSanitizationCustomizations::ImproperCodeSanitization | ||
|
||
/** | ||
* A taint-tracking configuration for reasoning about improper code sanitization vulnerabilities. | ||
*/ | ||
class Configuration extends TaintTracking::Configuration { | ||
Configuration() { this = "ImproperCodeSanitization" } | ||
|
||
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 } | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
...ipt/ql/src/semmle/javascript/security/dataflow/ImproperCodeSanitizationCustomizations.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,68 @@ | ||
/** | ||
* Provides default sources, sinks and sanitizers for reasoning about | ||
* improper code sanitization, as well as extension points for | ||
* adding your own. | ||
*/ | ||
|
||
import javascript | ||
|
||
/** | ||
* Classes and predicates for reasoning about improper code sanitization. | ||
*/ | ||
module ImproperCodeSanitization { | ||
/** | ||
* A data flow source for improper code sanitization. | ||
*/ | ||
abstract class Source extends DataFlow::Node { } | ||
|
||
/** | ||
* A data flow sink for improper code sanitization. | ||
*/ | ||
abstract class Sink extends DataFlow::Node { } | ||
|
||
/** | ||
* A sanitizer for improper code sanitization. | ||
*/ | ||
abstract class Sanitizer extends DataFlow::Node { } | ||
|
||
/** | ||
* A call to a HTML sanitizer seen as a source for improper code sanitization | ||
*/ | ||
class HtmlSanitizerCallAsSource extends Source { | ||
HtmlSanitizerCallAsSource() { this instanceof HtmlSanitizerCall } | ||
} | ||
|
||
/** | ||
* A call to `JSON.stringify()` seen as a source for improper code sanitization | ||
*/ | ||
class JSONStringifyAsSource extends Source { | ||
JSONStringifyAsSource() { this = DataFlow::globalVarRef("JSON").getAMemberCall("stringify") } | ||
} | ||
|
||
/** | ||
* A leaf in a string-concatenation, where the string-concatenation constructs code that looks like a function. | ||
*/ | ||
class FunctionStringConstruction extends Sink, StringOps::ConcatenationLeaf { | ||
FunctionStringConstruction() { | ||
exists(StringOps::ConcatenationRoot root, int i | | ||
root.getOperand(i) = this and | ||
not exists(this.getStringValue()) | ||
| | ||
exists(StringOps::ConcatenationLeaf functionLeaf | | ||
functionLeaf = root.getOperand(any(int j | j < i)) | ||
| | ||
functionLeaf | ||
.getStringValue() | ||
.regexpMatch([".*function( )?([a-zA-Z0-9]+)?( )?\\(.*", ".*eval\\(.*", | ||
".*new Function\\(.*", "(^|.*[^a-zA-Z0-9])\\(.*\\)( )?=>.*"]) | ||
) | ||
) | ||
} | ||
} | ||
|
||
/** | ||
* A call to `String.prototype.replace` seen as a sanitizer for improper code sanitization. | ||
* All calls to replace that happens after the initial improper sanitization is seen as a sanitizer. | ||
*/ | ||
class StringReplaceCallAsSanitizer extends Sanitizer, StringReplaceCall { } | ||
} |
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.
See
codeql/javascript/ql/src/semmle/javascript/Regexp.qll
Lines 893 to 898 in cafbe14