# CVE-2024-4340: PYSEC-2026-1940 - sqlparse parsing heavily nested list leads to Denial of Service

Source: https://junglewise.ai/threats/cve-2024-4340-sqlparse-denial-of-service-via-heavily-nested-list  
Publisher: Junglewise Threat Intelligence, https://junglewise.ai/threats  
Published: 2026-07-07. Updated: 2026-09-26  
License: CC BY 4.0 with a link back

CVE-2024-4340 · Severity: low · CVSS 3.1 · Published 2026-07-07

sqlparse is a Python library that parses SQL statements. When processing SQL input containing deeply nested brackets, the parsing logic triggers uncontrolled recursion that exhausts the application's stack, causing it to crash. Any application using sqlparse to parse untrusted SQL input is vulnerable to denial of service.

Technologies: [sqlparse (PyPI)](https://junglewise.ai/threats/technologies/sqlparse). Vendors: [PyPI](https://junglewise.ai/threats/vendors/pypi).

## Quick answers

**Is CVE-2024-4340 exploited in the wild?** No. Junglewise Threat Intelligence has no report of CVE-2024-4340 being exploited in the wild.

**How severe is CVE-2024-4340?** Low severity, with a CVSS score of 3.1, published 7 July 2026.

**Which products and versions are affected?** sqlparse (PyPI) ECOSYSTEM:introduced=0,fixed=0.5.0.

**Is there a fix for CVE-2024-4340?** Yes. sqlparse (PyPI): upgrade to 0.5.0 or later. Junglewise finds every place you run it, drafts the upgrade and applies it once you approve.

**How is CVE-2024-4340 attacked?** It is attacked over the network.

**Am I affected by CVE-2024-4340?** Check whether you run sqlparse (PyPI) at an affected version. Junglewise does that check for you around the clock: connect your code, cloud and domains, and it matches new vulnerabilities like this one against what you actually run and tells you when one reaches you.

## How to fix

- sqlparse (PyPI): upgrade to 0.5.0 or later

  ```sh
  pip install "sqlparse>=0.5.0"
  ```


## Technical details

The vulnerability is an uncontrolled recursion flaw (CWE-674) in the flatten() method of the TokenList class. When parsing heavily nested bracket structures (e.g., thousands of nested square brackets), the recursive flatten() function is called repeatedly without depth limits, exhausting the call stack and raising a RecursionError. The attack vector is network-based with no authentication or user interaction required—any caller of sqlparse.parse() with attacker-controlled input can trigger the crash. The fix, available in version 0.5.0, implements a maximum recursion depth check that raises an exception before stack exhaustion occurs.

## Affected products

- sqlparse sqlparse before 0.5.0

## Identifiers

- CVE-2024-4340
- GHSA-2m57-hf25-phgg
- PYSEC-2026-1940

## Timeline

- 2024-04-15: disclosed
- 2024-04-15: patched: Fix released in version 0.5.0

## References

- [https://github.com/andialbrecht/sqlparse/security/advisories/GHSA-2m57-hf25-phgg](https://github.com/andialbrecht/sqlparse/security/advisories/GHSA-2m57-hf25-phgg)

## Related threats

- [sqlparse quadratic CPU consumption in reindent filter](https://junglewise.ai/threats/cve-2026-84305-sqlparse-quadratic-cpu-consumption-in-reindent-filter)
- [sqlparse quadratic DoS in group_comments](https://junglewise.ai/threats/cve-2026-71491-sqlparse-quadratic-dos-in-group-comments)
- [sqlparse code injection in Python and PHP output modes](https://junglewise.ai/threats/cve-2026-59894-sqlparse-code-injection-in-python-and-php-output-modes)
- [sqlparse inefficient regex handling in dollar-quoted literals](https://junglewise.ai/threats/cve-2026-59893-sqlparse-inefficient-regex-handling-in-dollar-quoted-literals)
- [sqlparse TokenList CPU denial of service](https://junglewise.ai/threats/cve-2026-54284-sqlparse-tokenlist-cpu-denial-of-service)
