Skip to content

Commit 7cf5210

Browse files
committed
Swift: Port the qhelp examples to Swift.
1 parent 9a95b9b commit 7cf5210

File tree

5 files changed

+24
-16
lines changed

5 files changed

+24
-16
lines changed

swift/ql/src/queries/Security/CWE-020/MissingRegexAnchor.qhelp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242

4343
</p>
4444

45-
<sample src="examples/MissingRegExpAnchor_BAD.js"/>
45+
<sample src="examples/MissingRegExpAnchorBad.swift"/>
4646

4747
<p>
4848

@@ -54,7 +54,7 @@
5454

5555
</p>
5656

57-
<sample src="examples/MissingRegExpAnchor_GOOD.js"/>
57+
<sample src="examples/MissingRegExpAnchorGood.swift"/>
5858

5959
<p>
6060

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
func handleUrl(_ urlString: String) {
2+
// get the 'url=' parameter from the URL
3+
let components = URLComponents(string: urlString)
4+
let redirectParam = components?.queryItems?.first(where: { $0.name == "url" })
5+
6+
// check we trust the host
7+
let regex = try Regex(#"https?://www\.example\.com"#) // BAD: the host of `url` may be controlled by an attacker
8+
if let match = redirectParam?.value?.firstMatch(of: regex) {
9+
// ... trust the URL ...
10+
}
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
func handleUrl(_ urlString: String) {
2+
// get the 'url=' parameter from the URL
3+
let components = URLComponents(string: urlString)
4+
let redirectParam = components?.queryItems?.first(where: { $0.name == "url" })
5+
6+
// check we trust the host
7+
let regex = try Regex(#"^https?://www\.example\.com"#) // GOOD: the host of `url` can not be controlled by an attacker
8+
if let match = redirectParam?.value?.firstMatch(of: regex) {
9+
// ... trust the URL ...
10+
}
11+
}

swift/ql/src/queries/Security/CWE-020/MissingRegexAnchor_BAD.js

Lines changed: 0 additions & 7 deletions
This file was deleted.

swift/ql/src/queries/Security/CWE-020/MissingRegexAnchor_GOOD.js

Lines changed: 0 additions & 7 deletions
This file was deleted.

0 commit comments

Comments
 (0)