skip to content

In ASP.NET Core MVC, how do [Bind] and [BindNever] stop over-posting on a form post, and why do they not protect a [FromBody] JSON payload?

level: seniorimportance: should knowfreq 50%

answer

  1. attacker adds an extra field
  2. [Bind] is an allow-list
  3. names compared ordinally
  4. excluded properties reset to default
  5. input formatters ignore both

basics

~20 s

[Bind("Sku,Quantity")] limits which properties model binding may set, and [BindNever] removes one property from binding. Both act only on value-provider binding (form, route, query); a JSON body goes through an input formatter that ignores them.

solid answer

~40 s

**Over-posting** is a client adding fields the form never showed, such as `IsApproved=true` on a stock adjustment bound straight to the entity. In MVC, `[Bind("Sku,Quantity,Reason")]` on the parameter or class is an allow-list: only those properties are bound, matched by exact, case-sensitive name. `[BindNever]` on a property excludes just that one. Both are honoured by the complex-object binder that reads form, route and query values. A `[FromBody]` parameter is deserialized by an input formatter such as `SystemTextJsonInputFormatter`, which knows nothing about these attributes, so a JSON payload can still set `IsApproved`. For JSON, accept a request type that has only the editable fields. `[Bind]` also has an edit trap: excluded properties are left at their default, so saving the bound object can overwrite stored values with nulls.

go deeper

for a junior

Know what over-posting means and that binding an entity directly from a request lets clients set fields they should not.

for a middle

Explain [Bind] as an allow-list and [BindNever] as a deny-list, including the ordinal name match and where each attribute can go.

for a senior

Recognise that input formatters ignore both attributes, avoid the edit-time reset to defaults, and steer JSON endpoints to dedicated input types.

for a principal

Set a team rule for request shapes that fails closed, balancing attribute-based guards on legacy MVC forms against the cost of separate input models.

## What over-posting is **Over-posting** (also called mass assignment) happens when an action binds the request straight onto a type that has more settable properties than the client should control. Take a stock-adjustment entity: ```csharp public class StockAdjustment { public int Id { get; set; } public string Sku { get; set; } = ""; public int Quantity { get; set; } public string Reason { get; set; } = ""; public bool IsApproved { get; set; } public string? ApprovedBy { get; set; } } ``` The clerk's form shows only SKU, quantity and reason. Model binding, however, fills **every** public settable property whose name appears in the request, so a crafted form post with `IsApproved=true&ApprovedBy=manager` approves the adjustment. ## The MVC binding-behaviour attributes | Attribute | Applies to | Effect | |---|---|---| | `[Bind("Sku,Quantity,Reason")]` | a class or an action parameter | allow-list: only the named properties are bound; also exposes `Prefix` | | `[BindNever]` | a property, class or parameter | never bound from request values | | `[BindRequired]` | a property, class or parameter | binding error when no value is found (form, route, query) | Details that matter in practice: - `BindAttribute` splits each string on commas and compares names **ordinally**, so `[Bind("sku")]` does not match a property named `Sku`. - `[BindNever]` is the deny-list counterpart: put it on `IsApproved` and `ApprovedBy` and the rest of the type stays bindable. - These are honoured by the **complex-object model binder**, which pulls keys from value providers: form fields, route values and the query string. ```csharp [HttpPost] public async Task<IActionResult> Create( [Bind("Sku,Quantity,Reason")] StockAdjustment adjustment) { // IsApproved and ApprovedBy keep their defaults db.StockAdjustments.Add(adjustment); await db.SaveChangesAsync(); return RedirectToAction(nameof(Index)); } ``` ## Why they do nothing for JSON A `[FromBody]` parameter is not built by the complex-object binder. The body model binder hands the stream to an **input formatter** (`SystemTextJsonInputFormatter` by default), and the formatter deserializes the whole object with System.Text.Json. The ASP.NET Core documentation is explicit that `[Bind]`, `[BindRequired]` and `[BindNever]` affect form data and **do not affect input formatters**. A JSON API therefore stays open to over-posting even with the attributes in place: - `{"sku":"BOLT-M8","quantity":-40,"reason":"damaged","isApproved":true}` sets `IsApproved` on the entity. The robust fix for JSON is a request type that contains only the client-editable fields, mapped onto the entity in the action. Serializer attributes such as System.Text.Json's `[JsonIgnore]` also stop a property from being read, but they change responses too, so they are a weaker tool than a dedicated input type. ## The edit trap `[Bind]` works best for **create** actions. In an **edit** action, binding into an entity and saving it is dangerous: 1. The form posts `Id`, `Sku`, `Quantity`, `Reason`. 2. `[Bind("Id,Sku,Quantity,Reason")]` binds those; `IsApproved` and `ApprovedBy` stay at `false` and `null`. 3. Passing the object to `db.Update(...)` and saving marks every property modified, so those defaults overwrite the stored approval. The documentation recommends view models over `[Bind]` for this reason. The safer edit pattern loads the entity, then copies only the editable values (or uses `TryUpdateModelAsync` with an explicit property list) before saving. ## What interviewers listen for - A concrete over-posting example, not just the term. - Allow-list (`[Bind]`) versus deny-list (`[BindNever]`), and the ordinal name match. - The clear statement that input formatters ignore both, so JSON needs a separate input type. - The edit-scenario reset to defaults, and a safer pattern for updates.

  • Why is [Bind] risky on an edit action that saves the bound entity?
    Properties outside the `[Bind]` list are not left untouched; they keep the new object's defaults. Passing that object to `Update` and calling `SaveChanges` writes those defaults over the stored values, for example clearing `ApprovedBy`. Load the entity first and copy only the editable fields, or use a view model.
  • Does [BindRequired] make a JSON body property mandatory?
    No. `[BindRequired]` adds a binding error when no value is found in form, route or query data, but a `[FromBody]` payload is deserialized by an input formatter that ignores it. For JSON, use validation attributes such as `[Required]` on the request type, or C# `required` members, which System.Text.Json enforces during deserialization.
  • Is [BindNever] safer than [Bind] when the entity gains a new sensitive property later?
    No, it is the opposite. `[Bind]` is an allow-list, so a property added later stays unbound until someone lists it. `[BindNever]` is a deny-list, so a new sensitive property is bindable until someone remembers to mark it. Allow-lists, or a dedicated input type, fail closed.

  • [BindNever] on IsApproved also blocks it from a [FromBody] JSON payload.
  • [Bind] leaves excluded properties holding their stored database values on an edit.
  • [Bind("sku")] matches a property named Sku because binding names are case-insensitive.
  • Over-posting cannot happen with JSON APIs because the client sends a fixed schema.
  • [BindRequired] makes a JSON body property mandatory during deserialization.