Skip to content

Commit 9601134

Browse files
committed
Swift: Create library test cases for REDOS vulnerable regexs.
1 parent f7860a3 commit 9601134

1 file changed

Lines changed: 50 additions & 0 deletions

File tree

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
2+
// --- stubs ---
3+
4+
struct URL {
5+
init?(string: String) {}
6+
}
7+
8+
struct AnyRegexOutput {
9+
}
10+
11+
protocol RegexComponent {
12+
}
13+
14+
struct Regex<Output> : RegexComponent {
15+
struct Match {
16+
}
17+
18+
init(_ pattern: String) throws where Output == AnyRegexOutput { }
19+
20+
func firstMatch(in string: String) throws -> Regex<Output>.Match? { return nil}
21+
22+
typealias RegexOutput = Output
23+
}
24+
25+
extension String {
26+
init(contentsOf: URL) {
27+
let data = ""
28+
self.init(data)
29+
}
30+
}
31+
32+
// --- tests ---
33+
34+
func myRegexpVariantsTests(myUrl: URL) throws {
35+
let tainted = String(contentsOf: myUrl) // tainted
36+
let untainted = "abcdef"
37+
38+
_ = try Regex(".*").firstMatch(in: tainted) // $ regex="call to Regex<AnyRegexOutput>.init(_:)" input=tainted
39+
40+
_ = try Regex("a*b").firstMatch(in: tainted) // $ regex="call to Regex<AnyRegexOutput>.init(_:)" input=tainted
41+
_ = try Regex("(a*)b").firstMatch(in: tainted) // $ regex="call to Regex<AnyRegexOutput>.init(_:)" input=tainted
42+
_ = try Regex("(a)*b").firstMatch(in: tainted) // $ regex="call to Regex<AnyRegexOutput>.init(_:)" input=tainted
43+
_ = try Regex("(a*)*b").firstMatch(in: tainted) // $ regex="call to Regex<AnyRegexOutput>.init(_:)" input=tainted MISSING: redos-vulnerable=
44+
_ = try Regex("((a*)*b)").firstMatch(in: tainted) // $ regex="call to Regex<AnyRegexOutput>.init(_:)" input=tainted MISSING: redos-vulnerable=
45+
46+
_ = try Regex("(a|aa?)b").firstMatch(in: tainted) // $ regex="call to Regex<AnyRegexOutput>.init(_:)" input=tainted
47+
_ = try Regex("(a|aa?)*b").firstMatch(in: tainted) // $ regex="call to Regex<AnyRegexOutput>.init(_:)" input=tainted MISSING: redos-vulnerable=
48+
49+
// TODO: test more variant expressions.
50+
}

0 commit comments

Comments
 (0)