Using AntiXss As The Default Encoder For ASP.NET

This is the third in a three part series related to HTML encoding blocks, aka the <%: ... %> syntax.

Scott Guthrie recently wrote about the new <%: %> syntax for HTML encoding output in ASP.NET 4. I also covered the topic of HTML encoding code nuggets in the past as well providing some insight into our design choices for the approach we took.

A commenter to Scott’s blog post asked,

Will it be possible to extend this so that is uses libraries like AntiXSS instead? See: http://antixss.codeplex.com/

The answer is yes!

ASP.NET 4 includes a new extensibility point which allows you to replace the default encoding logic with your own anywhere ASP.NET does encoding.

All it requires is to write a class which derives from System.Web.Util.HttpEncoder and register that class in Web.config via the encoderType attribute of the httpRuntime element.

Walkthrough

In the following section, I’ll walk you through setting this up. First, you’re going to need to download the AntiXSS library which is at version 3.1 at the time of this writing. On my machine, that dropped the AntiXSSLibrary.dll file at the following location: C:\Program Files (x86)\Microsoft Information Security\Microsoft Anti-Cross Site Scripting Library v3.1\Library

Create a new ASP.NET MVC application (note, this works for *any* ASP.NET application). Copy the assembly into the project directory somewhere where you’ll be able to find it. I typically have a “lib” folder or a “Dependencies” folder for this purpose. Right clicke on the References node of the project to add a reference to the assembly.

add-reference Add-Reference-dialogThe next step is to write a class that derives from HttpEncoder. Note that in the following listing, some methods were excluded which are included in the project.

using System;
using System.IO;
using System.Web.Util;
using Microsoft.Security.Application;

/// <summary>
/// Summary description for AntiXss
/// </summary>
public class AntiXssEncoder : HttpEncoder
{
  public AntiXssEncoder() { }

  protected override void HtmlEncode(string value, TextWriter output)
  {
    output.Write(AntiXss.HtmlEncode(value));
  }

  protected override void HtmlAttributeEncode(string value, TextWriter output)
  {
    output.Write(AntiXss.HtmlAttributeEncode(value));
  }

  protected override void HtmlDecode(string value, TextWriter output)
  {
      base.HtmlDecode(value, output);
  }

  // Some code omitted but included in the sample
}

Finally, register the type in web.config.

...
  <system.web>
    <httpRuntime encoderType="AntiXssEncoder, AssemblyName"/>
...

Note that you’ll need to replace AssemblyName with the actual name of your assembly. Also, in the sample included with this blog post, AntiXssEncoder is not in any namespace. If you put your encoder in a namespace, you’ll need to make sure to provide the fully qualified type name.

To prove that this is working, run the project in the debugger and set a breakpoint in the encoding method.

debugger-breakpoint

With that, you are all set to take full control over how strings are encoded in your application.

Note that Scott Hanselman and I gave a live demonstration of setting this up at Mix 10 this year as part of our security talk if you’re interested in watching it.

As usual, I’ve provided a sample ASP.NET MVC 2 project for Visual Studio 2010 which you can look at to see this in action.

What others have said

Requesting Gravatar... James Alexander Apr 06, 2010 9:10 AM
# re: Using AntiXss As The Default Encoder For ASP.NET
Super hawt
Requesting Gravatar... Andrew Apr 06, 2010 9:47 AM
# re: Using AntiXss As The Default Encoder For ASP.NET
Phil-

Looks great. I do wonder what the main difference between using MS default encoder vs a 3rd party library such as AntiXSS?
Requesting Gravatar... DonSleza4e Apr 06, 2010 9:49 AM
# re: Using AntiXss As The Default Encoder For ASP.NET
Awesome
Integrated lib with my asp.net mvc project ^^
Requesting Gravatar... schneitj Apr 06, 2010 9:50 AM
# re: Using AntiXss As The Default Encoder For ASP.NET
What is the counter argument to this? Why isn't it the default encoder already? Performance?
Requesting Gravatar... Ryan Smith Apr 06, 2010 10:26 AM
# re: Using AntiXss As The Default Encoder For ASP.NET
@schneitj
My guess is because if you are migrating from a previous version, if it's the default then you'd be double encoding.
Requesting Gravatar... Barry Dorrans Apr 06, 2010 10:31 AM
# re: Using AntiXss As The Default Encoder For ASP.NET
Heh. So as the lead developer on AntiXSS/WPL I can say that the next public release will have its own class for this. Actually, it'll have its own assembly for this, as it's a NET 4.0 only function.

