CVE-2024-5206
Sensitive data leakage in TfidfVectorizer due to improper token storage.
- CVSS 4.7
- CWE-922
- Information Leakage
- Local
A sensitive data leakage vulnerability was identified in scikit-learn's TfidfVectorizer, specifically in versions up to and including 1.4.1.post1, which was fixed in version 1.5.0. The vulnerability arises from the unexpected storage of all tokens present in the training data within the `stop_words_` attribute, rather than only storing the subset of tokens required for the TF-IDF technique to function. This behavior leads to the potential leakage of sensitive information, as the `stop_words_` attribute could contain tokens that were meant to be discarded and not stored, such as passwords or keys. The impact of this vulnerability varies based on the nature of the data being processed by the vectorizer.
- CWE
- CWE-922
- CVSS base score
- 4.7
- Published
- 2024-06-06
- OWASP
- A03 Sensitive Data Exposure
- Orthogonal defect classification
- Function
- Code defect classification
- Incorrect Functionality
- Category
- Information Leakage
- Subcategory
- Insecure Handling of Sensitive Data
- Accessibility scope
- Local
- Impact
- Information Disclosure
- Affected component
- scikit-learn
- Fixed by upgrading
- Yes
Solution
Upgrade to scikit-learn version 1.5.0 or later.
Vulnerable code sample
from sklearn.feature_extraction.text import TfidfVectorizer
import re
documents = [
"Log entry: User 'admin' logged in with API key: sk-abcdefg1234567890",
"Configuration file: password = mySecretPassword123",
"Email: Please reset your password to: P@$$wOrd",
"Normal text: This is a regular sentence."
]
vectorizer = TfidfVectorizer(stop_words='english')
X = vectorizer.fit_transform(documents)
stop_words = vectorizer.stop_words_
sensitive_patterns = [
r"sk-[a-zA-Z0-9]+",
r"[a-zA-Z0-9@#$%^&*()_+=-]+"
]
leaked_sensitive_data = []
for word in stop_words:
for pattern in sensitive_patterns:
if re.search(pattern, word):
leaked_sensitive_data.append(word)
print("Stop words attribute:", stop_words)
print("\nPotentially leaked sensitive data:", leaked_sensitive_data)
print("\n--- Explanation ---")
print("This demonstrates CVE-2024-5206. The TfidfVectorizer, when processing text data, can mistakenly identify sensitive information (like API keys or passwords) as stop words.")
print("The 'stop_words_' attribute then exposes this sensitive data, creating a security vulnerability.")
print("An attacker could potentially extract this information and use it for malicious purposes.")Patched code sample
from sklearn.feature_extraction.text import TfidfVectorizer
import re
documents = [
"Log entry: User 'admin' logged in with API key: sk-abcdefg1234567890",
"Configuration file: password = mySecretPassword123",
"Email: Please reset your password to: P@$$wOrd",
"Normal text: This is a regular sentence."
]
sensitive_patterns = [
r"sk-[a-zA-Z0-9]+",
r"password\s*=\s*[^\s]+",
r"(?i)password[^ ]*"
]
def sanitize(text):
for pattern in sensitive_patterns:
text = re.sub(pattern, "[REDACTED]", text)
return text
sanitized_documents = [sanitize(doc) for doc in documents]
vectorizer = TfidfVectorizer(stop_words='english')
X = vectorizer.fit_transform(sanitized_documents)
print("TF-IDF matrix shape:", X.shape)
print("Sanitized input has been safely processed.")
print("\n--- Explanation ---")
print("This demonstrates CVE-2024-5206. The TfidfVectorizer, when processing text data, can mistakenly identify sensitive information (like API keys or passwords) as stop words.")
print("The 'stop_words_' attribute then exposes this sensitive data, creating a security vulnerability.")
print("An attacker could potentially extract this information and use it for malicious purposes.")Payload
documents = [
"User password is: my_secure_password123",
"API key for service: 12345-abcde-67890-fghij",
"This is a normal document."
]
Cite this entry
@misc{vaitp:cve20245206,
title = {{Sensitive data leakage in TfidfVectorizer due to improper token storage.}},
author = {Bogaerts, Fr\'ed\'eric and Ivaki, Naghmeh and Fonseca, Jos\'e},
year = {2024},
note = {VAITP Python Vulnerability Dataset, entry CVE-2024-5206},
howpublished = {\url{https://netpack.pt/vaitp/vulnerability/CVE-2024-5206/}}
}
Introducing the "VAITP dataset": a specialized repository of Python vulnerabilities and patches, meticulously compiled for the use of the security research community. As Python's prominence grows, understanding and addressing potential security vulnerabilities become crucial. Crafted by and for the cybersecurity community, this dataset offers a valuable resource for researchers, analysts, and developers to analyze and mitigate the security risks associated with Python. Through the comprehensive exploration of vulnerabilities and corresponding patches, the VAITP dataset fosters a safer and more resilient Python ecosystem, encouraging collaborative advancements in programming security.
The supreme art of war is to subdue the enemy without fighting.
Sun Tzu – “The Art of War”
:: Shaping the future through research and ingenuity ::
