# Flaws in Client Application

> *Flaws in the client's application's implementation of OAuth.*

## Implicit Grant Type

* The implicit grant type sends access tokens via the browser as a URL fragment.
* If the app wants to maintain the session after the user closes the page, it needs to store the current user data, i.e., user ID and access token. It will often submit this data to the server in a `POST` request and then assign the user a session cookie.
* The problem arises due to the fact that the server does not have any secrets/passwords to compare with the submitted data -> it is **implicitly trusted**. This request is exposed to attackers via their browser and can be leveraged for user impersonation.

<figure><img src="https://3960676229-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmjLkek16kB60c2WFd5lf%2Fuploads%2Fz0IDW8bKCNoAh47Lw3Iv%2Fimplicit_flow.png?alt=media&#x26;token=a8047490-74a9-4407-9447-46daaf301870" alt=""><figcaption><p>Image taken from <a href="https://www.youtube.com/watch?v=t_4CmFRyKk0"><em>here</em></a>.</p></figcaption></figure>

### LAB: Authentication Bypass via OAuth Implicit Flow

> **Goal**: Log into the application as `carlos`.

<figure><img src="https://3960676229-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmjLkek16kB60c2WFd5lf%2Fuploads%2Fk77D717OV9LHaqBB7v2a%2Fauth_bypass_via_oauth_implicit_flow_1.png?alt=media&#x26;token=c334ed98-5924-4de0-8110-edc137c6413a" alt=""><figcaption></figcaption></figure>

<figure><img src="https://3960676229-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmjLkek16kB60c2WFd5lf%2Fuploads%2FjLpfzqzT30ZqS6eS2jeg%2Fauth_bypass_via_oauth_implicit_flow_2.png?alt=media&#x26;token=c0745457-5ec8-4765-86f6-12cd15b6eaf7" alt=""><figcaption></figcaption></figure>

The access token provided (Step 3) is not tied with a specific user. If we change the email address, which in this case is used as the user identification, we can authenticate as any other user (Step 5).

<figure><img src="https://3960676229-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmjLkek16kB60c2WFd5lf%2Fuploads%2F3uBGP2taUXZFmcadH3ux%2Fauth_bypass_via_oauth_implicit_flow.png?alt=media&#x26;token=1cf1e1d1-d089-44a8-a741-383368dec596" alt=""><figcaption></figcaption></figure>

## Flawed CSRF protection

* The optional, but highly recommended, `state` parameter should contain the hash of something tied to the user's session when the OAuth flow is initiated.
* If the authorization request does not contain the `state` parameter, it means that an attacker can potentially initiate an OAuth flow themselves before tricking a user's browser into completing it (similar to a classic CSRF attack).

### LAB: Forced OAuth Profile Linking

> **Goal**: Use a CSRF attack to attach your own social media profile to `admin` and delete `carlos`.

The authorization request has not `state` parameter and redirects to `/oauth-linking`. After copying the URL, we need to drop the request so the code will remain valid for use.

<figure><img src="https://3960676229-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmjLkek16kB60c2WFd5lf%2Fuploads%2FemaGgdAN7UDJ1tZyUTi2%2Fforced_oauth_profile_linking.png?alt=media&#x26;token=cb067a21-fd24-41be-a314-4daf1e93aa86" alt=""><figcaption></figcaption></figure>

When the admin loads the `iframe` payload, it will complete the OAuth flow resulting in attaching their account to `wiener`'s social media profile. Thus, when `wiener`'s social profile is used to log into the application, the account will have elevated privileges.

<figure><img src="https://3960676229-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmjLkek16kB60c2WFd5lf%2Fuploads%2FOOAsrdA8XiZMxpGTAN7B%2Fforced_oauth_profile_linking_2.png?alt=media&#x26;token=5decb6e1-1eef-4b8b-8125-fa9d3942e4bc" alt=""><figcaption></figcaption></figure>
