Skip to content
mentoor

Topic 2 of 5 · 4 min read

Authentication and access control checks

Goal: Find the missing ownership check in a generated endpoint and state the fix.

Authentication asks who you are; authorisation asks what you may touch. Generated code often does the first and forgets the second. An endpoint that checks for a valid login but then loads a record by id, without checking that the record belongs to that user, is still broken. OWASP ranks Broken Access Control first in its 2025 Top 10.

Check yourself · 1 of 3

What is the difference between authentication and authorisation?

Choose one answer

Why it matters

When you ask an agent for "an endpoint to fetch an order", it writes the query that works. The test passes because the test user fetches their own order. Nobody tries user 2 fetching user 1's order, so the gap survives until a stranger changes a number in the URL.

What to check, in order

  1. Is the route protected at all? New routes added by an agent often skip the auth middleware. Compare with neighbouring routes.
  2. Is the check on the server? Hiding a button in the UI is not a control.
  3. Is there an ownership check? Every read, update and delete by id must confirm the object belongs to the caller, or the caller holds a role that allows it.
  4. Is the identity taken from the session or token? Never from a request field such as userId in the body, which the caller controls.
  5. Do tokens and sessions behave? Short expiry, signature verified (never just decoded), cookies marked HttpOnly and Secure, logout that actually invalidates.
  6. Is the default deny? New routes should be closed until someone opens them.

Before and after

 app.get('/orders/:id', requireLogin, async (req, res) => {
-  const order = await db.orders.findById(req.params.id);
+  const order = await db.orders.findOne({ id: req.params.id, userId: req.user.id });
+  if (!order) return res.status(404).end();
   res.json(order);
 });

The fix puts the owner into the query itself, so there is no gap between loading and checking. Returning 404 rather than 403 avoids confirming that the id exists.

What goes wrong

  • Trusting the client. A role: "admin" field in a request body is accepted because the model copied the shape of the request.
  • Checking login only. The route has requireLogin and everyone feels safe.
  • Decoding a token instead of verifying it. A JSON Web Token that is only decoded can be forged by anyone.

Examples

The forgotten route

An agent adds DELETE /projects/:id next to existing routes that all use requireLogin and requireRole('editor'). The new route has neither. A diff review spots it by listing the middleware on each route side by side.

Ask for a test where a second user tries the call and expects a 403 or 404.

Key points

  • Authentication proves identity; authorisation limits what that identity may access. A route needs both.
  • Every read, update and delete by id needs an ownership or role check on the server.
  • Take the user identity from the verified session or token, never from a field the caller sends.
  • Compare new endpoints with their neighbours: agents often forget the auth middleware.
  • Putting the owner into the database query closes the gap between loading and checking.

Common mistakes

  • Confusing a logged-in user with an authorised one.
  • Trusting a user id or role sent in the request body.
  • Hiding a control in the UI and treating that as protection.
  • Decoding a token without verifying its signature and expiry.

Check yourself · 2 of 3

A generated endpoint loads an order by the id in the URL after the login check passes. What is still missing?

Choose one answer

Check yourself · 3 of 3

A generated update handler reads role from the request body to decide whether to allow an admin action. What is the fix?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic