Loris S cd03a1dd3d
Modify S5144&S6547: Improve fixes (#2912)
## Review

A dedicated reviewer checked the rule description successfully for:

- [ ] logical errors and incorrect information
- [ ] information gaps and missing content
- [ ] text style and tone
- [ ] PR summary and labels follow [the
guidelines](https://github.com/SonarSource/rspec/#to-modify-an-existing-rule)
2023-08-21 10:51:21 +02:00

64 lines
1.3 KiB
Plaintext

== How to fix it in ASP.NET
=== Code examples
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[] allowedSchemes = { "https" };
private readonly string[] allowedDomains = { "trusted1.example.com", "trusted2.example.com" };
[HttpGet]
public IActionResult ImageFetch(string location)
{
Uri uri = new Uri(location);
if (!allowedDomains.Contains(uri.Host) && !allowedSchemes.Contains(uri.Scheme))
{
return BadRequest();
}
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
return Ok();
}
}
----
=== How does this work?
include::../../common/fix/pre-approved-list.adoc[]
=== Pitfalls
include::../../common/pitfalls/starts-with.adoc[]