Interview question
Prevent overposting with an explicit update DTO
Updates only public mutable fields and leaves ownership, status, and audit values server-controlled.
TL;DR
Updates only public mutable fields and leaves ownership, status, and audit values server-controlled.
Request DTOs, explicit mapping, allowlisted updates, audit ownership, and mass-assignment protection.
Practice the problem like a real interview: restate, reason, implement, and test.
Allow a user to change display name and locale. Ignore no unknown privileged fields silently: the JSON contract should reject them, and mapping must never touch role, owner id, or status.
A payload containing isAdmin: true fails JSON binding or remains outside the DTO; the stored role is unchanged.
I make the writable contract smaller than the entity. Strict JSON unmapped-member handling catches client mistakes, while explicit assignment is the final defense against privileged field updates.
builder.Services.ConfigureHttpJsonOptions(options =>
options.SerializerOptions.UnmappedMemberHandling = JsonUnmappedMemberHandling.Disallow);
app.MapPut("/me/profile", async Task<Results<NoContent, NotFound>>
(UpdateProfileRequest request, ClaimsPrincipal principal,
AppDbContext db, TimeProvider clock, CancellationToken ct) =>
{
var userId = principal.RequireUserId();
var user = await db.Users.SingleOrDefaultAsync(x => x.Id == userId, ct);
if (user is null) return TypedResults.NotFound();
user.DisplayName = request.DisplayName.Trim();
user.Locale = request.Locale;
user.UpdatedAt = clock.GetUtcNow();
await db.SaveChangesAsync(ct);
return TypedResults.NoContent();
}).RequireAuthorization();
public sealed record UpdateProfileRequest(string DisplayName, string Locale);
The DTO documents writable fields and explicit assignment stays readable during review. Strict unknown-member handling is a compatibility choice; if an API intentionally tolerates additive client fields, mapping still protects privileged columns.
Move to the linked follow-up, next path step, prerequisite, or deeper variant.
Practice the next layer of the same subject.