Cybersecurity-Mastery

Home › Web Security › OAuth 2.0 and 2.1

OAuth 2.0 and 2.1

Grant types, PKCE, redirect URI validation, token leakage and confused-deputy attacks.

Advanced⌨ ~72 min hands-on📖 ~24 min read

▶ Here to build, not read? Jump to the Hands-On Practice.

Prerequisites: Web Authentication · Authorization · JSON Web Tokens

Overview

OAuth 2.0 is an authorization framework: it lets a user grant a third-party application limited access to their resources on another service without sharing their password, by issuing scoped access tokens. It is the machinery behind "Sign in with Google" and countless API integrations. Crucially, OAuth is about delegated authorization (what an app may do), not authentication (who the user is) — conflating the two is the source of a whole class of vulnerabilities, which is exactly why OpenID Connect was layered on top to do authentication properly.

OAuth's security rests on a small number of parameters being validated strictly: the redirect_uri (where the authorization response is sent), the state (CSRF protection), and — in modern flows — PKCE (which binds the authorization request to the client that started it). Most OAuth breaks are not cryptographic; they are validation failures at these seams that let an attacker steal an authorization code or token, or trick a client into accepting one it should not.

How it works

The recommended authorization-code flow with PKCE:

  1. The client redirects the user to the authorization server with client_id, redirect_uri, scope, state, and a PKCE code_challenge.
  2. The user authenticates and consents; the server redirects back to the registered redirect_uri with an authorization code and the state.
  3. The client verifies state, then exchanges the code (plus the PKCE code_verifier) at the token endpoint for an access token (and optionally a refresh token).
  4. The client calls the resource API with the bearer access token.

