6

How can I use the Html.TextAreaForwithout encoding it? I know it's a security risk but I have a separate class that sanitizes any text.

Example:

@Html.TextAreaFor(model =>model.PostBodyText, 10, 100, 1)

I'm planning to use it with TinyMCE.

Regards RaVen

UPDATE I'm using the new Razor View Engine.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
RaVen
  • 775
  • 2
  • 8
  • 21

3 Answers3

8

You will need to roll your own:

<textarea cols="100" id="PostBodyText" name="PostBodyText" rows="10">
    @MvcHtmlString.Create(Model.PostBodyText)
</textarea>

Of course in terms of security this could be very dangerous as your site is now vulnerable to XSS attacks. So the question is why having a separate class that sanitizes all the text when you can simply rely on the HTML helpers to do the job for you?

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • Yeah I know but I'm filtering out any javascript tags... I only allow some safe html tags. But yeah TinyMCE may cause a security flaw. Again Thanks very much. :) – RaVen Nov 22 '10 at 12:54
1

As an alternative option you might wanna use ValidateInput as described here. An example in MVC style would be:

[ValidateInput(false)]
public ActionResult Method(){
     return View()
}

[ValidateInput(false)]
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Method(){
    // your stuff here
    RedirectToAction("index"); // or something 
}

I think that is more the MVC way to go. Now your controller tells you there is a security issue in that controller method. Your view can be any normal view using html helpers etc. Note that this enables all sorts of input, not filtered. It will work with TinyMCE though.

//edit

woops I see you need to add

<httpRuntime requestValidationMode="2.0"/>

to webconfig as well in new versions of MVC. Guess it might not be the way to go.

bastijn
  • 5,841
  • 5
  • 27
  • 43
1

Use [AllowHtml] on the model property. As I learned in In ASP.NET MVC 3, how do I get at the model using Razor Syntax in a Create() View?.

Community
  • 1
  • 1