Skip to content

Commit

Permalink
log forging example
Browse files Browse the repository at this point in the history
  • Loading branch information
felickz committed Sep 26, 2024
1 parent 93b8a66 commit 71d6ac8
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
27 changes: 27 additions & 0 deletions LogForgingMiddleware.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using System;
using System.Threading.Tasks;

public class LogForgingMiddleware
{
private readonly RequestDelegate _next;
private readonly ILogger<LogForgingMiddleware> _logger;

public LogForgingMiddleware(RequestDelegate next, ILogger<LogForgingMiddleware> logger)
{
_next = next;
_logger = logger;
}

public async Task InvokeAsync(HttpContext context)
{
string username = context.Request.Query["username"];
// BAD: User input logged as-is
_logger.LogWarning(username + " log in requested.");

Check failure

Code scanning / CodeQL

Log entries created from user input High

This log entry depends on a
user-provided value
.
// GOOD: User input logged with new-lines removed
_logger.LogWarning(username?.Replace(Environment.NewLine, "") + " log in requested");

await _next(context);
}
}
1 change: 1 addition & 0 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,6 @@
app.UseAuthorization();

app.MapControllers();
app.UseMiddleware<LogForgingMiddleware>();

app.Run();

0 comments on commit 71d6ac8

Please sign in to comment.