Skip to content

Lesson 2 of 9 · 22m

Authentication, Tokens, and Scope Confusion

Decode tokens, compare server-enforced scopes with client assumptions, and identify the auth edges where APIs over-trust callers.

Most API authentication is boring until it suddenly isn't. The dangerous mistakes are rarely "the password check was missing." They are token confusion, scope confusion, audience confusion, and authorization checks that trust a claim because it looks official. Treat the token as input, not truth.

Decode before you attack

JWTs are usually signed, not encrypted. Decode them locally and inspect every claim:

TOKEN='eyJhbGciOiJIUzI1NiIs...'
printf '%s' "$TOKEN" | cut -d. -f2 | tr '_-' '/+' | base64 -d 2>/dev/null | jq

Pay attention to aud, iss, exp, scope, sub, tenant IDs, role arrays, and custom claims. Then compare what the server does with what the client believes. A frontend may hide an admin button, while the backend happily honors a token containing a role the user interface never expected to see.

Bearer means bearer — test possession boundaries

A bearer token is a key, not an identity proof in the human sense. Replay it from another IP, device, and user-agent where rules permit. Rotate it after password reset, logout, account disablement, and organization removal. If any of those transitions leaves the token valid, you have session lifetime or revocation failure.

curl -sS https://api.target.local/v1/me   -H "authorization: Bearer $TOKEN" | jq

Scope confusion is a silent privilege escalation

OAuth scopes are often coarse (read, write) while resource authorization is fine-grained (invoice:read for your tenant only). Test combinations deliberately:

token intended for public API   -> internal-only route
user token                      -> machine-to-machine route
read-only OAuth client          -> mutating endpoint
tenant A token                  -> tenant B object
service account JWT             -> user-scoped route

Also test alg confusion and key selection only in a lab. The classic bug is accepting alg: none or choosing attacker-controlled keys through jku/kid. Modern libraries are safer, but custom verification middleware keeps reintroducing the same mistakes.

GraphQL and RPC make auth less visible

A single /graphql route can front dozens of operations. Authorization must be per field and per object, not merely per endpoint. If a query succeeds but a mutation fails, do not stop: mutations often nest object fetch paths through differently protected resolvers.

Field notes

  • Never report "JWT exposed in URL" without checking whether it is actually a bearer credential; correlation IDs are not tokens.
  • sub is not always a user ID. In machine-to-machine flows it may be a client ID with entirely different privilege semantics.
  • Clock skew can make an expired token look valid in one test and invalid in the next; use fresh tokens for lifecycle tests.
  • If cookies and bearer tokens are both accepted, test CSRF and token precedence separately.

Was this lesson helpful?

Sign in to rate