Api Design hard
A user updates their profile via PATCH /users/123 with { name: 'John', role: 'admin' }. If your code does `user.update(req.body)`, what's the vulnerability?
- A The application is vulnerable to 'Mass Assignment', allowing a malicious user to modify internal-only fields (like 'role') by including them in the JSON body of the request.
- B The PATCH request will trigger a blind SQL injection vulnerability because the dynamic user.update() method does not use parameterized queries for the incoming key-value pairs.
- C The code is susceptible to a Cross-Site Request Forgery (CSRF) attack, as the server-side logic does not validate the Origin or Referer headers for partial resource updates.
- D The operation will result in a permanent data loss (Tombstone collision) because the application fails to fetch the current resource state before applying the incoming update delta.
Answer: A. The application is vulnerable to 'Mass Assignment', allowing a malicious user to modify internal-only fields (like 'role') by including them in the JSON body of the request.
**Mass Assignment** (or Overposting) occurs when an API endpoint binds client input directly to internal objects without filtering. Attackers can guess fields (like `role`, `isAdmin`, `balance`) and overwrite them. To prevent this, always use **Data Transfer Objects (DTOs)** or explicitly whitelist the fields that can be updated.
One system design question a day, with spaced repetition so the ones you miss come back.
Get the appRelated questions
- You're designing a REST API for a blog. Which endpoint best represents 'get all comments for post 123'?
- A client wants to update only the 'status' field of an order, leaving other fields unchanged. Which HTTP method is most appropriate?
- A client calls POST /users with valid data, and the user is created. What status code should the server return?
- A client retries a failed 'POST /payments' request due to network timeout. The server receives the request twice. Without idempotency handling, what can happen?
- An API returns 10,000 products. Returning all in one response is impractical. Which pagination approach is simple and stateless?
- Your API serves both mobile apps and third-party integrations. Which authentication method is most appropriate?