The trust hinges on: exact redirect_uri matching (so codes cannot be sent to an attacker), state verification (so responses cannot be forged/CSRF'd), and PKCE (so a stolen code cannot be redeemed by anyone but the original client). Weaken any one and specific attacks open up.

flowchart TD
    A["OAuth 2.0 and 2.1"] --> B["Concepts & terminology"]
    A --> C["Internal mechanics"]
    C --> D["Trust boundaries"]
    D --> E["Attack surface"]
    D --> F["Defensive controls"]
    E --> G["Detection & telemetry"]
    F --> G
    G --> H["Verification in the lab"]
🔑 Key terms
  • Authorization code — A short-lived code exchanged for tokens at the token endpoint — the safest grant.
  • Access token / Refresh token — The bearer credential for API calls / a longer-lived credential to get new access tokens.
  • redirect_uri — Where the authorization response is delivered; must be matched exactly to prevent code theft.
  • state — An opaque value echoed back to bind request and response — CSRF protection.
  • PKCE — Proof Key for Code Exchange — binds the code to the client that requested it (RFC 7636).
  • Scope — The specific, limited permissions a token grants.
Example
In the wild. Account takeover via redirect_uri + open redirect (recurring bug-bounty pattern). Researchers

repeatedly chain a client's open redirect with permissive redirect_uri validation to exfiltrate authorization codes and take over accounts. It is the clearest demonstration that OAuth's safety is only as strong as its redirect and state validation — the crypto is rarely the problem.

Hands-On Practice

Tip
This is the main event. Bring up the lab, then work the walkthrough — every command has a ▸ Run button (needs the lab runner) and a 📋 Copy button. ~72 min, Kali attacker, fully local.

🧪 Lab setup — Web Security

  • bWAPP — http://localhost:8081 — 100+ web bugs, best for targeted practice
  • DVWA — http://localhost:8082 — adjustable security levels
  • Juice Shop — http://localhost:8083 — modern SPA, full OWASP Top 10
  • WebGoat — http://localhost:8084 — guided lessons
docker-compose.yml
# web-lab/docker-compose.yml  —  Web Security part environment
services:
  bwapp:
    image: raesene/bwapp
    ports: ["8081:80"]
    networks: [labnet]
  dvwa:
    image: vulnerables/web-dvwa
    ports: ["8082:80"]
    networks: [labnet]
  juice-shop:
    image: bkimminich/juice-shop
    ports: ["8083:3000"]
    networks: [labnet]
  webgoat:
    image: webgoat/webgoat
    ports: ["8084:8080"]
    networks: [labnet]

networks:
  labnet:
    driver: bridge
    # routed here so your browser/Burp on the host can reach the apps;
    # for a jeopardy-style setup put the attacker box on labnet and set internal: true

Uses the Web Security lab environment.

Guided walkthrough

1Provision & baseline
command
# bring up the part environment, then confirm you can reach `bWAPP` from Kali
Expected output
Target reachable; you have a clean starting point.
Always capture 'normal' before you change anything — it's your reference.
2Reproduce the technique
command
# perform the core oauth 2.0 and 2.1 technique step by step against bWAPP
Expected output
You observe the expected effect and can repeat it reliably.
Owning the mechanism by hand beats running a tool you don't understand.
3Observe what happened
command
# inspect logs / responses / process or network activity
Expected output
You can point to exactly where and why it worked.
The evidence here is what a defender would alert on.
4Apply the primary control
command
# implement the single most important fix for this technique
Expected output
Repeating the technique now fails.
Fix the class, not the one payload — then prove it's closed.

Live terminal

kali@oauth — start the lab runner for a live shelloffline
Start the Lab Runner (see labserver/README.md), then click Connect lab runner and paste the token. Until then, copy commands from the walkthrough and run them in your own Kali terminal.
$

Try it yourself

Note
Try each for real. Open the hint only when stuck, the solution only to check.

Challenge 1. Extend the walkthrough with one variation of the oauth 2.0 and 2.1 technique and predict the result before running it.

Hint

Change one variable (payload, target, or control) at a time.

Solution

Answers vary — the goal is a correct prediction confirmed by observation.

Challenge 2. Find and apply a second defensive control for oauth 2.0 and 2.1 and show it also blocks the technique.

Hint

Think in layers: prevention, detection, and least privilege.

Solution

Any independent control that breaks the chain counts — name why it works.

Challenge 3. Write a one-paragraph report of your oauth 2.0 and 2.1 test mapped to CWE/ATT&CK and the fix.

Hint

Structure: finding → impact → evidence → remediation.

Solution

A defender should be able to act on your report without asking you anything.

Detect & defend (blue-team view)

  • Re-run the technique while watching your telemetry — attack and detection are two views of one event.
  • Turn what you observed into a detection rule (Sigma/YARA) and confirm it fires without flooding.

Skills check

You can move on when you can, without notes:

Going deeper — The classic OAuth attacks — all validation failures

Each maps to a weakened parameter:

  • redirect_uri manipulation — loose matching (prefix/substring/open redirect on the client) lets the code be delivered to the attacker. Fix: exact, pre-registered matching.
  • Missing/unchecked state — enables CSRF (login CSRF, forced account linking). Fix: generate, bind and verify state.
  • Authorization code interception — a public client's code stolen in transit is replayed. Fix: PKCE.
  • Implicit grant token leakage — tokens in the URL fragment leak via history/referrer. Fix: don't use implicit; use code+PKCE (RFC 9700).
  • Token/audience confusion — using OAuth for authentication and trusting an access token as proof of identity; or accepting a token minted for another client. Fix: use OpenID Connect ID tokens and validate aud/iss.
  • Mix-up attacks — a client confused about which authorization server responded. Fix: the iss parameter and strict metadata handling.

Check Yourself

Quick self-test — answers hidden. If you can't answer without notes, redo the relevant walkthrough step.

Q1. In one sentence, what security problem does OAuth 2.0 and 2.1 address or create?

Show answer

OAuth 2.0 and 2.1 matters because its behaviour determines where trust is placed; misplacing that trust is the root of the associated bug class.

Q2. Name one trust boundary involved in OAuth 2.0 and 2.1 and what crosses it.

Show answer

Any point where attacker-influenced data meets privileged processing — e.g. user input reaching an interpreter, or a token reaching a verifier.

Q3. Give one attack against OAuth 2.0 and 2.1 and the single control that most reduces its impact.

Show answer

State the attack precisely, then the primary control (validation, encoding, isolation, authentication, or least privilege) — not a laundry list.

Interview angles

  • Walk me through how OAuth 2.0 and 2.1 works, then tell me where you would attack it.
  • How would you detect OAuth 2.0 and 2.1 being abused in an environment you defend?
  • What is the difference between mitigating and eliminating the risk associated with OAuth 2.0 and 2.1?

Pitfalls & Best Practice

Common mistakes

  • Loose redirect_uri matching (prefix/wildcard) instead of exact, pre-registered URIs.
  • Omitting or not verifying the state parameter, opening CSRF and forced account-linking.
  • Using the deprecated implicit grant and exposing tokens in URL fragments.
  • Treating an OAuth access token as proof of identity instead of using OpenID Connect.
  • Not validating token aud/iss, allowing token/audience confusion across clients.

Do this instead

  • Use the authorization-code flow with PKCE for all client types; avoid the implicit grant.
  • Require exact redirect_uri matching against a pre-registered allow-list.
  • Generate, bind and verify state on every authorization request.
  • For authentication, use OpenID Connect and validate the ID token's signature, aud and iss.
  • Scope tokens minimally, keep lifetimes short, and store refresh tokens securely.

Reference

MITRE ATT&CK Mapping

TechniqueID
ATT&CK techniqueT1528

OWASP Mapping

  • See OWASP Top 10 for the relevant category when this applies to web systems.

CWE Mapping

CWEWeakness
CWE-601Open Redirect
CWE-352CSRF

CAPEC Mapping

  • No single attack pattern; see the CAPEC chapter to map behaviour to patterns.

CVE References

  • No canonical CVE for a concept page. Search the NVD for concrete instances when studying real cases.

NIST References

RFC References

Research Papers

  • Reflections on Trusting Trust — Ken Thompson, 1984
  • Smashing the Stack for Fun and Profit — Aleph One, Phrack 49, 1996
  • The Protection of Information in Computer Systems — Saltzer & Schroeder, 1975

Books

  • The Web Application Hacker's Handbook, 2nd ed. — Stuttard & Pinto
  • Real-World Bug Hunting — Peter Yaworski
  • The Tangled Web — Michal Zalewski
  • Web Security for Developers — Malcolm McDonald

Official Documentation


Part of the Web Security section of Cybersecurity-Mastery. 🟠 Advanced · ~72 min hands-on · Last updated 2026-08-12.

Cybersecurity-Mastery — interactive security learning · runs locally