How secure the EntityId in hidden field for Editing Form in Asp.Net Core MVC?

那年仲夏 提交于 2019-12-12 04:23:22

问题


I'd like to create the form for editing some Entity (for example a post) in the database using the Entity Framework Core.

I want to protect the value PostId in the hidden field before rewriting to another value from the browser. I'm wondering about checking the user permissions before updating but I want to create some encryption/signing or something like that.

How can I encrypt or sign the PostId and in the controller decrypt or validate it?

I've created the example form for editing the post like this:

Entity - Post:

public class Post
{
  [Key]
  public int PostId { get; set; }

  [Required]
  [StringLength(40)]
  public string Title { get; set; }
}

Controller - PostsController with Edit method:

[HttpPost]
[ValidateAntiForgeryToken]
    public async Task<IActionResult> Edit(int id, [Bind("PostId,Title")] Post post)
    {
        if (ModelState.IsValid)
        {
          //Update method
        }
        return View(post);
    }

Form for editing:

@model EFGetStarted.AspNetCore.NewDb.Models.Post

@{
    ViewBag.Title = "Edit Post";
}

<h2>@ViewData["Title"]</h2>

<form asp-controller="Posts" asp-action="Edit" method="post" asp-antiforgery="true" class="form-horizontal" role="form">
    <div class="form-horizontal">

        <div asp-validation-summary="All" class="text-danger"></div>

        <input asp-for="PostId" type="hidden" />

        <div class="form-group">
            <label asp-for="Title" class="col-md-2 control-label"></label>
            <div class="col-md-10">
                <input asp-for="Title" class="form-control" />
                <span asp-validation-for="Title" class="text-danger"></span>
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Edit" class="btn btn-default" />
            </div>
        </div>
    </div>
</form>

回答1:


By encrypting it you don't get any real business value and if the intent is so prevent one user to edit/modify posts he has no access to, you should do it in the backend by following the "Never trust the client" principle and always validate input on the server.

Easiest way to do is to use only the post ID from the model posted in and validate if the user has permissions to modify it. For this the new policy based systems offers resource based permissions which are well documented and can be used to validate the permissions.

Once done, passed take over the values and save the changes.

Also you shouldn't use persistence models inside the views, they easily break your API or your forms when the you change the database layout and navigation properties may cause issues (circular references etc.); especially later on, when lazy loading is implemented (lazy loading can't happen async as its inside a property, so the db call will block the thread).




回答2:


Take a look at Sergey Akopov's blog post where he proposes a mechanism to deal with this scenario within ASP.NET MVC. His solution is to write a Html Helper that can be called within your view to generate a hidden input to accompany each input that you wish to make "tamper proof". This hidden input contains an encrypted copy of the value that you want to be tamper proof. When the form is posted, the server checks that the posted value and accompanying encrypted value still match - he writes a filter attribute which is applied to the corresponding controller action to perform this check. This adds an extra layer of "never trust the client" security.

Another example here has an interesting discussion (in the comments) around the potential security flaws inherent in this approach - The main one being that a determined attacker could "farm" valid combinations of secure field and encrypted value from their editing sessions, and subsequently use these farmed values to post tampered data with future edits.



来源:https://stackoverflow.com/questions/41520780/how-secure-the-entityid-in-hidden-field-for-editing-form-in-asp-net-core-mvc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!