CVE-2022-40896
ReDoS vulnerability in pygments 2.15.0 via SmithyLexer: Resource exhaustion with crafted input
- CVSS 5.5
- CWE-434 Unrestricted Upload of File with Dangerous Type
- Input Validation and Sanitization
- Remote
A ReDoS issue was discovered in pygments/lexers/smithy.py in pygments through 2.15.0 via SmithyLexer.
- CVSS base score
- 5.5
- Published
- 2023-07-19
- OWASP
- A03 Injection
- Orthogonal defect classification
- Function
- Code defect classification
- Incorrect Functionality
- Category
- Input Validation and Sanitization
- Subcategory
- Insecure Parsing or Deserialization
- Accessibility scope
- Remote
- Impact
- Denial of Service (DoS)
- Affected component
- Pygments
- Fixed by upgrading
- Yes
Solution
Update pygments to version 2.16.0 or higher.
Vulnerable code sample
import re
class SmithyLexer:
# Example of a vulnerable regular expression that can lead to ReDoS
token_pattern = r'([a-zA-Z_][a-zA-Z0-9_]*|"(?:[^"\\]|\\.)*")\s*:\s*([^,}]+)'
def tokenize(self, input_string):
tokens = re.findall(self.token_pattern, input_string)
return tokens
# Example usage
lexer = SmithyLexer()
input_string = 'exampleKey: "exampleValue", anotherKey: "anotherValue"'
tokens = lexer.tokenize(input_string)
print(tokens)Patched code sample
import re
class SmithyLexer:
# Example of a fixed regular expression to prevent ReDoS
# The original regex may have been too permissive, leading to catastrophic backtracking
# Here, we use a more specific pattern to avoid that issue
token_pattern = r'(?:(?:[a-zA-Z_][a-zA-Z0-9_]*|"(?:[^"\\]|\\.)*")\s*:\s*[^,}]+)'
def tokenize(self, input_string):
tokens = re.findall(self.token_pattern, input_string)
return tokens
# Example usage
lexer = SmithyLexer()
input_string = 'exampleKey: "exampleValue", anotherKey: "anotherValue"'
tokens = lexer.tokenize(input_string)
print(tokens)Published fix
Public sources link this CVE to the changes below. The code samples above were generated to illustrate the defect; these links lead to the code the project actually changed.
- SQL+Jinja: use a simpler regex in analyse_text (pygments/pygments)
pygments/lexers/templates.py · SqlJinjaLexer.analyse_text - Improve the Smithy metadata matcher. (pygments/pygments)
pygments/lexers/smithy.py - Improve Java properties lexer (#2404) (pygments/pygments)
pygments/lexers/configs.py
Cite this entry
@misc{vaitp:cve202240896,
title = {{ReDoS vulnerability in pygments 2.15.0 via SmithyLexer: Resource exhaustion with crafted input}},
author = {Bogaerts, Fr\'ed\'eric and Ivaki, Naghmeh and Fonseca, Jos\'e},
year = {2023},
note = {VAITP Python Vulnerability Dataset, entry CVE-2022-40896},
howpublished = {\url{https://netpack.pt/vaitp/vulnerability/CVE-2022-40896/}}
}
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 ::
