What are JWT?
Tags: jwt, security, webdev
Published: 2023-11-29 19:50:38

---
title: What are JWT?
published: true
cover_image: https://blog.atolcd.com/wp-content/uploads/2023/10/jwt-blog.png
canonical_url: https://blog.ltgt.net/jwt/
description: This article's goal is to present what JWTs are, whenever you face them. As we'll see, you won't deliberately choose to use JWTs in a project, and more importantly: you won't use JWTs as session tokens.
tags: jwt, security, webdev
---

_This is a translation of an article I wrote for our internal knowledge base at work, and that we later decided to <a href=https://blog.atolcd.com/json-web-token-jwt/ hreflang=fr>publish</a> (in French)._

---

This article's goal is to present what JWTs are, whenever you face them. As [we'll see](#criticism), you won't deliberately choose to use JWTs in a project, and more importantly: you won't use JWTs as _session tokens_.

## What is it?

> JSON Web Token (JWT) is a compact, URL-safe means of representing data to be transferred between two parties. The data is encoded as a JSON object that can be signed and/or encrypted.

This is, paraphrased, the definition from the IETF standard that defines it ([RFC 7519](https://datatracker.ietf.org/doc/html/rfc7519)).

## What's the point? What's the use case?

So the goal is to transfer data, with some guarantees (or none, by the way): authenticity, integrity, even possibly confidentiality (if the message is encrypted). There are therefore many possible uses.

JWT is thus used in OpenID Connect to encode the [ID Token](https://openid.net/specs/openid-connect-core-1_0.html#IDToken) that forwards to the application information on the authentication process that took place at the identity server. OpenID Connect also uses JWT to encode _aggregated claims_: information from other identity servers, for which we'll want to verify the authenticity and integrity.

A JWT _might_ be used to authenticate to a server, such as with the OAuth 2 JWT Bearer ([RFC 7523](https://datatracker.ietf.org/doc/html/rfc7523)).

Still in OAuth 2 land, access tokens _could_ themselves be JWTs ([RFC 9068](https://datatracker.ietf.org/doc/html/rfc9068)), authorization request parameters _could_ be encoded as a JWT ([RFC 9101](https://datatracker.ietf.org/doc/html/rfc9101)), as well as token introspection responses ([IETF draft: JWT Response for OAuth Token Introspection](https://tools.ietf.org/html/draft-ietf-oauth-jwt-introspection-response)), and finally dynamic client registration uses a JWT to identify the software of which an instance attempts to register (so-called _software statements_ of [RFC 7591](https://datatracker.ietf.org/doc/html/rfc7591)).

## How does it work?

A JWT is composed of at least 2 parts, separated with a `.` (dot), the first one always being the header. Each part is always encoded as _base64url_, a variant of Base 64 with the `+` and `/` characters (that have special meaning in URLs) replaced with `-` and `_` respectively, and without the trailing `=`.

There are two types of JWTs: JSON Web Signature (JWS, defined by [RFC 7515](https://datatracker.ietf.org/doc/html/rfc7515)), and JSON Web Encryption (JWE, defined by [RFC 7516](https://datatracker.ietf.org/doc/html/rfc7516)). The most common case is the JWS, composed of 2 or 3 parts: the header, the payload, and optionally the signature. JWEs are rarer (and more complex) so I won't talk about them here.

The header, common to both types, describes the type of JWT (JWS or JWE) as well as the different signature, MAC, or encryption algorithms being used (codified by [RFC 7518](https://datatracker.ietf.org/doc/html/rfc7518)), along with other useful information, as a JSON object.  
In the case of JWS, we'll find the signature or MAC algorithm, possibly a key identifier (whenever multiple keys can be used, e.g. to allow for key rotation), or even a URL pointing to information about the keys (in JWKS format, defined by [RFC 7517](https://datatracker.ietf.org/doc/html/rfc7517)), etc.

In the case of JWS, the payload will generally be a JSON object with the transfered data (but technically could be another JWT).

The third part is the signature or MAC. This part is absent if the header says the JWT is unprotected (`"alg":"none"`).

For debugging, one can use the [JWT Debugger](https://jwt.io/#debugger-io) by Auth0 to decode JWTs _(beware not to use it with sensitive data, only on JWTs coming from test servers)_.

⚠️ JWT being almost always used in security-related contexts, handle them with care, specifically when it comes to their cryptographical components.

One **MUST** use dedicated libraries to manipulate JWTs, and be careful to use them correctly to avoid introducing vulnerabilities.

[RFC 8725](https://datatracker.ietf.org/doc/html/rfc8725) has a set of best practices when manipulating and using JWTs.

## Criticism

Numerous security experts, among them cryptographers, vehemently criticize JWTs and advise against their use.

The main criticism relates to its complexity, even though it could look _simple_ to developers:

 * first, you need to know how to decode UTF-8 and JSON ; that's as many sources of bugs (and potential vulnerabilities).
 * and of course because it's a generic format capable of signing and/or encrypting, or even not protecting anything at all (`"alg":"none"`), with a [list of supported algorithms](https://www.iana.org/assignments/jose/jose.xhtml#web-signature-encryption-algorithms) as long as your arm, you have to handle many cases (even if only to reject them).

As a result, [a number of vulnerabilities](https://0xn3va.gitbook.io/cheat-sheets/web-application/json-web-token-vulnerabilities) have been identified; among them ([identified as soon as March 2015](https://auth0.com/blog/critical-vulnerabilities-in-json-web-token-libraries/)):

 * As the JWT itself declares the algorithm used to sign or encrypt it, software that receives it needs to partly trust it, or correctly check the used algorithm against a list of authorized algorithms. Because of its apparent simplicity, many libraries came out that didn't do those necessary checks and readily accepted unprotected JWTs (`"alg":"none"`), allowing an attacker to use any JWT, without authenticity or integrity check. And as incredible as it may seem, [we still find](https://www.howmanydayssinceajwtalgnonevuln.com/) vulnerable applications nowadays!

   Note: in the same way, the header can directly include the public key to be used to verify the signature. Using it will prove the integrity of the JWT, but not its authenticity as the signature could have been generated by anyone.

 * Another attack involes using the public key intended to verify an asymmetric signature (`"alg":"RS256"` or `"alg":"ES256"`) as a MAC key (`"alg":"HS256"`): the application receiving the JWT could then mistakenly validate the MAC and allow the JWT in. Anybody could then create a JWS that would be accepted by the application, when that one _thinks_ it's verifying an asymmetric signature.

   This vulnerability could be due to a misuse of the library used to verify JWTs, but also in some cases directly to its API that cannot tell between a public key and a shared secret (generally for the sake of making it easy to use).

Aside: despite ID Tokens in OpenID Connect being JWTs, you won't actually need to verify their signature as you generally get them through HTTPS, that already guarantees authenticity and integrity (and confidentiality), which saves us from a whole class of vulnerabilities.

Another criticism is due to the misuse of JWT, most often by ignorance or lack of expertise in software security: validity of a JWT is directly verifiable, without the need for a database of valid tokens or a validation service (authenticity and integrity are verifiable, so the validity period contained within in the JWT are _reliable_), but it makes the JWT **impossible to revoke** (unless you add such a mecanism –possibly based on the `jti` claim, initially designed to protect against replay attacks– going against the whole reason for which JWT was chosen in the first place). If a JWT is used as a _session token_ for example, it then becomes impossible to sign out or terminate a session. In most use cases (in the specifications), a JWT is validated and used as soon as it's received from the issuer, so revocation is not even an issue. It's when a JWT is stored by the receiver for a later use that the problem arises (such as with a _session token_ or an _access token_).

Some articles critical of JWT:

 * [JWT should not be your default for sessions](https://evertpot.com/jwt-is-a-bad-default/) (by Evert Pot, developper)
 * [Why JWTs Suck as Session Tokens](https://developer.okta.com/blog/2017/08/17/why-jwts-suck-as-session-tokens) (by Okta, vendor of an identity management platform)
 * [section "JSON Web Tokens" of "API Tokens: A Tedious Survey"](https://fly.io/blog/api-tokens-a-tedious-survey/#jwt) (by Thomas H. Ptacek, security researcher)
 * [Alternatives to JWTs](https://www.scottbrady91.com/jose/alternatives-to-jwts) (by Scott Brady, engineering manager specializing in identity management systems)
 * [Stop using JWTs for sessions](http://cryto.net/~joepie91/blog/2016/06/13/stop-using-jwt-for-sessions/) and [Stop using JWT for sessions, part 2: Why your solution doesn’t work](http://cryto.net/~joepie91/blog/2016/06/19/stop-using-jwt-for-sessions-part-2-why-your-solution-doesnt-work/) (on a web site surprisingly without HTTPS)
 * [No Way, JOSE! Javascript Object Signing and Encryption is a Bad Standard That Everyone Should Avoid](https://paragonie.com/blog/2017/03/jwt-json-web-tokens-is-bad-standard-that-everyone-should-avoid) (by Scott “CiPHPerCoder” Arciszewski, cryptographer)
 * [How to Write a Secure JWT Library If You Absolutely Must](https://scottarc.blog/2023/09/06/how-to-write-a-secure-jwt-library-if-you-absolutely-must/) (by the same author)