# Authorization and Access Control

> Decide what each user may see and do with roles, ownership checks and multi-tenant rules enforced on the server. Free notes, key concepts, resources and a mini...

Canonical: https://www.internhack.in/roadmaps/tracks/full-stack/authorization-rbac
Last updated: 2026-11-28

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](https://www.internhack.in/roadmaps/tracks/full-stack/sessions-vs-jwt)

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

- [OWASP Authorization Cheat Sheet](https://cheatsheetseries.owasp.org/cheatsheets/Authorization_Cheat_Sheet.html) (OWASP)
- [OWASP Top 10](https://owasp.org/www-project-top-ten/) (OWASP)
- [PostgreSQL Row Security Policies](https://www.postgresql.org/docs/current/ddl-rowsecurity.html) (PostgreSQL)

Previous: [Auth with a Hosted Provider (Clerk)](https://www.internhack.in/roadmaps/tracks/full-stack/clerk) · Next: [Web Security Essentials](https://www.internhack.in/roadmaps/tracks/full-stack/web-security)

## More in Authentication, Authorization and Security

- [Authentication Fundamentals and Password Storage](https://www.internhack.in/roadmaps/tracks/full-stack/auth-fundamentals)
- [Sessions, Cookies and JWTs](https://www.internhack.in/roadmaps/tracks/full-stack/sessions-vs-jwt)
- [OAuth 2.0, OpenID Connect and Social Login](https://www.internhack.in/roadmaps/tracks/full-stack/oauth-and-social-login)
- [Auth with Auth.js (NextAuth)](https://www.internhack.in/roadmaps/tracks/full-stack/authjs)
- [Auth with a Hosted Provider (Clerk)](https://www.internhack.in/roadmaps/tracks/full-stack/clerk)
- [Authorization and Access Control](https://www.internhack.in/roadmaps/tracks/full-stack/authorization-rbac)
- [Web Security Essentials](https://www.internhack.in/roadmaps/tracks/full-stack/web-security)

## Related

- [Roadmaps](https://www.internhack.in/roadmaps)
- [Full-Stack Developer Roadmap](https://www.internhack.in/roadmaps/tracks/full-stack)
