Skip to content
md ~/writing/kurs-kodundaki-aciklar-mass-assignment-idor
All posts

· 6 min read

Two Holes I Found in Course Code: Admin from the Sign-Up Form, and IDOR

Two Holes I Found in Course Code: Admin from the Sign-Up Form, and IDOR

Most of us write our first big project by following a course. A course focuses on showing how a feature works; how that feature could be abused is usually out of scope. Sahaf, my second-hand book shop, started as the project of an ASP.NET Core MVC course I followed in 2024. Two years later, rewriting it as a real product, I looked at the old code and found two serious holes: anyone could make themselves an admin through mass assignment, and anyone could change someone else's cart through IDOR. This post covers both, how they worked and how I closed them.

1. Becoming an admin from the sign-up form

On the old registration page, right below the address fields, there was a drop-down: role selection. In the course it was added so an admin could create employee or company accounts. But nothing on the page limited it to admins, and the server assigned whatever role the form sent, without any check:

// old code (course)
if (!String.IsNullOrEmpty(Input.Role))
{
    await _userManager.AddToRoleAsync(user, Input.Role);
}
else
{
    await _userManager.AddToRoleAsync(user, SD.Role_Customer);
}

So the first visitor to the site could pick Admin from the list while signing up and walk into the admin panel. Hiding the list would not have changed the outcome: the fields a form sends are easy to change with the browser's developer tools or a hand-written request. As long as the server accepts a Role field, anyone who sends it can use it.

This hole is called mass assignment, or overposting: the server accepts the fields that arrive with the request because the form has them anyway, without deciding for itself which fields to accept from whom.

The fix: a field is read according to permission

In the new version the role is always customer by default. A posted role is considered only if the request comes from an admin and the role is on the list of known roles:

// Role and company are posted fields: only an admin may set them, and
// only to known values. Everyone else becomes a plain customer.
var role = SD.Role_Customer;
int? companyId = null;
if (byAdmin && !string.IsNullOrEmpty(model.Role))
{
    if (!SD.Roles.Contains(model.Role))
        ModelState.AddModelError(nameof(model.Role), t["account.invalidRole"]);
    role = model.Role;
    // ...
}

Two rules are at work. First, a field being posted does not mean it is accepted; the decision is made on the server, based on who sent the request. Second, even an accepted value is checked against a known list.

I applied the same principle to data updates. In the course code, many updates attached the object from the form to the database as is, with calls like Db.Categories.Update(category). That means fields not on the form but present in the request get written too. In the new version an update loads the record from the database and copies only the editable fields onto it:

public void CopyEditableFrom(Product other)
{
    Title = other.Title.Trim();
    TitleEn = other.TitleEn?.Trim();
    Author = other.Author.Trim();
    // ...
    Price = other.Price;
}

Fields such as the ID, the creation date and the cover image are not on that list, so they are not written even if a request carries them.

2. Someone else's cart: IDOR and changes over GET

Each row on the cart page had plus, minus and remove buttons. In the old code they looked like this:

// old code (course)
public IActionResult Plus(int cartId)
{
    var cartFromDb = _unitOfWork.ShoppingCart.Get(u => u.Id == cartId);
    cartFromDb.Count += 1;
    _unitOfWork.ShoppingCart.Update(cartFromDb);
    _unitOfWork.Save();
    return RedirectToAction(nameof(Index));
}

That short method has three separate problems.

No ownership check. The cart row is looked up by ID only; who it belongs to is never checked. Any logged-in user could change the number in the address, /Customer/Cart/Remove?cartId=41, 42, 43 and so on, and remove items from, or change quantities in, other people's carts. This is called IDOR, short for Insecure Direct Object Reference: an object can be reached directly by its ID, and nothing checks whether it belongs to the user.

Sequential IDs. int IDs go 1, 2, 3; no knowledge is needed to guess which numbers to try.

Changes over GET. The methods had no [HttpPost], so following a link or an image's address was enough. On another site, an <img src=".../Customer/Cart/Remove?cartId=41"> loaded without the user noticing could make the request with the user's session. GET requests do not carry the form verification token that provides CSRF protection.

The fix: three layers

[HttpPost]
public IActionResult Plus(Guid cartId) => ChangeCount(cartId, +1);

[HttpPost]
public IActionResult Remove(Guid cartId)
{
    var cart = OwnCart(cartId);
    if (cart == null) return NotFound();
    unitOfWork.ShoppingCart.Remove(cart);
    unitOfWork.Save();
    return RedirectToAction(nameof(Index));
}

// A cart row is reachable only by its owner: another user's id gives 404.
private ShoppingCart? OwnCart(Guid cartId)
{
    var userId = User.UserId();
    return unitOfWork.ShoppingCart.Get(c => c.Id == cartId && c.ApplicationUserId == userId, tracked: true);
}
  • Ownership inside the query: the row is looked up by ID and owner together. Someone else's row returns 404 as if it did not exist; even its existence is not revealed.
  • Unguessable IDs: cart row IDs are now Guids. The ownership check is the real protection; unguessable IDs are a second layer.
  • Changes only over POST: with AutoValidateAntiforgeryToken, every POST on the site must carry a CSRF token. A request triggered by another site cannot know that token.

The test checks that none of the three actions can touch another user's row:

[Fact]
public void Another_users_cart_row_cannot_be_removed_or_changed()
{
    var (controller, db) = Create("attacker");
    var cart = Seed(db, "owner", 3);

    Assert.IsType<NotFoundResult>(controller.Remove(cart.Id));
    Assert.IsType<NotFoundResult>(controller.Plus(cart.Id));
    Assert.IsType<NotFoundResult>(controller.Minus(cart.Id));
    Assert.Equal(3, db.ShoppingCarts.Single().Count);
}

The same rule applies to orders: an order can be viewed only by its owner or an admin.

Why was the course like this?

I'm not writing this to look down on the course. A course keeps code simple to teach one lesson: a role picker on the sign-up form is the short way to show how roles are assigned, and the cart actions show updating a record in the database. The problem is learning code turning into a real product as is. Before putting a project in a portfolio, you have to ask the question the course didn't: who can do this, and to whose data?

What I took away

  • A posted field is not an accepted field. The server decides which fields it accepts from whom, and checks values against a known list.
  • Every record reached by ID needs ownership in the query. Not does this record exist? but does this user have such a record?
  • State-changing actions are never GET. A GET can be triggered by a link or an image and falls outside CSRF protection.
  • An unguessable ID is an extra layer, not a replacement for the check. The real protection is the ownership check.
  • Course code is a starting point, not a product. The question to ask when rereading it: what would someone who wants to abuse this code do?