CVE-2026-71491: sqlparse: Quadratic O(n²) DoS in group_comments
Summary A comment-only statement (-- c\nn) may cause a Denial of Service (DoS).
Details Location: sqlparse/engine/grouping.py:331-341 (groupcomments), invoked first in group() at grouping.py:439. Reachable via sqlparse.parse() and sqlparse.format(sql, stripcomments=True).
A statement made of many single-line comments ('-- c\n' repeated) lexes in O(n) but groupcomments is O(n²):
python def groupcomments(tlist): tidx, token = tlist.tokennextby(t=T.Comment) while token: eidx, end = tlist.tokennotmatching( lambda tk: imt(tk, t=T.Comment) or tk.isnewline, idx=tidx) ... tidx, token = tlist.tokennextby(t=T.Comment, idx=tidx)
The while loop runs n times and each tokennextby / tokennotmatching rescans the O(n) remaining tokens. When all tokens are comments/newlines nothing ever groups, yet the full scan is repeated per token.
Two following factors increase the severity:
1. groupcomments runs first in group() (grouping.py:439), before the groupmatching token-count guard (grouping.py:34-39). So the entire quadratic cost is paid even on oversized input. MAXGROUPINGTOKENS does not provide protection on this vector. 2. It sits on the primary sanitizer path: format(sql, stripcomments=True), used by query loggers, SQL firewalls, ORMs, and migration tools.
PoC Tested using Python 3.14:
python import time, sqlparse for n in (1000, 2000, 4000): s = "-- c\n" n t = time.perfcounter() sqlparse.format(s, stripcomments=True) print(f"n={n:5d} format(stripcomments)={1000(time.perfcounter()-t):7.1f} ms")
Output:
n= 1000 format(stripcomments)= 106.0 ms n= 2000 format(stripcomments)= 403.3 ms n= 4000 format(stripcomments)= 1602.8 ms
Time increase of ~4× per 2× input (quadratic). parse() shows the identical curve. Instrumented scan counts are exactly 1.0M / 4.0M / 16.0M tokens for n=1000/2000/4000. A ~250 KB comment-only payload forces minutes of CPU regardless of the 10000 token cap.
Impact Denial of Service
Other sources
sqlparse is a non-validating SQL parser module for Python. Prior to 0.6.0, groupcomments in sqlparse/engine/grouping.py repeatedly rescans comment-only statements before the MAXGROUPINGTOKENS guard, causing quadratic CPU consumption through sqlparse.parse() and sqlparse.format(sql, stripcomments=True). This issue is fixed in version 0.6.0.
— MITRE
Affected Software
Remediation
Recommended actions to resolve this vulnerability, in priority order.
- Upgrade
Upgrade
pip/sqlparseto a version that resolves this vulnerability.Fixed in 0.6.0 - Upgrade
Upgrade
sqlparseto a version that resolves this vulnerability.Fixed in 0.6.0
Event History
Frequently Asked Questions
What is the severity of CVE-2026-71491?
CVE-2026-71491 is rated with a risk level of 28.
How do I fix CVE-2026-71491?
To fix CVE-2026-71491, upgrade sqlparse to version 0.6.0 or later.
What is the impact of CVE-2026-71491?
CVE-2026-71491 can lead to a Denial of Service (DoS) due to excessive CPU consumption.
Which versions of sqlparse are affected by CVE-2026-71491?
CVE-2026-71491 affects all sqlparse versions prior to 0.6.0.
What type of attack does CVE-2026-71491 represent?
CVE-2026-71491 represents a quadratic time complexity issue that can be exploited for Denial of Service.