Skip to content

Commit aab0f6a

Browse files
committed
JS: Now BadHtmlSanitizers new RegExp with unknown flags is also flagged.
1 parent 60e6ac2 commit aab0f6a

File tree

4 files changed

+13
-2
lines changed

4 files changed

+13
-2
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,14 @@ class StringReplaceCall extends DataFlow::MethodCallNode {
119119
RegExp::isGlobal(this.getRegExp().getFlags()) or this.getMethodName() = "replaceAll"
120120
}
121121

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

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)