github / github/codeql

C#: Missing modelling of Newtonsoft.Json StringEscapeHandling

オープン
#15,155 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る
false-positive
主要言語
CodeQL
スター
10.1k
フォーク
2.1k
平均マージ
2日 15時間
マージ済み PR(30日)
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 を短くまとめたダイジェスト。