Unintended updates in mvc With Example

sardarmudassaralikhan

Sardar Mudassar Ali Khan

Posted on July 4, 2023

Unintended updates in mvc With Example

In MVC, unintended updates can occur when binding user-submitted data to a model without properly validating and restricting the properties that can be updated. This can lead to potential security risks, such as over-posting attacks or unintended modifications of sensitive data.

To prevent unintended updates, you should implement measures to validate and restrict the properties that can be updated. One common approach is to use view models that only contain the properties specifically required for editing, instead of binding directly to the actual model.

Here's an example to illustrate the issue and the solution:

Assume you have a Person model with properties such as Id, Name, Age, and IsAdmin.

public class Person
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int Age { get; set; }
    public bool IsAdmin { get; set; }
}
Enter fullscreen mode Exit fullscreen mode

Consider a scenario where you have an edit form for updating the person's name and age. Without proper precautions, a malicious user could modify the HTML form and include an additional field for the IsAdmin property, thereby attempting to grant themselves administrative privileges.

To mitigate this risk, you can use a view model that includes only the properties that should be updated.

public class PersonEditViewModel
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int Age { get; set; }
}
Enter fullscreen mode Exit fullscreen mode

Then, modify the controller to use the view model:

public class PersonController : Controller
{
    // GET: /Person/Edit/{id}
    public ActionResult Edit(int id)
    {
        var person = GetPersonById(id);

        // Map the relevant properties from the model to the view model
        var viewModel = new PersonEditViewModel
        {
            Id = person.Id,
            Name = person.Name,
            Age = person.Age
        };

        return View(viewModel);
    }

    // POST: /Person/Edit/{id}
    [HttpPost]
    public ActionResult Edit(PersonEditViewModel viewModel)
    {
        if (ModelState.IsValid)
        {
            var person = GetPersonById(viewModel.Id);

            // Update only the properties that should be allowed
            person.Name = viewModel.Name;
            person.Age = viewModel.Age;

            // Save the changes to the database

            return RedirectToAction("Details", new { id = person.Id });
        }

        // If there are validation errors, redisplay the edit form
        return View(viewModel);
    }
}
Enter fullscreen mode Exit fullscreen mode

By using a view model, you limit the properties that can be updated to only those explicitly defined in the view model. This prevents unintended updates of sensitive properties like IsAdmin. When mapping the view model back to the actual model, you update only the allowed properties.

Using this approach ensures that only the intended properties can be modified, reducing the risk of unintended updates and potential security vulnerabilities.

💖 💪 🙅 🚩
sardarmudassaralikhan
Sardar Mudassar Ali Khan

Posted on July 4, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related