Uncontrolled composite format strings in C#

External input controls a .NET composite format string

Description

.NET composite formatting interprets items in the form {index[,alignment][:formatString]}. The format selects which supplied arguments appear, how often they appear, and their alignment and individual formatting. An attacker can control this output structure if external input becomes the composite format argument to String.Format, StringBuilder.AppendFormat, TextWriter.Write or WriteLine, or Console.Write or WriteLine.

The impact differs from C printf vulnerabilities. Managed .NET composite formatting has no memory-write directive like %n and does not read arbitrary values from the call stack. Do not attribute C/C++ arbitrary memory access or direct code execution to this condition.

Potential impact

  • Changed or repeated indices can expose string representations of other arguments passed to the same call, including sensitive values supplied as format arguments.
  • Unclosed braces, out-of-range indices or invalid items can throw FormatException. Unhandled exceptions can cause failed requests or repeated service disruption.
  • Very large alignment widths can produce large output and consume memory. A custom IFormatProvider or ICustomFormatter may also expose application-specific formatting paths to the caller's choices.
  • Altered logs, responses or notifications can mislead downstream parsers or operators when the output informs security decisions or operational procedures.

Remediation

  1. Define the format in code and pass outside text as a value. A developer-written interpolated string also keeps the format fixed.
  2. To offer display styles, map IDs such as compact or labelled to fixed format strings. If callers must submit a whole format, allow only exact matches to a small set of literals and limit argument types and output size. Prefix and substring checks accept other formats too.
  3. CompositeFormat.Parse checks syntax and returns a reusable parsed format. A valid format still determines which arguments appear and how they are displayed.
  4. Removing only opening or closing braces can leave malformed formats. Removing both kinds changes the text. Use a fixed format even if a helper is named Sanitize.

Examples

Before

csharp
using Microsoft.AspNetCore.Mvc;

[ApiController]
[Route("format")]
public sealed class FormatController : ControllerBase
{
    [HttpGet]
    public string Render(
        [FromQuery] string format,
        [FromQuery] string displayName)
    {
        string internalReference = HttpContext.TraceIdentifier;
        return string.Format(format, displayName, internalReference);
    }
}

Here the caller controls which arguments appear, including internalReference. Invalid braces or an out-of-range argument index cause FormatException.

After

Literal format

csharp
using System.Globalization;

return string.Format(
    CultureInfo.InvariantCulture,
    "사용자: {0}",
    displayName);

The application fixes the format and culture. To print a single string as ordinary text, use Console.WriteLine(message) or TextWriter.Write(message).

Predefined format selection

csharp
using System.Globalization;

static string SelectFormat(string style) => style switch
{
    "compact" => "{0}",
    "labelled" => "사용자: {0}",
    _ => "사용자: {0}"
};

string format = SelectFormat(style);
return string.Format(CultureInfo.InvariantCulture, format, displayName);

SelectFormat returns a literal for every style, including the default.

Check argument counts and types and review custom formatters. CA2241 and tests can help.

References