Skip to content

Commit 16fe7a2

Browse files
authored
Merge pull request #17815 from michaelnebel/csharp/logforgingreplacelineending
C#: Add sanitizer to `cs/log-forging`.
2 parents 2312f9d + 1217c55 commit 16fe7a2

File tree

5 files changed

+23
-7
lines changed

5 files changed

+23
-7
lines changed

csharp/ql/lib/semmle/code/csharp/frameworks/System.qll

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,14 @@ class SystemStringClass extends StringType {
354354
result.getReturnType() instanceof StringType
355355
}
356356

357+
/** Gets the `ReplaceLineEndings(string) method. */
358+
Method getReplaceLineEndingsMethod() {
359+
result.getDeclaringType() = this and
360+
result.hasName("ReplaceLineEndings") and
361+
result.getNumberOfParameters() = 1 and
362+
result.getReturnType() instanceof StringType
363+
}
364+
357365
/** Gets a `Format(...)` method. */
358366
Method getFormatMethod() {
359367
result.getDeclaringType() = this and

csharp/ql/lib/semmle/code/csharp/security/dataflow/LogForgingQuery.qll

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,9 @@ private class ExternalLoggingExprSink extends Sink {
7070
private class StringReplaceSanitizer extends Sanitizer {
7171
StringReplaceSanitizer() {
7272
exists(Method m |
73-
exists(SystemStringClass s | m = s.getReplaceMethod() or m = s.getRemoveMethod())
73+
exists(SystemStringClass s |
74+
m = s.getReplaceMethod() or m = s.getRemoveMethod() or m = s.getReplaceLineEndingsMethod()
75+
)
7476
or
7577
m = any(SystemTextRegularExpressionsRegexClass r).getAReplaceMethod()
7678
|
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
category: minorAnalysis
3+
---
4+
* C#: The method `string.ReplaceLineEndings(string)` is now considered a sanitizer for the `cs/log-forging` query.

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ public void ProcessRequest(HttpContext ctx)
2323
logger.Warn(username.Replace(Environment.NewLine, "") + " logged in");
2424
// GOOD: New-lines removed
2525
logger.Warn(username.Replace(Environment.NewLine, "", StringComparison.InvariantCultureIgnoreCase) + " logged in");
26+
// GOOD: New-lines replaced
27+
logger.Warn(username.ReplaceLineEndings("") + " logged in");
2628
// GOOD: Html encoded
2729
logger.Warn(WebUtility.HtmlEncode(username) + " logged in");
2830
// BAD: Logged as-is to TraceSource
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
#select
22
| LogForging.cs:21:21:21:43 | ... + ... | LogForging.cs:18:27:18:49 | access to property QueryString : NameValueCollection | LogForging.cs:21:21:21:43 | ... + ... | This log entry depends on a $@. | LogForging.cs:18:27:18:49 | access to property QueryString | user-provided value |
3-
| LogForging.cs:29:50:29:72 | ... + ... | LogForging.cs:18:27:18:49 | access to property QueryString : NameValueCollection | LogForging.cs:29:50:29:72 | ... + ... | This log entry depends on a $@. | LogForging.cs:18:27:18:49 | access to property QueryString | user-provided value |
4-
| LogForging.cs:33:26:33:33 | access to local variable username | LogForging.cs:18:27:18:49 | access to property QueryString : NameValueCollection | LogForging.cs:33:26:33:33 | access to local variable username | This log entry depends on a $@. | LogForging.cs:18:27:18:49 | access to property QueryString | user-provided value |
3+
| LogForging.cs:31:50:31:72 | ... + ... | LogForging.cs:18:27:18:49 | access to property QueryString : NameValueCollection | LogForging.cs:31:50:31:72 | ... + ... | This log entry depends on a $@. | LogForging.cs:18:27:18:49 | access to property QueryString | user-provided value |
4+
| LogForging.cs:35:26:35:33 | access to local variable username | LogForging.cs:18:27:18:49 | access to property QueryString : NameValueCollection | LogForging.cs:35:26:35:33 | access to local variable username | This log entry depends on a $@. | LogForging.cs:18:27:18:49 | access to property QueryString | user-provided value |
55
| LogForgingAsp.cs:12:21:12:43 | ... + ... | LogForgingAsp.cs:8:32:8:39 | username : String | LogForgingAsp.cs:12:21:12:43 | ... + ... | This log entry depends on a $@. | LogForgingAsp.cs:8:32:8:39 | username | user-provided value |
66
edges
77
| LogForging.cs:18:16:18:23 | access to local variable username : String | LogForging.cs:21:21:21:43 | ... + ... | provenance | |
8-
| LogForging.cs:18:16:18:23 | access to local variable username : String | LogForging.cs:29:50:29:72 | ... + ... | provenance | |
9-
| LogForging.cs:18:16:18:23 | access to local variable username : String | LogForging.cs:33:26:33:33 | access to local variable username | provenance | |
8+
| LogForging.cs:18:16:18:23 | access to local variable username : String | LogForging.cs:31:50:31:72 | ... + ... | provenance | |
9+
| LogForging.cs:18:16:18:23 | access to local variable username : String | LogForging.cs:35:26:35:33 | access to local variable username | provenance | |
1010
| LogForging.cs:18:27:18:49 | access to property QueryString : NameValueCollection | LogForging.cs:18:16:18:23 | access to local variable username : String | provenance | |
1111
| LogForging.cs:18:27:18:49 | access to property QueryString : NameValueCollection | LogForging.cs:18:27:18:61 | access to indexer : String | provenance | MaD:1 |
1212
| LogForging.cs:18:27:18:61 | access to indexer : String | LogForging.cs:18:16:18:23 | access to local variable username : String | provenance | |
@@ -18,8 +18,8 @@ nodes
1818
| LogForging.cs:18:27:18:49 | access to property QueryString : NameValueCollection | semmle.label | access to property QueryString : NameValueCollection |
1919
| LogForging.cs:18:27:18:61 | access to indexer : String | semmle.label | access to indexer : String |
2020
| LogForging.cs:21:21:21:43 | ... + ... | semmle.label | ... + ... |
21-
| LogForging.cs:29:50:29:72 | ... + ... | semmle.label | ... + ... |
22-
| LogForging.cs:33:26:33:33 | access to local variable username | semmle.label | access to local variable username |
21+
| LogForging.cs:31:50:31:72 | ... + ... | semmle.label | ... + ... |
22+
| LogForging.cs:35:26:35:33 | access to local variable username | semmle.label | access to local variable username |
2323
| LogForgingAsp.cs:8:32:8:39 | username : String | semmle.label | username : String |
2424
| LogForgingAsp.cs:12:21:12:43 | ... + ... | semmle.label | ... + ... |
2525
subpaths

0 commit comments

Comments
 (0)