← Back to Node.js, page 9

nodejs-0163 · Hard senior

What is broken object level authorization (IDOR), and how do you design authorization checks with roles and ownership in a Node.js API?

Authentication says who you are, authorization says what you may do. IDOR (broken object level authorization) happens when a route trusts an id from the URL and returns the object without checking that it belongs to the caller, so changing /docs/2 to /docs/3 shows someone else's data. Fix it by checking permission for every object on the server, using one policy function (role plus ownership), scoping queries by owner, and not trusting client-supplied owner fields.

Full answer

The bug

  • GET /docs/:id loads the document by id and returns it. The user is logged in, so the route passes the authentication check, but nothing asks whether this user may see this document. Anyone can enumerate ids. It is number one on the OWASP API Security list.

Defences

  • Check on the server, every time, for every object and every action (read, update, delete). Do not rely on hidden ids or UI buttons.
  • One policy function: can(user, action, resource) that combines role rules (RBAC: admin may delete anything, teacher may read students in their class) and ownership or relationship rules (ABAC: owner may update). Call it from every route, so rules live in one tested place.
  • Scope queries: WHERE id = $1 AND owner_id = $2, or tenant_id = current tenant. A row that does not belong to you is simply not found.
  • Do not accept owner or role fields from the request body (mass assignment). Set them on the server from the session.
  • Return 404 instead of 403 when revealing existence is itself a leak. Use 403 when the user knows the resource exists and lacks rights.
  • Unpredictable ids (UUIDs) make guessing harder, but they are not an authorization control.
  • Test authorization: for each route, a test that user B cannot read, change or delete user A's object.

Related issues: broken function level authorization (a normal user calls an admin route), privilege escalation by changing the role in a token, and missing checks in background jobs and GraphQL resolvers.

javascript
const express = require('express');

const docs = new Map([[1, { id: 1, ownerId: 'asha', title: 'Asha notes' }], [2, { id: 2, ownerId: 'ravi', title: 'Ravi salary' }]]);
const roles = { asha: 'student', ravi: 'student', teacher1: 'teacher', root: 'admin' };

// One policy function: role rules plus ownership.
function can(user, action, doc) {
  if (roles[user] === 'admin') return true;
  if (action === 'read' && roles[user] === 'teacher') return true;
  return doc.ownerId === user;
}

const app = express();
app.use(express.json());
app.use((req, res, next) => { req.user = req.get('x-user'); next(); });     // stands in for real authentication

app.get('/vulnerable/docs/:id', (req, res) => {
  const doc = docs.get(Number(req.params.id));
  doc ? res.json(doc) : res.status(404).json({ error: 'not found' });      // no ownership check: IDOR
});

app.get('/docs/:id', (req, res) => {
  const doc = docs.get(Number(req.params.id));
  if (!doc || !can(req.user, 'read', doc)) return res.status(404).json({ error: 'not found' });   // 404 hides existence
  res.json(doc);
});
app.put('/docs/:id', (req, res) => {
  const doc = docs.get(Number(req.params.id));
  if (!doc || !can(req.user, 'read', doc)) return res.status(404).json({ error: 'not found' });
  if (!can(req.user, 'update', doc)) return res.status(403).json({ error: 'forbidden' });
  doc.title = String(req.body.title);                                      // ownerId from the body is ignored
  res.json(doc);
});

const server = app.listen(0, '127.0.0.1', async () => {
  const base = 'http://127.0.0.1:' + server.address().port;
  const call = async (label, path, user, method = 'GET', body) => {
    const res = await fetch(base + path, { method, headers: { 'x-user': user, 'content-type': 'application/json' }, body: body && JSON.stringify(body) });
    console.log(label.padEnd(44), res.status, JSON.stringify(await res.json()));
  };
  await call('asha reads doc 2 (vulnerable route)', '/vulnerable/docs/2', 'asha');
  await call('asha reads doc 2 (policy route)', '/docs/2', 'asha');
  await call('asha reads her own doc 1', '/docs/1', 'asha');
  await call('teacher reads doc 1 (role rule)', '/docs/1', 'teacher1');
  await call('teacher updates doc 1 (not allowed)', '/docs/1', 'teacher1', 'PUT', { title: 'x' });
  await call('asha updates doc 1, tries to change owner', '/docs/1', 'asha', 'PUT', { title: 'Renamed', ownerId: 'ravi' });
  server.close();
});

The first request shows the IDOR bug. The policy routes allow owners and the teacher read rule, hide other people's documents, and ignore an owner field sent in the body. Install first with npm install express (run here with express 5.3.0). Save it as app.js and run it with node app.js. Output (Node.js v24.22.1, run once): asha reads doc 2 (vulnerable route) 200 {"id":2,"ownerId":"ravi","title":"Ravi salary"} asha reads doc 2 (policy route) 404 {"error":"not found"} asha reads her own doc 1 200 {"id":1,"ownerId":"asha","title":"Asha notes"} teacher reads doc 1 (role rule) 200 {"id":1,"ownerId":"asha","title":"Asha notes"} teacher updates doc 1 (not allowed) 403 {"error":"forbidden"} asha updates doc 1, tries to change owner 200 {"id":1,"ownerId":"asha","title":"Renamed"}

Key points

  • IDOR means the object was returned without an ownership check.
  • Authorize every object and action on the server.
  • Use one tested policy function and owner-scoped queries.
  • Never accept owner or role from the request body.
  • Test that user B cannot touch user A's data.

Common mistakes

  • Checking only that the user is logged in.
  • Relying on UUIDs or hidden ids as protection.
  • Accepting ownerId or role from the request body.
  • Putting the checks only in the UI.
  • Forgetting the same checks in GraphQL resolvers and background jobs.

Follow-ups

  • When do you return 403 and when 404?
  • How would you scope every database query to the current tenant?
  • How do you test authorization rules in CI?

Tips

  • Define IDOR with the URL example.
  • Say 'one policy function'.
  • Mention mass assignment.