如何解决 Checkmarx 扫描问题报告的 "Deserialization of Untrusted Data" 错误

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

我已将 Checkmarx 扫描工具与 Azure DevOps 管道集成。在 运行 宁 ASP.Net Core Web API 相关管道后,我得到了高风险结果。我尝试了多种方法和 运行 管道。现在仍然没有运气。你能帮我解决这个问题吗?

C#代码:

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; }
}

错误信息:

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

Checkmarx 未将 JsonSerializer 识别为安全的反序列化程序。您将不得不覆盖 Checkmarx 查询以将 JsonSerializer 作为使用 Checkmarx CxAudit 的消毒剂之一,或者如果您没有 CxAudit,您将不得不与您的 AppSec 团队争论这应该被标记为不可利用 System.Text.Json 威胁模型:

缓解

System.Type 实例的序列化和反序列化 默认情况下 JsonSerializer 不支持。可以实施自定义转换器来处理 System.Type 个实例,但应注意避免处理不受信任的数据。

https://github.com/dotnet/runtime/blob/main/src/libraries/System.Text.Json/docs/ThreatModel.md