00:00

#0804

Role Claim Trust

Medium+100 XPA07:2021 Identification and Authentication FailuresCWE-347
JWTAuthorizationRole Escalation

Scenario

Your API accepts JWT tokens and trusts the 'role' claim directly for authorization decisions.

The server maintains a list of trusted roles, but never checks whether the claimed role is in that list.

An attacker can craft a token with role='admin' and gain elevated privileges.

Trusting unverified role claims from tokens without a server-side allowlist enables horizontal and vertical privilege escalation — a common pattern in API authorization bypasses.

Your Tasks

  1. Inspect authorise — it checks payload.role === requiredRole but never verifies the role is in trustedRoles.
  2. Fix it: throw 'Untrusted role claim' if payload.role is not in trustedRoles.
  3. If role IS in trustedRoles but doesn't match requiredRole, throw 'Insufficient role'.
  4. If role is in trustedRoles and matches requiredRole, return {authorised:true}.

Examples

Example 1Untrusted role bypasses authorization (bug)

authorise({sub:'eve',role:'admin'}, 'admin', ['user'])
// Returns {authorised:true} — should throw 'Untrusted role claim'!

Example 2Trusted role with correct permission

authorise({sub:'alice',role:'admin'}, 'admin', ['admin','user'])
// Returns {authorised:true}

Constraints

  • payload has sub (string) and role (string)
  • requiredRole is a string
  • trustedRoles is an array of strings
  • Throw exactly 'Untrusted role claim' or 'Insufficient role' — no other messages

Hint

References

solution.js
Ln 1, Col 1UTF-8JavaScript
Sandbox ready
0/0/0not run