The idea behind mitigation of OAuth 2.0 code interception attacks

Angela Jakimovska
Netcetera Tech Blog
2 min readMay 13, 2022

--

If you have ever heard about the OAuth 2.0 protocol, it is very likely that you came across the “PKCE” term. So, what is “PKCE”?

Source: Photo by carboxaldehyde from Pexels

As stated in the standard:

OAuth 2.0 public clients are susceptible to the authorization code interception attack. In this attack, the attacker intercepts the authorization code
returned from the authorization endpoint within a communication path
not protected by Transport Layer Security (TLS), such as inter-
application communication within the client’s operating system
.”

What can be done about this? This is where “PKCE”, which stands for “Proof Key for Code Exchange”, comes into play. To mitigate this attack, a dynamically created high-entropy cryptographic random key called “code verifier” is uniquely created for every authorization request, from which a transformed value is derived, called “code challenge”. These values are then used in the communication with the authorization and the token endpoint.

To understand how this helps to mitigate the issue take a look at this flow:

Simplified diagram of the protocol flow

In the diagram above the client creates the “code verifier” and the transformed value, the “code challenge”. Then, sends the “code challenge” and the transformation method along with the Authorization Request . The Authorization Endpoint receives the Authorization Request, records the “code challenge” and the transformation method and returns the authorization code. The client then sends an Access Token Request with the authorization code and the “code verifier” to the token endpoint, where then the “code verifier” is transformed and compared to the “code challenge”. If the comparison shows the values are not equal, access is denied.

And that’s it in a nutshell :). If you want to learn more details and educate yourself further, check out the “Proof Key for Code Exchange by OAuth Public Clients” specification referenced below.

Reference: https://datatracker.ietf.org/doc/html/rfc7636

--

--

Senior Software Engineer enthusiastic about well-crafted, quality software. #android #softwaredevelopment