github / github/codeql

C#: Missing modelling of Newtonsoft.Json StringEscapeHandling

未关闭
#15,155 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看
false-positive
主要语言
CodeQL
星标
10.1k
派生
2.1k
平均合并
2 天 15 小时
30 天内合并 PR
141

描述

**Description of the false positive**

The C# analysis (correctly) models `Newtonsoft.Json.JsonConvert.SerializeObject` as transmitting taint from its first argument to its result. However, the second argument to the method can be a `JsonSerializerSettings` object, which in particular can specify how to escape strings (cf [`StringEscapeHandling`](https://www.newtonsoft.com/json/help/html/t_newtonsoft_json_stringescapehandling.htm)), which prevents taint from being transmitted in many cases. I don't think this is being modelled at the moment.

**Code samples or links to source code**

For example, consider [this line of code](https://github.com/gramirezl/RepoTestCodeQL/blob/bd319cdf8b92f62bbbae98f89791fc3fb206fd17/pedidos-service/Omicron.Pedidos.Services/AlmacenService/AlmacenService.cs#L67C13-L70C14):

```cs
var stringContent = new StringContent(JsonConvert.SerializeObject(dataToSend), UnicodeEncoding.UTF8, "application/json");
```

CodeQL flags this as an XSS because `dataToSend` is user controlled and `stringContent` is embedded unsafely into HTML. (I have my doubts about that alert for other reasons, but that's not the issue here).

Now let's say I rewrite this line as follow:

```cs
var settings = new JsonSerializerSettings
{
StringEscapeHandling = StringEscapeHandling.EscapeHtml
};
var stringContent = new StringContent(JsonConvert.SerializeObject(dataToSend, settings), UnicodeEncoding.UTF8, "application/json");
```

CodeQL still flags the rewritten code, but I believe that's a false positive.

贡献指南

打开贡献指南

评估

这个 Issue 还没有评估数据。

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。