Skip to content

Use of Cryptographically Weak Pseudo-Random Number Generator for API Key Generation

Moderate
41tair published GHSA-fw4f-cghv-2v33 Feb 11, 2026

Package

npm https://github.com/langgenius/dify (npm)

Affected versions

< v1.4.2

Patched versions

v1.4.2

Description

Summary

The software uses Python's random module for generating API keys. This module relies on a cryptographically weak pseudo-random number generator (Mersenne Twister), which could allow an attacker to predict the generated API keys.

Details

Vulnerability Description

The API key generation process in this software relies on a cryptographically weak Pseudo-Random Number Generator (PRNG), specifically the "Mersenne Twister" algorithm. While Mersenne Twister has good statistical properties, it is deterministic and therefore not suitable for generating security-sensitive values like API keys.

Steps to Reproduce

Tracing the API Key Generation Flow
The generate_api_key() method, responsible for generating API keys, was found to call the generate_string() function to create a random string.

Identifying the Insecure Random Function
The generate_string() function was found to use random.choice() from Python's standard library to select characters for the string.

Confirming the random Module's Specification
According to Python's official documentation, the random module uses Mersenne Twister as its core generator. The same documentation explicitly warns that it is deterministic and completely unsuitable for cryptographic purposes.

Python uses the Mersenne Twister as the core generator. ... However, being completely deterministic, it is not suitable for all purposes, and is completely unsuitable for cryptographic purposes.
(Source: Python Official Documentation - random)

PoC

The following code demonstrates that by fixing the seed of the random module, the same API key is consistently generated, proving the deterministic (and thus predictable) nature of the random number generator.

import random
import string

# A simplified version of the potentially vulnerable function
def generate_string(n):
    letters_digits = string.ascii_letters + string.digits
    return ''.join(random.choice(letters_digits) for i in range(n))

# 1. Generate an API key with a fixed seed
random.seed(12345)
api_key_1 = generate_string(24)
print(f"First generation (seed=12345): {api_key_1}")

# 2. Generate another API key with the same fixed seed
random.seed(12345)
api_key_2 = generate_string(24)
print(f"Second generation (seed=12345): {api_key_2}")

# 3. Verify the results
assert api_key_1 == api_key_2
print("\nConfirmed that the same API key is always generated from the same seed.")

Impact

This vulnerability may allow an attacker to predict generated API keys. If an attacker successfully predicts a key, they could impersonate legitimate users, leading to unauthorized access to confidential information, data tampering, or other malicious actions.

Severity

Moderate

CVSS overall score

This score calculates overall vulnerability severity from 0 to 10 and is based on the Common Vulnerability Scoring System (CVSS).
/ 10

CVSS v3 base metrics

Attack vector
Network
Attack complexity
High
Privileges required
None
User interaction
None
Scope
Unchanged
Confidentiality
High
Integrity
Low
Availability
None

CVSS v3 base metrics

Attack vector: More severe the more the remote (logically and physically) an attacker can be in order to exploit the vulnerability.
Attack complexity: More severe for the least complex attacks.
Privileges required: More severe if no privileges are required.
User interaction: More severe when no user interaction is required.
Scope: More severe when a scope change occurs, e.g. one vulnerable component impacts resources in components beyond its security scope.
Confidentiality: More severe when loss of data confidentiality is highest, measuring the level of data access available to an unauthorized user.
Integrity: More severe when loss of data integrity is the highest, measuring the consequence of data modification possible by an unauthorized user.
Availability: More severe when the loss of impacted component availability is highest.
CVSS:3.1/AV:N/AC:H/PR:N/UI:N/S:U/C:H/I:L/A:N

CVE ID

No known CVE

Weaknesses

Use of Cryptographically Weak Pseudo-Random Number Generator (PRNG)

The product uses a Pseudo-Random Number Generator (PRNG) in a security context, but the PRNG's algorithm is not cryptographically strong. Learn more on MITRE.

Credits