CIDADAXSEC
← All topics
Expert

OAuth authentication

OAuth flaws exploit redirect_uri, implicit flow, and access token theft to hijack victim accounts.

Material

OAuth lets a third-party application access a resource on the user behalf without ever revealing the password. The provider issues a code or token through the browser, which the application then exchanges. Because codes and tokens travel in URLs, the weak points are the redirect_uri parameter, the state value, and how the client stores the token.

How the authorization flow works

The authorization code flow sends the user to the provider login page and redirects back to the registered redirect_uri with a short-lived code. The application server exchanges that code with its client secret for an access token, so the code never travels over the public network. The implicit flow differs: the token comes straight back in the URL fragment, exposing it to the browser and to logs.

http
GET /oauth/authorize?response_type=code&client_id=myapp&redirect_uri=https://app.example.com/callback&scope=openid%20profile&state=8f3c1a HTTP/1.1
Host: accounts.example.com

http
GET /oauth/authorize?response_type=token&client_id=myapp&redirect_uri=https://app.example.com/callback&state=8f3c1a HTTP/1.1
Host: accounts.example.com

The redirect_uri is the destination after authentication, and the provider decides whether that URI is legitimate before redirecting. With loose matching, such as checking only a prefix or ignoring port and path, an attacker registers their own URI and receives the victim code. The state parameter closes the CSRF gap and binds the login to the request that started it.

Attack forms

  • Loose redirect_uri: the attacker registers their own URI and receives the victim code or token.
  • Token theft via open redirect: a redirect page on a legitimate site forwards the parameters to the attacker domain.
  • Proxy page: a form action or redirect script passes the token to the attacker server.
  • Implicit flow: the token sits in the URL fragment, so it leaks via Referer, logs, or browser history.
  • Forced profile linking: the attacker gets their account linked to the victim account when the provider does not verify email.
  • Dynamic client registration: open registration is used to register a malicious redirect_uri or trigger SSRF on the metadata endpoint.

Prevention

  • Register redirect_uri values explicitly and match exactly on scheme, host, port, and path.
  • Always send a random state parameter and verify it on the callback.
  • Use authorization code with PKCE, and avoid the implicit flow unless the client genuinely cannot keep a secret.
  • Never place tokens in a URL path; use a secure, httpOnly, SameSite cookie when the browser must hold one.
  • Verify identity before linking accounts: compare the issuer claim and a verified email address.
  • Close the dynamic client registration endpoint, or restrict the values clients may configure.
javascript
// Verifikasi callback: exact match + state + code hanya sekali pakai
const EXPECTED = new Set(['https://app.example.com/callback']);
const expectedState = session.get('oauth_state');

app.get('/callback', (req, res) => {
  const { code, state, redirect_uri } = req.query;
  if (!EXPECTED.has(redirect_uri)) return res.sendStatus(400);
  if (!expectedState || state !== expectedState) return res.sendStatus(400);
  session.delete('oauth_state');
  exchangeCode(code).then(token => res.json({ ok: true }));
});
Note the two distinct failure modes. A redirect_uri bug steals someone else credentials, while a state bug binds the victim login to the attacker. Both can coexist in the same provider, so test them separately.

Labs in this topic

Labs run on a domain separate from this site, so a payload cannot reach your account session.

Labs for this category land in Phase 2. The material is readable now.

Challenges in this category

Challenges from the PortSwigger Web Security Academy. The original labs run on PortSwigger domains, so the link opens there.