-
Notifications
You must be signed in to change notification settings - Fork 1.9k
JS: support sanitizers that sanitize individual chars in js/shell-commandconstructed-from-input #4921
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
JS: support sanitizers that sanitize individual chars in js/shell-commandconstructed-from-input #4921
Changes from all commits
2aa59a3
8b03ab0
7eab085
6423c32
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -161,16 +161,19 @@ module UnsafeShellCommandConstruction { | |
| } | ||
| } | ||
|
|
||
| /** | ||
| * Gets an unsafe shell character. | ||
| */ | ||
| private string getAShellChar() { | ||
| result = ["&", "`", "$", "|", ">", "<", "#", ";", "(", ")", "[", "]", "\n"] | ||
| } | ||
|
|
||
| /** | ||
| * A chain of replace calls that replaces all unsafe chars for shell-commands. | ||
| */ | ||
| class ChainSanitizer extends Sanitizer, IncompleteBlacklistSanitizer::StringReplaceCallSequence { | ||
| ChainSanitizer() { | ||
| forall(string char | | ||
| char = ["&", "`", "$", "|", ">", "<", "#", ";", "(", ")", "[", "]", "\n"] | ||
| | | ||
| this.getAMember().getAReplacedString() = char | ||
| ) | ||
| forall(string char | char = getAShellChar() | this.getAMember().getAReplacedString() = char) | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -208,4 +211,50 @@ module UnsafeShellCommandConstruction { | |
| e = x | ||
| } | ||
| } | ||
|
|
||
| private import semmle.javascript.dataflow.internal.AccessPaths | ||
| private import semmle.javascript.dataflow.InferredTypes | ||
|
|
||
| /** | ||
| * Holds if `instance` is an instance of the access-path `ap`, and there exists a guard | ||
| * that ensures that `instance` is not equal to `char`. | ||
| * | ||
| * For example if `ap` is `str[i]` and `char` is `<`: | ||
| * ```JavaScript | ||
| * if (str[i] !== "<" && ...) { | ||
| * var foo = str[i]; // <- `instance` | ||
| * } | ||
| * ``` | ||
| * or | ||
| * ```JavaScript | ||
| * if (!(str[i] == "<" || ...)) { | ||
| * var foo = str[i]; // <- `instance` | ||
| * } | ||
| * ``` | ||
| */ | ||
| private predicate blocksCharInAccess(AccessPath ap, string char, Expr instance) { | ||
|
Contributor
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. If the performance works out, we should try to move this
Contributor
Author
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. So generalize the sanitizer such that other queries can use it? |
||
| exists(BasicBlock bb, ConditionGuardNode guard, EqualityTest test | | ||
| test.getAnOperand().mayHaveStringValue(char) and | ||
| char = getAShellChar() and | ||
| guard.getTest() = test and | ||
| guard.dominates(bb) and | ||
| test.getAnOperand() = ap.getAnInstance() and | ||
| instance = ap.getAnInstanceIn(bb) and | ||
| guard.getOutcome() != test.getPolarity() | ||
| ) | ||
| } | ||
|
|
||
| /** | ||
| * A sanitizer for a single character, where the character cannot be an unsafe shell character. | ||
| */ | ||
| class SanitizedChar extends Sanitizer, DataFlow::ValueNode { | ||
| override PropAccess astNode; | ||
|
|
||
| SanitizedChar() { | ||
| exists(AccessPath ap | this.asExpr() = ap.getAnInstance() | | ||
| forall(string char | char = getAShellChar() | blocksCharInAccess(ap, char, astNode)) | ||
esbena marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ) and | ||
| astNode.getPropertyNameExpr().analyze().getTheType() = TTNumber() | ||
| } | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.