'How to resolve "Deserialization of Untrusted Data" error reported by Checkmarx scan issue

I have integrated Checkmarx scan tool with Azure DevOps pipeline. I am getting high risk results after running the ASP.Net Core Web API related pipeline. I have tried multiple approaches and run the pipeline. Still now there is no luck. Could you please help me to resolve the issue?

C# Code:

Uri requestUri = new Uri("https://webapi.com/token");
HttpContent httpContent = new StringContent(System.Text.Json.JsonSerializer.Serialize(new { access_token = accessToken }), Encoding.UTF8, "application/json");
var result = await client.PostAsync(requestUri, httpContent);
if (result != null && result.IsSuccessStatusCode)
{
  var content = await result.Content.ReadAsStreamAsync();
  var authResponse = await System.Text.Json.JsonSerializer.DeserializeAsync<TokenResponse>(content);
  var authToken = authResponse.AuthenticationToken;
}

TokenResponse.cs:

class TokenResponse
{
  public string AuthenticationToken { get; set; }
}

Error Message:

The serialized object PostAsync processed in async in the file at line 42 is deserialized by DeserializeAsync in the file at line 46.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source