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

ConceptQuestion it answersTypical mechanism
AuthenticationWho are you?Password plus MFA, passkey, single sign-on
Session managementAre 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

FailureExampleDefence
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 invoicesCheck ownership on the server for every object; use authorisation policies
Missing function-level checksA normal user can open the admin page or call an admin API simply by knowing the URLEnforce role checks on every endpoint, not just in the menu
Privilege escalation through parametersA 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 browsingHidden files or paths are protected only by being unlistedAuthentication and authorisation on all resources; no security by obscurity
Client-side only enforcementA button is hidden in the interface, but the underlying request works for anyoneEnforce on the server; treat the client as untrusted
CORS misconfigurationOverly broad cross-origin rules let other sites read authenticated responsesExplicit origin allow-lists; never reflect arbitrary origins with credentials
Metadata manipulationTampering with a token or hidden field to change identity or priceSign 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

  1. Deny by default. If no rule grants access, access is refused.
  2. Enforce on the server, on every request.
  3. Least privilege. Users and services get only the permissions they need.
  4. Centralise logic. Use one tested authorisation layer instead of scattered checks.
  5. Prefer ownership and attribute checks, not only roles: “this user may edit their own orders”.
  6. Log denied access and alert on repeated failures.
  7. 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.

FlawRiskDefence
Predictable session IDsAttackers can guess valid sessionsUse framework-generated, long random identifiers
Session ID in the URLLeaks through logs, referrers and sharingUse cookies, never URLs
Session fixationAn attacker sets a known session ID before login, which stays valid afterwardsIssue a new ID on login and privilege change
Missing cookie flagsScripts or plain-HTTP traffic can expose the cookieSet Secure, HttpOnly, SameSite
No expiry or logout invalidationStolen or old sessions remain usableIdle and absolute timeouts; server-side invalidation on logout
Weak token design (for example, JWT misuse)Forged or unverified tokens acceptedVerify 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

  1. 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.
  2. 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.
  3. 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.
  4. 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.

FunctionGuestCustomerStaffAdmin
View public productsYesYesYesYes
View own ordersNoYesYesYes
View all ordersNoNoYesYes
Change user rolesNoNoNoYes

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

  1. Log in to your lab app and open developer tools, Application tab, Cookies. Check the flags on the session cookie.
  2. 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 curl against your own app. Sessions must die server-side on logout.
  3. Log in twice from two browsers and see whether the session ID changes. A new login should produce a new identifier.
  4. 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

  1. Explain IDOR using an everyday analogy, such as a hotel key card that opens every room.
  2. Rewrite a vulnerable update-profile endpoint so only allowed fields can be changed.
  3. List four session cookie attributes and what each protects against.
  4. Why should an application return identical responses for “does not exist” and “not yours”?
  5. 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.

Key takeaways
  • 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.

Related Lessons