Summary
sqlparse: Quadratic O(n²) DoS in group_comments
A comment-only statement (-- c\n*n) may cause a Denial of Service (DoS).
Details
Location: sqlparse/engine/grouping.py:331-341 (group_comments), invoked first in group() at grouping.py:439. Reachable via sqlparse.parse() and sqlparse.format(sql, strip_comments=True).
A statement made of many single-line comments ('-- c\n' repeated) lexes in O(n) but group_comments is O(n²):
def group_comments(tlist):
tidx, token = tlist.token_next_by(t=T.Comment)
while token:
eidx, end = tlist.token_not_matching(
lambda tk: imt(tk, t=T.Comment) or tk.is_newline, idx=tidx)
...
tidx, token = tlist.token_next_by(t=T.Comment, idx=tidx)
The while loop runs n times and each token_next_by / token_not_matching 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:
group_commentsruns first ingroup()(grouping.py:439), before the_group_matchingtoken-count guard (grouping.py:34-39). So the entire quadratic cost is paid even on oversized input.MAX_GROUPING_TOKENSdoes not provide protection on this vector.- It sits on the primary sanitizer path:
format(sql, strip_comments=True), used by query loggers, SQL firewalls, ORMs, and migration tools.
PoC
Tested using Python 3.14:
import time, sqlparse
for n in (1000, 2000, 4000):
s = "-- c\n" * n
t = time.perf_counter()
sqlparse.format(s, strip_comments=True)
print(f"n={n:5d} format(strip_comments)={1000*(time.perf_counter()-t):7.1f} ms")
Output:
n= 1000 format(strip_comments)= 106.0 ms
n= 2000 format(strip_comments)= 403.3 ms
n= 4000 format(strip_comments)= 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
Crafted input forces the application to consume excessive CPU, memory, or other resources, degrading or denying service. Typical impact: denial of service.
Affected versions
Security releases
Kodem intelligence
Severity tells you how bad this could be in the worst case. It does not tell you whether you are exposed. Exploitability and impact are functions of runtime truth: whether the vulnerable code is present, reachable, and actually executes in your application. A vulnerable package can sit in your dependency tree and never run.
Kodem, an Intelligent Application Security platform, uses runtime intelligence to reveal which vulnerabilities actually execute in production, so teams prioritize the ones that genuinely matter. Kodem's runtime-powered SCA identifies whether this CVE is reachable in your applications.
Already deployed Kodem?
See it in your environmentNew to Kodem? Get a demo →Remediation advice
Kodem Kai can prioritize this vulnerability in your dependency tree and generate a fix recommendation.
Frequently Asked Questions
- What is CVE-2026-71491? CVE-2026-71491 is a high-severity uncontrolled resource consumption vulnerability in sqlparse (pip), affecting versions <= 0.5.5. It is fixed in 0.6.0. Crafted input forces the application to consume excessive CPU, memory, or other resources, degrading or denying service.
- Which versions of sqlparse are affected by CVE-2026-71491? sqlparse (pip) versions <= 0.5.5 is affected.
- Is there a fix for CVE-2026-71491? Yes. CVE-2026-71491 is fixed in 0.6.0. Upgrade to this version or later.
- Is CVE-2026-71491 exploitable, and should I be worried? Whether CVE-2026-71491 is exploitable in your environment depends on whether the vulnerable code is present and reachable. A CVSS score is a worst-case rating; it does not account for your specific deployment, configuration, or usage patterns. Kodem, an Intelligent Application Security platform, uses runtime intelligence to show which vulnerabilities actually execute in production, so you can focus on the ones that represent real risk. Get a demo
- What actually determines whether CVE-2026-71491 is exploitable, and how bad it is? Exploitability and impact are not fixed properties of a CVE. They depend on runtime truth: whether the vulnerable code is present, reachable, and actually executes in your application. A high CVSS score on a dependency that never runs is not the same as real risk. Kodem, an Intelligent Application Security platform, uses runtime intelligence to reveal which vulnerabilities actually execute in production, so teams prioritize the ones that genuinely matter.
- How do I fix CVE-2026-71491? Upgrade
sqlparseto 0.6.0 or later.