Skip to main content
Security guideReviewed

OAuth vulnerabilities: how "Sign in with" goes wrong, and the callback checks that refuse each attack

When a customer clicks "Sign in with" a provider, your application runs the OAuth 2.0 authorisation code flow, and OpenID Connect tells it who signed in.

This guide explains each step in plain English, what state, PKCE, the nonce and an exactly registered redirect URI each protect, the attacks that work when one is missing, and a lab with a mock provider and callback tests you run on your own machine with Node.js alone.

Authorisation code flow sequence and callback test lab

An illustration with invented users. Part 1 sets out the authorisation code flow with PKCE and OpenID Connect as nine numbered steps between the browser, your application and the provider's two endpoints, with the value that protects each step and the specification section behind it.

How to use it

Parts 2 to 5 are a Node.js lab with no installs: a mock provider, an application with a "Sign in with" button in a flawed and a fixed version, thirteen callback scenarios printed request by request, and a regression test that fails on the flawed version. Run it on your own machine, for learning and for testing code you own or are authorised to test.

oauth-oidc-security.txt

Introduction5 lines

ILLUSTRATION ONLY. The provider, the application, the users Alice, Bob and Mallory, their addresses and every request below are invented for this page. The lab runs on your own machine, binds to 127.0.0.1 on ports the system picks, keeps everything in memory and signs nobody in to anything real. Use it for learning and for testing code you own or are authorised in writing to test. What this asset is, and what it is not. Part 1 is the authorisation code flow with PKCE and OpenID Connect as nine numbered steps, each with the value that protects it and the specification section behind that value. Parts 2 to 5 are a lab: a mock provider, an application with a "Sign in with" button in a flawed and a fixed version, thirteen scenarios that print every request in order, and a regression test. A mock provider proves what your callback code does with a provider's answers. It proves nothing about what a real provider does. Written and run on: Node.js v26.0.0, the only version it was run on. It uses node:http, node:crypto, node:test, node:assert and the fetch function built into Node.js, so there is nothing to install. Every output below comes from that run; timings will differ on your machine.

What happens when someone clicks "Sign in with"

A "Sign in with" button hands the job of checking who someone is to another service, the provider.

Two standards sit behind it. OAuth 2.0, which the IETF published as RFC 6749, lets an application get access to something on a person's behalf. OpenID Connect, from the OpenID Foundation, builds on that flow so the application also learns who signed in.

The same OAuth flow can be used when a business application connects to another system on a customer's behalf, such as their accounting or calendar service: delegated access, with no sign-in involved.

RFC 6749 section 4.1 describes the authorisation code flow in five steps. Your application sends the browser to the provider's authorisation endpoint with its client identifier, the access it wants (the scope), a value to keep track of the request (state) and the address to come back to (the redirect URI).

The provider authenticates the person and establishes whether they agree. If they do, it sends the browser back to that address with an authorisation code.

Your application then sends the code to the provider's token endpoint, authenticating itself as it does so, and the provider checks the code and the redirect URI before answering with an access token.

The code is what makes the design work. RFC 6749 section 1.3.1 explains that the person authenticates only with the provider, so their credentials are never shared with your application, and that the code lets the provider authenticate your application and send the access token straight to it, without passing it through the browser.

That last exchange is a server-to-server request. Every other step travels through the browser, where a value can be copied, swapped or replayed, which is why the callback, the address the browser comes back to, needs the checks this page is about.

OAuth grants access; OpenID Connect says who signed in

This distinction matters more than any other on the page.

RFC 6749 section 1.1 defines four roles: the resource owner, who can grant access; the resource server, which holds what is being accessed; the client, an application making requests on the resource owner's behalf and with their authorisation; and the authorisation server, which issues access tokens after authenticating the resource owner and obtaining authorisation.

Section 1.4 defines an access token as a string representing an authorisation issued to the client, and says the string is usually opaque to the client.

OpenID Connect Core 1.0 draws the line explicitly. Its introduction says RFC 6749 and RFC 6750 define how to obtain and use access tokens but do not define standard methods to provide identity information, and that without profiling, OAuth 2.0 is incapable of providing information about the authentication of an end-user.

The OAuth and OIDC chapter of OWASP's Application Security Verification Standard (ASVS) 5.0 says the same in fewer words: by itself, OAuth is not designed for user authentication.

