mirror of
https://github.com/github/codeql.git
synced 2026-05-02 12:15:17 +02:00
JS: whitelist delimiter unwrapping for js/incomplete-sanitization
This commit is contained in:
@@ -101,6 +101,34 @@ predicate allBackslashesEscaped(DataFlow::Node nd) {
|
||||
allBackslashesEscaped(nd.getAPredecessor())
|
||||
}
|
||||
|
||||
/**
|
||||
* Holds if `repl` looks like a call to "String.prototype.replace" that deliberately removes the first occurrence of `str`.
|
||||
*/
|
||||
predicate removesFirstOccurence(DataFlow::MethodCallNode repl, string str) {
|
||||
repl.getMethodName() = "replace" and
|
||||
repl.getArgument(0).getStringValue() = str and
|
||||
repl.getArgument(1).getStringValue() = ""
|
||||
}
|
||||
|
||||
/**
|
||||
* Holds if `leftUnwrap` and `rightUnwrap` unwraps a string from a pair of surrounding delimiters.
|
||||
*/
|
||||
predicate isDelimiterUnwrapper(
|
||||
DataFlow::MethodCallNode leftUnwrap, DataFlow::MethodCallNode rightUnwrap
|
||||
) {
|
||||
exists(string left, string right |
|
||||
left = "[" and right = "]"
|
||||
or
|
||||
left = "{" and right = "}"
|
||||
or
|
||||
left = "(" and right = ")"
|
||||
|
|
||||
removesFirstOccurence(leftUnwrap, left) and
|
||||
removesFirstOccurence(rightUnwrap, right) and
|
||||
leftUnwrap.getAMethodCall() = rightUnwrap
|
||||
)
|
||||
}
|
||||
|
||||
from MethodCallExpr repl, Expr old, string msg
|
||||
where
|
||||
repl.getMethodName() = "replace" and
|
||||
@@ -122,7 +150,10 @@ where
|
||||
)
|
||||
) and
|
||||
// don't flag replace operations in a loop
|
||||
not DataFlow::valueNode(repl.getReceiver()) = DataFlow::valueNode(repl).getASuccessor+()
|
||||
not DataFlow::valueNode(repl.getReceiver()) = DataFlow::valueNode(repl).getASuccessor+() and
|
||||
// dont' flag unwrapper
|
||||
not isDelimiterUnwrapper(repl.flow(), _) and
|
||||
not isDelimiterUnwrapper(_, repl.flow())
|
||||
or
|
||||
exists(RegExpLiteral rel |
|
||||
isBackslashEscape(repl, rel) and
|
||||
|
||||
@@ -15,18 +15,12 @@
|
||||
| tst.js:61:10:61:18 | s.replace | This replaces only the first occurrence of "'" + "". |
|
||||
| tst.js:65:10:65:18 | s.replace | This replaces only the first occurrence of "'". |
|
||||
| tst.js:69:10:69:18 | s.replace | This replaces only the first occurrence of "'" + "". |
|
||||
| tst.js:130:2:130:10 | s.replace | This replaces only the first occurrence of '['. |
|
||||
| tst.js:130:2:130:27 | s.repla ... replace | This replaces only the first occurrence of ']'. |
|
||||
| tst.js:132:2:132:10 | s.replace | This replaces only the first occurrence of '{'. |
|
||||
| tst.js:132:2:132:27 | s.repla ... replace | This replaces only the first occurrence of '}'. |
|
||||
| tst.js:133:2:133:10 | s.replace | This replaces only the first occurrence of '<'. |
|
||||
| tst.js:133:2:133:27 | s.repla ... replace | This replaces only the first occurrence of '>'. |
|
||||
| tst.js:135:2:135:10 | s.replace | This replaces only the first occurrence of '['. |
|
||||
| tst.js:135:2:135:30 | s.repla ... replace | This replaces only the first occurrence of ']'. |
|
||||
| tst.js:136:2:136:10 | s.replace | This replaces only the first occurrence of '{'. |
|
||||
| tst.js:136:2:136:30 | s.repla ... replace | This replaces only the first occurrence of '}'. |
|
||||
| tst.js:138:6:138:14 | s.replace | This replaces only the first occurrence of '['. |
|
||||
| tst.js:139:6:139:14 | s.replace | This replaces only the first occurrence of ']'. |
|
||||
| tst.js:140:2:140:10 | s.replace | This replaces only the first occurrence of /{/. |
|
||||
| tst.js:140:2:140:27 | s.repla ... replace | This replaces only the first occurrence of /}/. |
|
||||
| tst.js:141:2:141:10 | s.replace | This replaces only the first occurrence of ']'. |
|
||||
|
||||
Reference in New Issue
Block a user