在 ASP.Net MVC 中删除记录的安全方法

Secure way to Delete a record in ASP.Net MVC

我想从我的 ASP.Net MVC 5 网站中删除一个产品。我想知道添加 [AntiForgeryToken][Authorize] 是否足以保护删除操作?

查看

 <p>Delete: @Model.Name</p>
 @using (Html.BeginForm("Delete", "ProductController", FormMethod.Post, new { ProductId = Model.ProductId }))
 {
    @Html.AntiForgeryToken()
    <button type="submit">Delete</button>
 }

控制器

[HttpPost]
[Authorize]
[ValidateAntiForgeryToken]
public ActionResult Delete(long ProductId)
{
    /* Do I need to check if the logged in User has permission to delete the product?
    var product = ProductRepository.Get(Id);
    if (product.Creator == User.Identity.GetUserId<long>())
    {
        ProductRepository.Delete(ProductId);
    }
    */

    // or, can I avoid the trip to DB and just delete the record?        
    ProductRepository.Delete(ProductId); 
}

场景:黑客在我的网站上注册并创建了一个有效帐户。现在黑客查看他自己的产品,显然他有一个 AntiForgeryToken。他现在可以只更改浏览器中的 ProductId 并 Post 请求删除别人的 Product 吗?

简答。这还不够。

防伪令牌只是说发出原始页面请求的人就是进行更新的人。

基本授权属性只是验证用户是否已登录。

您要找的是数据安全。 Microsoft 自己的网站上有一个 example

根据您在上一段中所说的内容,黑客可以注册一个帐户来创建他们自己的产品列表,并根据您在 url 中向他们展示的内容推测出合法的其他记录以进行编辑

假设你有一个 url

https://example.com/product/edit/13

是什么阻止了 user/hacker 猜测

https://example.com/product/edit/12 要么 https://example.com/product/edit/14

如果没有说明用户可以或不能记录哪些记录的数据级别的安全性 access/update,您将 运行 陷入恶意用户可以查看或编辑各种信息的境地。

这就是 FISERV 发现暴露其他客户端信息的确切场景

来自文章

Hermansen had signed up to get email alerts any time a new transaction posted to his account, and he noticed the site assigned his alert a specific “event number.” Working on a hunch that these event numbers might be assigned sequentially and that other records might be available if requested directly, Hermansen requested the same page again but first edited the site’s code in his browser so that his event number was decremented by one digit.