OpenID Connect adds the missing piece. A client asks for it by including the openid scope value, and the provider returns an ID Token: a JSON Web Token, which the specification says MUST be signed, carrying claims about the authentication.

The required claims include the issuer (iss), a subject identifier for the person that the issuer never reassigns (sub), the audience, which MUST contain your client ID (aud), and an expiry time (exp). The specification calls the ID Token the primary extension that OpenID Connect makes to OAuth 2.0 to enable end-users to be authenticated. In its terms the provider is an OpenID Provider and your application is a relying party.

So sign people in from an ID Token you have validated, and use an access token only to call the API it was issued for. An application that takes an access token on its own as proof of who is signed in is relying on something RFC 6749 does not define.

For delegated access with no sign-in, such as letting a customer connect a calendar, plain OAuth is the right tool and the identity questions on this page do not arise, though the state, PKCE and redirect URI checks still apply.

What this guide covers, and where the neighbouring guides take over

This guide covers "Sign in with" and delegated access as a business application implements them: the authorisation code flow, the callback, and the checks that make the callback safe to accept.

Four neighbouring guides take over where it stops. The JWT security guide covers the token format itself: signature algorithms, keys and claim validation in general. The session management guide covers the session your application creates once the callback succeeds. The open redirects guide covers open redirectors, which MITRE catalogues as CWE-601, URL Redirection to Untrusted Site, and which RFC 9700 section 2.1 says clients and authorisation servers MUST NOT expose, because they can enable exfiltration of authorisation codes and access tokens. The authentication failures guide covers your own sign-in flows: passwords, recovery and account enumeration.

The values that protect the flow, in plain terms

Each value below has one job.

The asset sets out the specification section behind each one, step by step.

  • state: a random value your application sends out and expects back, so the callback can tell that the browser arriving is the one that set off. RFC 6749 section 10.12 says the client MUST implement CSRF protection for its redirection URI and SHOULD use state to carry the binding value, which MUST contain a non-guessable value.
  • PKCE (Proof Key for Code Exchange, RFC 7636): your application keeps a random secret, the code_verifier, sends only its SHA-256 hash, the code_challenge, at the start, and shows the secret at the end, so a code that reaches anyone else is no use to them. RFC 7636 section 4.6 says the provider MUST return an invalid_grant error when the two do not match.
  • nonce: a random value your application sends at the start, which the provider copies into the ID Token, so an ID Token from any other sign-in does not match. OpenID Connect Core section 2 says that when the ID Token carries one, the client MUST verify it equals the value sent.
  • The registered redirect URI: the one address the provider may send the code to, written down in advance. RFC 9700 section 2.1 says the authorisation server MUST compare redirect URIs by exact string matching, with one exception for port numbers in localhost addresses used by native apps.
  • The client credential: how your application proves to the token endpoint that it is your application. Under RFC 6749 section 4.1.3, a confidential client MUST authenticate when it exchanges a code.
  • The code itself: RFC 6749 section 4.1.2 says it MUST expire shortly after it is issued and that a maximum lifetime of 10 minutes is RECOMMENDED, says the client MUST NOT use it more than once, and says the authorisation server MUST deny a code used more than once.

What RFC 9700 changed

RFC 9700, the IETF's Best Current Practice for OAuth 2.0 Security (BCP 240, published January 2025), updates RFC 6749 on several of these points. It says public clients MUST use PKCE and that PKCE is RECOMMENDED for confidential clients, and adds that although PKCE was designed to protect native apps, the advice applies to all kinds of OAuth clients, including web applications.

RFC 6749 section 2.1 defines a confidential client as one capable of keeping its credentials confidential, such as a client running on a secure server with restricted access to the client credentials, or capable of secure client authentication by other means, and a public client as one that can do neither, such as an installed native application or a browser-based application.

RFC 9700 also says authorisation servers MUST support PKCE, and that clients SHOULD use a challenge method that does not expose the verifier, which it says is currently only S256.

On cross-site request forgery, RFC 9700 section 2.1 says a client that has made sure the provider supports PKCE MAY rely on it, notes that in OpenID Connect flows the nonce provides the protection, and says that otherwise one-time-use state values securely bound to the browser MUST be used.

Section 2.1.2 says clients SHOULD NOT use the implicit grant, which returns an access token in the authorisation response itself, unless access token injection in the authorisation response is prevented and the token leakage risks it describes are mitigated, and section 2.4 says the resource owner password credentials grant, which exposes the person's credentials to the application, MUST NOT be used.

