Visitar URL original
C#: Missing modelling of Newtonsoft.Json StringEscapeHandling · Issue #15155 · github/codeql · GitHub
The Wayback Machine - https://web.archive.org/web/20240111220312/https://github.com/github/codeql/issues/15155
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C#: Missing modelling of Newtonsoft.Json StringEscapeHandling #15155

Open
max-schaefer opened this issue Dec 19, 2023 · 0 comments
Open

C#: Missing modelling of Newtonsoft.Json StringEscapeHandling #15155

max-schaefer opened this issue Dec 19, 2023 · 0 comments

Comments

@max-schaefer
Copy link
Collaborator

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), 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:

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:

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant