views:

34

answers:

1

I'm working through NerdDinner and I'm a bit confused about the following section...

First they've added a form for creating a new dinner, with a bunch of textboxes delcared like:

<%= Html.TextArea("Description") %>

They then show two ways of binding form input to the model:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create() {
    Dinner dinner = new Dinner();
    UpdateModel(dinner);
    ...
}

or:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(Dinner dinner) { ... }

Ok, great, that all looks really easy so far.

Then a bit later on they say:

It is important to always be paranoid about security when accepting any user input, and this is also true when binding objects to form input. You should be careful to always HTML encode any user-entered values to avoid HTML and JavaScript injection attacks

Huh? MVC is managing the data binding for us. Where/how are you supposed to do the HTML encoding?

+1  A: 

You generally (but not always) want to HTML encode the values before writing them out, typically in your views, but possibly from the controller as well.

Some info here: http://weblogs.asp.net/scottgu/archive/2010/04/06/new-lt-gt-syntax-for-html-encoding-output-in-asp-net-4-and-asp-net-mvc-2.aspx

RedFilter
so it doesn't matter what I let the user enter into the form, as long as I don't output it again without encoding? I don't really know how HTML injection works.
fearofawhackplanet
Yes. There are many types of cross-site scripting attacks, but a simple check on your own site is to try entering `<script>alert('XSS vulnerability')</script>` anywhere the user can enter data (including when creating their user name). If you see the alert popup (assuming you have javascript turned on), you'll have found a place that needs HTML encoding. It is best to HTML encode **all** output, and only remove that encoding when necessary.
RedFilter
ok thanks orbman
fearofawhackplanet