This page's advice is to use all of them together: state, PKCE with S256 and, when you sign people in, the nonce. OWASP ASVS 5.0 requirement 10.1.2, at Level 2, asks the same of all three values: not guessable, specific to the transaction, and securely bound to the client and the browser session that started it.

They overlap, and in the lab the mock provider switches off one of its own checks at a time while the fixed application's check still refuses the attack.

OAuth vulnerabilities in the callback, and how to test for each

Each attack below is described at the level you need to recognise it and test for it in an application you own or are authorised in writing to test.

The scenario numbers refer to the lab in the asset, which runs every one against a flawed and a fixed version of the same application.

Login CSRF: a missing or unchecked state

RFC 6749 section 10.12 describes a CSRF attack against the client's redirection URI: the attacker injects their own authorisation code or access token, which can result in the client using an access token associated with the attacker's resources rather than the victim's. 1 min read

Code injection, and what PKCE does about it

Authorisation codes can leak. 1 min read

ID Token replay and the nonce

A signature check stops a forged ID Token. 1 min read

Open and loosely matched redirect URIs

Some authorisation servers let a client register a pattern instead of a complete redirect URI. 1 min read

A code presented twice

An authorisation code travels in a URL, so it can be copied. 1 min read

Account takeover through automatic account linking

This is where an OAuth vulnerability becomes an account takeover. 2 min read

More than one provider: mix-up attacks

If your application offers more than one provider, RFC 9700 section 4.4 describes mix-up attacks, in which a provider under an attacker's control tricks your application into sending it a code or access token issued by an honest provider. 1 min read

Running the callback lab on your own machine

The lab is an illustration.

A mock provider with four invented users and an application with a "Sign in with" button run on your own machine, bound to 127.0.0.1 on ports the system picks, with everything kept in memory. The application comes in two versions. The flawed one sends no state, nonce or PKCE values, registers its callback as a pattern and matches accounts by email address.

The fixed one does each of those properly. Both check the ID Token's signature, issuer, audience and expiry the same way, so every difference in the results comes from the checks on this page.

It uses only what ships with Node.js, and it was written and run on Node.js 26.0.0; every output in the asset comes from that run.

Be clear about what it proves. A mock provider proves what your callback code does with a provider's answers, not what a real provider does. To isolate the application's own checks, three scenarios switch one of the mock provider's checks off: S4 leaves the nonce out of the ID Token, S5 stops enforcing PKCE and S10 lets a code be used twice. The specifications require a provider to do all three, so read those scenarios as defence in depth.

  • Save the six files into an empty folder and open a terminal there. There is nothing to install.
  • Run node run-scenarios.mjs. For each of the thirteen scenarios it prints every request the scripted browsers make, every server-to-server request to the token endpoint and the result, first for the flawed application and then for the fixed one, followed by five checks on the mock provider's token endpoint.
  • Run node callback.test.mjs. All thirteen tests pass against the fixed application. Run node callback.test.mjs flawed and twelve fail; the Node.js test runner documentation says the process exit code is set to 1 when any test fails, so a pipeline step running the test fails too.
  • Test only applications you own or are authorised in writing to test, with invented test users.

Pointing the tests at your own application

Keep the mock provider: it is what lets a test hand your callback a leaked code, an ID Token without a nonce or an unverified address on demand. Start your own application on a test port in place of lab-client.mjs, with its provider settings pointed at the mock's issuer address, and keep the scenarios that exercise your callback, S1 to S7 and S10 to S13.

If your sign-in library reads the provider's settings from a discovery document or its keys from a key set address, add those two routes to the mock first.

Scenarios S8 and S9 and checks P1 to P5 exercise the provider. For a real provider, check those through your client's registration in the provider's console and the provider's own documentation, not by sending crafted requests to its service.

Use a maintained library, and check what it is set to do

Writing the protocol yourself means getting every check in the asset right, plus the signature and key handling around them.

This page's advice is to use a maintained library for the protocol and an established provider for the authorisation server, and the sources point the same way. The ASVS 5.0 chapter on OAuth and OIDC says that, given the complexity of the area, it is vitally important for a secure OAuth or OIDC solution to use well-known industry-standard authorisation servers and apply the recommended security configuration.

The OWASP Top 10:2025 says that ideally a premade, well-trusted system handles authentication, identity and session management, and to transfer this risk whenever possible by buying and using a hardened and well tested system.

