ASP.NET Output Encoding

=Description= Cross-site scripting attacks exploit vulnerabilities in web page validation by injecting client-side script code. The script code embeds itself in response data, which is sent back to an unsuspecting user. In addition to validating input, any data retrieved from untrusted or shared sources should be encoded on output. For example: data retrieved from a database that may have had malicious input persisted to it.

=Encoding Output Values in Code= Use  to encode untrusted data for use in HTML output: var encodedHtml = Server.HtmlEncode(untrustedData);

Use  to encode untrusted data for use in the key/value pairs of a URL query string. var url = "https://www.bing.com/search?q=" + HttpUtility.UrlEncode(untrustedData);

=Encoding Output Values in HTML markup= Starting with ASP.NET 4.0 you can HTML encode values in markup with the  syntax, as shown below. <%: untrustedData %>

Or, in Razor syntax, you can HTML encode with, as shown below. @untrustedData

Starting with ASP.NET 4.5 you can also HTML encode the result of data-binding expressions. Just add a colon to the end of the <%# prefix that marks the data-binding expression:  <%#: Item.Products.Name %> 

=IHtmlString= If you have model properties that are used to display raw HTML you should consider using the  and   classes starting with .NET 4.0. These both implement the  interface and will instruct ASP.NET to skip output encoding when using   or   in HTML markup. Converting a property on your view model from  to   will instruct ASP.NET that HTML encoding has already been accounted for.

public class User {   public int Id { get; set; } public string Name { get; set; } public MvcHtmlString Description { get; set; } // Output encoding is handled manually }

=AntiXssEncoder= By default the ASP.NET encoding methods use a black-listing technique that evaluates the string for a set of character combinations that may indicate presence of malicious script. A superior approach is to use a white-listing technique for validation, which can be achieved using the Anti-Cross Site Scripting Library from Microsoft. Starting with ASP.NET 4.5 you can specify that the  from this library be used as the default encoder for you entire application using the   setting in web.config as shown below. 

If you are using a version of .NET earlier than 4.5, you will need to download and include the library as a reference to your project, and then use the earlier library name for the encodeType setting as shown below. 

In addition to the common  and   methods, the Anti-Cross Site Scripting Library provides the following   methods for more specialized output encoding needs:

CssEncode
Encodes the specified string for use in cascading style sheets (CSS). This method encodes all characters except those that are in the safe list, by using the CSS escape character (/) followed by up to six hexadecimal digits.

HtmlFormUrlEncode
Encodes the specified string for use in form submissions whose MIME type is "application/x-www-form-urlencoded". This method encodes all characters except those that are in the safe list. Characters are encoded by using %SINGLE_BYTE_HEX notation.

XmlAttributeEncode
Encodes the specified string for use in XML attributes, and is slightly more restrictive than XmlEncode below. This method encodes all characters except those that are in the safe list. Characters are encoded by using &#DECIMAL; notation.

XmlEncode
Encodes the specified string for use in XML. This method encodes all characters except those that are in the safe list. Characters are encoded by using &#DECIMAL; notation.

=References=
 * Output Encoding
 * HttpUtility Methods
 * Which ASP.NET Controls Automatically Encode?
 * New <%: %> Syntax for HTML Encoding Output in ASP.NET 4 (and ASP.NET MVC 2)
 * HTML Encoded Data-Binding Expressions
 * IHtmlString Interface
 * What is an MvcHtmlString and when should I use it?
 * Microsoft Anti-Cross Site Scripting Library V4.3
 * AntiXssEncoder Methods
 * ASP.NET Request Validation