VAITP Dataset

← Back to the dataset

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.

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/}}
}

Download this entry as JSON Download the whole dataset

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 ::

Before you use the VAITP AI models

Please read and accept the following disclaimer:

By choosing "I understand and accept", you acknowledge that you understand and agree to these terms.