Skip to content

Commit caa2e72

Browse files
committed
insecure-binaryformatter-deserialization-csharp
1 parent 3422a00 commit caa2e72

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
id: insecure-binaryformatter-deserialization-csharp
2+
severity: warning
3+
language: csharp
4+
message: >-
5+
The BinaryFormatter type is dangerous and is not recommended for data
6+
processing. Applications should stop using BinaryFormatter as soon as
7+
possible, even if they believe the data they're processing to be
8+
trustworthy. BinaryFormatter is insecure and can't be made secure.
9+
note: >-
10+
[CWE-502] Deserialization of Untrusted Data.
11+
[REFERENCES]
12+
- https://docs.microsoft.com/en-us/dotnet/standard/serialization/binaryformatter-security-guide
13+
14+
ast-grep-essentials: true
15+
16+
utils:
17+
MATCH_PATTERN_BinaryFormatter:
18+
pattern: new BinaryFormatter()
19+
any:
20+
- inside:
21+
stopBy: end
22+
follows:
23+
stopBy: end
24+
kind: using_directive
25+
pattern: using System.Runtime.Serialization.Formatters.Binary;
26+
- inside:
27+
kind: global_statement
28+
stopBy: end
29+
follows:
30+
stopBy: end
31+
kind: using_directive
32+
pattern: using System.Runtime.Serialization.Formatters.Binary
33+
not:
34+
inside:
35+
kind: object_creation_expression
36+
stopBy: end
37+
not:
38+
inside:
39+
kind: variable_declarator
40+
stopBy: end
41+
42+
rule:
43+
matches: MATCH_PATTERN_BinaryFormatter
44+
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
id: insecure-binaryformatter-deserialization-csharp
2+
3+
invalid:
4+
- |
5+
using System.Runtime.Serialization.Formatters.Binary;
6+
namespace InsecureDeserialization
7+
{
8+
public class InsecureBinaryFormatterDeserialization
9+
{
10+
public void BinaryFormatterDeserialization(string json)
11+
{
12+
try
13+
{
14+
BinaryFormatter binaryFormatter = new BinaryFormatter();
15+
16+
MemoryStream memoryStream = new MemoryStream(Encoding.UTF8.GetBytes(json));
17+
binaryFormatter.Deserialize(memoryStream);
18+
memoryStream.Close();
19+
}
20+
catch (Exception e)
21+
{
22+
Console.WriteLine(e);
23+
}
24+
}
25+
}
26+
}

0 commit comments

Comments
 (0)