Skip to content

Commit 63ab478

Browse files
committed
Swift: Flag parse failures in the test.
1 parent 44eb7bf commit 63ab478

2 files changed

Lines changed: 19 additions & 9 deletions

File tree

swift/ql/test/library-tests/regex/redos_variants.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ func myRegexpVariantsTests(myUrl: URL) throws {
130130
_ = try Regex("(.|\\n)*!").firstMatch(in: tainted)
131131

132132
// NOT GOOD; attack: "\n".repeat(100) + "." TODO: investigate, we should be getting this one.
133-
_ = try Regex("(?s)(.|\\n)*!").firstMatch(in: tainted) // $ MISSING: redos-vulnerable=
133+
_ = try Regex("(?s)(.|\\n)*!").firstMatch(in: tainted) // $ hasParseFailure MISSING: redos-vulnerable=
134134

135135
// GOOD
136136
_ = try Regex("([\\w.]+)*").firstMatch(in: tainted)
@@ -410,10 +410,10 @@ func myRegexpVariantsTests(myUrl: URL) throws {
410410
_ = try Regex("X(\\x61|b)+Y").firstMatch(in: tainted)
411411

412412
// NOT GOOD TODO: we should get this one
413-
_ = try Regex("X(\\x{061}|a)*Y").firstMatch(in: tainted) // $ MISSING: redos-vulnerable=
413+
_ = try Regex("X(\\x{061}|a)*Y").firstMatch(in: tainted) // $ hasParseFailure= MISSING: redos-vulnerable=
414414

415415
// GOOD
416-
_ = try Regex("X(\\x{061}|b)+Y").firstMatch(in: tainted)
416+
_ = try Regex("X(\\x{061}|b)+Y").firstMatch(in: tainted) // $ hasParseFailure
417417

418418
// NOT GOOD
419419
_ = try Regex("X(\\p{Digit}|7)*Y").firstMatch(in: tainted) // $ redos-vulnerable=
@@ -452,13 +452,13 @@ func myRegexpVariantsTests(myUrl: URL) throws {
452452
_ = try Regex("\\b(\\d|0)*x").firstMatch(in: tainted) // $ redos-vulnerable=
453453

454454
// GOOD - possessive quantifiers don't backtrack
455-
_ = try Regex("(a*+)*+b").firstMatch(in: tainted)
456-
_ = try Regex("(a*)*+b").firstMatch(in: tainted)
457-
_ = try Regex("(a*+)*b").firstMatch(in: tainted)
455+
_ = try Regex("(a*+)*+b").firstMatch(in: tainted) // $ hasParseFailure
456+
_ = try Regex("(a*)*+b").firstMatch(in: tainted) // $ hasParseFailure
457+
_ = try Regex("(a*+)*b").firstMatch(in: tainted) // $ hasParseFailure
458458

459459
// BAD
460460
_ = try Regex("(a*)*b").firstMatch(in: tainted) // $ redos-vulnerable=
461461

462462
// BAD - but not detected due to the way possessive quantifiers are approximated
463-
_ = try Regex("((aa|a*+)b)*c").firstMatch(in: tainted) // $ MISSING: redos-vulnerable=
463+
_ = try Regex("((aa|a*+)b)*c").firstMatch(in: tainted) // $ hasParseFailure MISSING: redos-vulnerable=
464464
}

swift/ql/test/library-tests/regex/regex.ql

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import swift
22
import codeql.swift.regex.Regex
3+
private import codeql.swift.regex.internal.ParseRegex
34
private import codeql.swift.regex.RegexTreeView::RegexTreeView as TreeView
45
import codeql.regex.nfa.ExponentialBackTracking::Make<TreeView>
56
import TestUtilities.InlineExpectationsTest
@@ -8,7 +9,7 @@ bindingset[s]
89
string quote(string s) { if s.matches("% %") then result = "\"" + s + "\"" else result = s }
910

1011
module RegexTest implements TestSig {
11-
string getARelevantTag() { result = ["regex", "input", "redos-vulnerable"] }
12+
string getARelevantTag() { result = ["regex", "input", "redos-vulnerable", "hasParseFailure"] }
1213

1314
predicate hasActualResult(Location location, string element, string tag, string value) {
1415
exists(TreeView::RegExpTerm t, string pump, State s, string prefixMsg |
@@ -18,6 +19,15 @@ module RegexTest implements TestSig {
1819
tag = "redos-vulnerable" and
1920
value = ""
2021
)
22+
or
23+
exists(RegexEval eval, RegExp regex |
24+
eval.getARegex() = regex and
25+
regex.failedToParse(_) and
26+
location = eval.getLocation() and
27+
element = eval.toString() and
28+
tag = "hasParseFailure" and
29+
value = ""
30+
)
2131
}
2232

2333
predicate hasOptionalResult(Location location, string element, string tag, string value) {
@@ -29,7 +39,7 @@ module RegexTest implements TestSig {
2939
value = quote(input.toString())
3040
)
3141
or
32-
exists(RegexEval eval, Expr regex |
42+
exists(RegexEval eval, RegExp regex |
3343
eval.getARegex() = regex and
3444
location = eval.getLocation() and
3545
element = eval.toString() and

0 commit comments

Comments
 (0)