CVE-2021-27291
📋 TL;DR
CVE-2021-27291 is a Regular Expression Denial of Service (ReDoS) vulnerability in Pygments syntax highlighting library versions 1.1 through 2.7.3. Attackers can craft malicious input to trigger exponential/cubic complexity regular expressions, causing excessive CPU consumption and service disruption. Any application using vulnerable Pygments versions for parsing programming language code is affected.
💻 Affected Systems
- Pygments
📦 What is this software?
Fedora by Fedoraproject
Fedora by Fedoraproject
Pygments by Pygments
⚠️ Risk & Real-World Impact
Worst Case
Complete denial of service with prolonged CPU exhaustion, potentially causing application crashes, service unavailability, and resource starvation affecting other services on the same host.
Likely Case
Temporary service degradation or unavailability when processing malicious input, requiring service restart and causing user disruption.
If Mitigated
Minimal impact with proper input validation, rate limiting, and updated Pygments version; potential brief performance degradation.
🎯 Exploit Status
Proof of concept available in GitHub gist; exploitation requires submitting malicious code to be processed by Pygments.
🛠️ Fix & Mitigation
✅ Official Fix
Patch Version: 2.7.4
Vendor Advisory: https://github.com/pygments/pygments/commit/2e7e8c4a7b318f4032493773732754e418279a14
Restart Required: Yes
Instructions:
1. Update Pygments: pip install --upgrade pygments==2.7.4
2. Restart all applications using Pygments
3. Verify no dependencies pin older vulnerable versions
🔧 Temporary Workarounds
Input validation and sanitization
allImplement strict input validation to reject or sanitize code containing patterns that trigger ReDoS
Rate limiting and timeout
allImplement processing timeouts and rate limiting for code parsing operations
🧯 If You Can't Patch
- Implement Web Application Firewall (WAF) rules to block suspicious patterns in code submissions
- Isolate Pygments processing to dedicated containers with strict resource limits and monitoring
🔍 How to Verify
Check if Vulnerable:
Check Pygments version: python -c "import pygments; print(pygments.__version__)" - if version is between 1.1 and 2.7.3 inclusive, system is vulnerable.
Check Version:
python -c "import pygments; print(pygments.__version__)"
Verify Fix Applied:
After update, verify version is 2.7.4 or higher: python -c "import pygments; print(pygments.__version__)"
📡 Detection & Monitoring
Log Indicators:
- Unusually long processing times for code parsing
- High CPU usage spikes in Pygments-related processes
- Application timeouts or crashes during code highlighting
Network Indicators:
- Repeated submissions of similar code patterns
- Unusually large or complex code submissions to endpoints using Pygments
SIEM Query:
source="application_logs" AND ("pygments" OR "syntax highlighting") AND (duration>10s OR "timeout" OR "CPU spike")
🔗 References
- https://gist.github.com/b-c-ds/b1a2cc0c68a35c57188575eb496de5ce
- https://github.com/pygments/pygments/commit/2e7e8c4a7b318f4032493773732754e418279a14
- https://lists.debian.org/debian-lts-announce/2021/03/msg00024.html
- https://lists.debian.org/debian-lts-announce/2021/05/msg00003.html
- https://lists.debian.org/debian-lts-announce/2021/05/msg00006.html
- https://lists.fedoraproject.org/archives/list/package-announce%40lists.fedoraproject.org/message/GSJRFHALQ7E3UV4FFMFU2YQ6LUDHAI55/
- https://lists.fedoraproject.org/archives/list/package-announce%40lists.fedoraproject.org/message/WSLD67LFGXOX2K5YNESSWAS4AGZIJTUQ/
- https://www.debian.org/security/2021/dsa-4878
- https://www.debian.org/security/2021/dsa-4889
- https://gist.github.com/b-c-ds/b1a2cc0c68a35c57188575eb496de5ce
- https://github.com/pygments/pygments/commit/2e7e8c4a7b318f4032493773732754e418279a14
- https://lists.debian.org/debian-lts-announce/2021/03/msg00024.html
- https://lists.debian.org/debian-lts-announce/2021/05/msg00003.html
- https://lists.debian.org/debian-lts-announce/2021/05/msg00006.html
- https://lists.fedoraproject.org/archives/list/package-announce%40lists.fedoraproject.org/message/GSJRFHALQ7E3UV4FFMFU2YQ6LUDHAI55/
- https://lists.fedoraproject.org/archives/list/package-announce%40lists.fedoraproject.org/message/WSLD67LFGXOX2K5YNESSWAS4AGZIJTUQ/
- https://www.debian.org/security/2021/dsa-4878
- https://www.debian.org/security/2021/dsa-4889