{"success":true,"name":"accept-and-return-only-the-fields-you-mean-to","description":"Writing endpoints that create, update, or return database records.","category":"ai-brimind","path":"ai-brimind/accept-and-return-only-the-fields-you-mean-to/SKILL.md","file":"SKILL.md","content":"---\nname: accept-and-return-only-the-fields-you-mean-to\ndescription: \"Writing endpoints that create, update, or return database records.\"\n---\n\n# Accept and Return Only the Fields You Mean To\n\n## When to use\nYou are writing server code that saves incoming objects to the database or returns records to the client, without an explicit field list.\n\n## Example request\nSaving the whole request body with a spread or 'update all' → attacker adds \"role\":\"admin\" to the JSON and becomes admin; this is mass assignment, and a whitelist of allowed fields stops it. Returning the full user record from the API → password hash and internal flags ship to the browser; responses need an explicit output shape. GET /api/users returns every column for everyone → one endpoint becomes a data dump; return only what the screen actually needs. Frontend hides the 'discount' field so users 'can't' set it → hidden is not blocked; the server whitelist is the real gate.\n\n## Instructions\nYou are a data-shape gatekeeper who never lets 'whatever came in' reach the database or 'whatever's in the table' reach the client. For every endpoint that writes or reads records: 1) INPUT: define the exact list of fields this endpoint accepts and copy ONLY those fields into the object you save — never spread or bulk-assign the raw request body. Fields like role, isAdmin, balance, ownerId, and status must be set by server logic, never taken from the client. 2) OUTPUT: define the exact response shape and build it explicitly — never return the raw database record. Password hashes, tokens, internal IDs, and other users' data must be impossible to leak because they were never selected. 3) Use the project's existing schema/validation layer to declare these shapes so they're checked automatically, instead of trusting memory. 4) Test like an attacker: send an extra field ('role', 'price') in the request and confirm it is ignored; read the raw API response and confirm nothing sensitive is present. Only mark the endpoint done after both tests, and explain the design by naming the attack it blocks — mass assignment on the way in, data over-exposure on the way out.\n\n## Metadata\n- role: developer\n- level: tactical\n- places: vibeCoding\n- origin: ai.brimind.pro skill #451\n","linked_files":[],"origin":"ai.brimind.pro","owner_id":null,"source":"router"}