59 lines
1.1 KiB
Plaintext
59 lines
1.1 KiB
Plaintext
=== How to fix it in ASP.NET
|
|
|
|
include::../../common/fix/code-rationale.adoc[]
|
|
|
|
==== Noncompliant code example
|
|
|
|
[source,csharp,diff-id=1,diff-type=noncompliant]
|
|
----
|
|
using System.Web;
|
|
using System.Web.Mvc;
|
|
|
|
public class ExampleController : Controller
|
|
{
|
|
[HttpGet]
|
|
public IActionResult ImageFetch(string location)
|
|
{
|
|
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(location);
|
|
|
|
return Ok();
|
|
}
|
|
}
|
|
----
|
|
|
|
==== Compliant solution
|
|
|
|
[source,csharp,diff-id=1,diff-type=compliant]
|
|
----
|
|
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[]
|
|
|
|
=== Pitfalls
|
|
|
|
include::../../common/pitfalls/starts-with.adoc[]
|
|
|
|
|