8b. OAuth
1. OAuth Security Overview
OAuth is a widely used authorization framework that enables secure access delegation for web, mobile, and desktop applications without sharing credentials.
1.1 OAuth Workflow
graph LR
A[Resource Owner/User] -->|Authorize| B[Authorization Server]
B -->|Authorization code| C[Client callback]
C -->|Code + PKCE verifier| E[Token endpoint]
E -->|Access token| C
C -->|Request Resource| D[Resource Server]
D -->|Provide Resource| CKey Entities in OAuth:
- Resource Owner (User): Grants access to their data.
- Client (App): Requests access to user data.
- Authorization Server: Issues access tokens.
- Resource Server: Hosts protected resources.
2. OAuth Flows and Security Considerations
2.1 Authorization Code Grant vs. Implicit Grant
| Aspect | Authorization Code Grant | Implicit Grant |
|---|---|---|
| Security | More secure (tokens not exposed to browser). | Less secure (tokens exposed to browser). |
| Token Storage | Server-side | Client-side (e.g., browser) |
| Flow Complexity | Requires extra step (authorization code exchange). | Direct token issuance in URL. |
| Use Case | Secure web applications & APIs. | Single-page applications (SPA) — removed in OAuth 2.1; use Authorization Code + PKCE instead. |
✅ Mitigation: Avoid Implicit Grant in modern applications due to its security risks.
2.2 OAuth Attack Vectors & Exploits
Attackers commonly exploit OAuth misconfigurations in redirect URIs, token storage, or validation processes.
2.2.1 Redirect URI Manipulation (Access Token Theft)
Improper validation of the redirect_uri parameter allows an attacker to intercept access tokens.
Attack Steps
- Victim clicks a crafted authorization request with a malicious redirect URI:
http://hubgit.htb/authorization/auth?response_type=code&client_id=0e8f12335b0bf225&redirect_uri=http://attacker.htb/callback&state=somevalue
- With
response_type=code, the authorization server redirects with an authorization code, not an access token. - A vulnerable redirect policy can disclose the code; PKCE and exact redirect URI matching limit whether it can be redeemed.
✅ Mitigation:
- Strict URI validation: Use exact matching instead of wildcard matching (
*.domain.com). - Use PKCE (Proof Key for Code Exchange) to prevent token theft.
2.2.2 CSRF in OAuth Flow (Login CSRF)
OAuth uses the state parameter to prevent Cross-Site Request Forgery (CSRF).
Attack Scenario
- Attacker prepares an OAuth login request with a valid
codebut without astateparameter. - Victim unknowingly clicks the crafted link, logging them into the attacker's account.
- The victim may be logged into the client under the attacker's account, producing login CSRF or account confusion rather than necessarily hijacking the victim's session.
✅ Mitigation:
- Enforce the
stateparameter in OAuth requests. - Use a cryptographically secure
statevalue (e.g., a random nonce).
2.2.3 Token Reuse & Token Replay Attacks
OAuth tokens without expiration can be stolen and reused indefinitely.
Attack Scenario
- Attacker steals a valid access token from session storage.
- The attacker reuses the token to impersonate the victim.
✅ Mitigation:
- Use short-lived access tokens with refresh tokens.
- Implement token revocation mechanisms.
2.2.4 Weak JWT Token Signature Verification
OAuth access tokens may be opaque or structured; some deployments use JWTs. If a JWT access token is used, its verifier must apply the expected issuer, audience, algorithm, key, type, and lifetime policy.
- Change the
algtonone(Algorithm Confusion Attack). - Brute-force weak HS256 secrets.
Attack Example
Modify JWT Header:
{
"alg": "none",
"typ": "JWT"
}
Use a token without a signature:
eyJhbGciOiJub25lIiwidHlwIjoiSldUIn0.eyJ1c2VyIjoiYWRtaW4ifQ.
✅ Mitigation:
- Enforce RS256 over HS256.
- Reject
nonealgorithm.
2.2.5 OAuth Open Redirect Exploitation
If a service allows open redirects, attackers can chain them with OAuth flows.
Attack Example
Attacker exploits an open redirect:
http://hubgit.htb/redirect?target=http://attacker.htb
OAuth authentication redirects the victim via the open redirect, exposing the token.
✅ Mitigation:
- Block open redirects.
- Use a strict allowlist for redirect URIs.
2.2.6 Malicious OAuth Clients
Attackers may register a malicious OAuth client and request excessive consent. This differs from operating a fake authorization server.
Attack Steps
- A user is induced to authorize a malicious registered client.
- The authorization server issues only the scopes the user or policy permits.
- Excessive consent can expose API access until the grant is revoked.
✅ Mitigation:
- Use Trusted OAuth Providers Only.
- Enforce client registration review processes.
3. OAuth Security Best Practices
| Best Practice | Description |
|---|---|
| Use PKCE (Proof Key for Code Exchange) | Protects authorization codes from redemption by another client. OAuth 2.1 remains a draft; use current OAuth security BCP guidance as the normative baseline. |
| Short Token Expiry | Access tokens should expire quickly to reduce misuse risk. |
| Use Refresh Tokens Carefully | Obtains new access tokens without repeating end-user authorization; rotate or sender-constrain them where appropriate. |
| Sender-constrain Tokens | Use mechanisms such as DPoP or mutual TLS where the ecosystem supports them. |
| Enforce HTTPS | Prevents MITM attacks intercepting OAuth requests. |
| Restrict Scope | Limit token permissions (Principle of Least Privilege). |
| Use Signed JWTs | Prevents token forgery. |
4. Exploit & Lab Setup
graph TD
A[User] -->|Log in with HubGit| B[Authorization Server]
B -->|Access Token| C[OAuth Client: academy.htb]
C -->|Request User Data| D[Resource Server: hubgit.htb]
D -->|Provide User Data| CValidation 1: Redirect URI rejection
Use a local authorization-server fixture, synthetic client, and non-secret code. Confirm that an unregistered redirect URI is rejected and logged.
# Capture OAuth Authorization Request
https://auth.lab.invalid/authorize?response_type=code&client_id=synthetic-client&redirect_uri=https://unregistered.lab.invalid/callback
✅ Mitigation:
- Enforce exact redirect URI validation.
- Use a strict allowlist.
Validation 2: JWT access-token verifier policy
If the local fixture issues JWT access tokens, submit a synthetic token signed by an untrusted lab key and confirm the resource server rejects it and logs the reason. Do not crack or collect credentials.
✅ Mitigation:
- Configure an explicit algorithm and issuer/key binding appropriate to the architecture.
- Rotate keys safely and verify rejection of retired or untrusted keys.
5. OAuth Vulnerabilities Table
| Attack | Description | Mitigation |
|---|---|---|
| Redirect URI Manipulation | Trick user into redirecting tokens to attacker. | Strict URI validation. |
| OAuth Login CSRF | Victim logs into attacker’s account unknowingly. | Enforce state parameter. |
| Token Replay Attack | Attacker steals and reuses access tokens. | Short-lived tokens + token binding. |
JWT none Algorithm | Remove signature to bypass verification. | Enforce RS256 and reject none. |
| OAuth Open Redirect | Use open redirect to steal OAuth tokens. | Disable open redirects. |
| Malicious OAuth Clients | Fake OAuth apps steal credentials. | Use trusted providers only. |