CVE-2021-27291: pygments - Regular Expression Denial of Service (ReDoS)
Summary
CVE-2021-27291: In pygments 1.1+, fixed in 2.7.4, the lexers used to parse programming languages rely heavily on regular expressions. Affects pygments across 2 version ranges.
>= 1.1, < 2.7.4
< 2e7e8c4a7b318f4032493773732754e418279a14
Fixed in 2e7e8c4a7b318f4032493773732754e418279a14. How to fix ↓
Also tracked for
- py3-pygments · Alpine:v3.11
The same CVE-2021-27291 is tracked separately for each ecosystem and vendor. Its fix information is the same; the package and affected versions differ.
Check if this vulnerability affects your application
Upload a manifest or SBOM and we'll show whether you're affected, plus every other risky package in the same scan.
No signup required · Results in about 60 seconds
Background
Pygments is a syntax highlighting library for programming languages. It provides lexers to parse and analyze code in various languages. The library is widely used in software development and other applications that require code highlighting and analysis. However, versions 1.1 and above of pygments are vulnerable to a Regular Expression Denial of Service (ReDoS) attack. This vulnerability arises from the use of regular expressions with exponential or cubic worst-case complexity in the lexers. An attacker can exploit this vulnerability by crafting malicious input, leading to a denial of service.
Vulnerability Detail
The vulnerability in pygments arises from the use of regular expressions with exponential or cubic worst-case complexity in the lexers. When processing certain input, the regular expressions can be exploited by an attacker to cause a denial of service. By carefully crafting malicious input, an attacker can trigger excessive backtracking in the regular expressions, leading to a significant increase in processing time and resource consumption. This can result in the application becoming unresponsive or crashing, causing a denial of service for legitimate users.
How To Fix
Latest published version: 2.21.0 as of 22 Oct 2026
pip install --upgrade "pygments==2.21.0"
Upgrading to it may include breaking changes; the version below is the minimum fixed release for this vulnerability.
If you need the minimum change
2e7e8c4a7b318f4032493773732754e418279a14
This is the first release that contains the fix.
pip install --upgrade "pygments==2e7e8c4a7b318f4032493773732754e418279a14"
Tip — Not sure which version your app runs? Vulert can match this fix to it. Scan free →
Workaround
There is no known workaround for this vulnerability. It is essential to update pygments to the latest version to mitigate the risk.
Conclusion
In conclusion, pygments versions 1.1 and above are vulnerable to a Regular Expression Denial of Service (ReDoS) attack. This can be exploited by an attacker to cause a denial of service by crafting malicious input. To mitigate this vulnerability, it is crucial to update pygments to version 2.7.4 or later. For further support on vulnerability remediation, you can contact DevNack.com.
See every vulnerability recorded for pygments →
FAQ
Pygments is a syntax highlighting library used for code analysis and highlighting in various programming languages.
The vulnerability allows an attacker to cause a denial of service by exploiting the regular expression complexity in pygments. This can result in the application becoming unresponsive or crashing.
To fix this vulnerability, update pygments to version 2.7.4 or later using the command 'pip install --upgrade pygments'.
There is no known workaround for this vulnerability. It is crucial to update pygments to the latest version.
For more information about this vulnerability, you can refer to the official advisory at https://github.com/pypa/advisory-database/blob/main/vulns/pygments/PYSEC-2021-141.yaml
Related vulnerabilities
Recent vulnerabilities recorded for pygments.