Skip to content

Commit 0581b91

Browse files
authored
Merge pull request #10554 from michaelnebel/csharp/datetime-sanitizer
C#: Consider DateTime as simple type sanitizer.
2 parents f4ef434 + c3c90dd commit 0581b91

File tree

5 files changed

+39
-2
lines changed

5 files changed

+39
-2
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
category: minorAnalysis
3+
---
4+
* `DateTime` expressions are now considered simple type sanitizers. This affects a wide range of security queries.

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,12 @@ class UrlSanitizedExpr extends Expr {
5454
* An expression node with a simple type.
5555
*/
5656
class SimpleTypeSanitizedExpr extends DataFlow::ExprNode {
57-
SimpleTypeSanitizedExpr() { this.getType() instanceof SimpleType }
57+
SimpleTypeSanitizedExpr() {
58+
exists(Type t | t = this.getType() |
59+
t instanceof SimpleType or
60+
t instanceof SystemDateTimeStruct
61+
)
62+
}
5863
}
5964

6065
/**

csharp/ql/test/query-tests/Security Features/CWE-117/LogForging.expected

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,16 @@ edges
44
| LogForging.cs:17:27:17:49 | access to property QueryString : NameValueCollection | LogForging.cs:26:50:26:72 | ... + ... |
55
| LogForging.cs:17:27:17:61 | access to indexer : String | LogForging.cs:20:21:20:43 | ... + ... |
66
| LogForging.cs:17:27:17:61 | access to indexer : String | LogForging.cs:26:50:26:72 | ... + ... |
7+
| LogForgingAsp.cs:8:32:8:39 | username : String | LogForgingAsp.cs:12:21:12:43 | ... + ... |
78
nodes
89
| LogForging.cs:17:27:17:49 | access to property QueryString : NameValueCollection | semmle.label | access to property QueryString : NameValueCollection |
910
| LogForging.cs:17:27:17:61 | access to indexer : String | semmle.label | access to indexer : String |
1011
| LogForging.cs:20:21:20:43 | ... + ... | semmle.label | ... + ... |
1112
| LogForging.cs:26:50:26:72 | ... + ... | semmle.label | ... + ... |
13+
| LogForgingAsp.cs:8:32:8:39 | username : String | semmle.label | username : String |
14+
| LogForgingAsp.cs:12:21:12:43 | ... + ... | semmle.label | ... + ... |
1215
subpaths
1316
#select
1417
| LogForging.cs:20:21:20:43 | ... + ... | LogForging.cs:17:27:17:49 | access to property QueryString : NameValueCollection | LogForging.cs:20:21:20:43 | ... + ... | $@ flows to log entry. | LogForging.cs:17:27:17:49 | access to property QueryString | User-provided value |
1518
| LogForging.cs:26:50:26:72 | ... + ... | LogForging.cs:17:27:17:49 | access to property QueryString : NameValueCollection | LogForging.cs:26:50:26:72 | ... + ... | $@ flows to log entry. | LogForging.cs:17:27:17:49 | access to property QueryString | User-provided value |
19+
| LogForgingAsp.cs:12:21:12:43 | ... + ... | LogForgingAsp.cs:8:32:8:39 | username : String | LogForgingAsp.cs:12:21:12:43 | ... + ... | $@ flows to log entry. | LogForgingAsp.cs:8:32:8:39 | username | User-provided value |
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System;
2+
using Microsoft.AspNetCore.Http;
3+
using Microsoft.AspNetCore.Http.Headers;
4+
using Microsoft.AspNetCore.Mvc;
5+
6+
public class AspController : ControllerBase
7+
{
8+
public void Action1(string username)
9+
{
10+
var logger = new ILogger();
11+
// BAD: Logged as-is
12+
logger.Warn(username + " logged in");
13+
}
14+
15+
public void Action1(DateTime date)
16+
{
17+
var logger = new ILogger();
18+
// GOOD: DateTime is a sanitizer.
19+
logger.Warn($"Warning about the date: {date:yyyy-MM-dd}");
20+
}
21+
}
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
1-
semmle-extractor-options: ${testdir}/../../../resources/stubs/System.Web.cs /r:System.Collections.Specialized.dll /r:System.Runtime.Extensions.dll /r:System.Diagnostics.TraceSource.dll
1+
semmle-extractor-options: /nostdlib /noconfig
2+
semmle-extractor-options: --load-sources-from-project:${testdir}/../../../resources/stubs/_frameworks/Microsoft.NETCore.App/Microsoft.NETCore.App.csproj
3+
semmle-extractor-options: --load-sources-from-project:../../../resources/stubs/_frameworks/Microsoft.AspNetCore.App/Microsoft.AspNetCore.App.csproj
4+
semmle-extractor-options: ${testdir}/../../../resources/stubs/System.Web.cs

0 commit comments

Comments
 (0)