Description
Log forging, or log injection, occurs when untrusted data contains characters that have special meaning in the output format. In line-oriented text logs, CR, LF or other line separators can create rows that resemble separate events. Delimited logs, terminals, HTML viewers and automated parsers have their own separators and control characters that can alter record structure or presentation.
Microsoft.Extensions.Logging message templates and named properties help retain structure and support searching, but they do not guarantee that every provider neutralizes newlines in property values. Check the final provider, transport format, collector and viewer together.
Potential impact
- Fabricated success or failure events can hide real activity.
- Damaged audit integrity and accountability can mislead investigations and alerts.
- Corrupted records or fields can cause missed events, processing failures or downstream injection.
- Unbounded input can exhaust log storage and collection capacity.
Remediation
Encode for the output format
Choose a provider that encodes values for its output format. The JSON console formatter uses Utf8JsonWriter for JSON strings. If you use AddJsonConsole, also check the output of other production providers and downstream transformations with control characters in the logged value.
Use a constant message template with named properties for structured logging, as CA2254 recommends. For line-oriented logs, also handle CR and LF in each value.
Neutralize line separators in text logs
On .NET 6 or later, String.ReplaceLineEndings(" ") replaces CR, LF, CRLF, NEL, LS, FF and PS with spaces. The parameterless ReplaceLineEndings() changes each break to the platform newline.
Replacing newlines in a logged value prevents that value from adding extra lines to a text log. CSV, terminal output and HTML viewers need encoding appropriate to their format. Check what a helper such as Sanitize actually handles.
For fixed event names such as LOGIN and LOGOUT, compare the value with a list of literals defined in code and log the value you checked. Keep external configuration and runtime changes out of the list. Limit value length and exclude passwords, tokens and cryptographic keys.
Examples
Before
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
[ApiController]
[Route("audit")]
public sealed class VulnerableAuditController : ControllerBase
{
private readonly ILogger<VulnerableAuditController> _logger;
public VulnerableAuditController(ILogger<VulnerableAuditController> logger)
{
_logger = logger;
}
[HttpPost]
public IActionResult Login([FromQuery] string user)
{
_logger.LogWarning("Failed login for {User}", user);
return Ok();
}
}
The first call passes user unchanged, including any line breaks.
After
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
[ApiController]
[Route("audit")]
public sealed class SafeAuditController : ControllerBase
{
private readonly ILogger<SafeAuditController> _logger;
public SafeAuditController(ILogger<SafeAuditController> logger)
{
_logger = logger;
}
[HttpPost]
public IActionResult Login([FromQuery] string user)
{
var safeUser = user.ReplaceLineEndings(" ");
_logger.LogWarning("Failed login for {User}", safeUser);
return Ok();
}
}
safeUser replaces line breaks with spaces before the value is logged as {User}.
References
- OWASP Logging Cheat Sheet
- OWASP Log Injection
- OWASP Top 10:2025 A09 - Security Logging and Alerting Failures
- OWASP Top 10:2021 A09 - Security Logging and Monitoring Failures
- OWASP ASVS 5.0.0 V16.4.1
- CWE-117: Improper Output Neutralization for Logs
- .NET
String.ReplaceLineEndings - .NET C# logging
- .NET console log formatting
- .NET CA2254: Template should be a static expression
- .NET 10
LoggerExtensionssource - .NET 10
JsonConsoleFormattersource