2022-08-04 17:21:56 +02:00
|
|
|
=== How to fix it in ASP.NET
|
|
|
|
|
2022-08-05 12:24:14 +02:00
|
|
|
include::../../common/fix/code-rationale.adoc[]
|
2022-08-04 17:21:56 +02:00
|
|
|
|
2022-09-15 10:28:08 +02:00
|
|
|
==== Non-compliant code example
|
|
|
|
|
|
|
|
[source,csharp,diff-id=1,diff-type=noncompliant]
|
2022-08-04 17:21:56 +02:00
|
|
|
----
|
|
|
|
using System.Web;
|
|
|
|
using System.Web.Mvc;
|
|
|
|
|
2022-08-05 12:24:14 +02:00
|
|
|
public class ExampleController : Controller
|
2022-08-04 17:21:56 +02:00
|
|
|
{
|
|
|
|
[HttpGet]
|
2022-08-05 12:24:14 +02:00
|
|
|
public void Redirect(string url)
|
2022-08-04 17:21:56 +02:00
|
|
|
{
|
2022-09-15 10:28:08 +02:00
|
|
|
Response.Redirect(url);
|
2022-08-04 17:21:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
----
|
2022-09-15 10:28:08 +02:00
|
|
|
|
|
|
|
==== Compliant solution
|
|
|
|
|
|
|
|
[source,csharp,diff-id=1,diff-type=compliant]
|
2022-08-04 17:21:56 +02:00
|
|
|
----
|
|
|
|
using System.Web;
|
|
|
|
using System.Web.Mvc;
|
|
|
|
|
2022-08-05 12:24:14 +02:00
|
|
|
public class ExampleController : Controller
|
2022-08-04 17:21:56 +02:00
|
|
|
{
|
|
|
|
private readonly string[] allowedUrls = { "/", "/login", "/logout" };
|
|
|
|
|
|
|
|
[HttpGet]
|
2022-08-05 12:24:14 +02:00
|
|
|
public void Redirect(string url)
|
2022-08-04 17:21:56 +02:00
|
|
|
|
|
|
|
{
|
|
|
|
if (allowedUrls.Contains(url))
|
|
|
|
{
|
|
|
|
Response.Redirect(url);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
----
|
|
|
|
|
2022-09-15 10:28:08 +02:00
|
|
|
include::../../common/fix/how-does-this-work.adoc[]
|
2022-09-15 14:25:49 +02:00
|
|
|
|
|
|
|
=== Pitfalls
|
|
|
|
|
|
|
|
include::../../common/pitfalls/starts-with.adoc[]
|