CVE-2023-48238: Json web token Json-web-token Library Is Vulnerable To Vulnerability

HIGH
Advisory Identifier: GHSA-4xw9-cx39-r355
CVSS 7.5
CWE-20 CWE-345
Published
Advisory Summary

CVE-2023-48238 is a HIGH (CVSS 7.5) json-web-token library is vulnerable to vulnerability (CWE-20: Improper Input Validation) affecting Json web token libraries in the npm ecosystem. The issue involves json-web-token library is vulnerable to a jwt algorithm confusion attack. Remediation is available by upgrading affected packages to version 4.0.0.

CVE-2023-48238 at a Glance

CVE Identifier CVE-2023-48238
GHSA Identifier GHSA-4xw9-cx39-r355
Severity HIGH
CVSS v3.1 Base Score 7.5
CWE Classification CWE-20: Improper Input Validation
Affected Product Json web token
Package Ecosystem npm
Published Date
Last Updated

Security Impact

CVSS: 7.5

The security impact metrics below are parsed directly from the CVSS v3.1 vector string (CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:N/I:H/A:N).

Attack Vector Network (AV:N)
Attack Complexity Low (AC:L)
Privileges Required None (PR:N)
User Interaction None (UI:N)
Scope Unchanged (S:U)
Confidentiality None (C:N)
Integrity High (I:H)
Availability None (A:N)

How to Fix CVE-2023-48238

To remediate this vulnerability, upgrade all affected Json web token dependencies to a patched release. Both direct and transitive dependencies should be checked, as frameworks or security libraries may pull in vulnerable versions transitively.

json-web-token fixed: 4.0.0
npm install json-web-token@4.0.0
Verify Direct and Transitive Dependencies

Run the following command in your project directory to verify that all resolved dependencies use the patched release:

npm ls json-web-token

How to Check if You Are Affected

To determine whether your Node.js application is affected by CVE-2023-48238, inspect your project dependencies using npm:

npm ls json-web-token

Analyze the terminal output to locate any version of Json web token in your dependency tree. If any resolved version matches the vulnerable ranges listed in the table below (for example, versions earlier than 4.0.0), your application is affected.

Note: The npm ls json-web-token command inspects dependencies declared in your package.json and lockfile; it does not perform automated runtime dynamic scanning.

Technical Details

Vulnerability Mechanism & Technical Analysis

This vulnerability is classified under CWE-20: Improper Input Validation, where the product receives input or data, but it does not validate or incorrectly validates that the input has the properties that are required to process the data safely and correctly. In Json web token, software state validation or input boundaries are circumvented during processing. Review the official source advisory statement below for specific affected components, classes, and reproduction details.

Source Advisory Statement

### Summary The json-web-token library is vulnerable to a JWT algorithm confusion attack. ### Details On line 86 of the 'index.js' file, the algorithm to use for verifying the signature of the JWT token is taken from the JWT token, which at that point is still unverified and thus shouldn't be trusted. To exploit this vulnerability, an attacker needs to craft a malicious JWT token containing the HS256 algorithm, signed with the public RSA key of the victim application. This attack will only work against this library is the RS256 algorithm is in use, however it is a best practice to use that algorithm. ### PoC Take a server running the following code: ```javascript const express = require('express'); const jwt = require('json-web-token'); const fs = require('fs'); const path = require('path'); const app = express(); const port = 3000; // Load the keys from the file const publicKeyPath = path.join(__dirname, 'public-key.pem'); const publicKey = fs.readFileSync(publicKeyPath, 'utf8'); const privateKeyPath = path.join(__dirname, 'private-key.pem'); const privateKey = fs.readFileSync(privateKeyPath, 'utf8'); app.use(express.json()); // Endpoint to generate a JWT token with admin: False app.get('/generateToken', async (req, res) => { const payload = { admin: false, name: req.query.name }; const token = await jwt.encode(privateKey, payload, 'RS256', function (err, token) { res.json({ token }); }); }); // Middleware to verify the JWT token function verifyToken(req, res, next) { const token = req.query.token; jwt.decode(publicKey, token, (err, decoded) => { if (err) { console.log(err) return res.status(401).json({ message: 'Token authentication failed' }); } req.decoded = decoded; next(); }); } // Endpoint to check if you are the admin or not app.get('/checkAdmin', verifyToken, (req, res) => { res.json(req.decoded); }); app.listen(port, () => { console.log(`Server is running on port ${port}`); }); ``` **Public key recovery** First, an attacker needs to recover the public key from the server in any way possible. It is possible to extract this from just two JWT tokens as shown below. Grab two different JWT tokens and utilize the following tool: `https://github.com/silentsignal/rsa_sign2n/blob/release/standalone/jwt_forgery.py` ``` python3 jwt_forgery.py token1 token2 ``` The tool will generate 4 different public keys, all in different formats. Try the following for all 4 formats. **Algorithm confusion** Change the JWT to the HS256 algorithm and modify any of the contents to your liking at `https://jwt.io/`. Copy the resulting JWT token and use with the following tool: `https://github.com/ticarpi/jwt_tool`. ``` python /opt/jwt_tool/jwt_tool.py --exploit k -pk public_key token ``` You will now get a resulting JWT token that is validly signed. ### Impact Applications using the RS256 algorithm, are vulnerable to this algorithm confusion attack which allows attackers to sign arbitrary payloads that the verifier will accept. ### Solution Either one of the following solutions will work. 1. Change the signature of the `decode` function to ensure that the algorithm is set in that call 2. Check whether or not the secret could be a public key in the decode function and in that case, set the key to be a public key. > Fixed in 4.0.0. Both encode and decode now reject combinations where the key type (PEM vs plain secret) doesn't match the algorithm family. Upgrade with npm install json-web-token@^4.

