Skip to content

Commit 1ee9957

Browse files
authored
Merge pull request github#9807 from erik-krogh/endFilter
JS: recognize "-->" as a bad tag filter
2 parents 9914824 + 38ca68f commit 1ee9957

File tree

3 files changed

+24
-4
lines changed

3 files changed

+24
-4
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@
1515
| tst.js:20:3:20:57 | (<[a-z\\/!$]("[^"]*"\|'[^']*'\|[^'">])*>\|<!(--.*?--\\s*)+>) | Comments ending with --> are matched differently from comments ending with --!>. The first is matched with capture group 3 and comments ending with --!> are matched with capture group 1. |
1616
| tst.js:21:6:21:249 | <(?:(?:!--([\\w\\W]*?)-->)\|(?:!\\[CDATA\\[([\\w\\W]*?)\\]\\]>)\|(?:!DOCTYPE([\\w\\W]*?)>)\|(?:\\?([^\\s\\/<>]+) ?([\\w\\W]*?)[?/]>)\|(?:\\/([A-Za-z][A-Za-z0-9\\-_\\:\\.]*)>)\|(?:([A-Za-z][A-Za-z0-9\\-_\\:\\.]*)((?:\\s+[^"'>]+(?:(?:"[^"]*")\|(?:'[^']*')\|[^>]*))*\|\\/\|\\s+)>)) | This regular expression only parses --> (capture group 1) and not --!> as an HTML comment end tag. |
1717
| tst.js:22:6:22:33 | <!--([\\w\\W]*?)-->\|<([^>]*?)> | Comments ending with --> are matched differently from comments ending with --!>. The first is matched with capture group 1 and comments ending with --!> are matched with capture group 2. |
18+
| tst.js:31:6:31:8 | --> | This regular expression only parses --> and not --!> as a HTML comment end tag. |

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,10 @@ doFilters(filters)
2626

2727
var strip = '<script([^>]*)>([\\S\\s]*?)<\/script([^>]*)>'; // OK - it's used with the ignorecase flag
2828
new RegExp(strip, 'gi');
29+
30+
var moreFilters = [
31+
/-->/g, // NOT OK - doesn't match --!>
32+
/^>|^->|<!--|-->|--!>|<!-$/g, // OK
33+
];
34+
35+
doFilters(moreFilters);

shared/regex/codeql/regex/nfa/BadTagFilterQuery.qll

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,15 @@ module Make<RegexTreeViewSig TreeImpl> {
2323
RootTerm root, string str, boolean ignorePrefix, boolean testWithGroups
2424
) {
2525
// the regexp must mention "<" and ">" explicitly.
26-
forall(string angleBracket | angleBracket = ["<", ">"] |
27-
any(RegExpConstant term | term.getValue().matches("%" + angleBracket + "%")).getRootTerm() =
28-
root
26+
(
27+
forall(string angleBracket | angleBracket = ["<", ">"] |
28+
any(RegExpConstant term | term.getValue().matches("%" + angleBracket + "%")).getRootTerm() =
29+
root
30+
)
31+
or
32+
// or contain "-->" / "--!>" / "<--" / "<!--"
33+
root =
34+
any(RegExpConstant term | term.getValue() = ["-->", "--!>", "<--", "<!--"]).getRootTerm()
2935
) and
3036
ignorePrefix = true and
3137
(
@@ -40,7 +46,7 @@ module Make<RegexTreeViewSig TreeImpl> {
4046
"<script src='foo'></script>", "<SCRIPT>foo</SCRIPT>", "<script\tsrc=\"foo\"/>",
4147
"<script\tsrc='foo'></script>", "<sCrIpT>foo</ScRiPt>",
4248
"<script src=\"foo\">foo</script >", "<script src=\"foo\">foo</script foo=\"bar\">",
43-
"<script src=\"foo\">foo</script\t\n bar>"
49+
"<script src=\"foo\">foo</script\t\n bar>", "-->", "--!>", "--"
4450
] and
4551
testWithGroups = false
4652
)
@@ -107,6 +113,12 @@ module Make<RegexTreeViewSig TreeImpl> {
107113
") and not --!> as an HTML comment end tag."
108114
)
109115
or
116+
// CVE-2021-4231 - matching only "-->" but not "--!>".
117+
regexp.matches("-->") and
118+
not regexp.matches("--!>") and
119+
not regexp.matches("--") and
120+
msg = "This regular expression only parses --> and not --!> as a HTML comment end tag."
121+
or
110122
regexp.matches("<!-- foo -->") and
111123
not regexp.matches("<!-- foo\n -->") and
112124
not regexp.matches("<!- foo ->") and

0 commit comments

Comments
 (0)