Security with QueryString values in Asp.net MVC

2020-02-05 10:31发布

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?

7条回答
Animai°情兽
2楼-- · 2020-02-05 10:52

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)]

查看更多
做自己的国王
3楼-- · 2020-02-05 10:54

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.

查看更多
beautiful°
4楼-- · 2020-02-05 11:10

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.

查看更多
够拽才男人
5楼-- · 2020-02-05 11:11

Enrypting and decrypting query params is a trivial process and there are some great examples of how to do so using an HttpModule here on StackOverflow.

"You Don't", "You can't", or "It's not easy" are simply not acceptable responses in this day and age...

查看更多
何必那么认真
6楼-- · 2020-02-05 11:11

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/

查看更多
再贱就再见
7楼-- · 2020-02-05 11:12

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.

查看更多
登录 后发表回答