views:

35

answers:

2

What if I have ChangePassword form with hidden ID field of the user.

BadPerson knows id of GoodPerson. He opens Change Password form with FireBug, changes his Id to GoodPerson's Id, so password changes for GoodPerson.

Of course I can create some server logic that will prevent this, but I think there should be some out of the box solution, wich throws if hidden field been changed, wich I don't know.

Thank's in advance.

EDIT Ok, Change Password is a bad example. Any edit form where I have id in hidden field has same problem.

+2  A: 

There is nothing that will let you know that a value of a hidden field's value has been changed or not. For a user to change his password it means that he needs to be authenticated. When using forms authentication the ID of the currently authenticated user is stored in an encrypted cookie which cannot be modified.

This is to say that you shouldn't use hidden fields for storing the currently connected user. Just use the built-in FormsAuthentication mechanism in ASP.NET and never store such information in hidden fields. The way ASP.NET knows that the value of the cookie hasn't been tampered with is that it signs it with the machineKey specified in the configuration.

There's an important rule that you should follow when dealing with security and authentication: always use built-in security mechanisms, never roll your own.

Darin Dimitrov
I'll second that, don't try to do it yourself, use forms authentication, it takes care of all of that for you.
Doobi
I would also like to add that when allowing the user to change their password, it's often a good idea to have them enter their current password along with the new password. This provides an extra point of validation that you can perform.
BradBrening
Ok, but what if it's not a user, but some other object, for wich it's very convinient to store it's id. >There is nothing that will let you know that a value of a hidden field's value has been changed or notI'm not agree with this. Good old viewstate could :) There should be something I belive. Otherwise there is a huge hole in security.
er-v
@BradBrening, totally agree with you, that's how it should be done, unfortunately sometimes GUIs are designed by people who know nothing about security and might say that an additional field is not necessary here.
Darin Dimitrov
@er-v, the thing is to sign the value. Google about HMAC-SHA1 Signatures.
Darin Dimitrov
Why this has been down-voted? Could you please leave a comment when down-voting? Is there something wrong with my answer?
Darin Dimitrov
+1  A: 

I agree with Darin that forms authentication will take care of the specific problem mentioned above (changing a password). This answer is for the more general question of making sure that a hidden field's value is not changed.

The best solution to this is to include another hidden field which contains a hash of the first hidden field's value. That way if the 1st hidden field is changed you will know it. The generated HTML looks something like this:

<input id="productId" name="productId" type="hidden" value="1" />
<input id="productId_sha1" name="productId_sha1" type="hidden" value="vMrgoclb/K+6EQ+6FK9K69V2vkQ=" />

This article shows how to do it and includes source code for an extension Html.SecuredHiddenField which will take care of the logic for you.

Keltex