2022-09-01 17:36:53 +02:00
|
|
|
=== How to fix it in ASP.NET
|
|
|
|
|
|
|
|
include::../../common/fix/code-rationale.adoc[]
|
|
|
|
|
2022-10-18 16:03:10 +02:00
|
|
|
==== Noncompliant code example
|
2022-09-15 10:28:08 +02:00
|
|
|
|
|
|
|
[source,csharp,diff-id=1,diff-type=noncompliant]
|
2022-09-01 17:36:53 +02:00
|
|
|
----
|
|
|
|
using System.Web;
|
|
|
|
using System.Web.Mvc;
|
|
|
|
|
2022-11-23 17:38:23 +01:00
|
|
|
public class ExampleController: Controller
|
2022-09-01 17:36:53 +02:00
|
|
|
{
|
|
|
|
[HttpGet]
|
|
|
|
public IActionResult ImageFetch(string location)
|
|
|
|
{
|
2022-09-15 10:28:08 +02:00
|
|
|
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(location);
|
2022-09-01 17:36:53 +02:00
|
|
|
|
|
|
|
return Ok();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
----
|
2022-09-15 10:28:08 +02:00
|
|
|
|
|
|
|
==== Compliant solution
|
|
|
|
|
|
|
|
[source,csharp,diff-id=1,diff-type=compliant]
|
2022-09-01 17:36:53 +02:00
|
|
|
----
|
|
|
|
using System.Web;
|
|
|
|
using System.Web.Mvc;
|
|
|
|
|
2022-11-23 17:38:23 +01:00
|
|
|
public class ExampleController: Controller
|
2022-09-01 17:36:53 +02:00
|
|
|
{
|
2022-11-23 17:38:23 +01:00
|
|
|
private readonly string[] allowedSchemes = { "https" };
|
|
|
|
private readonly string[] allowedDomains = { "trusted1.example.com", "trusted2.example.com" };
|
2022-09-01 17:36:53 +02:00
|
|
|
|
|
|
|
[HttpGet]
|
|
|
|
public IActionResult ImageFetch(string location)
|
|
|
|
{
|
|
|
|
Uri uri = new Uri(location);
|
|
|
|
|
2022-11-23 17:38:23 +01:00
|
|
|
if (!allowedDomains.Contains(uri.Host) && !allowedSchemes.Contains(uri.Scheme))
|
2022-09-01 17:36:53 +02:00
|
|
|
{
|
|
|
|
return BadRequest();
|
|
|
|
}
|
|
|
|
|
|
|
|
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
|
|
|
|
|
|
|
|
return Ok();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
----
|
|
|
|
|
2022-09-15 10:28:08 +02:00
|
|
|
include::../../common/fix/how-does-this-work.adoc[]
|
2022-09-15 14:25:49 +02:00
|
|
|
|
2022-11-23 17:38:23 +01:00
|
|
|
The compliant code example uses such an approach.
|
|
|
|
|
2022-09-15 14:25:49 +02:00
|
|
|
=== Pitfalls
|
|
|
|
|
|
|
|
include::../../common/pitfalls/starts-with.adoc[]
|
|
|
|
|
|
|
|
|