views:

2316

answers:

6

How do you properly ensure that a user isnt tampering with querystring values or action url values? For example, you might have a Delete Comment action on your CommentController which takes a CommentID. The action url might look like /Comments/Delete/3 to delete the comment with the id 3.

Now obviously you dont want anyone to be able to delete comment 3. Normally on the owner of the comment or an admin has permission to do so. Ive seen this security enforced different ways and would like to know how some of you do it.

Do you make multiple Database calls to retrieve the comment and check that the author of the comment matches the user invoking the delete action?

Do you instead pass the CommentID and the UserID down to the stored procedure who does the delete and do a Delete where UserID and CommentID equal the values passed in?

Is it better to encrypt the query string values?

A: 

I've done funky things take the querystring, compress it, Base64 or just hex encode it, so that "commentid=4&userid=12345" becomes "code=1a2b23de12769"

It's basically "Security through obscurity" but it does make a lot of work for someone trying to hack the site.

James Curran
+14  A: 

You don't.

It is a cardinal rule of programming, especially in this day and age, that you never trust any input which comes from the user, the browser, the client, etc.

It is also a cardinal rule of programming that you should probably not try to implement encryption and security yourself, unless you really know what you are doing. And even if you do know what you are doing, you will only remain one step ahead of the tard-crackers. The smart ones are still going to laugh at you.

Do the extra query to ensure the logged-in user has the right set of permissions. That will make everyone's lives just that much simpler.

Justice
A: 

You cannot easily do this.

I have fond memories of a site that used action urls to do deletes.

All was good until they started search crawling the intranet.

Ooops, goodbye data.

I would recommend a solution whereby you do not use querystrings for anything you do not wish to be edited.

Nat
Then how do you suggest I edit/delete things then? Keep in mind I'm using Asp.net MVC
Vyrotek
You should be POSTing to your controller methods to invoke a delete - and verifying the request credentials (cookie/username/password/whatever) before performing the delete. See @Schotime's post.
Dylan Beattie
+1  A: 

You can also allow only Post requests to Delete controller action by using the Accept Verbs attribute as seen below.

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Delete(int? id)
{
    //Delete
}

Then you could also use the antiforgery token as discussed here:

http://blog.codeville.net/2008/09/01/prevent-cross-site-request-forgery-csrf-using-aspnet-mvcs-antiforgerytoken-helper/

Schotime
In MVC 2.0 you can also invoke Http delete. Just put [HttpDelete] instead of [HttpPost] on your action, and then make the form submit using the delete protocol instead of post.
Josh
+2  A: 

Vyrotek: The input method is not important. GET, POST, encrypted/obfuscated GET - no real difference. No matter the way your application receives commands, to perform an administrative action it must make sure that the issuing user is allowed to do the stuff he wants. The permission check must take place AFTER the command is received and BEFORE it gets executed. Otherwise it's no security at all.

rciq
+1  A: 

Consider using technique outlined in Stephen Walther's article Tip #46 – Don’t use Delete Links because they create Security Holes which uses [AcceptVerbs(HttpVerbs.Delete)]

Arnold Zokas