Repository navigation
C#: Recognize safe output encoding in cs/log-forging #22737
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
Open
Bubby4j
wants to merge
1
commit into
github:main
Choose a base branch
from
Bubby4j:fix/cs-json-safe-log-encoding
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
782 changes: 782 additions & 0 deletions
782
csharp/ql/lib/semmle/code/csharp/security/dataflow/JsonLoggingConfiguration.qll
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
csharp/ql/src/change-notes/2026-10-01-json-logging-log-forging.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| --- | ||
| category: minorAnalysis | ||
| --- | ||
| * The `cs/log-forging` query now uses a conservative, database-wide heuristic to recognize | ||
| supported code-configured Serilog and .NET JSON-only logging setups. This reduces false-positive | ||
| results for standard logging calls when all visible logging configuration in the database is | ||
| safe and understood. |
4 changes: 4 additions & 0 deletions
4
.../ql/test/query-tests/Security Features/CWE-117-JsonLoggingBuiltInSafe/LogForging.expected
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| #select | ||
| edges | ||
| nodes | ||
| subpaths |
4 changes: 4 additions & 0 deletions
4
csharp/ql/test/query-tests/Security Features/CWE-117-JsonLoggingBuiltInSafe/LogForging.qlref
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| query: Security Features/CWE-117/LogForging.ql | ||
| postprocess: | ||
| - utils/test/PrettyPrintModels.ql | ||
| - utils/test/InlineExpectationsTestQuery.ql |
20 changes: 20 additions & 0 deletions
20
csharp/ql/test/query-tests/Security Features/CWE-117-JsonLoggingBuiltInSafe/Test.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| using Microsoft.AspNetCore.Mvc; | ||
| using Microsoft.Extensions.Logging; | ||
| using System.Web; | ||
|
|
||
| class BuiltInJsonLoggingController : ControllerBase | ||
| { | ||
| public void Action(HttpContext context) | ||
| { | ||
| using var factory = LoggerFactory.Create(logging => logging.AddJsonConsole()); | ||
| var logger = factory.CreateLogger("Example"); | ||
| var genericLogger = factory.CreateLogger<BuiltInJsonLoggingController>(); | ||
| string input = context.Request.QueryString["input"]; | ||
| logger.LogInformation("Input: {Input}", input); | ||
| logger.LogInformation($"Input: {input}"); | ||
| genericLogger.LogInformation("Input: {Input}", input); | ||
|
|
||
| var explicitlyDisposed = LoggerFactory.Create(logging => logging.AddJsonConsole()); | ||
| explicitlyDisposed.Dispose(); | ||
| } | ||
| } | ||
3 changes: 3 additions & 0 deletions
3
csharp/ql/test/query-tests/Security Features/CWE-117-JsonLoggingBuiltInSafe/options
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| semmle-extractor-options: /nostdlib /noconfig | ||
| 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 |
15 changes: 15 additions & 0 deletions
15
...l/test/query-tests/Security Features/CWE-117-JsonLoggingBuiltInUnsafe/LogForging.expected
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| #select | ||
| | Test.cs:16:49:16:53 | access to local variable input | Test.cs:15:24:15:50 | access to property QueryString : NameValueCollection | Test.cs:16:49:16:53 | access to local variable input | This log entry depends on a $@. | Test.cs:15:24:15:50 | access to property QueryString | user-provided value | | ||
| edges | ||
| | Test.cs:15:16:15:20 | access to local variable input : String | Test.cs:16:49:16:53 | access to local variable input | provenance | | | ||
| | Test.cs:15:24:15:50 | access to property QueryString : NameValueCollection | Test.cs:15:16:15:20 | access to local variable input : String | provenance | | | ||
| | Test.cs:15:24:15:50 | access to property QueryString : NameValueCollection | Test.cs:15:24:15:59 | access to indexer : String | provenance | MaD:1 | | ||
| | Test.cs:15:24:15:59 | access to indexer : String | Test.cs:15:16:15:20 | access to local variable input : String | provenance | | | ||
| models | ||
| | 1 | Summary: System.Collections.Specialized; NameValueCollection; false; get_Item; (System.String); ; Argument[this]; ReturnValue; taint; df-generated | | ||
| nodes | ||
| | Test.cs:15:16:15:20 | access to local variable input : String | semmle.label | access to local variable input : String | | ||
| | Test.cs:15:24:15:50 | access to property QueryString : NameValueCollection | semmle.label | access to property QueryString : NameValueCollection | | ||
| | Test.cs:15:24:15:59 | access to indexer : String | semmle.label | access to indexer : String | | ||
| | Test.cs:16:49:16:53 | access to local variable input | semmle.label | access to local variable input | | ||
| subpaths |
4 changes: 4 additions & 0 deletions
4
...p/ql/test/query-tests/Security Features/CWE-117-JsonLoggingBuiltInUnsafe/LogForging.qlref
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| query: Security Features/CWE-117/LogForging.ql | ||
| postprocess: | ||
| - utils/test/PrettyPrintModels.ql | ||
| - utils/test/InlineExpectationsTestQuery.ql |
18 changes: 18 additions & 0 deletions
18
csharp/ql/test/query-tests/Security Features/CWE-117-JsonLoggingBuiltInUnsafe/Test.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| using Microsoft.AspNetCore.Mvc; | ||
| using Microsoft.Extensions.Logging; | ||
| using System.Web; | ||
|
|
||
| class MixedBuiltInLoggingController : ControllerBase | ||
| { | ||
| public void Configure() | ||
| { | ||
| LoggerFactory.Create(logging => logging.ClearProviders().AddJsonConsole()); | ||
| LoggerFactory.Create(logging => logging.AddConsole()); | ||
| } | ||
|
|
||
| public void Action(HttpContext context, ILogger logger) | ||
| { | ||
| string input = context.Request.QueryString["input"]; // $ Source | ||
| logger.LogInformation("Input: {Input}", input); // $ Alert | ||
| } | ||
| } |
3 changes: 3 additions & 0 deletions
3
csharp/ql/test/query-tests/Security Features/CWE-117-JsonLoggingBuiltInUnsafe/options
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| semmle-extractor-options: /nostdlib /noconfig | ||
| 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 |
4 changes: 4 additions & 0 deletions
4
csharp/ql/test/query-tests/Security Features/CWE-117-JsonLoggingHostSafe/LogForging.expected
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| #select | ||
| edges | ||
| nodes | ||
| subpaths |
4 changes: 4 additions & 0 deletions
4
csharp/ql/test/query-tests/Security Features/CWE-117-JsonLoggingHostSafe/LogForging.qlref
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| query: Security Features/CWE-117/LogForging.ql | ||
| postprocess: | ||
| - utils/test/PrettyPrintModels.ql | ||
| - utils/test/InlineExpectationsTestQuery.ql |
24 changes: 24 additions & 0 deletions
24
csharp/ql/test/query-tests/Security Features/CWE-117-JsonLoggingHostSafe/Test.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| using Microsoft.AspNetCore.Builder; | ||
| using Microsoft.Extensions.Logging; | ||
| using Microsoft.Extensions.Hosting; | ||
| using System.Web; | ||
|
|
||
| class JsonHost | ||
| { | ||
| public void Configure() | ||
| { | ||
| var builder = WebApplication.CreateBuilder(); | ||
| builder.Logging.ClearProviders(); | ||
| builder.Logging.AddJsonConsole(); | ||
|
|
||
| var genericHost = new HostApplicationBuilder(); | ||
| genericHost.Logging.ClearProviders(); | ||
| genericHost.Logging.AddJsonConsole(); | ||
| } | ||
|
|
||
| public void Action(HttpContext context, ILogger logger) | ||
| { | ||
| string input = context.Request.QueryString["input"]; | ||
| logger.LogInformation("Input: {Input}", input); | ||
| } | ||
| } |
3 changes: 3 additions & 0 deletions
3
csharp/ql/test/query-tests/Security Features/CWE-117-JsonLoggingHostSafe/options
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| semmle-extractor-options: /nostdlib /noconfig | ||
| 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 |
15 changes: 15 additions & 0 deletions
15
...st/query-tests/Security Features/CWE-117-JsonLoggingSerilogForwarding/LogForging.expected
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| #select | ||
| | Test.cs:18:49:18:53 | access to local variable input | Test.cs:17:24:17:50 | access to property QueryString : NameValueCollection | Test.cs:18:49:18:53 | access to local variable input | This log entry depends on a $@. | Test.cs:17:24:17:50 | access to property QueryString | user-provided value | | ||
| edges | ||
| | Test.cs:17:16:17:20 | access to local variable input : String | Test.cs:18:49:18:53 | access to local variable input | provenance | | | ||
| | Test.cs:17:24:17:50 | access to property QueryString : NameValueCollection | Test.cs:17:16:17:20 | access to local variable input : String | provenance | | | ||
| | Test.cs:17:24:17:50 | access to property QueryString : NameValueCollection | Test.cs:17:24:17:59 | access to indexer : String | provenance | MaD:1 | | ||
| | Test.cs:17:24:17:59 | access to indexer : String | Test.cs:17:16:17:20 | access to local variable input : String | provenance | | | ||
| models | ||
| | 1 | Summary: System.Collections.Specialized; NameValueCollection; false; get_Item; (System.String); ; Argument[this]; ReturnValue; taint; df-generated | | ||
| nodes | ||
| | Test.cs:17:16:17:20 | access to local variable input : String | semmle.label | access to local variable input : String | | ||
| | Test.cs:17:24:17:50 | access to property QueryString : NameValueCollection | semmle.label | access to property QueryString : NameValueCollection | | ||
| | Test.cs:17:24:17:59 | access to indexer : String | semmle.label | access to indexer : String | | ||
| | Test.cs:18:49:18:53 | access to local variable input | semmle.label | access to local variable input | | ||
| subpaths |
4 changes: 4 additions & 0 deletions
4
.../test/query-tests/Security Features/CWE-117-JsonLoggingSerilogForwarding/LogForging.qlref
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| query: Security Features/CWE-117/LogForging.ql | ||
| postprocess: | ||
| - utils/test/PrettyPrintModels.ql | ||
| - utils/test/InlineExpectationsTestQuery.ql |
20 changes: 20 additions & 0 deletions
20
csharp/ql/test/query-tests/Security Features/CWE-117-JsonLoggingSerilogForwarding/Test.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using Microsoft.Extensions.Logging; | ||
| using Serilog; | ||
| using Serilog.Formatting.Json; | ||
| using System.Web; | ||
|
|
||
| class ForwardingLogging | ||
| { | ||
| public void Configure(IServiceCollection services) | ||
| { | ||
| services.AddSerilog(configuration => configuration.WriteTo.Console(new JsonFormatter()), | ||
| writeToProviders: true); | ||
| } | ||
|
|
||
| public void Action(HttpContext context, Microsoft.Extensions.Logging.ILogger logger) | ||
| { | ||
| string input = context.Request.QueryString["input"]; // $ Source | ||
| logger.LogInformation("Input: {Input}", input); // $ Alert | ||
| } | ||
| } |
4 changes: 4 additions & 0 deletions
4
csharp/ql/test/query-tests/Security Features/CWE-117-JsonLoggingSerilogForwarding/options
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| semmle-extractor-options: /nostdlib /noconfig | ||
| semmle-extractor-options: --load-sources-from-project:../../../resources/stubs/_frameworks/Microsoft.AspNetCore.App/Microsoft.AspNetCore.App.csproj | ||
| semmle-extractor-options: --load-sources-from-project:../../../resources/stubs/Serilog/Serilog.csproj | ||
| semmle-extractor-options: ${testdir}/../../../resources/stubs/System.Web.cs |
18 changes: 18 additions & 0 deletions
18
.../ql/test/query-tests/Security Features/CWE-117-JsonLoggingSerilogSafe/LogForging.expected
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| #select | ||
| | Test.cs:65:35:65:39 | access to local variable input | Test.cs:55:24:55:50 | access to property QueryString : NameValueCollection | Test.cs:65:35:65:39 | access to local variable input | This log entry depends on a $@. | Test.cs:55:24:55:50 | access to property QueryString | user-provided value | | ||
| | Test.cs:66:50:66:54 | access to local variable input | Test.cs:55:24:55:50 | access to property QueryString : NameValueCollection | Test.cs:66:50:66:54 | access to local variable input | This log entry depends on a $@. | Test.cs:55:24:55:50 | access to property QueryString | user-provided value | | ||
| edges | ||
| | Test.cs:55:16:55:20 | access to local variable input : String | Test.cs:65:35:65:39 | access to local variable input | provenance | | | ||
| | Test.cs:55:16:55:20 | access to local variable input : String | Test.cs:66:50:66:54 | access to local variable input | provenance | | | ||
| | Test.cs:55:24:55:50 | access to property QueryString : NameValueCollection | Test.cs:55:16:55:20 | access to local variable input : String | provenance | | | ||
| | Test.cs:55:24:55:50 | access to property QueryString : NameValueCollection | Test.cs:55:24:55:59 | access to indexer : String | provenance | MaD:1 | | ||
| | Test.cs:55:24:55:59 | access to indexer : String | Test.cs:55:16:55:20 | access to local variable input : String | provenance | | | ||
| models | ||
| | 1 | Summary: System.Collections.Specialized; NameValueCollection; false; get_Item; (System.String); ; Argument[this]; ReturnValue; taint; df-generated | | ||
| nodes | ||
| | Test.cs:55:16:55:20 | access to local variable input : String | semmle.label | access to local variable input : String | | ||
| | Test.cs:55:24:55:50 | access to property QueryString : NameValueCollection | semmle.label | access to property QueryString : NameValueCollection | | ||
| | Test.cs:55:24:55:59 | access to indexer : String | semmle.label | access to indexer : String | | ||
| | Test.cs:65:35:65:39 | access to local variable input | semmle.label | access to local variable input | | ||
| | Test.cs:66:50:66:54 | access to local variable input | semmle.label | access to local variable input | | ||
| subpaths |
4 changes: 4 additions & 0 deletions
4
csharp/ql/test/query-tests/Security Features/CWE-117-JsonLoggingSerilogSafe/LogForging.qlref
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| query: Security Features/CWE-117/LogForging.ql | ||
| postprocess: | ||
| - utils/test/PrettyPrintModels.ql | ||
| - utils/test/InlineExpectationsTestQuery.ql |
68 changes: 68 additions & 0 deletions
68
csharp/ql/test/query-tests/Security Features/CWE-117-JsonLoggingSerilogSafe/Test.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| using System; | ||
| using System.Diagnostics; | ||
| using System.Web; | ||
| using Microsoft.AspNetCore.Mvc; | ||
| using Microsoft.AspNetCore.Builder; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using Microsoft.Extensions.Logging; | ||
| using Serilog; | ||
| using Serilog.Formatting.Json; | ||
| using Serilog.Formatting.Compact; | ||
| using Company; | ||
|
|
||
| namespace Custom | ||
| { | ||
| class ILogger | ||
| { | ||
| public void Warn(string message) { } | ||
| } | ||
| } | ||
|
|
||
| class JsonLoggingController : ControllerBase | ||
| { | ||
| public void Configure() | ||
| { | ||
| Log.Logger = new LoggerConfiguration() | ||
| .WriteTo.Async(write => write.Console(new JsonFormatter())) | ||
| .CreateBootstrapLogger(); | ||
|
|
||
| var builder = WebApplication.CreateBuilder(); | ||
| builder.AddCompanyDefaults(); | ||
| builder.Services.AddBusinessServices(); | ||
| builder.Services.AddSerilog((provider, configuration) => configuration | ||
| .Enrich.FromLogContext() | ||
| .Enrich.WithProperty("Application", "test") | ||
| .WriteTo.Console(new JsonFormatter()) | ||
| .WriteTo.Console(new JsonFormatter(closingDelimiter: null, renderMessage: true)) | ||
| .WriteTo.Console(new CompactJsonFormatter()) | ||
| .WriteTo.Console(new RenderedCompactJsonFormatter(new JsonValueFormatter())) | ||
| .WriteTo.File(new JsonFormatter(), "safe.log") | ||
| .AuditTo.Console(new JsonFormatter()), | ||
| preserveStaticLogger: false, | ||
| writeToProviders: false); | ||
| var app = builder.Build(); | ||
| app.MapGet("/", (Microsoft.AspNetCore.Http.HttpContext context, | ||
| Microsoft.Extensions.Logging.ILogger<JsonLoggingController> logger) => | ||
| { | ||
| string input = context.Request.Query["input"].ToString(); | ||
| logger.LogInformation("Input: {Input}", input); | ||
| return input; | ||
| }); | ||
| } | ||
|
|
||
| public void Action(HttpContext context, Microsoft.Extensions.Logging.ILogger logger, Serilog.ILogger serilog) | ||
| { | ||
| string input = context.Request.QueryString["input"]; // $ Source | ||
| logger.LogInformation("Input: {Input}", input); | ||
| logger.LogInformation($"Input: {input}"); | ||
| serilog.Information("Input: {Input}", input); | ||
|
|
||
| var concrete = new LoggerConfiguration() | ||
| .WriteTo.Console(new JsonFormatter()) | ||
| .CreateLogger(); | ||
| concrete.Information("Input: {Input}", input); | ||
|
|
||
| new Custom.ILogger().Warn(input); // $ Alert | ||
| new TraceSource("test").TraceInformation(input); // $ Alert | ||
| } | ||
| } |
4 changes: 4 additions & 0 deletions
4
csharp/ql/test/query-tests/Security Features/CWE-117-JsonLoggingSerilogSafe/options
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| semmle-extractor-options: /nostdlib /noconfig | ||
| semmle-extractor-options: --load-sources-from-project:../../../resources/stubs/_frameworks/Microsoft.AspNetCore.App/Microsoft.AspNetCore.App.csproj | ||
| semmle-extractor-options: --load-sources-from-project:../../../resources/stubs/Serilog/Serilog.csproj | ||
| semmle-extractor-options: ${testdir}/../../../resources/stubs/System.Web.cs |
15 changes: 15 additions & 0 deletions
15
...l/test/query-tests/Security Features/CWE-117-JsonLoggingSerilogUnsafe/LogForging.expected
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| #select | ||
| | Test.cs:23:49:23:53 | access to local variable input | Test.cs:22:24:22:50 | access to property QueryString : NameValueCollection | Test.cs:23:49:23:53 | access to local variable input | This log entry depends on a $@. | Test.cs:22:24:22:50 | access to property QueryString | user-provided value | | ||
| edges | ||
| | Test.cs:22:16:22:20 | access to local variable input : String | Test.cs:23:49:23:53 | access to local variable input | provenance | | | ||
| | Test.cs:22:24:22:50 | access to property QueryString : NameValueCollection | Test.cs:22:16:22:20 | access to local variable input : String | provenance | | | ||
| | Test.cs:22:24:22:50 | access to property QueryString : NameValueCollection | Test.cs:22:24:22:59 | access to indexer : String | provenance | MaD:1 | | ||
| | Test.cs:22:24:22:59 | access to indexer : String | Test.cs:22:16:22:20 | access to local variable input : String | provenance | | | ||
| models | ||
| | 1 | Summary: System.Collections.Specialized; NameValueCollection; false; get_Item; (System.String); ; Argument[this]; ReturnValue; taint; df-generated | | ||
| nodes | ||
| | Test.cs:22:16:22:20 | access to local variable input : String | semmle.label | access to local variable input : String | | ||
| | Test.cs:22:24:22:50 | access to property QueryString : NameValueCollection | semmle.label | access to property QueryString : NameValueCollection | | ||
| | Test.cs:22:24:22:59 | access to indexer : String | semmle.label | access to indexer : String | | ||
| | Test.cs:23:49:23:53 | access to local variable input | semmle.label | access to local variable input | | ||
| subpaths |
4 changes: 4 additions & 0 deletions
4
...p/ql/test/query-tests/Security Features/CWE-117-JsonLoggingSerilogUnsafe/LogForging.qlref
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| query: Security Features/CWE-117/LogForging.ql | ||
| postprocess: | ||
| - utils/test/PrettyPrintModels.ql | ||
| - utils/test/InlineExpectationsTestQuery.ql |
25 changes: 25 additions & 0 deletions
25
csharp/ql/test/query-tests/Security Features/CWE-117-JsonLoggingSerilogUnsafe/Test.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| using Microsoft.AspNetCore.Mvc; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using Microsoft.Extensions.Logging; | ||
| using Serilog; | ||
| using Serilog.Formatting.Json; | ||
| using System.Web; | ||
|
|
||
| class UnsafeJsonLoggingController : ControllerBase | ||
| { | ||
| public void Configure(IServiceCollection services, | ||
| Microsoft.Extensions.Configuration.IConfiguration externalConfiguration) | ||
| { | ||
| services.AddSerilog(configuration => configuration | ||
| .WriteTo.Console(new JsonFormatter())); | ||
|
|
||
| // A discovered external configuration reader vetoes the database-wide optimization. | ||
| new LoggerConfiguration().ReadFrom.Configuration(externalConfiguration).CreateLogger(); | ||
| } | ||
|
|
||
| public void Action(HttpContext context, Microsoft.Extensions.Logging.ILogger logger) | ||
| { | ||
| string input = context.Request.QueryString["input"]; // $ Source | ||
| logger.LogInformation("Input: {Input}", input); // $ Alert | ||
| } | ||
| } |
4 changes: 4 additions & 0 deletions
4
csharp/ql/test/query-tests/Security Features/CWE-117-JsonLoggingSerilogUnsafe/options
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| semmle-extractor-options: /nostdlib /noconfig | ||
| semmle-extractor-options: --load-sources-from-project:../../../resources/stubs/_frameworks/Microsoft.AspNetCore.App/Microsoft.AspNetCore.App.csproj | ||
| semmle-extractor-options: --load-sources-from-project:../../../resources/stubs/Serilog/Serilog.csproj | ||
| semmle-extractor-options: ${testdir}/../../../resources/stubs/System.Web.cs |
15 changes: 15 additions & 0 deletions
15
...est/query-tests/Security Features/CWE-117-JsonLoggingStandaloneLogger/LogForging.expected
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| #select | ||
| | Test.cs:17:49:17:53 | access to local variable input | Test.cs:16:24:16:50 | access to property QueryString : NameValueCollection | Test.cs:17:49:17:53 | access to local variable input | This log entry depends on a $@. | Test.cs:16:24:16:50 | access to property QueryString | user-provided value | | ||
| edges | ||
| | Test.cs:16:16:16:20 | access to local variable input : String | Test.cs:17:49:17:53 | access to local variable input | provenance | | | ||
| | Test.cs:16:24:16:50 | access to property QueryString : NameValueCollection | Test.cs:16:16:16:20 | access to local variable input : String | provenance | | | ||
| | Test.cs:16:24:16:50 | access to property QueryString : NameValueCollection | Test.cs:16:24:16:59 | access to indexer : String | provenance | MaD:1 | | ||
| | Test.cs:16:24:16:59 | access to indexer : String | Test.cs:16:16:16:20 | access to local variable input : String | provenance | | | ||
| models | ||
| | 1 | Summary: System.Collections.Specialized; NameValueCollection; false; get_Item; (System.String); ; Argument[this]; ReturnValue; taint; df-generated | | ||
| nodes | ||
| | Test.cs:16:16:16:20 | access to local variable input : String | semmle.label | access to local variable input : String | | ||
| | Test.cs:16:24:16:50 | access to property QueryString : NameValueCollection | semmle.label | access to property QueryString : NameValueCollection | | ||
| | Test.cs:16:24:16:59 | access to indexer : String | semmle.label | access to indexer : String | | ||
| | Test.cs:17:49:17:53 | access to local variable input | semmle.label | access to local variable input | | ||
| subpaths |
4 changes: 4 additions & 0 deletions
4
...l/test/query-tests/Security Features/CWE-117-JsonLoggingStandaloneLogger/LogForging.qlref
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| query: Security Features/CWE-117/LogForging.ql | ||
| postprocess: | ||
| - utils/test/PrettyPrintModels.ql | ||
| - utils/test/InlineExpectationsTestQuery.ql |
19 changes: 19 additions & 0 deletions
19
csharp/ql/test/query-tests/Security Features/CWE-117-JsonLoggingStandaloneLogger/Test.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| using Microsoft.Extensions.Logging; | ||
| using Serilog; | ||
| using Serilog.Formatting.Json; | ||
| using System.Web; | ||
|
|
||
| class StandaloneLogger | ||
| { | ||
| public void Configure() | ||
| { | ||
| // A safe static logger does not establish the configuration of an injected logger. | ||
| Log.Logger = new LoggerConfiguration().WriteTo.Console(new JsonFormatter()).CreateLogger(); | ||
| } | ||
|
|
||
| public void Action(HttpContext context, Microsoft.Extensions.Logging.ILogger logger) | ||
| { | ||
| string input = context.Request.QueryString["input"]; // $ Source | ||
| logger.LogInformation("Input: {Input}", input); // $ Alert | ||
| } | ||
| } |
4 changes: 4 additions & 0 deletions
4
csharp/ql/test/query-tests/Security Features/CWE-117-JsonLoggingStandaloneLogger/options
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| semmle-extractor-options: /nostdlib /noconfig | ||
| semmle-extractor-options: --load-sources-from-project:../../../resources/stubs/_frameworks/Microsoft.AspNetCore.App/Microsoft.AspNetCore.App.csproj | ||
| semmle-extractor-options: --load-sources-from-project:../../../resources/stubs/Serilog/Serilog.csproj | ||
| semmle-extractor-options: ${testdir}/../../../resources/stubs/System.Web.cs |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In some of the tests there is a description of what is being tested; I would prefer that there is a pretty elborate description in all of them - as the test-cases are now somewhat more complicated.