Skip to content

Commit 03ca29a

Browse files
committed
Swift: Adapt the IncompleteHostnameRegex qhelp for Swift.
1 parent 1805b07 commit 03ca29a

File tree

4 files changed

+30
-14
lines changed

4 files changed

+30
-14
lines changed

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

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

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646

4747
</p>
4848

49-
<sample src="examples/IncompleteHostnameRegExp.js"/>
49+
<sample src="IncompleteHostnameRegexBad.swift"/>
5050

5151
<p>
5252

@@ -59,15 +59,16 @@
5959
<p>
6060

6161
Address this vulnerability by escaping <code>.</code>
62-
appropriately: <code>let regex = /^((www|beta)\.)?example\.com/</code>.
62+
to <code>\.</code>:
6363

6464
</p>
6565

66+
<sample src="IncompleteHostnameRegexGood.swift"/>
67+
6668
</example>
6769

6870
<references>
69-
<li>MDN: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions">Regular Expressions</a></li>
70-
<li>OWASP: <a href="https://www.owasp.org/index.php/Server_Side_Request_Forgery">SSRF</a></li>
71-
<li>OWASP: <a href="https://cheatsheetseries.owasp.org/cheatsheets/Unvalidated_Redirects_and_Forwards_Cheat_Sheet.html">XSS Unvalidated Redirects and Forwards Cheat Sheet</a>.</li>
71+
<li>OWASP: <a href="https://www.owasp.org/index.php/Server_Side_Request_Forgery">Server Side Request Forgery</a></li>
72+
<li>OWASP: <a href="https://cheatsheetseries.owasp.org/cheatsheets/Unvalidated_Redirects_and_Forwards_Cheat_Sheet.html">Unvalidated Redirects and Forwards Cheat Sheet</a></li>
7273
</references>
7374
</qhelp>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
func handleUrl(_ urlString: String) {
3+
// get the 'url=' parameter from the URL
4+
let components = URLComponents(string: urlString)
5+
let redirectParam = components?.queryItems?.first(where: { $0.name == "url" })
6+
7+
// check we trust the host
8+
let regex = #/^(www|beta).example.com//#
9+
if let match = redirectParam?.value?.firstMatch(of: regex) {
10+
// ... trust the URL ...
11+
}
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
func handleUrl(_ urlString: String) {
3+
// get the 'url=' parameter from the URL
4+
let components = URLComponents(string: urlString)
5+
let redirectParam = components?.queryItems?.first(where: { $0.name == "url" })
6+
7+
// check we trust the host
8+
let regex = #/^(www|beta)\.example\.com//#
9+
if let match = redirectParam?.value?.firstMatch(of: regex) {
10+
// ... trust the URL ...
11+
}
12+
}

0 commit comments

Comments
 (0)