For Next.js applications, the Next.js authentication guide recommends an authentication library over a custom solution, for increased security and simplicity. The Next.js guide on this site covers the rest of that framework.

When choosing, the OpenID Foundation says its free suite of conformance tests lets developers assess how well their implementations conform to its most mature, stable standards and profiles, and implementations that complete its self-certification process can use the OpenID Certified mark. A library runs with whatever settings your application gives it, so after choosing one, check these.

  • It sends state, a nonce and an S256 code_challenge on every sign-in, and checks all three on the way back (scenarios S2 to S7).
  • It validates the ID Token's iss, aud, exp and nonce as OpenID Connect Core section 3.1.3.7 requires, and checks the signature with the keys the provider supplies.
  • Your code finds accounts by iss and sub, never by email address, and any option to link accounts by matching email addresses is switched off or sits behind a confirmation step (S11 to S13).
  • Each sign-in attempt is used once (S10).
  • Tokens go only to the parts of the system that need them. ASVS 5.0 requirement 10.1.1, at Level 2, gives the example of a backend-for-frontend, where access and refresh tokens are only accessible to the backend.
  • Then run the callback tests against your application with the library in place. They test the configuration you deployed, not the library in general.

What to put in a new integration's configuration

Some of these are set in the provider's console rather than in your code.

Record what you chose for each new integration, and why.

  • Exact redirect URIs. List each callback in full, one per environment, with no wildcards, and remove any you no longer use. RFC 9700 section 2.1 says the authorisation server MUST use exact string matching, except for port numbers in localhost redirect URIs of native apps, and ASVS 5.0 requirement 10.4.1, at Level 1, asks for a client-specific allowlist of pre-registered URIs compared by exact string comparison. RFC 9700 section 2.6 adds that authorisation servers MUST NOT allow redirect URIs using the http scheme, except for native clients using loopback redirection.
  • The narrowest scopes. Ask for openid and only the profile data and API access the feature uses. RFC 9700 section 2.3 says an access token's privileges SHOULD be restricted to the minimum required, and ASVS 5.0 requirements 10.2.3 (Level 3) and 10.4.11 (Level 2) ask the client to request, and the authorisation server to assign, only the scopes needed. Under RFC 6749 section 3.3 the authorisation server MAY ignore part or all of the scope you asked for, and MUST include the scope response parameter when what it issued differs, so read the scope that comes back.
  • Grant types. Switch on the authorisation code grant and leave the others off unless a feature needs one. RFC 9700 section 2.1.2 says clients SHOULD NOT use the implicit grant unless access token injection in the authorisation response is prevented and the token leakage risks it describes are mitigated, and section 2.4 says the resource owner password credentials grant MUST NOT be used. ASVS 5.0 requirement 10.4.4, at Level 1, asks the authorisation server to allow each client only the grants it needs and says those two must no longer be used.
  • PKCE required, with S256. If the provider lets you require PKCE for your client, require it. RFC 9700 section 2.1.1 says public clients MUST use PKCE and recommends it for confidential clients, and ASVS 5.0 requirement 10.4.6, at Level 2, asks the authorisation server to require it and not to accept the plain method.
  • Client authentication. Keep the client secret on your server, never in browser code or a mobile app, and rotate it when someone who had access to it leaves. RFC 9700 section 2.5 says authorisation servers SHOULD enforce client authentication where feasible, and that asymmetric methods, such as mutual TLS or signed JWTs (private_key_jwt), are RECOMMENDED, because with them the authorisation server does not need to store sensitive symmetric keys.
  • The number of providers. If you accept more than one, build the mix-up defence described above before launch.

OAuth 2.1 is still a draft

OAuth 2.1 gathers many of these rules into one document. When this page was reviewed, the IETF datatracker listed it as an active Internet-Draft, draft-ietf-oauth-v2-1-17, last updated on 30 October 2026, and the draft itself says an Internet-Draft should be cited only as work in progress.

Its non-normative list of changes from OAuth 2.0 includes building PKCE into the authorisation code grant, exact redirect URI matching, removing the PKCE plain method, and leaving out the implicit and resource owner password credentials grants.

The OWASP OAuth 2.0 cheat sheet already marks the implicit grant as deprecated and tells clients to use the authorisation code grant with PKCE for all client types. Until the draft is published as an RFC, configure to RFC 9700.

What to review when a provider relationship changes

