Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4f899a9

Browse files
author
Max Schaefer
committed
JavaScript: Recognize string escaping using .replace with a callback.
1 parent 5dcf55e commit 4f899a9

3 files changed

Lines changed: 18 additions & 0 deletions

File tree

javascript/ql/src/Security/CWE-116/DoubleEscaping.ql

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,14 @@ class GlobalStringReplacement extends Replacement, DataFlow::MethodCallNode {
156156
override predicate replaces(string input, string output) {
157157
input = getStringValue(pattern) and
158158
output = this.getArgument(1).getStringValue()
159+
or
160+
exists(DataFlow::FunctionNode replacer, DataFlow::PropRead pr, DataFlow::ObjectLiteralNode map |
161+
replacer = getCallback(1) and
162+
replacer.getParameter(0).flowsToExpr(pr.getPropertyNameExpr()) and
163+
pr = map.getAPropertyRead() and
164+
pr.flowsTo(replacer.getAReturn()) and
165+
map.asExpr().(ObjectExpr).getPropertyByName(input).getInit().getStringValue() = output
166+
)
159167
}
160168

161169
override DataFlow::Node getInput() {

javascript/ql/test/query-tests/Security/CWE-116/DoubleEscaping/DoubleEscaping.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@
55
| tst.js:53:10:53:33 | s.repla ... , '\\\\') | This replacement may produce '\\' characters that are double-unescaped $@. | tst.js:53:10:54:33 | s.repla ... , '\\'') | here |
66
| tst.js:60:7:60:28 | s.repla ... '%25') | This replacement may double-escape '%' characters from $@. | tst.js:59:7:59:28 | s.repla ... '%26') | here |
77
| tst.js:68:10:70:38 | s.repla ... &") | This replacement may double-escape '&' characters from $@. | tst.js:68:10:69:39 | s.repla ... apos;") | here |
8+
| tst.js:79:10:79:66 | s.repla ... &") | This replacement may double-escape '&' characters from $@. | tst.js:79:10:79:43 | s.repla ... epl[c]) | here |

javascript/ql/test/query-tests/Security/CWE-116/DoubleEscaping/tst.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,12 @@ function badEncode(s) {
6969
.replace(indirect2, "'")
7070
.replace(indirect3, "&");
7171
}
72+
73+
function badEncodeWithReplacer(s) {
74+
var repl = {
75+
'"': """,
76+
"'": "'",
77+
"&": "&"
78+
};
79+
return s.replace(/["']/g, (c) => repl[c]).replace(/&/g, "&");
80+
}

0 commit comments

Comments
 (0)