Skip to content

C#: Consider DateTime as simple type sanitizer. #10554

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Sep 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
category: minorAnalysis
---
* `DateTime` expressions are now considered simple type sanitizers. This affects a wide range of security queries.
7 changes: 6 additions & 1 deletion csharp/ql/lib/semmle/code/csharp/security/Sanitizers.qll
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,12 @@ class UrlSanitizedExpr extends Expr {
* An expression node with a simple type.
*/
class SimpleTypeSanitizedExpr extends DataFlow::ExprNode {
SimpleTypeSanitizedExpr() { this.getType() instanceof SimpleType }
SimpleTypeSanitizedExpr() {
exists(Type t | t = this.getType() |
t instanceof SimpleType or
t instanceof SystemDateTimeStruct
)
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@ edges
| LogForging.cs:17:27:17:49 | access to property QueryString : NameValueCollection | LogForging.cs:26:50:26:72 | ... + ... |
| LogForging.cs:17:27:17:61 | access to indexer : String | LogForging.cs:20:21:20:43 | ... + ... |
| LogForging.cs:17:27:17:61 | access to indexer : String | LogForging.cs:26:50:26:72 | ... + ... |
| LogForgingAsp.cs:8:32:8:39 | username : String | LogForgingAsp.cs:12:21:12:43 | ... + ... |
nodes
| LogForging.cs:17:27:17:49 | access to property QueryString : NameValueCollection | semmle.label | access to property QueryString : NameValueCollection |
| LogForging.cs:17:27:17:61 | access to indexer : String | semmle.label | access to indexer : String |
| LogForging.cs:20:21:20:43 | ... + ... | semmle.label | ... + ... |
| LogForging.cs:26:50:26:72 | ... + ... | semmle.label | ... + ... |
| LogForgingAsp.cs:8:32:8:39 | username : String | semmle.label | username : String |
| LogForgingAsp.cs:12:21:12:43 | ... + ... | semmle.label | ... + ... |
subpaths
#select
| 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 |
| 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 |
| 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 |
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Headers;
using Microsoft.AspNetCore.Mvc;

public class AspController : ControllerBase
{
public void Action1(string username)
{
var logger = new ILogger();
// BAD: Logged as-is
logger.Warn(username + " logged in");
}

public void Action1(DateTime date)
{
var logger = new ILogger();
// GOOD: DateTime is a sanitizer.
logger.Warn($"Warning about the date: {date:yyyy-MM-dd}");
}
}
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
semmle-extractor-options: ${testdir}/../../../resources/stubs/System.Web.cs /r:System.Collections.Specialized.dll /r:System.Runtime.Extensions.dll /r:System.Diagnostics.TraceSource.dll
semmle-extractor-options: /nostdlib /noconfig
semmle-extractor-options: --load-sources-from-project:${testdir}/../../../resources/stubs/_frameworks/Microsoft.NETCore.App/Microsoft.NETCore.App.csproj
semmle-extractor-options: --load-sources-from-project:../../../resources/stubs/_frameworks/Microsoft.AspNetCore.App/Microsoft.AspNetCore.App.csproj
semmle-extractor-options: ${testdir}/../../../resources/stubs/System.Web.cs