Swift: Port the qhelp examples to Swift.

This commit is contained in:
Geoffrey White
2023-10-27 12:42:59 +01:00
parent 9a95b9bcda
commit 7cf5210063
5 changed files with 24 additions and 16 deletions

View File

@@ -42,7 +42,7 @@
</p>
<sample src="examples/MissingRegExpAnchor_BAD.js"/>
<sample src="examples/MissingRegExpAnchorBad.swift"/>
<p>
@@ -54,7 +54,7 @@
</p>
<sample src="examples/MissingRegExpAnchor_GOOD.js"/>
<sample src="examples/MissingRegExpAnchorGood.swift"/>
<p>

View File

@@ -0,0 +1,11 @@
func handleUrl(_ urlString: String) {
// get the 'url=' parameter from the URL
let components = URLComponents(string: urlString)
let redirectParam = components?.queryItems?.first(where: { $0.name == "url" })
// check we trust the host
let regex = try Regex(#"https?://www\.example\.com"#) // BAD: the host of `url` may be controlled by an attacker
if let match = redirectParam?.value?.firstMatch(of: regex) {
// ... trust the URL ...
}
}

View File

@@ -0,0 +1,11 @@
func handleUrl(_ urlString: String) {
// get the 'url=' parameter from the URL
let components = URLComponents(string: urlString)
let redirectParam = components?.queryItems?.first(where: { $0.name == "url" })
// check we trust the host
let regex = try Regex(#"^https?://www\.example\.com"#) // GOOD: the host of `url` can not be controlled by an attacker
if let match = redirectParam?.value?.firstMatch(of: regex) {
// ... trust the URL ...
}
}

View File

@@ -1,7 +0,0 @@
app.get("/some/path", function(req, res) {
let url = req.param("url");
// BAD: the host of `url` may be controlled by an attacker
if (url.match(/https?:\/\/www\.example\.com\//)) {
res.redirect(url);
}
});

View File

@@ -1,7 +0,0 @@
app.get("/some/path", function(req, res) {
let url = req.param("url");
// GOOD: the host of `url` can not be controlled by an attacker
if (url.match(/^https?:\/\/www\.example\.com\//)) {
res.redirect(url);
}
});