CVE-2021-20270: Infinite Loop in Pygments
Summary
CVE-2021-20270: Infinite Loop in Pygments. Affects pygments versions >= 1.5, < 2.7.4. Fixed in 2.7.4. How to fix ↓
Also tracked for
- py3-pygments · Alpine:v3.14
The same CVE-2021-20270 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 package that supports over 500 programming languages. It is widely used in various applications and development environments to enhance code readability. However, Pygments versions 1.5 to 2.7.3 are vulnerable to an infinite loop in the SMLLexer module. This vulnerability can be triggered when performing syntax highlighting of a Standard ML (SML) source file that contains only the "exception" keyword.
Vulnerability Detail
The infinite loop in Pygments can lead to a denial of service (DoS) attack. An attacker can exploit this vulnerability by providing a specially crafted SML source file, causing the Pygments library to enter an infinite loop and consume excessive system resources. This can result in the unavailability of the affected application or system, impacting its functionality and performance. The vulnerability has a CVSS score of 7.5, indicating a high severity level.
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
2.7.4
This is the first release that contains the fix.
pip install --upgrade "pygments==2.7.4"
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 crucial to update Pygments to the latest version to mitigate the risk of a DoS attack.
Conclusion
The infinite loop vulnerability in Pygments can be exploited to cause a denial of service attack. It is crucial to update Pygments to version 2.7.4 or later to mitigate this risk. For further assistance in vulnerability remediation, please contact DevNack.com.
See every vulnerability recorded for pygments →
FAQ
Pygments is a syntax highlighting package used for enhancing code readability in various programming languages.
Pygments versions 1.5 to 2.7.3 are affected by this vulnerability.
This vulnerability can lead to a denial of service (DoS) attack, causing the affected application or system to become unresponsive.
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. Updating Pygments is the recommended solution.
Related vulnerabilities
Recent vulnerabilities recorded for pygments.