Using AntiXss As The Default Encoder For ASP.NET
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.
- Html Encoding Code Blocks With ASP.NET 4
- Html Encoding Nuggets With ASP.NET MVC 2
- Using AntiXss as the default encoder for ASP.NET
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.
The 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.
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.
作者:Chuck Lu GitHub |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
2019-03-09 Inversion of Control Containers and the Dependency Injection pattern
2019-03-09 82. Remove Duplicates from Sorted List II
2019-03-09 83. Remove Duplicates from Sorted List
2018-03-09 fixed和absolute
2017-03-09 Func委托和Action委托
2016-03-09 override (C# Reference)
2016-03-09 virtual (C# Reference)