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

Skip to content

Commit 5349e0f

Browse files
author
Max Schaefer
committed
JavaScript: Recognise wrapped chains of replacements.
1 parent 02d16b1 commit 5349e0f

3 files changed

Lines changed: 18 additions & 2 deletions

File tree

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,13 @@ abstract class Replacement extends DataFlow::Node {
115115
result.getOutput() = getASimplePredecessor*(getInput())
116116
}
117117

118+
/**
119+
* Gets the next replacement in this chain of replacements.
120+
*/
121+
Replacement getNextReplacement() {
122+
this = result.getPreviousReplacement()
123+
}
124+
118125
/**
119126
* Gets an earlier replacement in this chain of replacements that
120127
* performs an escaping.
@@ -231,8 +238,8 @@ class WrappedReplacement extends Replacement, DataFlow::CallNode {
231238

232239
WrappedReplacement() {
233240
exists(DataFlow::FunctionNode wrapped | wrapped.getFunction() = getACallee() |
234-
wrapped.getParameter(i).flowsTo(inner.getInput()) and
235-
inner.getOutput().flowsTo(wrapped.getAReturn())
241+
wrapped.getParameter(i).flowsTo(inner.getPreviousReplacement*().getInput()) and
242+
inner.getNextReplacement*().getOutput().flowsTo(wrapped.getAReturn())
236243
)
237244
}
238245

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
@@ -9,3 +9,4 @@
99
| tst.js:86:10:86:22 | JSON.parse(s) | This replacement may produce '\\' characters that are double-unescaped $@. | tst.js:86:10:86:47 | JSON.pa ... g, "<") | here |
1010
| tst.js:99:10:99:66 | s.repla ... &amp;") | This replacement may double-escape '&' characters from $@. | tst.js:99:10:99:43 | s.repla ... epl[c]) | here |
1111
| tst.js:107:10:107:53 | encodeD ... &amp;") | This replacement may double-escape '&' characters from $@. | tst.js:107:10:107:30 | encodeD ... otes(s) | here |
12+
| tst.js:115:10:115:47 | encodeQ ... &amp;") | This replacement may double-escape '&' characters from $@. | tst.js:115:10:115:24 | encodeQuotes(s) | here |

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,3 +106,11 @@ function encodeDoubleQuotes(s) {
106106
function badWrappedEncode(s) {
107107
return encodeDoubleQuotes(s).replace(/&/g, "&amp;");
108108
}
109+
110+
function encodeQuotes(s) {
111+
return s.replace(/"/g, "&quot;").replace(/'/g, "&apos;");
112+
}
113+
114+
function badWrappedEncode2(s) {
115+
return encodeQuotes(s).replace(/&/g, "&amp;");
116+
}

0 commit comments

Comments
 (0)