CVE-2021-27291: pygments - Regular Expression Denial of Service (ReDoS)

pygments Updated 02 Oct 2025

Summary

17/03/2021
Yes i
High
7.5

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

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.

Free scan

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.

Check my app free

No signup required · Results in about 60 seconds

Sponsored Devnack Penetration testing for web, API and mobile Manual testing by engineers who build software, not an auditor's checklist. Trusted by teams at Swift and Google Talk to Devnack

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 →

Share this vulnerability

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.

Related vulnerabilities

Recent vulnerabilities recorded for pygments.

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 →

Subscribe to Vulert on Telegram
Daily digest of new CVEs, critical and high severity, affected packages and fixes — plus a weekly roundup.
Join the channel →
Help improve this record
Report an issue or send feedback