views:

70

answers:

2

Hi I have a MVC site and will have users inputting text to be displayed on a web page. I am trying to come up with a graceful way of handling any HTML that the user try's to input - as you will probably be aware MVC 2 throws an error for any HTML in the text.

I use ViewModels and decorate my properties with filters from the DataAnotation class to validate my forms.

Anybody now of such a way?

Is there some crazy regex that will NOT match HTML but anything else or some other way?

I am open to any suggestions.

Thanks,

Simon

+2  A: 

Adding the following attribute will stop the runtime from complaining:

[ValidateInput(false)]
public ActionResult SomeEvilAction ()
{
    /* ... */
}

Now it's your task to HTML encode every input you display back on a page:

<%= HttpUtility.HtmlEncode (Model.Text) %>

or

<%: Model.Text %>
Developer Art
Thanks - I was looking for something more graceful, that will prompt the user that HTML is not allowed. I want to preserve carriage returns from the text box so I have written a HTML helper to turn these into <br/> and remove the potentially malicous script tags. Bearing in mind that to even get the script tags near the page - they would have to get through MVC's Malicous input check. So for this one instance my text does not get encoded, to run html on the page they would need direct DB access!
Csharper
It always was your task to HTML-encode every piece of text you display on the page. **ASP.NET's anti-XSS filtering will not adequately protect you.** At best it merely obscures the real problems, at worst it adds fun new bugs to your otherwise secure application. I wouldn't use it for anything, ever, and I'm very disappointed in Microsoft that they seem to think this wrong-headed approach is in any way valid.
bobince
A: 

I did this exact thing on a site I did the other day.

I am using a WYSIWYG editor that puts in proper html, not bb code.

I disabled validation on the page from the page directive to stop mvc throwing the potentially unsafe code exception and removed all instance of scripts tags using regex.

See Developer Art's post

You may need to add this to your web.config

<httpRuntime requestValidationMode="2.0" />

The regex I used is as follows:

(?<startTag><\s*script[^>]*>)(?<scriptContent>[\s\S]*?)(?<endTag><\s*/script>)

This will give you 3 named groups. startTag scriptContent endTag

So you can do a replace on the script element and show the content of the script, or remove it altogether.

Anything you wish to do really.

jimplode