Skip to content

Commit 6fa741b

Browse files
ESS-ENNSakshis
andauthored
Add C# BinaryFormatter and Ruby force_ssl security rules with tests (#148)
* removed missing-secure-java * httponly-false-csharp * use-of-md5-digest-utils-java * removing use-of-md5-digest-utils and httponly-false-csharp * force-ssl-false-ruby * insecure-binaryformatter-deserialization-csharp --------- Co-authored-by: Sakshis <[email protected]>
1 parent 895f00b commit 6fa741b

File tree

5 files changed

+128
-0
lines changed

5 files changed

+128
-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: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
id: force-ssl-false-ruby
2+
language: ruby
3+
severity: warning
4+
message: >-
5+
Checks for configuration setting of force_ssl to false. Force_ssl
6+
forces usage of HTTPS, which could lead to network interception of
7+
unencrypted application traffic. To fix, set config.force_ssl = true.
8+
note: >-
9+
[CWE-311] Missing Encryption of Sensitive Data.
10+
[REFERENCES]
11+
- https://github.com/presidentbeef/brakeman/blob/main/lib/brakeman/checks/check_force_ssl.rb
12+
13+
ast-grep-essentials: true
14+
15+
utils:
16+
config.force_ssl = $FAL:
17+
kind: assignment
18+
all:
19+
- has:
20+
kind: call
21+
pattern: config.force_ssl
22+
- has:
23+
regex: ^\s*false$
24+
25+
rule:
26+
kind: assignment
27+
any:
28+
- matches: config.force_ssl = $FAL
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
id: force-ssl-false-ruby
2+
snapshots:
3+
? |
4+
def bad_ssl
5+
config.force_ssl = false
6+
end
7+
: labels:
8+
- source: config.force_ssl = false
9+
style: primary
10+
start: 12
11+
end: 36
12+
- source: config.force_ssl
13+
style: secondary
14+
start: 12
15+
end: 28
16+
- source: 'false'
17+
style: secondary
18+
start: 31
19+
end: 36
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+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
id: force-ssl-false-ruby
2+
valid:
3+
- |
4+
def bad_ssl
5+
config.force_ssl = true
6+
end
7+
invalid:
8+
- |
9+
def bad_ssl
10+
config.force_ssl = false
11+
end

0 commit comments

Comments
 (0)