CVE-2023-48238 Affected Json web token Versions

1 component(s)
Fixed Versions:
4.0.0
Product / Package Ecosystem Affected Versions Fixed Version
json-web-token
npm
< 4.0.0
4.0.0

References

Frequently Asked Questions

What is CVE-2023-48238?
CVE-2023-48238 is a security vulnerability in Json web token involving json-web-token library is vulnerable to a JWT algorithm confusion attack under CWE-20: Improper Input Validation.
Which Json web token versions are affected?
Affected versions include releases prior to 4.0.0 across affected packages: json-web-token.
What version fixes CVE-2023-48238?
Upgrading to 4.0.0 (or the latest patched release for your respective branch) resolves the vulnerability.
Is json-web-token affected?
Yes, json-web-token is affected in releases prior to 4.0.0. Upgrade your dependency to 4.0.0 or later.
What is the CVSS score of CVE-2023-48238?
CVE-2023-48238 has a CVSS v3.1 base score of 7.5 (HIGH) with vector string CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:N/I:H/A:N.
What CWE is associated with CVE-2023-48238?
CVE-2023-48238 is classified under CWE-20: Improper Input Validation (The product receives input or data, but it does not validate or incorrectly validates that the input has the properties that are required to process the data safely and correctly.).
How can I check npm dependencies for CVE-2023-48238?
Run the dependency tree command: 'npm ls json-web-token'. This command lists direct and transitive dependencies in your build configuration so you can verify if a vulnerable version is resolved.
Machine-readable OSV JSON OSV v1.5 Schema
Normalized schema output
{
  "schema_version" : "1.4.0",
  "id" : "GHSA-4xw9-cx39-r355",
  "modified" : "2026-06-08T23:38:28Z",
  "published" : "2023-11-17T22:48:15Z",
  "aliases" : [ "CVE-2023-48238" ],
  "summary" : "json-web-token library is vulnerable to a JWT algorithm confusion attack",
  "details" : "### Summary\nThe json-web-token library is vulnerable to a JWT algorithm confusion attack.\n\n### Details\nOn line 86 of the 'index.js' file, the algorithm to use for verifying the signature of the JWT token is taken from the JWT token, which at that point is still unverified and thus shouldn't be trusted. To exploit this vulnerability, an attacker needs to craft a malicious JWT token containing the HS256 algorithm, signed with the public RSA key of the victim application. This attack will only work against this library is the RS256 algorithm is in use, however it is a best practice to use that algorithm.\n\n### PoC\nTake a server running the following code:\n```javascript\nconst express = require('express');\nconst jwt = require('json-web-token');\nconst fs = require('fs');\nconst path = require('path');\n\nconst app = express();\nconst port = 3000;\n\n// Load the keys from the file\nconst publicKeyPath = path.join(__dirname, 'public-key.pem');\nconst publicKey = fs.readFileSync(publicKeyPath, 'utf8');\nconst privateKeyPath = path.join(__dirname, 'private-key.pem');\nconst privateKey = fs.readFileSync(privateKeyPath, 'utf8');\n\napp.use(express.json());\n\n// Endpoint to generate a JWT token with admin: False\napp.get('/generateToken', async (req, res) => {\n  const payload = { admin: false, name: req.query.name };\n  const token = await jwt.encode(privateKey, payload, 'RS256', function (err, token) {\n    res.json({ token });\n  });\n});\n\n// Middleware to verify the JWT token\nfunction verifyToken(req, res, next) {\n  const token = req.query.token;\n\n  jwt.decode(publicKey, token, (err, decoded) => {\n    if (err) {\n      console.log(err)\n      return res.status(401).json({ message: 'Token authentication failed' });\n    }\n\n    req.decoded = decoded;\n    next();\n  });\n}\n\n// Endpoint to check if you are the admin or not\napp.get('/checkAdmin', verifyToken, (req, res) => {\n  res.json(req.decoded);\n});\n\napp.listen(port, () => {\n  console.log(`Server is running on port ${port}`);\n});\n```\n\n**Public key recovery**\nFirst, an attacker needs to recover the public key from the server in any way possible. It is possible to extract this from just two JWT tokens as shown below.\nGrab two different JWT tokens and utilize the following tool: `https://github.com/silentsignal/rsa_sign2n/blob/release/standalone/jwt_forgery.py`\n```\npython3 jwt_forgery.py token1 token2\n```\nThe tool will generate 4 different public keys, all in different formats. Try the following for all 4 formats.\n\n**Algorithm confusion**\nChange the JWT to the HS256 algorithm and modify any of the contents to your liking at `https://jwt.io/`.\nCopy the resulting JWT token and use with the following tool: `https://github.com/ticarpi/jwt_tool`.\n```\npython /opt/jwt_tool/jwt_tool.py --exploit k -pk public_key token\n```\nYou will now get a resulting JWT token that is validly signed.\n\n### Impact\nApplications using the RS256 algorithm, are vulnerable to this algorithm confusion attack which allows attackers to sign arbitrary payloads that the verifier will accept.\n\n### Solution\nEither one of the following solutions will work.\n1. Change the signature of the `decode` function to ensure that the algorithm is set in that call\n2. Check whether or not the secret could be a public key in the decode function and in that case, set the key to be a public key.\n\n\n> Fixed in 4.0.0. Both encode and decode now reject combinations where the key type (PEM vs plain secret) doesn't match the algorithm family. Upgrade with npm install json-web-token@^4.",
  "severity" : [ {
    "type" : "CVSS_V3",
    "score" : "CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:N/I:H/A:N"
  } ],
  "affected" : [ {
    "package" : {
      "ecosystem" : "npm",
      "name" : "json-web-token"
    },
    "ranges" : [ {
      "type" : "ECOSYSTEM",
      "events" : [ {
        "introduced" : "0"
      }, {
        "fixed" : "4.0.0"
      } ]
    } ],
    "database_specific" : {
      "last_known_affected_version_range" : "<= 3.1.1"
    }
  } ],
  "references" : [ {
    "type" : "WEB",
    "url" : "https://github.com/joaquimserafim/json-web-token/security/advisories/GHSA-4xw9-cx39-r355"
  }, {
    "type" : "ADVISORY",
    "url" : "https://nvd.nist.gov/vuln/detail/CVE-2023-48238"
  }, {
    "type" : "WEB",
    "url" : "https://github.com/joaquimserafim/json-web-token/commit/b6e56b1346f48432d29133c76b65222ad93956b7"
  }, {
    "type" : "PACKAGE",
    "url" : "https://github.com/joaquimserafim/json-web-token"
  }, {
    "type" : "WEB",
    "url" : "https://github.com/joaquimserafim/json-web-token/blob/acf6a462471e1b14187eb77414e9161b8b7bff7e/index.js#L86"
  } ],
  "database_specific" : {
    "cwe_ids" : [ "CWE-20", "CWE-345" ],
    "severity" : "HIGH",
    "github_reviewed" : true,
    "github_reviewed_at" : "2023-11-17T22:48:15Z",
    "nvd_published_at" : "2023-11-17T22:15:07Z"
  }
}