The main difference between the two sets of encoders is the approach. The .NET encoder uses a small blacklist for its encoding, AntiXSS uses a large whitelist. While this is safer it does have a performance cost which some folks may find hard to swallow. We publish performance states for the WPL, but I don't think we've ever published stats for the raw encoders - I'll take a look at doing that.

Note that you don't need to use the WPL to use AntiXSS - you can manually encode to your heart's content and the next release will separate out the encoding classes into their own assembly again.
Requesting Gravatar... Paul Apr 06, 2010 11:55 AM
# re: Using AntiXss As The Default Encoder For ASP.NET
Nice post! Is this for people planning to stay on .NET 3.5 or lower, giving them proper encoding when they use <%=%> ??? Or is this some how better then the new <%:%> in .NET 4.0?
Requesting Gravatar... David Keaveny Apr 07, 2010 1:24 AM
# re: Using AntiXss As The Default Encoder For ASP.NET
This is beyond awesome. It's bey-awesome!
Requesting Gravatar... Harvey Kandola Apr 07, 2010 1:45 AM
# re: Using AntiXss As The Default Encoder For ASP.NET
Heads-up: AntiXSS does not work on Medium Trust.
Requesting Gravatar... Dhananjay Goyani Apr 07, 2010 5:49 AM
# re: Using AntiXss As The Default Encoder For ASP.NET
Good question Andrew.

Phil, Barry,
If AntiXss is more mature then I wonder why not to make it first class citizen in ASP.NET itself.
Requesting Gravatar... Barry Dorrans Apr 07, 2010 5:51 AM
# re: Using AntiXss As The Default Encoder For ASP.NET
Harvey you're correct, the current version doesn't. The next version will - well, the encoding bits will, HTML santisation will still require full trust I'm afraid.

Dhananjay that conversation has been started, but it's not one I've been involved with - I only started at MS 8 weeks ago!
Requesting Gravatar... haacked Apr 07, 2010 8:27 AM
# re: Using AntiXss As The Default Encoder For ASP.NET
Hi all, regarding why AntiXss isn't the default encoding. This may be something that may happen in the future, but there are several issues that need to be addressed first such as the Medium Trust issue.

Our team and there team do communicate with each other. It's obviously too late for ASP.NET 4, but could perhaps make it in a future version, which gives us time to make sure it's ready to be included if we choose that route.
Requesting Gravatar... Dhananjay Goyani Apr 07, 2010 9:24 PM
# re: Using AntiXss As The Default Encoder For ASP.NET
I understand. On an other note, I expect same treatment for MS Ajax Minifier.
Requesting Gravatar... Daoming Apr 14, 2010 1:55 AM
# re: Using AntiXss As The Default Encoder For ASP.NET
Hi Phil, would be be able to check the following link about file not found exception in MVC?

http://stackoverflow.com/questions/2641526

Many thanks.
Requesting Gravatar... Harvey Kandola Apr 17, 2010 2:34 AM
# re: Using AntiXss As The Default Encoder For ASP.NET
@Barry thanks for the medium trust support: we recently ended up ripping out the vast majority of the codebase and just used the string encoding bits (I think there was a lot of file/IO stuff that caused us issues).

Looking forward to the next release.
Requesting Gravatar... handcode Apr 28, 2010 2:20 AM
# AntiXss.HtmlEncode vsHttpUtility.HtmlEncode
Hi Phil,

What is the difference between AntiXss.HtmlEncode and HttpUtility.HtmlEncode?
Requesting Gravatar... SarahBrooks May 02, 2010 6:34 PM
# re: Using AntiXss As The Default Encoder For ASP.NET
Hi Phil,

The SampleApp that comes with the AntiXSS download targets the .NET 2.0 framework. The solution compiles in VS2008, but not in VS2010.

www.microsoft.com/.../details.aspx

Is AntiXSS referenced in projects targeting .NET 2.0 supported in VS2010?
Requesting Gravatar... Michael Hallock Jun 16, 2010 11:31 AM
# re: Using AntiXss As The Default Encoder For ASP.NET
What about HtmlHelpers? The Html Helpers in MVC2 explicitly call to HttpUtility.HtmlEncode() and HttpUtiliy.HtmlAttributeEncode(). I'm assuming this doesn't fix that little hole?
Requesting Gravatar... Michael Hallock Jun 16, 2010 1:12 PM
# re: Using AntiXss As The Default Encoder For ASP.NET
Answered my own Question - The overridden class (HttpEncoder) is the class that HttpUtility.HtmlEncode, etc. use, so this WOULD in fact cover all of the HtmlHelper classes as well...

Now I just have to upgrade to .NET 4.0...

What do you have to say?

(will show your gravatar)
Please add 4 and 4 and type the answer here: