JSON Web Token (JWT) Weaknesses | Qualys Security Blog (2024)

JSON Web Tokens, or JWTs, are an encoded set of claims commonly seen in REST APIs and Single page web applications (SPAs). These encoded claims are used to provide identification of the requester and other information related to accessing. It is a stateless mechanism, and the token is sent with every request requiring identification, typically in the Authorization header or as a URL parameter. Although there is some debate about them as session tokens (https://redis.com/blog/json-web-tokens-jwt-are-dangerous-for-user-sessions/) they are still used in that capacity as well. JWTs must be designed and implemented securely, or issues will be present. Qualys Web Application Scanning (WAS) will now detect vulnerabilities related to algorithms used in JWTs.

JWT Structure

A JWT contains three parts:

Header– Section contains the type of token and the algorithm in use. The most common algorithms seen are HMAC with SHA-256 (HS256), a symmetric algorithm, and RSA with SHA-256, an asymmetric algorithm. May include additional fields such as Key ID (kid).

Payload:Section contains the information about the user referred to as Claims.

Signature– Section is used to validate the token. An operation is performed on two previous parts with the addition of a secret or private key.

Each of these sections is combined into a single token using the following:

token = base64urlEncoding(header) + ‘.’ + base64urlEncoding(payload) + ‘.’ + base64urlEncoding(signature)

As the portions of the token are base64 encoded, it is trivial to decode and view the contents. Let’s look at an example taken from the Tiredful API (https://github.com/payatu/Tiredful-API):

Encoded:

eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJncm91cCI6IlN1cGVyaGVyb2VzIiwidXNlciI6ImJhdG1hbiIsImV4cCI6MTY2MjY1NDAwNiwiZW1haWwiOiJiYXRtYW5AZ290aGFtLmNvbSJ9.ROm-7vwGjTn8aaVOEodtS9u683-LJ6m8NdRAuAmiIik

Decoded:

Header

{

“alg”: “HS256”,

“typ”: “JWT”

}

Payload

{

“group”: “Superheroes”,

“user”: “batman”,

“exp”: 1662654006,

“email”: “batman@gotham.com

}

Implementation Issues

Qualys WAS is introducing two new QIDs to detect algorithms in JWTs found in Authorization headers. The QIDs are passive checks that will decode the token and review the contents.

QID 150571: JWT in Authorization Header Uses “none” Algorithm.

The QID will detect tokens using the “none” algorithm. This is the weakest “algorithm” as there isn’t a signature to validate the token. The lack of a signature will allow an attacker to easily modify a token to set arbitrary claims such as elevating privileges or impersonating another user.

{“alg”: “none”, “typ”: “JWT”}

JSON Web Token (JWT) Weaknesses | Qualys Security Blog (1)
JSON Web Token (JWT) Weaknesses | Qualys Security Blog (2)

QID 150572: JWT in Authorization Header Uses Symmetric Algorithm.

The QID will detect tokens using the HS256 algorithm. When designing the JWT implementation, one must select an algorithm to use. Choosing a symmetric algorithm is weaker as anyone knowing the secret can create or validate a token. If the secret is known, an attacker can forge a token with an arbitrary set of claims.

{“alg”: “HS256”, “typ”: “JWT”}

JSON Web Token (JWT) Weaknesses | Qualys Security Blog (3)
JSON Web Token (JWT) Weaknesses | Qualys Security Blog (4)

Other considerations

Algorithm Confusion
If a system designed for an asymmetric algorithm can be forced to process a JWT as HMAC symmetric algorithm or the none algorithm, a valid JWT could easily be forged. Ensure a whitelist of approved algorithms is utilized to prevent unexpected algorithms from being accepted.

Sensitive Content
As JWTs are base64 encoded, they can easily be decoded. It will be disclosed if sensitive data is stored in the payload portion. Do not use the payload section to store any data that should not be publicly available.

Secret Disclosure
In the case of symmetric or HMAC usage, a shared key/secret is used to create and verify the token. Implementation have been observed where the secret key is contained in client-side code or is easily guessable. If this key is known, anyone can create and verify valid tokens.

For asymmetric (RSA and EDCSA), if the private key leaks, valid JWTs could be forged. Of course, if a private key ever leaks, it’s never good.

Key ID Attacks
The Key ID (kid) parameter, if used, contains reference to the key utilized. If arbitrary values are accepted, this can lead to attacks such as directory traversal, server-side request forgery, and others.

Related

JSON Web Token (JWT) Weaknesses | Qualys Security Blog (2024)

FAQs

What are the weaknesses of JWT token? ›

Lack of Encryption

One of the most significant weaknesses of JWTs is their lack of encryption. JWTs are designed to be compact and self-contained, which means that the data within them is not encrypted. While they can be signed to ensure data integrity, sensitive information within a JWT remains exposed in plaintext.

Is JWT enough for security? ›

It's important to note that a JWT guarantees data ownership but not encryption. The reason is that the JWT can be seen by anyone who intercepts the token because it's serialized, not encrypted. It is strongly advised to use JWTs with HTTPS, a practice that extends to general web security.

Are JWT tokens insecure? ›

Some web applications rely on JSON Web Tokens (JWTs) for stateless authentication and access control instead of stateful ones with traditional session cookies. Some implementations are insecure and allow attackers to bypass controls, impersonate users, or retrieve secrets.

What is the problem with JWT? ›

Six threats to JWTs
  • Allowing the server to use a token without validation. ...
  • Using the same private key for different applications. ...
  • Using a weak signing algorithm. ...
  • Choosing a short and/or low-entropy private key. ...
  • Keeping sensitive data in a JWT's payload. ...
  • Confusing the keys.
Jun 7, 2023

Why JWTs are bad for authentication? ›

So why is JWT dangerous for user authentication? The biggest problem with JWT is the token revoke problem. Since it continues to work until it expires, the server has no easy way to revoke it.

What is better than JWT security? ›

Security: OAuth is a secure way to manage authorization flows, while JWT is a lightweight and self-contained token. It does not provide security on its own, but can be secure as part of a well designed authentication system.

What is more secure than JWT? ›

Secure: Opaque tokens do not contain any user information, making them more secure than JWT tokens. Flexible: Opaque tokens can be customized to store additional user information in the authorization server, which can be retrieved by the resource server when needed.

Is JWT the most secure? ›

Advantages of JWT

Security: JWTs are digitally signed, ensuring data integrity and preventing tampering. Using encryption algorithms enhances the security further. Cross-Domain Communication: JWTs can be used across different domains or microservices since they don't rely on cookies or server-side sessions.

Why avoid JWT? ›

JWTs which just store a simple session token are inefficient and less flexible than a regular session cookie, and don't gain you any advantage. The JWT specification itself is not trusted by security experts.

Can JWT tokens be stolen? ›

With businesses facing new security vulnerabilities every day, stolen JWT tokens could be the worst thing for any enterprise delivering online services. It's crucial for businesses to ensure maximum security at the consumer level and take necessary precautions to avoid a security breach.

Can you destroy a JWT token? ›

By definition, once generated, a jwt token is valid until expired. You can “logout” and remove the token from browser storage, but the token is still valid. There is no “standard” way to administratively invalidate a token once issued.

What is replacing JWT? ›

OAuth2, Passport, Spring Security, Auth0, and Keycloak are the most popular alternatives and competitors to JSON Web Token.

Is JWT best for authentication? ›

JWT (JSON Web Token) is a very popular way to authenticate users. It's a way to securely exchange data between client and server through a token. Here is how it works: User sends their credentials (i.e. username and password) to the server.

Are JWT tokens are prone to XSS attacks? ›

Cross-site scripting(XSS) and Cross-Site Request Forgery(CSRF) are likely to occur if a JSON Web Token(JWT) is not properly stored in the browser.

What is the limitation of JWT? ›

The maximum size of a JWT is determined by the size of the payload, which is the part of the JWT that contains the claims. The size of a JWT is limited by the maximum size of an HTTP header, which is typically 8KB.

What are the disadvantages of security tokens? ›

Cons of Using Tokens
  • Compromised Secret Key. One of the major cons of relying on tokens is that it relies on just one key. ...
  • Data Overhead. The overall size of a JWT is quite more than that of a normal session token, which makes it longer whenever more data is added to it. ...
  • Shorter Lifespan.

Top Articles
Latest Posts
Article information

Author: Edwin Metz

Last Updated:

Views: 5824

Rating: 4.8 / 5 (58 voted)

Reviews: 89% of readers found this page helpful

Author information

Name: Edwin Metz

Birthday: 1997-04-16

Address: 51593 Leanne Light, Kuphalmouth, DE 50012-5183

Phone: +639107620957

Job: Corporate Banking Technician

Hobby: Reading, scrapbook, role-playing games, Fishing, Fishing, Scuba diving, Beekeeping

Introduction: My name is Edwin Metz, I am a fair, energetic, helpful, brave, outstanding, nice, helpful person who loves writing and wants to share my knowledge and understanding with you.