Skip to content
build-3000
security

Authorization and Roles

Who may do what - ownership checks, roles and the least-privilege mindset.

Before this: Authentication

Objective

Enforce per-user data isolation and role-based permissions server-side, centralize authorization decisions, and internalize that hiding buttons is not access control.

Vocabulary

The words to use when directing your agent.

authorization
Deciding WHAT an authenticated identity may do - the other half of security.
ownership check
Confirming a record belongs to the requester before operating on it.
role
A named bundle of permissions - admin, member - assigned to users.
IDOR
Insecure Direct Object Reference: reaching others' records by guessing URLs or ids.
least privilege
Granting the minimum permissions needed - nothing extra 'for later'.

Understand it

IDOR: the one-change exploit

Authenticated user changes /expenses/41 to /expenses/42 and reads a stranger's data. No hacking tools - one edited digit. Prevention: every fetch/update/delete query includes WHERE user_id = current_session_user. Ownership rides along with every query, automatically.

Deny by default

New routes start forbidden until explicitly permitted. The reverse - open until someone remembers to restrict - guarantees eventual leaks. Whitelist thinking applies to permissions exactly as it did to input.

Core concepts

Authentication is not authorization

Logging in proves identity; it grants NOTHING by itself. Every operation re-asks: may THIS user act on THAT record? The question is asked fresh on every request, forever, no exceptions.

Guards are code, not CSS

Hidden menu items and disabled buttons are decoration. The API is the castle: if DELETE /expenses/43 lacks an ownership check, anyone logged-in deletes anyone's data by typing the URL. Authorization lives where the data lives.

Examples

Two roles, visible difference

Owner role manages workspace members; member role sees tasks. The permission matrix is three lines long - and every line maps to one guard function. Small matrices stay auditable; sprawling ones rot.

Centralized guard in action

requireOwnership('expense') runs before update/delete handlers. One function, one place to audit, applied everywhere via route wiring. Scattered if-checks across twenty handlers WILL miss one eventually.

Common mistakes

Common mistake

Reading the target record first, checking ownership second - or trusting an id from the request body.

Fix

Identity comes from the session; scope every query by it. Fetch-with-filter beats fetch-then-hope.

Common mistake

Granting broad permissions 'temporarily' during development.

Fix

Develop with the real permission matrix from day one; temporary openings become permanent breaches.

Working with your AI agent

Have your agent emit the full permission matrix as a table - role x action x allowed - before implementation, then wire every route through named guard functions matching that matrix. The matrix becomes your audit artifact and your test checklist simultaneously.

Build it

The module’s real project. Your agent builds; you direct and inspect.

Project: Private Ledger

Turn the tracker into a truly multi-user app: total data isolation between users, an admin/member role split, centralized guards - proven by an isolation matrix you can demo on demand.

Requirements

  • Each user's expenses fully isolated: reads, updates, deletes scoped by session identity in every query
  • Roles implemented: owner (manages members, sees workspace summary) vs member (owns only own data)
  • All guards centralized in named middleware/functions mapped to a written permission matrix
  • Unauthorized cross-access attempts return 403 envelope and are logged with actor and target
  • Permission matrix documented in README and matches implementation exactly

Context for your agent

Convert my authenticated Expense API into a private multi-user ledger. Add roles (owner, member) with a written permission matrix BEFORE implementation; centralize all authorization in named guard functions wired per-route; scope every store query by session user id; log denied attempts with actor and target resource. Demo isolation with two seeded users afterward: every cross-access attempt must fail with 403.

Verify it - twice

Check every item yourself first. Then hand your agent the prompt and compare verdicts.

Self-verified0/11
Functional
Architecture
Security
Testing
Production

Verification prompt - paste into your AI agent

Attack my Private Ledger's authorization as a hostile insider. Using two seeded accounts, attempt: reading user B's expenses as user A by id-guessing, updating B's record via crafted requests, deleting B's data, escalating from member to owner via request-body manipulation, and hitting owner-only routes as member. For each attempt report: HTTP result, whether anything leaked, PASS or FAIL against the DoD. Then diff my README permission matrix against the actual route wiring and name any guard that exists in one but not the other.

Run this against “Authorization and Roles” after your own checklist pass. Where the agent and you disagree, investigate before moving on.

Review questions

Answer out loud. If you cannot, revisit before moving on.

  1. State the difference between authentication and authorization in one sentence each.

  2. Walk through how IDOR would work against an app lacking ownership checks.

  3. Why is deny-by-default cheaper than cleaning up over-granted access later?

You can now

  • Design and enforce role-based permission matrices
  • Prevent IDOR structurally, query-by-query
  • Audit authorization as a system, not scattered checks

Up next

Testing Your App