Over-posting (mass assignment) happens when an API binds client input directly onto an entity and the client sends extra fields it should not control: [HttpPut("{id}")] public async Task<IActionResult> Update(int id, User user) // binds the entity directly { db.Users.Update(user); // client sent "IsAdmin": true or "Balance": 99999 await db.SaveChangesAsync(); return NoContent(); } Prevention: 1. Use dedicated input DTOs per operation public sealed record UpdateProfileRequest(string DisplayName, string? Bio); Only fields the user may change exist on the model, so anything else is ignored. 2. Map explicitly onto a loaded entity var user = await db.Users.FindAsync([id], ct); if (user is null) return NotFound(); user.DisplayName = request.DisplayName; user.Bio = request.Bio; await db.SaveChangesAsync(ct); Avoid db.Update(entity) with client data, which marks every column as modified. With mapping libraries, configure them to ignore sensitive members, or map manually for security-sensitive models. 3. Enforce authorization per resource Check that the caller owns or may edit this record (resource-based authorization), not just that they are logged in. Server-controlled fields — owner ID, role, prices, status transitions, timestamps — are set by server logic only. 4. Validate input Use data annotations or FluentValidation for lengths, formats and allowed values. Consider rejecting unknown JSON properties: in .NET 8+, JsonSerializerOptions.UnmappedMemberHandling = Disallow makes extra fields a 400 error instead of being silently ignored. 5. Separate output models as well Response DTOs prevent leaking sensitive fields (password hashes, internal flags) — over-exposure is the mirror image of over-posting. 6. JSON Patch caution If you support PATCH with JsonPatchDocument, apply it to a DTO, validate, and then map — never directly to the entity — and restrict allowed paths. Testing: add integration tests that send extra fields (isAdmin, ownerId) and assert they are rejected or ignored.
Ravindra Wadile · · 0 likes