Tip: Replacing Html.Encode Calls With New Html Encoding Syntax

Like the well disciplined secure developer that you are, when you built your ASP.NET MVC 1.0 application, you remembered to call Html.Encode every time you output a value that came from user input. Didn’t you?

Well, in ASP.NET MVC 2 running on ASP.NET 4, those calls can be replaced with the new HTML encoding syntax (aka code nugget). I’ve written a three part series on the topic.

But dang, going through all your source files cleaning up these calls is a pretty big pain. Don’t worry, I have your back. Just bring up the Find an Replace dialog (CTRL + SHIFT + H) and expand the Find options section and check the checkbox labeled Use and make sure Regular expressions is selected.

Then enter the following in the Find what textbox.

\<\%:b*=:b*Html.Encode\({[^%]*}\):b*\%\>

And enter the following in the Replace with textbox.

<%: \1 %>

Here’s a screenshot of what the dialog should look like (though yours won’t have the red box :P).

find-and-replaceNote that this regular expression I’m giving you is not foolproof. There are some very rare edge cases where it might not work, but for the vast majority of cases, it should work fine. At least, it works on my machine!

works-on-my-machine

Now that I’m finally done with updates to Professional ASP.NET MVC 2, I hope to get back to my regular blogging schedule. This will be only my third blog post this month, a new record low! And I love to blog! It’s been a busy past few months.

Tags:

What others have said

Requesting Gravatar... Jeff Atwood Apr 29, 2010 12:14 AM
# re: Tip: Replacing Html.Encode Calls With New Html Encoding Syntax
oh god that hideous fake regex syntax makes me want to puke every time I see it.

Someone PLEASE tell me that Visual Studio 2010 offers a standard Regex syntax here? PLEASE?
Requesting Gravatar... Dhananjay Goyani Apr 29, 2010 12:41 AM
# re: Tip: Replacing Html.Encode Calls With New Html Encoding Syntax
Man, this is awesome. Many thanks Phil.
Requesting Gravatar... tobi Apr 29, 2010 2:18 AM
# re: Tip: Replacing Html.Encode Calls With New Html Encoding Syntax
Also remember that you can replace Server.HtmlEncode and HttpUtility.HtmlEncode with this. Actually I think you should scan your app for <%= and eradicate all usages of this dangerous construct. Replace it with <:
Requesting Gravatar... shiju varghese Apr 29, 2010 2:49 AM
# re: Tip: Replacing Html.Encode Calls With New Html Encoding Syntax
Thanks Phil. Really useful tip
Requesting Gravatar... Justin Apr 29, 2010 7:29 AM
# re: Tip: Replacing Html.Encode Calls With New Html Encoding Syntax
Ok I have to agree with Jeff. I have a hard enough time remembering the real regex syntax. Can the regular regex synatax be used also?
Requesting Gravatar... Eric Malamisura Apr 29, 2010 7:37 AM
# re: Tip: Replacing Html.Encode Calls With New Html Encoding Syntax
Awesome, Phil we expect MVC 3 release next month k? Sweet! ;)
Requesting Gravatar... haacked Apr 30, 2010 3:54 PM
# re: Tip: Replacing Html.Encode Calls With New Html Encoding Syntax
Unfortunately, that screenshot is of Visual Studio 2010. I'll pass this feedback to the Visual Studio team. :)
Requesting Gravatar... Dan Watson Apr 30, 2010 3:57 PM
# re: Tip: Replacing Html.Encode Calls With New Html Encoding Syntax
Was just about to dig around all my views and do this.... thanks saved me a load of time :)
Requesting Gravatar... Imran May 04, 2010 12:29 AM
# re: Tip: Replacing Html.Encode Calls With New Html Encoding Syntax
I am also very much agree with Jeff. Why Visual Studio not use the stranded Regular Expression.
Requesting Gravatar... Mark May 04, 2010 9:57 AM
# re: Tip: Replacing Html.Encode Calls With New Html Encoding Syntax
For those jonsing for real regex in VS... let me share my love for this great VS addin:
www.codeproject.com/.../VS2008RegexAddIn.aspx
Requesting Gravatar... Chris Tattum May 19, 2010 8:04 AM
# re: Tip: Replacing Html.Encode Calls With New Html Encoding Syntax
This has just saved me a LOAD of time after upgrading to .NET4/VS2010. Thanks.
Requesting Gravatar... zire May 21, 2010 12:17 AM
# re: Tip: Replacing Html.Encode Calls With New Html Encoding Syntax
Useful article I hope it will work fine in my sample :)
Requesting Gravatar... Franck Quintana Jun 07, 2010 3:32 PM
# re: Tip: Replacing Html.Encode Calls With New Html Encoding Syntax
Thank you for this useful tip !

What do you have to say?

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