SQLParse CPU Denial of Service via Algorithmic Complexity
A complexity vulnerability in sqlparse <= 0.5.5 allows attackers to trigger CPU exhaustion through deeply nested SQL structures, achieving significant amplification and causing denial of service in downstream applications.
CVE search metadata
CVE search record: CVE-2026-54284. KEV: no. Product: sqlparse (<= 0.5.5). Brief: SQLParse CPU Denial of Service via Algorithmic Complexity. Brief link: https://feed.craftedsignal.io/briefs/2026-08-sqlparse-dos/
What's new
- 1. added coverage for sqlparse (<= 0.5.5) Aug 17, 18:46 via ghsa
The Python library sqlparse (version 0.5.5 and earlier) is vulnerable to an algorithmic complexity denial-of-service (DoS) attack. The vulnerability exists within TokenList.__init__, which performs an eager, recursive flattening of the SQL subtree (via str(self)) during the construction of every token group.
When sqlparse processes input containing deeply nested structures such as parentheses, CASE WHEN chains, or nested subqueries, the parser performs work proportional to O(n*d) (where n is the number of tokens and d is the nesting depth). This results in a massive CPU amplification - approximately 5000x for a ~2 KB payload - that consumes significant CPU time before reaching the library's built-in depth and token caps. An attacker can exhaust worker pools in multi-threaded web applications or lock up single-threaded services by sending a small number of these crafted malicious SQL payloads. The issue affects any consumer of sqlparse that exposes the parser to unauthenticated user input, including formatters, debug toolbars, and metadata analysis tools.
Attack Chain
- Attacker identifies a target application utilizing
sqlparseto process user-supplied SQL queries (e.g., SQL formatters or database debug interfaces). - Attacker crafts a malicious 1-2 KB SQL payload containing high levels of nesting, such as
SELECT (((((...)))))(2000+ levels) or deeply nestedCASE WHENbranches. - Attacker sends multiple parallel HTTP POST requests containing the crafted payload to the vulnerable endpoint.
- The application triggers
sqlparse.parse()orsqlparse.format()upon receiving the input. - The library's
TokenList.__init__is invoked recursively during grouping, triggering theO(n*d)flattening logic. - The system enters a high-CPU state while performing recursive flattening, effectively locking the worker process.
- The application worker remains unresponsive for seconds to tens of seconds per request.
- Concurrent requests exhaust the available worker pool, resulting in a denial-of-service condition for legitimate users.
Impact
Successful exploitation leads to resource exhaustion and service unavailability. Observations indicate that a single 2 KB payload can pin a CPU worker at 100% utilization for approximately 10 seconds. In environments with a limited worker pool, a small number of concurrent requests can result in total service outage. Downstream libraries, such as sql-metadata, also inherit this vulnerability, extending the impact to any tool using sqlparse for internal query analysis.
Recommendation
- Immediately upgrade
sqlparseto a version that implements the patch described in the advisory (replacing eagerstr(self)materialization with concatenation of cached values). - Implement request-size limits and payload-structure complexity heuristics at the WAF or reverse proxy level to detect and drop highly nested SQL patterns before they reach the application.
- Ensure application-level timeouts are configured for all database-related processing functions to prevent worker-pool starvation from slow-running parser tasks.
- Review internal usage of
sqlparsein debug toolbars or log formatters to ensure untrusted user input is not passed directly to library entry points without sanitization or strict depth validation.
Immediate actions
Upgrade sqlparse library to the patched version.
Mitigations
Implement request timeout and complexity monitoring on endpoints parsing user-supplied SQL.
CVE-2026-54284