Visitar URL original
Console logger should sanitize control characters in log messages · Issue #128727 · dotnet/runtime · GitHub
Skip to content

Console logger should sanitize control characters in log messages #128727

Closed

Description

@halter73

Summary

The built-in console logger (Microsoft.Extensions.Logging.Console) does not escape
or strip control characters (e.g., ANSI escape sequences, carriage returns, newlines,
backspaces) that may be present in logged values. This means that untrusted data flowing
through ILogger.Log(...) can emit arbitrary terminal control sequences to the console.

Problem

When applications log user-controlled input — such as HTTP request headers, query
strings, or form values — any embedded control characters are written verbatim to the
terminal. This can lead to:

  • Terminal manipulation — an attacker-controlled string can rewrite previously
    displayed lines, hide output, or alter colors to mislead an operator reading logs.
  • Confusing log output — control characters can break log parsing tools, corrupt
    structured log viewers, or cause unexpected behavior in CI/CD pipelines that consume
    stdout.

This affects all console formatters (Simple, Systemd, Json) that write to
System.Console / stdout without sanitizing the message or state values.

Reproduction

var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();

app.MapGet("/", (HttpContext ctx) =>
{
    var userInput = ctx.Request.Query["name"].ToString();
    app.Logger.LogInformation("Hello {Name}", userInput);
    return "OK";
});

app.Run();

Request: GET /?name=%1b%5b31mRED%20TEXT%1b%5b0m

The console output renders "RED TEXT" in red (or other terminal-dependent behavior)
rather than showing the escaped representation.

Proposed Solution

The console formatters should sanitize messages before writing to the output stream.
Control characters (Unicode categories Cc/Cf, excluding common whitespace like \n and
\t when they are part of the formatter's own layout) should be stripped or replaced
with an escape representation (e.g., \x1B or ␛).

This would be a defense-in-depth improvement — applications should already validate
input, but the logging infrastructure should not blindly pass control sequences to the
terminal.

Considerations

  • This is a behavioral change in the formatter output, so it may warrant an opt-out
    switch (e.g., ConsoleFormatterOptions.SanitizeControlCharacters = true by default).
  • The JsonConsoleFormatter already JSON-encodes strings, but should still sanitize
    values written outside of JSON payloads (e.g., timestamp prefixes from state values).
  • HttpLoggingMiddleware in ASP.NET Core is one specific caller that logs request/
    response data, but the fix belongs in the logging infrastructure itself so all
    consumers benefit.

Activity

  1. dotnet-policy-service commented on May 28, 2026

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @dotnet/area-extensions-logging
    See info in area-owners.md if you want to be subscribed.

  2. removed
    untriagedNew issue has not been triaged by the area owner
    on May 29, 2026
  3. svick commented on May 29, 2026

    @svick
    Member

    @punchready Do you have concerns about this issue that you want to voice?

  4. BrennanConroy commented on Jun 15, 2026

    @BrennanConroy
    Member

    @rosebyte can we unblock this? Looks like we probably need a new API for the option to opt-out or in to the new sanitize behavior. I would prefer sanitize by default, but understand it's a breaking change so might have push-back.

    Also looks like there needs to be a few characters that aren't escaped, like newlines and tabs.

  5. rosebyte commented on Jun 17, 2026

    @rosebyte
    Member

    @BrennanConroy, @halter73, to my understanding, all control characters except \t, \r, and \n are unexpected in logs and can be escaped. Moreover, I'd consider this more of a security vulnerability fix than a general breaking change (although we should mention it in the release notes), so I think we all agree that an opt-in approach wouldn't be sufficient.

    All the formatters are considered opinionated defaults that users can replace with custom formatters if they need different behaviour, so I assume we should keep this flow opt-out as well (although I don't see why anyone would want those characters to remain unescaped in logs).

  6. added a commit that references this issue on Jun 26, 2026
    cc41131
  7. added this to the 11.0.0 milestone on Jul 1, 2026
  8. added a commit that references this issue on Jul 15, 2026
    9c658c4
  9. locked and limited conversation to collaborators on Aug 1, 2026
  10. rosebyte commented on Aug 26, 2026

    @rosebyte
    Member

    Added breaking change issue dotnet/docs#55741

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions