Skip to content

Commit 1d81c77

Browse files
ianroofmichaelnebel
authored andcommitted
C#: Enhanced LogForgingQuery to treat C# Enums as simple types.
1 parent 45b55c0 commit 1d81c77

File tree

3 files changed

+52
-1
lines changed

3 files changed

+52
-1
lines changed

csharp/ql/lib/semmle/code/csharp/security/Sanitizers.qll

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ class SimpleTypeSanitizedExpr extends DataFlow::ExprNode {
5757
SimpleTypeSanitizedExpr() {
5858
exists(Type t | t = this.getType() or t = this.getType().(NullableType).getUnderlyingType() |
5959
t instanceof SimpleType or
60-
t instanceof SystemDateTimeStruct
60+
t instanceof SystemDateTimeStruct or
61+
t instanceof Enum
6162
)
6263
}
6364
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
category: fix
3+
---
4+
* Enhanced LogForgingQuery to treat C# Enums as simple types.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using System;
2+
using System.Diagnostics;
3+
using System.IO;
4+
using System.Net;
5+
using System.Web;
6+
using Microsoft.Extensions.Logging;
7+
8+
class ILogger
9+
{
10+
public void Warn(string message) { }
11+
}
12+
13+
enum TestEnum
14+
{
15+
TestEnumValue
16+
}
17+
18+
public class LogForgingSimpleTypes
19+
{
20+
public void Execute(HttpContext ctx)
21+
{
22+
// GOOD: int
23+
logger.Warn("Logging simple type (int):" 1);
24+
25+
// GOOD: long
26+
logger.Warn("Logging simple type (int):" 1L);
27+
28+
// GOOD: float
29+
logger.Warn("Logging simple type (float):" 1.1);
30+
31+
// GOOD: double
32+
logger.Warn("Logging simple type (double):" 1.1d);
33+
34+
// GOOD: decimal
35+
logger.Warn("Logging simple type (double):" 1.1m);
36+
37+
// GOOD: Enum
38+
logger.Warn("Logging simple type (Enum):" TestEnum.TestEnumVAlue);
39+
40+
// GOOD: DateTime
41+
logger.Warn("Logging simple type (int):" new DateTime());
42+
43+
// GOOD: DateTimeOffset
44+
logger.Warn("Logging simple type (int):" DateTimeOffset.UtcNow);
45+
}
46+
}

0 commit comments

Comments
 (0)