Broken Access Control, IDOR and Session Management Flaws
If one web security category deserves your full attention, it is access control. It sits at number one on the OWASP Top 10 (2021) because applications so often get it wrong: they check who you are but forget to check what you are allowed to do. Closely related are session flaws, where the token that proves who you are is weakly generated, exposed or never expires. This lesson explains both with clear examples, safe and unsafe code, and lab practice you can do on a deliberately vulnerable app.
Authentication, Session, Authorisation: Three Different Jobs
| Concept | Question it answers | Typical mechanism |
|---|---|---|
| Authentication | Who are you? | Password plus MFA, passkey, single sign-on |
| Session management | Are you still the same person who logged in? | Session cookie or token |
| Authorisation (access control) | Are you allowed to do this to this object? | Roles, permissions, ownership checks, policies |
Many serious breaches happen with authentication working perfectly. The user is genuinely logged in, but the application never checks whether they may see another customer’s data.
Common Access Control Failures
| Failure | Example | Defence |
|---|---|---|
| Insecure direct object reference (IDOR) | A request for invoice number 1043 returns the invoice even when it belongs to someone else, so changing the number shows other people’s invoices | Check ownership on the server for every object; use authorisation policies |
| Missing function-level checks | A normal user can open the admin page or call an admin API simply by knowing the URL | Enforce role checks on every endpoint, not just in the menu |
| Privilege escalation through parameters | A profile update accepts a hidden role field, so users can promote themselves (mass assignment) | Allow-list the fields that may be updated; ignore the rest |
| Forced browsing | Hidden files or paths are protected only by being unlisted | Authentication and authorisation on all resources; no security by obscurity |
| Client-side only enforcement | A button is hidden in the interface, but the underlying request works for anyone | Enforce on the server; treat the client as untrusted |
| CORS misconfiguration | Overly broad cross-origin rules let other sites read authenticated responses | Explicit origin allow-lists; never reflect arbitrary origins with credentials |
| Metadata manipulation | Tampering with a token or hidden field to change identity or price | Sign and verify tokens; recompute sensitive values on the server |
Vulnerable and fixed code
# Python / Flask style illustration
@app.route("/invoice/<int:invoice_id>")
@login_required
def invoice_unsafe(invoice_id):
# DANGEROUS: any logged-in user can request any invoice number
return get_invoice(invoice_id)
@app.route("/invoice/<int:invoice_id>")
@login_required
def invoice_safe(invoice_id):
invoice = get_invoice(invoice_id)
# SAFE: ownership is verified on the server before returning data
if invoice is None or invoice.owner_id != current_user.id:
abort(404) # 404 avoids confirming that the record exists
return invoice
Notice the safe version returns the same error whether the record is missing or belongs to someone else. Making responses identical avoids revealing which identifiers exist.
Principles for strong access control
- Deny by default. If no rule grants access, access is refused.
- Enforce on the server, on every request.
- Least privilege. Users and services get only the permissions they need.
- Centralise logic. Use one tested authorisation layer instead of scattered checks.
- Prefer ownership and attribute checks, not only roles: “this user may edit their own orders”.
- Log denied access and alert on repeated failures.
- Test authorisation automatically with a matrix of roles against functions.
Session Management Flaws
After login, the server issues a session identifier. Anyone holding it can act as that user, so it must be protected like a password.
| Flaw | Risk | Defence |
|---|---|---|
| Predictable session IDs | Attackers can guess valid sessions | Use framework-generated, long random identifiers |
| Session ID in the URL | Leaks through logs, referrers and sharing | Use cookies, never URLs |
| Session fixation | An attacker sets a known session ID before login, which stays valid afterwards | Issue a new ID on login and privilege change |
| Missing cookie flags | Scripts or plain-HTTP traffic can expose the cookie | Set Secure, HttpOnly, SameSite |
| No expiry or logout invalidation | Stolen or old sessions remain usable | Idle and absolute timeouts; server-side invalidation on logout |
| Weak token design (for example, JWT misuse) | Forged or unverified tokens accepted | Verify signatures and algorithms, check expiry, audience and issuer, keep tokens short-lived |
Secure cookie example
Set-Cookie: sessionid=9f8e...long-random-value; Secure; HttpOnly; SameSite=Lax; Path=/; Max-Age=1800
Safe JWT checks (concept)
- Verify the signature with the expected algorithm; never accept tokens that declare “no signature”.
- Check expiry (
exp), issuer (iss) and audience (aud). - Keep access tokens short-lived and use refresh tokens that can be revoked.
- Do not store sensitive data in tokens: they are encoded, not encrypted.
Practice Lab 1: See Access Control Gaps Safely
- Start OWASP Juice Shop (
127.0.0.1:3000) following the home lab lesson and register two accounts, User A and User B, in separate browser profiles. - As User A, view your basket and note the request in the browser’s developer tools: look at the identifier in the path or body.
- Think about the question: “What would happen if I changed this identifier to User B’s?” Then try it on your own lab accounts and record the result.
- Write a short finding: affected function, cause (missing ownership check), impact, fix. Use the template from the report-writing lesson.
Practice Lab 2: Build and Test an Authorisation Matrix
Draw a table for any app you know, with roles across the top and functions down the side. Mark each cell as allowed or denied, then test every cell.
| Function | Guest | Customer | Staff | Admin |
|---|---|---|---|---|
| View public products | Yes | Yes | Yes | Yes |
| View own orders | No | Yes | Yes | Yes |
| View all orders | No | No | Yes | Yes |
| Change user roles | No | No | No | Yes |
Any cell that works but should not is a vulnerability. Turn the matrix into automated tests so regressions are caught.
Practice Lab 3: Inspect Session Security
- Log in to your lab app and open developer tools, Application tab, Cookies. Check the flags on the session cookie.
- Log out, then check whether the old cookie still works if you replay it in a lab-only test with a command-line tool such as
curlagainst your own app. Sessions must die server-side on logout. - Log in twice from two browsers and see whether the session ID changes. A new login should produce a new identifier.
- Decode a JWT if the app uses one (it is Base64URL text, so you can read it) and list its claims. Is there anything sensitive inside?
# Decode a JWT payload in your own lab (example only)
echo "<payload-part-of-token>" | tr '_-' '/+' | base64 -d 2>/dev/null
Detecting Access Control Abuse
- Alert when one account requests many different object identifiers in a short time.
- Alert on repeated 403 and 404 responses from a single user.
- Log administrative actions with user, time and target.
- Review privileged accounts and API keys regularly.
Practice Exercises
- Explain IDOR using an everyday analogy, such as a hotel key card that opens every room.
- Rewrite a vulnerable update-profile endpoint so only allowed fields can be changed.
- List four session cookie attributes and what each protects against.
- Why should an application return identical responses for “does not exist” and “not yours”?
- Design a logging rule that would reveal someone enumerating invoice numbers.
Mentor tip: When you test an application, always use two accounts at the same privilege level plus one higher-privilege account. Most access control bugs only reveal themselves when you compare what each account can reach.
Educational use only: test only applications you own, deliberately vulnerable lab apps, or targets with written permission.
- Authentication proves identity; authorisation decides what that identity may do.
- Check ownership and permissions on the server for every request; deny by default.
- Protect sessions with random IDs, secure cookie flags, rotation and expiry.
- Test with multiple accounts and an authorisation matrix.
- Log and alert on denied access and identifier enumeration.