Skip to content

Commit 18c7b18

Browse files
committed
JS: Now BadHtmlSanitizers new RegExp with unknown flags is also flagged.
1 parent 89f3b6f commit 18c7b18

4 files changed

Lines changed: 13 additions & 2 deletions

File tree

javascript/ql/lib/semmle/javascript/StandardLibrary.qll

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,14 @@ class StringReplaceCall extends DataFlow::MethodCallNode {
117117
*/
118118
predicate isGlobal() { this.getRegExp().isGlobal() or this.getMethodName() = "replaceAll" }
119119

120+
/**
121+
* Holds if this is a global replacement, that is, the first argument is a regular expression
122+
* with the `g` flag, or this is a call to `.replaceAll()`.
123+
*/
124+
predicate maybeGlobal() {
125+
RegExp::maybeGlobal(this.getRegExp().tryGetFlags()) or this.getMethodName() = "replaceAll"
126+
}
127+
120128
/**
121129
* Holds if this call to `replace` replaces `old` with `new`.
122130
*/

javascript/ql/lib/semmle/javascript/security/IncompleteBlacklistSanitizer.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ private StringReplaceCall getAStringReplaceMethodCall(StringReplaceCall n) {
7474
module HtmlSanitization {
7575
private predicate fixedGlobalReplacement(StringReplaceCallSequence chain) {
7676
forall(StringReplaceCall member | member = chain.getAMember() |
77-
member.isGlobal() and member.getArgument(0) instanceof DataFlow::RegExpCreationNode
77+
member.maybeGlobal() and member.getArgument(0) instanceof DataFlow::RegExpCreationNode
7878
)
7979
}
8080

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,6 @@
6868
| tst.js:333:2:333:40 | s().rep ... g"),'') | This HTML sanitizer does not sanitize ampersands |
6969
| tst.js:333:2:333:40 | s().rep ... g"),'') | This HTML sanitizer does not sanitize double quotes |
7070
| tst.js:333:2:333:40 | s().rep ... g"),'') | This HTML sanitizer does not sanitize single quotes |
71+
| tst.js:337:2:337:46 | s().rep ... ()),'') | This HTML sanitizer does not sanitize ampersands |
72+
| tst.js:337:2:337:46 | s().rep ... ()),'') | This HTML sanitizer does not sanitize double quotes |
73+
| tst.js:337:2:337:46 | s().rep ... ()),'') | This HTML sanitizer does not sanitize single quotes |

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,5 +334,5 @@ function typicalBadHtmlSanitizers(s) {
334334
}
335335

336336
function typicalBadHtmlSanitizers(s) {
337-
s().replace(new RegExp("[<>]", unknown()),''); // NOT OK -- should be flagged, because it is st ill a bad sanitizer
337+
s().replace(new RegExp("[<>]", unknown()),''); // NOT OK
338338
}

0 commit comments

Comments
 (0)