Authorization and Access Control

Decide what each user may see and do with roles, ownership checks and multi-tenant rules enforced on the server.

Intermediate · about 5 hours · difficulty 3/5

Learn first: Sessions, Cookies and JWTs

Most serious API vulnerabilities are authorization bugs: a logged-in user reads or changes data that belongs to someone else. Broken access control tops the OWASP Top 10.

Models

  • Role-based (RBAC): STUDENT, RECRUITER, PLACEMENT_OFFICER, ADMIN with defined permissions.
  • Ownership checks: a student may edit only their own applications.
  • Multi-tenancy: a recruiter sees only applicants for their own company's openings; a placement officer sees only their college.
  • Attribute-based rules for finer control when roles are not enough.

Implementation

  • Put checks in one place: a can(user, action, resource) helper or policy functions used by every handler and server action.
  • Scope queries by the user: where: { id, studentId: user.id } instead of fetching by id and checking later.
  • Return 404 rather than 403 when revealing a record's existence is itself a leak.

Testing it

Write tests that log in as user A and try to read and update user B's records. This is the IDOR (insecure direct object reference) test, and it catches real bugs.

Pitfalls

  • Hiding buttons in the UI and calling it authorization.
  • Trusting a role or userId sent in the request body.
  • Sequential ids in URLs combined with missing ownership checks.

You are ready to move on when every mutation in your portal goes through a policy check, and an automated test proves a student cannot access another student's application.

Key concepts

  • Role-based access control
  • Ownership checks
  • Multi-tenant data scoping
  • Central policy functions
  • IDOR testing
  • Attribute-based access control
  • Row-level security in PostgreSQL

Mini project

Add STUDENT, RECRUITER and PLACEMENT_OFFICER roles to your portal with a central policy module, scope every query by user or company, and write tests that attempt cross-user access.

Check yourself

GET /applications/123 returns the application if the user is logged in. What is missing, and how would you fix it at the query level?

Free resources