# What is privilege escalation in a web app?

Canonical: https://hackergpt.org/learn/privilege-escalation/

**Privilege escalation is an account gaining permissions it was never granted. In a web app it is usually vertical, a customer reaching admin functions or giving itself an admin role, or horizontal, one user acting on another user's data. Both happen when the server trusts the client to enforce roles instead of checking them itself.**

Part of the roadmap: [Stage 5, Deeper web and APIs](https://hackergpt.org/learn/roadmap/#deeper)

## Why it matters

OWASP puts it under broken access control, the first category of the Top 10, whose list of common failures names "acting as an admin when logged in as a user". MITRE files the weakness as CWE-269, improper privilege management, and the missing check behind most cases as CWE-862, missing authorization. In APIs the vertical kind is API5:2023, broken function level authorization.

Its impact is the largest of the access-control bugs, because an admin role reaches every user's data at once. It is also easy to ship: the admin link is hidden in the interface, the page works for admins, and nobody sends the request behind it with a customer's session. Hiding a button is a design choice, not a permission check.

## Example: a customer acting as an admin on CyberGym

CyberGym, the legal practice target (OWASP Juice Shop), refuses its user list at /api/Users with 401 when you are logged out, so it checks authentication. With an ordinary customer's token the same request returned 200 and every account, admins included. A registration request with an extra "role":"admin" field the form never offered created an admin account. These are its Admin Section and Admin Registration challenges.

On your own app (https://your-app.example), log in with the lowest role you have and send each admin request with that session. The fix is a role check on the server, on every one of those routes, and an allow-list of the fields a user may set:

```
// Check the role on the server, not by hiding the link
app.get("/api/users", requireRole("admin"), listUsers);

// Copy only the fields a user may set; never the request body as a whole
const user = await Users.create({ email: req.body.email, password: req.body.password });
```

Run the same test after every new admin feature, since each one is a new route that needs its own check. Horizontal escalation is tested the same way with a second ordinary account, and is covered on the IDOR page.

## Common mistakes

- **Hiding admin links instead of checking the role.** Anyone can send the request behind a hidden button. Check the role on the server for every route.
- **Building a record straight from the request body.** An extra role or isAdmin field sets itself. List the fields a user may write.
- **Trusting a role claim the client can change.** A role in a cookie, a hidden field or an unverified token is user input. Read it from the server's own record.
- **Testing only as an admin.** Admin requests work for admins. Repeat them with the lowest role and with no session.

## Related terms

- [Broken access control](https://hackergpt.org/learn/broken-access-control/)
- [IDOR](https://hackergpt.org/learn/idor/)
- [BOLA](https://hackergpt.org/learn/bola/)
- [JSON Web Token (JWT)](https://hackergpt.org/learn/jwt/)

## Go deeper

- [Privilege escalation in a web app: from user to admin](https://hackergpt.org/blog/privilege-escalation-web-app/)
- [API security testing checklist](https://hackergpt.org/blog/api-security-testing-checklist/)
- [AppMap: every endpoint with the auth it requires](https://hackergpt.org/features/appmap/)

## Sources

- [OWASP Top 10:2021 A01 Broken Access Control](https://owasp.org/Top10/A01_2021-Broken_Access_Control/)
- [OWASP API Security Top 10 2023: API5 Broken Function Level Authorization](https://owasp.org/API-Security/editions/2023/en/0xa5-broken-function-level-authorization/)
- [CWE-269: Improper Privilege Management](https://cwe.mitre.org/data/definitions/269.html)
- [CWE-862: Missing Authorization](https://cwe.mitre.org/data/definitions/862.html)

Test only sites you own or have written permission to test. To practise without one, use CyberGym, a legal, deliberately vulnerable target. [Practise on CyberGym](https://cybergym.hackergpt.app).
