Skip to main content
Create your own
Lesson illustration

OAuth 2.0 Insecurity: Redirects and State Flaws

Hello! Welcome to your next lesson on advanced web and API security.

In our previous session, we delved into web cache poisoning, where you learned to manipulate caching infrastructure to serve malicious content to unsuspecting users. This involved exploiting the trust between a cache and a back-end server.

Today, we shift our focus to another area where trust is paramount: authentication protocols. Specifically, we'll be tackling OAuth 2.0. As a ubiquitous framework for "Log in with Google/Facebook/etc.", its complexity and flexible design make it a rich source of high-impact vulnerabilities. This lesson directly addresses the learning outcome: Exploit OAuth 2.0 implementation flaws such as insecure redirect URIs and state parameter weaknesses.

You will learn to identify and exploit two of the most common and critical OAuth 2.0 misconfigurations, both of which can lead directly to account takeover—a top-tier finding in any bug bounty program.

1. The OAuth 2.0 Authorization Code Flow: A Quick Refresher

Before we exploit it, let's quickly review the moving parts of the most common OAuth 2.0 flow, the "authorization code" grant type. Your computer science background means you're familiar with protocol handshakes, and this is no different. It involves three main parties: the client application (the site you're logging into), the user (you), and the OAuth service provider (e.g., Google).

#NahamCon2024: OAuth Secret | @BugBountyReportsExplained

To see this flow in action from a bug hunter's perspective, watch this short clip from the NahamCon talk 'OAuth Secret' by @BugBountyReportsExplained. It provides a concise, practical overview of the three main steps.

Watch from 00:40 to 03:45. Pay close attention to the flow of requests and the role of the code parameter, which is exchanged for a session cookie.

The key takeaway is this exchange:

  1. The user is redirected to the OAuth provider to grant permission.
  2. The provider redirects the user back to the client application with a temporary, one-time authorization code.
  3. The client application's back-end exchanges this code with the provider for an access token, which it then uses to log the user in.

The vulnerabilities we'll discuss today hinge on an attacker's ability to interfere with step 2 and either steal the code or inject their own code into a victim's session.

2. Flaw #1: Weak state Parameter and CSRF

The state parameter is designed to be a Cross-Site Request Forgery (CSRF) token for the OAuth flow. The client application generates a unique, unpredictable value, sends it in the initial request, and verifies that the same value is returned after the user authenticates with the OAuth provider. This ensures the user who is completing the flow is the same one who started it.

What happens if this protection is missing or implemented incorrectly?

OAuth 2.0 authentication vulnerabilities | Web Security Academy

Let's read the official explanation from PortSwigger's Web Security Academy. This section details how a missing state parameter enables a CSRF attack.

Read the subsection titled 'Flawed CSRF protection'. Focus on the scenario where an attacker can bind their own social media account to a victim's account on the client application.

As you just read, an attacker can start the OAuth flow in their own browser, get an authorization code linked to their account, and then trick the victim's browser into completing the flow with that code. If the victim is already logged into the client application, the application might link the attacker's social media account to the victim's profile, leading to an account takeover.

CSRF Attack on OAuth 2.0 'state' Parameter
This diagram shows the attack flow. The attacker obtains an authorization code (1) and crafts a malicious request (2). The victim's browser executes this request (3), sending the attacker's code to the client application. The application, lacking `state` validation, accepts the code and may link the attacker's identity to the victim's session.

Practical Demonstration

Theory is one thing; seeing it in action solidifies the concept.

Flawed CSRF Protection - State Param - Hacking Oauth Pt . 2 | Live Demo on Medium.com

The channel Hacking Simplified provides an excellent walkthrough of this exact vulnerability. The presenter first explains the concept and then solves a PortSwigger lab, demonstrating the exploit step-by-step.

Watch from 01:44 to 10:48. This covers: (1) a clear explanation of the account-linking CSRF attack, (2) a live exploit demo in a lab environment, and (3) an example of how Medium.com correctly uses the state parameter to prevent this attack.

This demonstration makes it clear: the state parameter isn't just a recommendation; it's a critical security control. Its absence or predictability (e.g., a static value) is a high-severity vulnerability.

Test your understanding!

You are testing a web application's "Login with Google" feature. You intercept the initial authorization request and observe a state parameter. However, you notice that its value is just a Base64-encoded timestamp of when the request was initiated (e.g., state=MTY3OTg3NjU0My4xMjM=). Is this implementation secure? Why or why not?

Show answer

This implementation is not secure. The purpose of the state parameter is to be an unguessable value tied to the user's session. A timestamp is predictable. An attacker can easily generate a valid timestamp for the current time, defeating the CSRF protection. They could initiate the flow, get their own code, and then craft a malicious URL for the victim containing their code and a freshly generated timestamp for the state parameter. The server would validate the timestamp as correctly formatted and recent, and the attack would succeed.

3. Flaw #2: Insecure redirect_uri Validation

The redirect_uri is arguably the most critical parameter in the entire flow. It tells the OAuth provider where to send the user—and their sensitive authorization code—after a successful login. If an attacker can control this URI, they can instruct the provider to send the victim's code to a server they control.

OAuth 2.0 Insecure Redirect URI Vulnerability
This image illustrates the core attack. An attacker crafts a link with a modified `redirect_uri` pointing to their own server. After the victim authenticates with the OAuth provider, the provider redirects them to the attacker's server, leaking the authorization `code` in the URL.

Authorization servers are supposed to validate the redirect_uri against a pre-registered whitelist. However, developers often implement this validation poorly, creating bypass opportunities.

OAuth 2.0 authentication vulnerabilities | Web Security Academy

The PortSwigger Web Security Academy provides the definitive guide on how these validation flaws occur and how to bypass them. This is essential reading.

Read the sections 'Leaking authorization codes and access tokens' and 'Flawed redirect_uri validation'. Focus on the different ways to bypass whitelisting, such as using open subdomains, path traversal, and exploiting parsing discrepancies.

Practical Bypass Techniques

The article you just read covers the theory. Now let's see some of the concrete payloads bug bounty hunters use to bypass weak validation.

#NahamCon2024: OAuth Secret | @BugBountyReportsExplained

Let's return to the NahamCon talk. This clip covers practical ways to exploit a flexible but whitelisted redirect_uri.

Watch from 06:55 to 11:47. The speaker discusses how to abuse a redirect_uri where you can control the path. The key takeaway is finding an open redirect vulnerability on the client application itself and using that page as your redirect URI. Also, note the list of common open redirect payloads shown around 07:45.

The most common and effective strategy for exploiting a weak redirect_uri is to chain it with another vulnerability on the client application. If the whitelist only allows redirects to *.client-app.com, your goal is to find a flaw on that domain that can leak the code for you. Examples include:

  • Open Redirect: Find a page like client-app.com/redirect?url=... that redirects to any URL. Set your redirect_uri to https://client-app.com/redirect?url=https://attacker.com. The code will be appended as a parameter and then forwarded to your server.
  • Reflected XSS: Find a page where a URL parameter is reflected without sanitization. Set the redirect_uri to this page. The code will be in the URL, and your XSS payload can read document.location and send it to your server.

4. Advanced Exploitation: Parameter Chaining

Top-tier bug hunters often combine multiple small issues to create a critical impact. In OAuth, you can manipulate other parameters to bypass protections or enable new attack vectors.

A powerful example is the response_mode parameter. It dictates how the code is delivered to the redirect_uri. The default is usually query (in the query string), but sometimes fragment (#) or form_post are supported.

#NahamCon2024: OAuth Secret | @BugBountyReportsExplained

This final clip from the NahamCon talk demonstrates how changing the response_mode can be used to bypass defenses and leak the code in creative ways.

Watch from 13:32 to 18:32. Focus on how switching to response_mode=fragment can help leak a code that would otherwise be consumed by the server immediately. This is an advanced technique that leverages subtle differences in browser behavior versus server-side code.

The key idea is that browsers handle URL fragments (#...) differently from query strings (?...). Fragments are typically processed only client-side and are not sent to the server. By changing response_mode to fragment, you might be able to get the code to appear in the browser's address bar at the redirect_uri without the back-end immediately consuming it, giving you a larger window to steal it with XSS or other client-side flaws.

Conclusion

OAuth 2.0 vulnerabilities are a prime example of how implementation flaws in complex protocols can undermine security. As a bug bounty hunter, mastering these attacks is essential.

Key Takeaways:

  • State Parameter is for CSRF: The state parameter must be present, unique per session, and unpredictable. Its absence or predictability allows an attacker to inject their identity into a victim's session.
  • Redirect URI is the Weakest Link: The redirect_uri must be strictly validated against an exact whitelist. Loosely configured whitelists (e.g., allowing any path or subdomain) are a gateway to code theft.
  • Chaining is Key: The most common exploit path for a weak redirect_uri is to chain it with another vulnerability on the client application, such as an open redirect or XSS, which acts as a proxy to leak the authorization code.
  • Explore All Parameters: Don't just focus on state and redirect_uri. Parameters like response_mode can alter the flow's behavior and open up new avenues for exploitation.

Next Lesson Preview:

We will continue our focus on API security by moving to another increasingly popular technology: GraphQL. In the next lesson, you will learn to test GraphQL endpoints for common vulnerabilities like information disclosure via introspection and batching attacks.

Can't find a good explanation? Sign up and we'll make it for you

Sign up