Skip to content

Commit 1d56330

Browse files
authored
Merge pull request #9782 from tamasvajk/cs/newtonsoft-deserialization
C#: Fix unsafe deserialization with `JsonConvert.DeserializeObject`
2 parents 73df8e4 + 740265d commit 1d56330

File tree

7 files changed

+633
-579
lines changed

7 files changed

+633
-579
lines changed

csharp/ql/lib/semmle/code/csharp/security/dataflow/UnsafeDeserializationQuery.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -889,7 +889,7 @@ private class YamlDotNetDeserializerDeserializeMethodSink extends ConstructorOrS
889889
}
890890

891891
/** Newtonsoft.Json.JsonConvert */
892-
private class NewtonsoftJsonConvertDeserializeObjectMethodSink extends ConstructorOrStaticMethodSink {
892+
private class NewtonsoftJsonConvertDeserializeObjectMethodSink extends Sink {
893893
NewtonsoftJsonConvertDeserializeObjectMethodSink() {
894894
exists(MethodCall mc, Method m |
895895
m = mc.getTarget() and
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
category: minorAnalysis
3+
---
4+
* The query `cs/unsafe-deserialization-untrusted-input` is not reporting on all calls of `JsonConvert.DeserializeObject` any longer, it only covers cases that explicitly use unsafe serialization settings.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using Newtonsoft;
2+
using Newtonsoft.Json;
3+
using System.Web.UI.WebControls;
4+
5+
class Test
6+
{
7+
public static object Deserialize1(TextBox data)
8+
{
9+
return JsonConvert.DeserializeObject(data.Text, new JsonSerializerSettings
10+
{
11+
TypeNameHandling = TypeNameHandling.None // OK
12+
});
13+
}
14+
15+
public static object Deserialize2(TextBox data)
16+
{
17+
return JsonConvert.DeserializeObject(data.Text, new JsonSerializerSettings
18+
{
19+
TypeNameHandling = TypeNameHandling.Auto // BAD
20+
});
21+
}
22+
23+
public static object Deserialize(TextBox data)
24+
{
25+
return JsonConvert.DeserializeObject(data.Text); // OK, not checking if JsonSerializerSettings is set globally with unsafe settings
26+
}
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
edges
2+
| ../../../../resources/stubs/Newtonsoft.Json/13.0.1/Newtonsoft.Json.cs:930:20:930:20 | 4 : Int32 | Test.cs:19:32:19:52 | access to constant Auto : Int32 |
3+
| Test.cs:9:46:9:49 | access to parameter data : TextBox | Test.cs:9:46:9:54 | access to property Text |
4+
| Test.cs:17:46:17:49 | access to parameter data : TextBox | Test.cs:17:46:17:54 | access to property Text |
5+
| Test.cs:19:32:19:52 | access to constant Auto : Int32 | Test.cs:17:57:20:9 | object creation of type JsonSerializerSettings |
6+
| Test.cs:19:32:19:52 | access to constant Auto : TypeNameHandling | Test.cs:17:57:20:9 | object creation of type JsonSerializerSettings |
7+
| Test.cs:25:46:25:49 | access to parameter data : TextBox | Test.cs:25:46:25:54 | access to property Text |
8+
nodes
9+
| ../../../../resources/stubs/Newtonsoft.Json/13.0.1/Newtonsoft.Json.cs:930:20:930:20 | 4 : Int32 | semmle.label | 4 : Int32 |
10+
| Test.cs:9:46:9:49 | access to parameter data : TextBox | semmle.label | access to parameter data : TextBox |
11+
| Test.cs:9:46:9:54 | access to property Text | semmle.label | access to property Text |
12+
| Test.cs:17:46:17:49 | access to parameter data : TextBox | semmle.label | access to parameter data : TextBox |
13+
| Test.cs:17:46:17:54 | access to property Text | semmle.label | access to property Text |
14+
| Test.cs:17:57:20:9 | object creation of type JsonSerializerSettings | semmle.label | object creation of type JsonSerializerSettings |
15+
| Test.cs:19:32:19:52 | access to constant Auto : Int32 | semmle.label | access to constant Auto : Int32 |
16+
| Test.cs:19:32:19:52 | access to constant Auto : TypeNameHandling | semmle.label | access to constant Auto : TypeNameHandling |
17+
| Test.cs:25:46:25:49 | access to parameter data : TextBox | semmle.label | access to parameter data : TextBox |
18+
| Test.cs:25:46:25:54 | access to property Text | semmle.label | access to property Text |
19+
subpaths
20+
#select
21+
| Test.cs:17:46:17:54 | access to property Text | Test.cs:17:46:17:49 | access to parameter data : TextBox | Test.cs:17:46:17:54 | access to property Text | $@ flows to unsafe deserializer. | Test.cs:17:46:17:49 | access to parameter data : TextBox | User-provided data |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Security Features/CWE-502/UnsafeDeserializationUntrustedInput.ql
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
semmle-extractor-options: /nostdlib /noconfig --load-sources-from-project:${testdir}/../../../../resources/stubs/Newtonsoft.Json/13.0.1/Newtonsoft.Json.csproj ${testdir}/../../../../resources/stubs/System.Web.cs

0 commit comments

Comments
 (0)