Sign-in depends on settings held by two organisations, so review it whenever either side changes.

  • Adding a second provider: add the mix-up defence, store with each sign-in attempt, bound to the browser, the issuer it was sent to, as RFC 9700 section 4.4.2 requires, and check that the new provider's accounts cannot link to existing local accounts by email address alone.
  • Moving to a new provider, or to a new tenant at the same one: the issuer can change, and with it every iss and sub pair your links are keyed on: OpenID Connect Core section 5.7 makes sub unique within its issuer, and the pair is the stable identifier. Plan how existing customers re-link through their current sign-in method rather than matching them by email address.
  • A change in how the provider verifies email addresses: OpenID Connect Core section 5.1 says the means of verification is context specific, so revisit any rule in your application that relies on email_verified.
  • People leaving: rotate the client secret and remove their access to the provider's console.
  • Retired environments and features: remove their redirect URIs and the scopes only they needed.
  • Library and provider updates: re-run the callback tests after each upgrade of your sign-in library and each change the provider announces to its authorisation or token endpoints. RFC 9700 section 2.1.1 recommends that authorisation servers publish code_challenge_methods_supported in their metadata, which is one place to confirm PKCE support is still there.

Where an outside test fits

Your own tests cover the scenarios you thought of.

A Clever Ops web application and API penetration test covers authentication and sessions, what each role can see and change, and the API called directly rather than only through the screens. Single sign-on is scoped separately: signing in through an identity provider adds the sign-in exchange, account linking and the provider settings to test, and needs a test identity provider you control.

Each finding comes with evidence, a fix written for your stack and the criteria the retest will use. If Clever Ops built or hosts your application, the work is a security review rather than an independent test, and the report says who built it and who hosts it.

Frequently asked questions

OAuth 2.0 lets an application get access to something on a person's behalf, using an access token that RFC 6749 defines as a string representing an authorisation issued to the client. OpenID Connect adds an ID Token with claims about the authentication, which the application must validate. OpenID Connect Core says that without that profile, OAuth 2.0 is incapable of providing information about the authentication of an end-user.

Not on its own. An access token records an authorisation for your application to call an API, and RFC 6749 does not define it as a statement of who signed in. Sign people in from an ID Token you have validated, with its issuer, audience, expiry and nonce checked as OpenID Connect Core section 3.1.3.7 sets out, and find the account by the iss and sub claims together.

PKCE, Proof Key for Code Exchange (RFC 7636), ties an authorisation code to a secret that only the application that asked for the code holds, so a stolen code is no use to someone who does not hold that secret, as long as the provider enforces PKCE. It was designed for native apps, but RFC 9700 says the advice applies to all kinds of OAuth clients, including web applications: public clients MUST use it, it is RECOMMENDED for confidential clients, and clients SHOULD use a method that does not expose the verifier, which RFC 9700 says is currently only S256.

It lets your callback check that the browser arriving with a code is the one that started the sign-in. RFC 6749 says the client MUST protect its redirection URI against CSRF and SHOULD use state to carry a non-guessable value bound to the browser. RFC 9700 adds that a client that has made sure the provider supports PKCE MAY rely on PKCE for this, that in OpenID Connect flows the nonce provides it, and that otherwise one-time-use state values securely bound to the browser MUST be used.

It ties an ID Token to the sign-in attempt that asked for it. Your application sends a fresh random value, the provider copies it into the ID Token, and OpenID Connect Core says the client MUST check that the two match. RFC 9700 says confidential OpenID Connect clients MAY use it instead of PKCE against code injection, provided they validate it in the ID Token from the token endpoint and disregard every token until that check succeeds.

Not automatically. OpenID Connect Core section 5.7 says email MUST NOT be used as a unique identifier, and that an issuer MAY reuse an email address for different people at different times. Link only after the person proves they own the existing account by signing in to it, and refuse to link at all when the provider does not say the address is verified.

RFC 9700 says clients SHOULD NOT use it, or any other response type that issues access tokens in the authorisation response, unless access token injection in the authorisation response is prevented and the token leakage risks it describes are mitigated, and says clients SHOULD instead use the authorisation code grant or another response type that issues access tokens in the token response. The OAuth 2.1 Internet-Draft leaves the implicit grant out, and the OWASP OAuth 2.0 cheat sheet marks it as deprecated.

Scope the right test for your application

Tell us what you have built, who uses it and when it needs to launch. You will get a plain-English recommendation: a website audit, a web application test or a referral to a specialist.