Skip to content

Commit 682aef9

Browse files
authored
Merge pull request #15212 from michaelnebel/csharp/stringreplace
C#: Fix Log forging false positive.
2 parents 7c6d30b + 0c78ccc commit 682aef9

File tree

4 files changed

+16
-10
lines changed

4 files changed

+16
-10
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -346,11 +346,11 @@ class SystemStringClass extends StringType {
346346
result.hasName("==")
347347
}
348348

349-
/** Gets the `Replace(string/char, string/char)` method. */
349+
/** Gets the `Replace(...)` method. */
350350
Method getReplaceMethod() {
351351
result.getDeclaringType() = this and
352352
result.hasName("Replace") and
353-
result.getNumberOfParameters() = 2 and
353+
result.getNumberOfParameters() in [2 .. 4] and
354354
result.getReturnType() instanceof StringType
355355
}
356356

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
category: minorAnalysis
3+
---
4+
* Fixed a Log forging false positive when using `String.Replace` to sanitize the input.

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
@@ -21,6 +21,8 @@ public void ProcessRequest(HttpContext ctx)
2121
logger.Warn(username + " logged in");
2222
// GOOD: New-lines removed
2323
logger.Warn(username.Replace(Environment.NewLine, "") + " logged in");
24+
// GOOD: New-lines removed
25+
logger.Warn(username.Replace(Environment.NewLine, "", StringComparison.InvariantCultureIgnoreCase) + " logged in");
2426
// GOOD: Html encoded
2527
logger.Warn(WebUtility.HtmlEncode(username) + " logged in");
2628
// BAD: Logged as-is to TraceSource
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
edges
22
| LogForging.cs:18:27:18:49 | access to property QueryString : NameValueCollection | LogForging.cs:18:27:18:61 | access to indexer : String |
33
| LogForging.cs:18:27:18:49 | access to property QueryString : NameValueCollection | LogForging.cs:21:21:21:43 | ... + ... |
4-
| LogForging.cs:18:27:18:49 | access to property QueryString : NameValueCollection | LogForging.cs:27:50:27:72 | ... + ... |
5-
| LogForging.cs:18:27:18:49 | access to property QueryString : NameValueCollection | LogForging.cs:31:26:31:33 | access to local variable username |
4+
| LogForging.cs:18:27:18:49 | access to property QueryString : NameValueCollection | LogForging.cs:29:50:29:72 | ... + ... |
5+
| LogForging.cs:18:27:18:49 | access to property QueryString : NameValueCollection | LogForging.cs:33:26:33:33 | access to local variable username |
66
| LogForging.cs:18:27:18:61 | access to indexer : String | LogForging.cs:21:21:21:43 | ... + ... |
7-
| LogForging.cs:18:27:18:61 | access to indexer : String | LogForging.cs:27:50:27:72 | ... + ... |
8-
| LogForging.cs:18:27:18:61 | access to indexer : String | LogForging.cs:31:26:31:33 | access to local variable username |
7+
| LogForging.cs:18:27:18:61 | access to indexer : String | LogForging.cs:29:50:29:72 | ... + ... |
8+
| LogForging.cs:18:27:18:61 | access to indexer : String | LogForging.cs:33:26:33:33 | access to local variable username |
99
| LogForgingAsp.cs:8:32:8:39 | username : String | LogForgingAsp.cs:12:21:12:43 | ... + ... |
1010
nodes
1111
| LogForging.cs:18:27:18:49 | access to property QueryString : NameValueCollection | semmle.label | access to property QueryString : NameValueCollection |
1212
| LogForging.cs:18:27:18:61 | access to indexer : String | semmle.label | access to indexer : String |
1313
| LogForging.cs:21:21:21:43 | ... + ... | semmle.label | ... + ... |
14-
| LogForging.cs:27:50:27:72 | ... + ... | semmle.label | ... + ... |
15-
| LogForging.cs:31:26:31:33 | access to local variable username | semmle.label | access to local variable username |
14+
| LogForging.cs:29:50:29:72 | ... + ... | semmle.label | ... + ... |
15+
| LogForging.cs:33:26:33:33 | access to local variable username | semmle.label | access to local variable username |
1616
| LogForgingAsp.cs:8:32:8:39 | username : String | semmle.label | username : String |
1717
| LogForgingAsp.cs:12:21:12:43 | ... + ... | semmle.label | ... + ... |
1818
subpaths
1919
#select
2020
| 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 |
21-
| LogForging.cs:27:50:27:72 | ... + ... | LogForging.cs:18:27:18:49 | access to property QueryString : NameValueCollection | LogForging.cs:27:50:27:72 | ... + ... | This log entry depends on a $@. | LogForging.cs:18:27:18:49 | access to property QueryString | user-provided value |
22-
| LogForging.cs:31:26:31:33 | access to local variable username | LogForging.cs:18:27:18:49 | access to property QueryString : NameValueCollection | LogForging.cs:31:26:31: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 |
21+
| 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 |
22+
| 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 |
2323
| 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 |

0 commit comments

Comments
 (0)