Skip to content
Katabench
Try free

Mass Assignment Guard

Easy About 12 min Free, no account needed

Decide which submitted fields may be applied to a record using an allowlist, not a blocklist.

The brief

The bug in plain terms: This is mass assignment (also called over-posting), where your endpoint copies whatever fields the user sent in their JSON straight onto a database record, including fields they were never meant to touch. An attacker just adds an extra field like "role": "admin" or "isAdmin": true to an ordinary request and quietly promotes their own account or edits data that should be off limits. The trap is that blocking the dangerous fields you can think of (a blocklist) always loses, because the next sensitive field someone adds to the model is writable by default, so the only safe approach is to list the few fields a user IS allowed to set and reject everything else.

A "profile update" endpoint takes the JSON the user submitted and writes each field it contains onto their account record. To keep them from changing their own identifier, it refuses the id field:

C#
return field != "id";

That is a blocklist, and it only names the one field someone happened to think of. The account record also has role, isAdmin, passwordHash, emailVerified, and more. Every one of those is still writable, so a user who adds "role": "admin" to the payload promotes themselves. This is mass assignment (also called over-posting), and a blocklist loses it the moment a new sensitive field is added to the model.

The safe direction is the opposite one: name the fields a user is allowed to edit and reject everything else. Implement CanAssign(field) to return true only when field is one a user may set on their own profile, namely displayName, bio, or avatarUrl, and false for anything else.

How this is graded

  • Functional tests: each genuinely user-editable field is accepted, and the id field the starter already blocks stays blocked.
  • Security tests: privilege fields like role and isAdmin must be rejected. The blocklist starter waves them through and fails.

Starter code

C#
using System;

public class Solution
{
    // Returns true when a submitted field may be written onto the user's record.
    public bool CanAssign(string field)
    {
        // BUG: a blocklist of one field. It stops "id" but leaves "role", "isAdmin",
        // "passwordHash" and every other sensitive field fully writable.
        return field != "id";
    }
}

How it is graded

Functional tests and adversarial payloads both have to pass before the solution is accepted.

C# secure coding exercises →

Related guides

What it practices

Allowlist validation
Accept only known-good values, fields and types instead of chasing bad ones.

Related katas