mirror of
https://github.com/github/codeql.git
synced 2026-04-29 02:35:15 +02:00
Swift: Port the qhelp examples to Swift.
This commit is contained in:
@@ -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>
|
||||
|
||||
|
||||
@@ -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 ...
|
||||
}
|
||||
}
|
||||
@@ -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 ...
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
});
|
||||
@@ -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);
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user