Cross-site scripting in ASP.NET Core HTML responses

Reflected XSS when ASP.NET Core returns request text directly as HTML

Description

ASP.NET Core's ControllerBase.Content, Results.Content and TypedResults.Content do not HTML-encode the supplied string. When request text is returned directly as text/html, the browser parses it as markup rather than data. An attacker can inject tags, event handlers or other executable markup and cause reflected cross-site scripting (XSS).

Input validation and output encoding are separate controls. Format checks can reduce the attack surface but cannot replace the handling required by HTML, attribute, URL, JavaScript and CSS contexts. A custom function named Sanitize, Encode or Clean is not sufficient evidence that its result is safe for the destination.

Potential impact

  • Theft of sessions or sensitive information
  • Phishing interfaces and modified page content
  • Requests or client-side actions using the victim's permissions
  • Malicious content delivered through a trusted site origin

Remediation

For ordinary text in a Razor view, write @value. Razor HTML-encodes strings. Html.Raw, HtmlString and other ways of supplying IHtmlContent can bypass encoding, so reserve them for trusted markup.

If the response needs no markup, return text/plain or structured data. When assembling HTML outside Razor, use WebUtility.HtmlEncode or HtmlEncoder.Default.Encode on the whole value just before writing it as ordinary element text. URLs, JavaScript and CSS need their own handling. To display data from JavaScript, pass it in an encoded data attribute or as JSON, then assign the text with textContent.

For user-authored HTML, choose a maintained sanitizer that allows only the markup appropriate for the insertion point. In the browser, use an up-to-date DOMPurify release immediately before insertion. Use the sanitized result as returned. Check browser support before relying on Element.setHTML().

Examples

Before

Controller response

csharp
using Microsoft.AspNetCore.Mvc;

public sealed class PreviewController : ControllerBase
{
    [HttpGet("/preview")]
    public IActionResult Preview([FromQuery] string html)
    {
        return Content(html, "text/html");
    }
}

Content returns the request's html as HTML without encoding it.

Minimal API response

csharp
using Microsoft.AspNetCore.Mvc;

app.MapGet("/preview", ([FromQuery] string html) =>
    Results.Content(html, "text/html"));

After

Recommended: ordinary Razor encoding

csharp
using Microsoft.AspNetCore.Mvc;

public sealed class PreviewController : Controller
{
    [HttpGet("/preview-name")]
    public IActionResult Preview([FromQuery] string name)
    {
        return View("Preview", model: name);
    }
}
html
<h1>@Model</h1>

@Model displays the name as text in the heading. Html.Raw(Model) bypasses Razor's encoding.

Recommended: responses without markup

csharp
public IActionResult Preview([FromQuery] string text)
{
    return Content(text, "text/plain");
}

Limited case: HTML text-node encoding

csharp
using System.Text.Encodings.Web;

public IActionResult Preview([FromQuery] string name)
{
    var encodedName = HtmlEncoder.Default.Encode(name);
    return Content($"<h1>{encodedName}</h1>", "text/html");
}

encodedName is encoded for an HTML text node, such as the text inside this <h1>. Scripts, styles, URLs and event-handler attributes each need separate handling.

When rich HTML is required

When the server delivers HTML as data, the browser can sanitize it just before assigning it to innerHTML.

javascript
const cleanHtml = DOMPurify.sanitize(untrustedHtml);
preview.innerHTML = cleanHtml;

Keep later templates and string operations from changing the sanitized HTML. For a server-side sanitizer, check maintenance, allowed elements, attributes and URL schemes, and the context where the result will be inserted.

References