|
| 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 | +class NSObject { |
| 33 | +} |
| 34 | + |
| 35 | +struct _NSRange { |
| 36 | + init(location: Int, length: Int) { } |
| 37 | +} |
| 38 | + |
| 39 | +typealias NSRange = _NSRange |
| 40 | + |
| 41 | +class NSRegularExpression : NSObject { |
| 42 | + struct Options : OptionSet { |
| 43 | + var rawValue: UInt |
| 44 | + } |
| 45 | + |
| 46 | + struct MatchingOptions : OptionSet { |
| 47 | + var rawValue: UInt |
| 48 | + } |
| 49 | + |
| 50 | + init(pattern: String, options: NSRegularExpression.Options = []) throws { } |
| 51 | + |
| 52 | + func stringByReplacingMatches(in string: String, options: NSRegularExpression.MatchingOptions = [], range: NSRange, withTemplate templ: String) -> String { return "" } |
| 53 | +} |
| 54 | + |
| 55 | +// --- tests --- |
| 56 | + |
| 57 | +func myRegexpTests(myUrl: URL) throws { |
| 58 | + let tainted = String(contentsOf: myUrl) // tainted |
| 59 | + let untainted = "abcdef" |
| 60 | + |
| 61 | + // Regex |
| 62 | + |
| 63 | + _ = "((a*)*b)" // GOOD (never used) |
| 64 | + _ = try Regex("((a*)*b)") // DUBIOUS (never used) |
| 65 | + _ = try Regex("((a*)*b)").firstMatch(in: untainted) // DUBIOUS (never used on tainted input) [FLAGGED] |
| 66 | + _ = try Regex("((a*)*b)").firstMatch(in: tainted) // BAD |
| 67 | + _ = try Regex(".*").firstMatch(in: tainted) // GOOD (safe regex) |
| 68 | + |
| 69 | + let str = "((a*)*b)" // BAD |
| 70 | + let regex = try Regex(str) |
| 71 | + _ = try regex.firstMatch(in: tainted) |
| 72 | + |
| 73 | + // NSRegularExpression |
| 74 | + |
| 75 | + _ = try? NSRegularExpression(pattern: "((a*)*b)") // DUBIOUS (never used) |
| 76 | + |
| 77 | + let nsregex1 = try? NSRegularExpression(pattern: "((a*)*b)") // DUBIOUS (never used on tainted input) [FLAGGED] |
| 78 | + _ = nsregex1?.stringByReplacingMatches(in: untainted, range: NSRange(location: 0, length: untainted.utf16.count), withTemplate: "") |
| 79 | + |
| 80 | + let nsregex2 = try? NSRegularExpression(pattern: "((a*)*b)") // BAD |
| 81 | + _ = nsregex2?.stringByReplacingMatches(in: tainted, range: NSRange(location: 0, length: tainted.utf16.count), withTemplate: "") |
| 82 | + |
| 83 | + let nsregex3 = try? NSRegularExpression(pattern: ".*") // GOOD (safe regex) |
| 84 | + _ = nsregex3?.stringByReplacingMatches(in: tainted, range: NSRange(location: 0, length: tainted.utf16.count), withTemplate: "") |
| 85 | +} |
0 commit comments