Broken access control

Authentication tells you who someone is. Authorisation decides what they may do, and it must be enforced on the server.

Intermediate35 min

Learning objectives

  • Recognise insecure direct object references (IDOR)
  • Enforce ownership checks inside data queries
  • Prevent mass assignment by allowlisting fields

Broken access control is the most common risk

It tops the OWASP Top 10 because it is easy to create by accident. Each route must answer two questions on every request: is this user logged in? and is this user allowed to act on this specific resource?

Insecure direct object references (IDOR)

An IDOR happens when an identifier from the request is used to fetch data without checking that the current user owns it.

// VULNERABLE: any logged-in user can read any invoice by changing the id
app.get('/invoices/:id', requireUser, async (req, res) => {
  const invoice = await db.get('SELECT * FROM invoices WHERE id = ?', [req.params.id]);
  res.json(invoice);
});

The fix is to put the ownership check in the query itself, so the database never returns data the user should not see:

// SAFE: ownership is part of the lookup
app.get('/invoices/:id', requireUser, async (req, res) => {
  const invoice = await db.get(
    'SELECT * FROM invoices WHERE id = ? AND owner_id = ?',
    [req.params.id, req.user.id]
  );

  if (!invoice) {
    return res.status(404).json({ error: 'Not found' });
  }

  res.json(invoice);
});

Returning 404 instead of 403 avoids confirming that the resource exists.

Mass assignment

Frameworks that map request bodies straight onto models can let users set fields they should not control, such as isAdmin or ownerId.

// VULNERABLE: the client decides which fields change
await db.run('UPDATE users SET ? WHERE id = ?', [req.body, req.user.id]);

// SAFE: only the fields the user is allowed to edit
const { displayName, bio } = req.body;
await db.run(
  'UPDATE users SET display_name = ?, bio = ? WHERE id = ?',
  [displayName, bio, req.user.id]
);

Design principles

  • Deny by default. A new route should be inaccessible until a rule explicitly allows it.
  • Enforce on the server, every time. Hiding a button in the UI is not access control. Attackers call APIs directly.
  • Centralise the rules. A single policy function or middleware is easier to audit than checks scattered through handlers.
  • Protect administrative paths with the same checks, not with obscurity. Hidden URLs are not secret.
  • Log access decisions for sensitive resources so you can detect abuse.

Lab: test with two accounts

  1. Create two users in a local app: Alice and Bob.
  2. Sign in as Alice and note the ID of one of her records.
  3. Sign in as Bob and request Alice’s record by its ID.
  4. The correct response is 404 or 403, with no data returned.

Write this as an automated test so the rule stays enforced as the code changes.

Key takeaways

  • Every data access needs an authorisation check on the server.
  • Put ownership conditions in the query, and return 404 for records the user cannot see.
  • Allowlist the fields a user may write, and never pass raw request bodies into updates.