--- title: Prevent Cross-Site Scripting (XSS) in ASP.NET Core author: tdykstra description: Learn about Cross-Site Scripting (XSS) and techniques for addressing this vulnerability in an ASP.NET Core app. ms.author: tdykstra monikerRange: '>= aspnetcore-3.1' ms.date: 2/15/2020 uid: security/cross-site-scripting --- # Prevent Cross-Site Scripting (XSS) in ASP.NET Core By [Rick Anderson](https://twitter.com/RickAndMSFT) Cross-Site Scripting (XSS) is a security vulnerability that enables a cyberattacker to place client side scripts (usually JavaScript) into web pages. When other users load affected pages, the cyberattacker's scripts run, enabling the cyberattacker to steal cookies and session tokens, change the contents of the web page through DOM manipulation, or redirect the browser to another page. XSS vulnerabilities generally occur when an application takes user input and outputs it to a page without validating, encoding or escaping it. This article applies primarily to ASP.NET Core MVC with views, Razor Pages, and other apps that return HTML that may be vulnerable to XSS. Web APIs that return data in the form of HTML, XML, or JSON can trigger XSS attacks in their client apps if they don't properly sanitize user input, depending on how much trust the client app places in the API. For example, if an API accepts user-generated content and returns it in an HTML response, a cyberattacker could inject malicious scripts into the content that executes when the response is rendered in the user's browser. To prevent XSS attacks, web APIs should implement input validation and output encoding. Input validation ensures that user input meets expected criteria and doesn't include malicious code. Output encoding ensures that any data returned by the API is properly sanitized so that it can't be executed as code by the user's browser. For more information, see [this GitHub issue](https://github.com/dotnet/AspNetCore.Docs/issues/28789). ## Protecting your application against XSS At a basic level, XSS works by tricking your application into inserting a `"; }
``` The preceding markup generates the following HTML: ```html ``` The preceding code generates the following output: ``` ``` >[!WARNING] > Do ***NOT*** concatenate untrusted input in JavaScript to create DOM elements or use `document.write()` on dynamically generated content. > > Use one of the following approaches to prevent code from being exposed to DOM-based XSS: > * `createElement()` and assign property values with appropriate methods or properties such as `node.textContent=` or `node.InnerText=`. > * `document.CreateTextNode()` and append it in the appropriate DOM location. > * `element.SetAttribute()` > * `element[attribute]=` ## Accessing encoders in code The HTML, JavaScript and URL encoders are available to your code in two ways: * Inject them via [dependency injection](xref:fundamentals/dependency-injection). * Use the default encoders contained in the `System.Text.Encodings.Web` namespace. When using the default encoders, then any customizations applied to character ranges to be treated as safe won't take effect. The default encoders use the safest encoding rules possible. To use the configurable encoders via DI your constructors should take an *HtmlEncoder*, *JavaScriptEncoder* and *UrlEncoder* parameter as appropriate. For example; ```csharp public class HomeController : Controller { HtmlEncoder _htmlEncoder; JavaScriptEncoder _javaScriptEncoder; UrlEncoder _urlEncoder; public HomeController(HtmlEncoder htmlEncoder, JavaScriptEncoder javascriptEncoder, UrlEncoder urlEncoder) { _htmlEncoder = htmlEncoder; _javaScriptEncoder = javascriptEncoder; _urlEncoder = urlEncoder; } } ``` ## Encoding URL Parameters If you want to build a URL query string with untrusted input as a value use the `UrlEncoder` to encode the value. For example, ```csharp var example = "\"Quoted Value with spaces and &\""; var encodedValue = _urlEncoder.Encode(example); ``` After encoding the encodedValue variable contains `%22Quoted%20Value%20with%20spaces%20and%20%26%22`. Spaces, quotes, punctuation and other unsafe characters are percent encoded to their hexadecimal value, for example a space character will become %20. >[!WARNING] > Don't use untrusted input as part of a URL path. Always pass untrusted input as a query string value. ## Customizing the Encoders By default encoders use a safe list limited to the Basic Latin Unicode range and encode all characters outside of that range as their character code equivalents. This behavior also affects Razor TagHelper and HtmlHelper rendering as it uses the encoders to output your strings. The reasoning behind this is to protect against unknown or future browser bugs (previous browser bugs have tripped up parsing based on the processing of non-English characters). If your web site makes heavy use of non-Latin characters, such as Chinese, Cyrillic or others this is probably not the behavior you want. :::moniker range=">= aspnetcore-6.0" The encoder safe lists can be customized to include Unicode ranges appropriate to the app during startup, in `Program.cs`: For example, using the default configuration using a Razor HtmlHelper similar to the following: ```htmlThis link text is in Chinese: @Html.ActionLink("汉语/漢語", "Index")
``` The preceding markup is rendered with Chinese text encoded: ```htmlThis link text is in Chinese: 汉语/漢語
``` To widen the characters treated as safe by the encoder, insert the following line into `Program.cs`.: ```csharp builder.Services.AddSingletonThis link text is in Chinese: @Html.ActionLink("汉语/漢語", "Index")
``` When you view the source of the web page you'll see it has been rendered as follows, with the Chinese text encoded; ```htmlThis link text is in Chinese: 汉语/漢語
``` To widen the characters treated as safe by the encoder you would insert the following line into the `ConfigureServices()` method in `startup.cs`; ```csharp services.AddSingletonThis link text is in Chinese: 汉语/漢語
``` Safe list ranges are specified as Unicode code charts, not languages. The [Unicode standard](https://unicode.org/) has a list of [code charts](https://www.unicode.org/charts/index.html) you can use to find the chart containing your characters. Each encoder, Html, JavaScript and Url, must be configured separately. > [!NOTE] > Customization of the safe list only affects encoders sourced via DI. If you directly access an encoder via `System.Text.Encodings.Web.*Encoder.Default` then the default, Basic Latin only safelist will be used. ## Where should encoding take place? The general accepted practice is that encoding takes place at the point of output and encoded values should never be stored in a database. Encoding at the point of output allows you to change the use of data, for example, from HTML to a query string value. It also enables you to easily search your data without having to encode values before searching and allows you to take advantage of any changes or bug fixes made to encoders. ## Validation as an XSS prevention technique Validation can be a useful tool in limiting XSS attacks. For example, a numeric string containing only the characters 0-9 won't trigger an XSS attack. Validation becomes more complicated when accepting HTML in user input. Parsing HTML input is difficult, if not impossible. Markdown, coupled with a parser that strips embedded HTML, is a safer option for accepting rich input. Never rely on validation alone. Always encode untrusted input before output, no matter what validation or sanitization has been performed.