From 3759f659cac4be52d3ae9cc00cee383b9ee42ce8 Mon Sep 17 00:00:00 2001 From: "Loris S." <91723853+loris-s-sonarsource@users.noreply.github.com> Date: Wed, 23 Oct 2024 15:34:00 +0200 Subject: [PATCH] Modified S5144(C#): Use HttpClient instead of old WebRequest (#4431) --- rules/S5144/csharp/how-to-fix-it/dotnet.adoc | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/rules/S5144/csharp/how-to-fix-it/dotnet.adoc b/rules/S5144/csharp/how-to-fix-it/dotnet.adoc index 64086c821f1..df0b495826e 100644 --- a/rules/S5144/csharp/how-to-fix-it/dotnet.adoc +++ b/rules/S5144/csharp/how-to-fix-it/dotnet.adoc @@ -14,11 +14,14 @@ using System.Web.Mvc; public class ExampleController: Controller { [HttpGet] - public IActionResult ImageFetch(string location) + public async Task ImageFetch(string location) { - HttpWebRequest request = (HttpWebRequest)WebRequest.Create(location); + await using Stream stream = + await client.GetStreamAsync(location); // Noncompliant + var exampleImage = + await JsonSerializer.DeserializeAsync(stream); - return Ok(); + return Ok(example ?? new()); } } ---- @@ -36,7 +39,7 @@ public class ExampleController: Controller private readonly string[] allowedDomains = { "trusted1.example.com", "trusted2.example.com" }; [HttpGet] - public IActionResult ImageFetch(string location) + public async Task ImageFetch(string location) { Uri uri = new Uri(location); @@ -45,9 +48,12 @@ public class ExampleController: Controller return BadRequest(); } - HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri); + await using Stream stream = + await client.GetStreamAsync(location); + var exampleImage = + await JsonSerializer.DeserializeAsync(stream); - return Ok(); + return Ok(example ?? new()); } } ----