HighGHSA-f2ff-p2ww-7p4pCVE-2026-71491PYSEC-2026-3697

sqlparse: Quadratic O(n²) DoS in group_comments

Published 17 Aug 2026 · Updated 10 Sept 2026

Is your project on an affected sqlparse?

Private repo? Upload its lockfile instead, or connect GitHub to check every repo.

Look after several Python apps? Indie watches up to 15 repos, private ones too, for $5 a month. See Indie

Affected package

Affected
<0.6.0
Fixed in
0.6.0
Update to the fixed version for your release line, for example:pip install 'sqlparse>=0.6.0'

Details

Summary

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:

1. group_comments runs first in group() (grouping.py:439), before the _group_matching token-count guard (grouping.py:34-39). So the entire quadratic cost is paid even on oversized input. MAX_GROUPING_TOKENS does not provide protection on this vector. 2. 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

Is GHSA-f2ff-p2ww-7p4p in any of your client repos?

Checking one package by hand is fine. Checking it across forty client projects is not. NPMscan reads the lockfiles in every GitHub repo you pick, finds each project with an affected sqlparse, and gives you the command that fixes it.

More advisories for sqlparse

  • MediumCVE-2026-84305sqlparse: Reindentation of tuple lists causes near-cap quadratic CPU consumption · 1 Sept 2026
  • HighCVE-2026-59893sqlparse: Inefficient Regex Handling of Dollar-Quoted SQL Literals Leads to ReDoS (Denial of Service) · 17 Aug 2026
  • HighCVE-2026-54284sqlparse: TokenList.__init__ materializes O(subtree) value per group, causing CPU DoS before depth/token caps trigger · 17 Aug 2026
  • MediumCVE-2026-59894sqlparse: Generated Python and PHP snippets allow SQL string breakout through unescaped backslashes · 17 Aug 2026
  • MediumGHSA-27jp-wm6q-gp25sqlparse: formatting list of tuples leads to denial of service · 13 Feb 2026
  • HighCVE-2024-4340sqlparse parsing heavily nested list leads to Denial of Service · 15 Apr 2024
All 9 advisories for sqlparse

References

Advisory data from OSV.dev (opens OSV.dev in a new tab), refreshed daily.