Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TinyMCE, AntiXSS, MVC3 and GetSafeHtmlFragment

I've read through lots of postings on SO regarding XSS and how to deal. Generally, the consensus is whitelist over blacklist and avoid using Regular Expressions (too many variants to deal with).

I'm working on an ASP.Net MVC3 application. I need to be able to display HTML from the user entry (ex. < strong >, < ul >, < li >, etc...) but I don't want any XSS risks.

I'm using the AntiXSS package via Nuget. In my model, I have

[AllowHtml]
public string UserDetails{ get; set; }

In my view, I have TinyMCE hooked into the textarea.

In my controller, I get the post from the View and sanitize it:

using Microsoft.Security.Application;
...
string SanitizedDetails = Sanitizer.GetSafeHtmlFragment(model.UserDetails);

My question: Did I do it right? Am I protected from most XSS issues or am I barking up the wrong tree?

like image 326
Mike Smith Avatar asked Jun 04 '12 01:06

Mike Smith


1 Answers

You are posted against certain forms. Allowing html is a dangerous operation as it is, so you are trying to mitigate it the best you can. Your approach is pretty good here.

There are other options to help but unfortunately not everything is production ready. There are the Content Security Policy headers that are partially supported by various browsers. Ex: http://www.w3.org/TR/CSP/

So what you have is decent, but you could enhance this a little bit if you want to venture into content security policy (for one)

I go over quite a few XSS attacks here if you are interested. http://www.pluralsight-training.net/microsoft/Courses/TableOfContents?courseName=hack-proofing-dotnet-app

You may want to include an additional sanitize prior to rendering (and before saving) in case another attack (sql injection for example) has inserted xss code into your html.

like image 135
Adam Tuliper Avatar answered Oct 15 '22 22:10

Adam Tuliper