views:

793

answers:

3

In the intent of preventing XSS attacks, I am updating a page in which we have a textbox that accepts HTML, stores it in a database and retrieves and renders it at a later time.

My understanding is that I can sanitize the HTML using AntiXSS.GetSafeHtmlFragment() method. As long as I do this before storing the HTML in the database, am I covered? Do I need to do anything when the HTML is outputted on a web page?

Also, it appears that the white list is kind of a black box. Is there a way to update this based on our requirements?

+1  A: 

You should be set. Though obviously this won't protect you from anything already in the database.

You could use AntiXSS.GetSafeHtmlFragment() while outputting the page instead of when saving. But doing when saving is probably safer. You would not want to do it both while rendering and saving though.

The whitelist is not editable.

David Hogue
A: 

You're almost there. You need to make sure that you choose the proper encoding. For example, if the user input went into a url, you'd need to use AntiXSS.UrlEncode(), and if it went into JavaScript you'd want to use AntiXSS.JavaScriptEncode(). If you can't guarantee when you save the input what the output format will be, it's better to do the sanitizing at output time.

Annie
... remembering to sanitize for SQL Injection
Robert Fraser
So, if the user enters HTML that I sanitize using GetSafeHtmlFragment, I would then need to dig throgh all the URLs (a href) and JS code and passit through AntiXSS.UrlEncode and AntiXSS.JavaScriptEncode?
You need to escape the output for the context it's going to be used in. Generally people accomplish this by using a template system which autoescapes things as they're output, instead of checking every single field individually. Read the whole article I linked, it should help: http://msdn.microsoft.com/en-us/library/aa973813.aspx
Annie
A: 

In regards to your question about "black box": yes, it's a black box, and my understanding is that you can't edit it. If you're looking for more granularity, check out the AntiSamy.NET project.

nahsra