55 lines
1.0 KiB
Plaintext
55 lines
1.0 KiB
Plaintext
=== How to fix it in ASP.NET
|
|
|
|
include::../../common/fix/code-rationale.adoc[]
|
|
|
|
[cols="a"]
|
|
|===
|
|
h| Non-compliant code example
|
|
|
|
|
[source,csharp]
|
|
----
|
|
using System.Web;
|
|
using System.Web.Mvc;
|
|
|
|
public class ExampleController : Controller
|
|
{
|
|
[HttpGet]
|
|
public IActionResult ImageFetch(string location)
|
|
{
|
|
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(location); // Noncompliant
|
|
|
|
return Ok();
|
|
}
|
|
}
|
|
----
|
|
h| Compliant solution
|
|
|
|
|
[source,csharp]
|
|
----
|
|
using System.Web;
|
|
using System.Web.Mvc;
|
|
|
|
public class ExampleController : Controller
|
|
{
|
|
private readonly string[] allowedUrls = { "www.example.com", "example.com" };
|
|
|
|
[HttpGet]
|
|
public IActionResult ImageFetch(string location)
|
|
|
|
{
|
|
Uri uri = new Uri(location);
|
|
|
|
if (!allowedUrls.Contains(uri.Host))
|
|
{
|
|
return BadRequest();
|
|
}
|
|
|
|
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
|
|
|
|
return Ok();
|
|
}
|
|
}
|
|
----
|
|
|===
|
|
|
|
include::../../common/fix/how-does-this-work.adoc[] |