Skip to content

Commit c09df2e

Browse files
committed
Swift: Add test cases for the isLineAnchoredHostnameRegExp query case.
1 parent 8f115bf commit c09df2e

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

swift/ql/test/query-tests/Security/CWE-020/UnanchoredUrlRegex.swift

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ typealias NSRange = _NSRange
1212

1313
func NSMakeRange(_ loc: Int, _ len: Int) -> NSRange { return NSRange(location: loc, length: len) }
1414

15-
class NSTextCheckingResult : NSObject {
16-
}
15+
class NSTextCheckingResult : NSObject { }
1716

1817
class NSRegularExpression : NSObject {
1918
struct Options : OptionSet {
2019
var rawValue: UInt
2120

2221
static var caseInsensitive: NSRegularExpression.Options { get { return Options(rawValue: 1) } }
22+
static var anchorsMatchLines: NSRegularExpression.Options { get { return Options(rawValue: 2) } }
2323
}
2424

2525
struct MatchingOptions : OptionSet {
@@ -99,4 +99,24 @@ func tests(url: String, secure: Bool) throws {
9999

100100
_ = try NSRegularExpression(pattern: #"\.com|\.org"#).matches(in: input, range: inputRange) // OK, has no domain name
101101
_ = try NSRegularExpression(pattern: #"example\.com|whatever"#).matches(in: input, range: inputRange) // OK, the other disjunction doesn't match a hostname [FALSE POSITIVE]
102+
103+
// tests for the `isLineAnchoredHostnameRegExp` case
104+
105+
let attackUrl1 = "evil.com/blabla?\ngood.com"
106+
let attackUrl1Range = NSMakeRange(0, attackUrl1.utf16.count)
107+
_ = try NSRegularExpression(pattern: "^good\\.com$").matches(in: attackUrl1, range: attackUrl1Range) // OK
108+
_ = try NSRegularExpression(pattern: "^good\\.com$", options: .anchorsMatchLines).matches(in: attackUrl1, range: attackUrl1Range) // BAD [NOT DETECTED]: with the .anchorsMatchLines option this matches the attack URL
109+
_ = try NSRegularExpression(pattern: "(?i)^good\\.com$").matches(in: attackUrl1, range: attackUrl1Range) // OK
110+
_ = try NSRegularExpression(pattern: "(?i)^good\\.com$", options: .anchorsMatchLines).matches(in: attackUrl1, range: attackUrl1Range) // BAD [NOT DETECTED]: with the .anchorsMatchLines option this matches the attack URL
111+
_ = try NSRegularExpression(pattern: "^good\\.com$|^another\\.com$").matches(in: attackUrl1, range: attackUrl1Range) // OK
112+
_ = try NSRegularExpression(pattern: "^good\\.com$|^another\\.com$", options: .anchorsMatchLines).matches(in: attackUrl1, range: attackUrl1Range) // BAD [NOT DETECTED]: with the .anchorsMatchLines option this matches the attack URL
113+
114+
let attackUrl2 = "evil.com/blabla?\ngood.com/"
115+
let attackUrl2Range = NSMakeRange(0, attackUrl2.utf16.count)
116+
_ = try NSRegularExpression(pattern: "^good\\.com/").matches(in: attackUrl2, range: attackUrl2Range) // OK
117+
_ = try NSRegularExpression(pattern: "^good\\.com/", options: .anchorsMatchLines).matches(in: attackUrl2, range: attackUrl2Range) // BAD [NOT DETECTED]: with the .anchorsMatchLines option this matches the attack URL
118+
_ = try NSRegularExpression(pattern: "(?i)^good\\.com/").matches(in: attackUrl2, range: attackUrl2Range) // OK
119+
_ = try NSRegularExpression(pattern: "(?i)^good\\.com/", options: .anchorsMatchLines).matches(in: attackUrl2, range: attackUrl2Range) // BAD [NOT DETECTED]: with the .anchorsMatchLines option this matches the attack URL
120+
_ = try NSRegularExpression(pattern: "^good\\.com/|^another\\.com/").matches(in: attackUrl2, range: attackUrl2Range) // OK
121+
_ = try NSRegularExpression(pattern: "^good\\.com/|^another\\.com/", options: .anchorsMatchLines).matches(in: attackUrl2, range: attackUrl2Range) // BAD [NOT DETECTED]: with the .anchorsMatchLines option this matches the attack URL
102122
}

0 commit comments

Comments
 (0)