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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With