Signing in with a third-party provider ends with a redirect back into your app, and that redirect carries a code the app turns into a token. On the web the redirect lands on a unique URL. In a native app it lands on a URL scheme that another app may also have registered, so the code can reach the wrong app. PKCE is the check that makes a stolen code worthless.

Key terms

  • Deep link: a URL that sends data straight into a native app from an outside source, either through the app's custom scheme, such as recipes://recipe/12, or through an https link.
  • Verification code: the value an OAuth2 provider sends back in the redirect after a successful login, which the app exchanges for a JWT.
  • PKCE: an extension to the OAuth 2 spec, pronounced "Pixy", that verifies the authorization request and the token exchange come from the same client.
  • code_verifier: a large random string the client generates and keeps in memory.
  • code_challenge: the SHA 256 hash of the code_verifier.

Nothing reserves a URL scheme

Custom-scheme deep links are a weakness mobile apps have and the web doesn't. There is no central registry of URL schemes, so a malicious app can register the same scheme as yours and receive the links meant for you, along with the data in them. A link like recipes://recipe/12 leaks nothing worth stealing. A link that carries a token hands it to whoever caught the link, which is why deep links should never carry sensitive information.

The platforms resolve a contested scheme differently. When two or more apps can open a link, Android shows a disambiguation dialog and lets the user choose. iOS chooses by itself, and the user never finds out. Apple introduced a first-come-first-served rule in iOS 11, but the hole can still be exploited in other ways. On iOS, universal links are the secure way to link to content inside your app.

The OAuth2 redirect is the weak step

In OAuth2, which OpenID Connect builds on, the user authenticates with a third party. After a successful login, that party redirects back to the app with a verification code, and the app exchanges the code for a JWT. On the web this step is safe because web URLs are unique. An app's redirect has no such guarantee, since any app can claim the same scheme, so a hijacking app can receive the verification code meant for yours. An app using OAuth2 therefore needs PKCE as an extra check.

How code_verifier and code_challenge pair up

PKCE uses the SHA 256 hash algorithm, which has three properties the flow depends on. Its output has the same length whatever the input. The same input always gives the same output, so anyone holding the input can recompute the hash. And it is one way: the input can't be recovered from the hash.

A login with PKCE runs in this order:

  1. The app generates the code_verifier, a large random string, and keeps it in memory.
  2. It computes the code_challenge, the SHA 256 of that verifier.
  3. The /authorize request carries the code_challenge. The verifier stays in the app.
  4. The user signs in, and the provider redirects back with the verification code.
  5. To exchange that code, the app now sends the code_verifier that produced the challenge.
  6. The identity provider (IdP) computes the code_challenge from the verifier it received, compares it with the one from the first /authorize request, and returns the access token only if they match.

Steps 3 and 5 are the pair to watch. The challenge goes out first, and the verifier goes out only after the authorize request has returned.

A stolen code on its own

Picture the hijacking app catching the redirect in step 4. It now holds a valid verification code, but exchanging the code also takes the code_verifier, and that string never left your app's memory before your own exchange request. Even a party that saw the code_challenge can't work back to the verifier, because SHA 256 can't be reversed. Only the app that started the authorization flow can turn the code into a JWT, so the stolen code is useless by itself.

PKCE protects the code exchange. Deep links themselves stay insecure, so tokens and other sensitive values still never go into one.

PKCE from React Native

react-native-app-auth is an SDK for talking to OAuth2 providers. It wraps the native AppAuth-iOS and AppAuth-Android libraries and can support PKCE, but only when your identity provider supports it, so confirm that on the provider side before you count on the protection.

✓ Completed