2021-12-13 16:18:55 +01:00
User-provided data, such as URL parameters, POST data payloads, or cookies, should always be considered untrusted and tainted. Applications performing HTTP redirects based on tainted data could enable an attacker to redirect users to a malicious site to, for example, steal login credentials.
2021-04-26 17:29:13 +02:00
This problem could be mitigated in any of the following ways:
2021-12-13 16:18:55 +01:00
* Validate the user-provided data based on a whitelist and reject input not matching.
* Redesign the application to not perform redirects based on user-provided data.
2020-06-30 12:50:28 +02:00
== Noncompliant Code Example
2021-11-05 14:12:29 +01:00
Symfony
2022-02-04 17:28:24 +01:00
[source,php]
2020-06-30 12:50:28 +02:00
----
2021-11-05 14:12:29 +01:00
public function redirect(Request $request)
{
$url = $request->query->get('url');
return $this->redirect($url); // Noncompliant
}
public function setLocatioHeader(Request $request)
{
$url = $request->query->get('url');
$response = new Response('Redirecting...', 302);
$response->headers->set('Location', $url); // Noncompliant
return $response;
}
----
Laravel
2022-02-04 17:28:24 +01:00
[source,php]
2021-11-05 14:12:29 +01:00
----
public function redirect(Request $request)
{
$url = $request->input('url');
return $this->redirect($url); // Noncompliant
}
public function setLocatioHeader(Request $request)
{
$url = $request->input('url');
return response("", 302)
->header('Location', $url) // Noncompliant
}
2020-06-30 12:50:28 +02:00
----
== Compliant Solution
2021-11-05 14:12:29 +01:00
Symfony
2022-02-04 17:28:24 +01:00
[source,php]
2021-11-05 14:12:29 +01:00
----
public function redirect(Request $request)
{
$url = $request->query->get('url');
$allowedUrls = ["/index", "/login", "/logout"];
if (in_array($url, $allowedUrls, true)) {
return $this->redirect($url);
} else {
$this->redirect("/");
}
}
----
Laravel
2022-02-04 17:28:24 +01:00
[source,php]
2020-06-30 12:50:28 +02:00
----
2021-11-05 14:12:29 +01:00
public function redirect(Request $request)
{
$url = $request->input('url');
$allowedUrls = ["/index", "/login", "/logout"];
if (in_array($url, $allowedUrls, true)) {
return $this->redirect($url);
} else {
return redirect("/");
}
2020-06-30 12:50:28 +02:00
}
----
include::../see.adoc[]
2021-09-20 15:38:42 +02:00
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
include::../message.adoc[]
include::../highlighting.adoc[]
endif::env-github,rspecator-view[]