CVE-2024-4340: Passing a heavily nested list to sqlparse.parse() leads to a Denial of Service due to RecursionError.

Published Apr 15, 2024
·
Updated

Summary Passing a heavily nested list to sqlparse.parse() leads to a Denial of Service due to RecursionError.

Details + PoC Running the following code will raise Maximum recursion limit exceeded exception: py import sqlparse sqlparse.parse('[' 10000 + ']' 10000) We expect a traceback of RecursionError: py Traceback (most recent call last): File "triggersqlparsenestedlist.py", line 3, in <module> sqlparse.parse('[' 10000 + ']' 10000) File "/home/uriya/.local/lib/python3.10/site-packages/sqlparse/init.py", line 30, in parse return tuple(parsestream(sql, encoding)) File "/home/uriya/.local/lib/python3.10/site-packages/sqlparse/engine/filterstack.py", line 36, in run stmt = grouping.group(stmt) File "/home/uriya/.local/lib/python3.10/site-packages/sqlparse/engine/grouping.py", line 428, in group func(stmt) File "/home/uriya/.local/lib/python3.10/site-packages/sqlparse/engine/grouping.py", line 53, in groupbrackets groupmatching(tlist, sql.SquareBrackets) File "/home/uriya/.local/lib/python3.10/site-packages/sqlparse/engine/grouping.py", line 48, in groupmatching tlist.grouptokens(cls, openidx, closeidx) File "/home/uriya/.local/lib/python3.10/site-packages/sqlparse/sql.py", line 328, in grouptokens grp = grpcls(subtokens) File "/home/uriya/.local/lib/python3.10/site-packages/sqlparse/sql.py", line 161, in init super().init(None, str(self)) File "/home/uriya/.local/lib/python3.10/site-packages/sqlparse/sql.py", line 165, in str return ''.join(token.value for token in self.flatten()) File "/home/uriya/.local/lib/python3.10/site-packages/sqlparse/sql.py", line 165, in <genexpr> return ''.join(token.value for token in self.flatten()) File "/home/uriya/.local/lib/python3.10/site-packages/sqlparse/sql.py", line 214, in flatten yield from token.flatten() File "/home/uriya/.local/lib/python3.10/site-packages/sqlparse/sql.py", line 214, in flatten yield from token.flatten() File "/home/uriya/.local/lib/python3.10/site-packages/sqlparse/sql.py", line 214, in flatten yield from token.flatten() [Previous line repeated 983 more times] RecursionError: maximum recursion depth exceeded

Fix suggestion The flatten() function of TokenList class should limit the recursion to a maximal depth: py from sqlparse.exceptions import SQLParseError

MAXDEPTH = 100

def flatten(self, depth=1): """Generator yielding ungrouped tokens.

This method is recursively called for all child tokens. """ if depth >= MAXDEPTH: raise SQLParseError('Maximal depth reached') for token in self.tokens: if token.isgroup: yield from token.flatten(depth + 1) else: yield token

Impact Denial of Service (the impact depends on the use). Anyone parsing a user input with sqlparse.parse() is affected.

Other sources

Passing a heavily nested list to sqlparse.parse() leads to a Denial of Service due to RecursionError.

https://github.com/advisories/GHSA-2m57-hf25-phgg https://github.com/andialbrecht/sqlparse/commit/b4a39d9850969b4e1d6940d32094ee0b42a2cf03 https://research.jfrog.com/vulnerabilities/sqlparse-stack-exhaustion-dos-jfsa-2024-001031292/

Red Hat

Affected Software

7 affected componentsFixes available
pip/sqlparse<0.5.0
0.5.0
ubuntu/sqlparse<0.4.2-1ubuntu0.22.04.2
0.4.2-1ubuntu0.22.04.2
ubuntu/sqlparse<0.4.2-1ubuntu1.1
0.4.2-1ubuntu1.1
ubuntu/sqlparse<0.4.4-1ubuntu0.1
0.4.4-1ubuntu0.1
ubuntu/sqlparse<0.5.0
0.5.0
debian/sqlparse<=0.4.1-1, <=0.4.2-1
0.5.1-2
redhat/sqlparse<0.5.0
0.5.0

Remediation

Recommended actions to resolve this vulnerability, in priority order.

  1. Upgrade

    Upgrade pip/sqlparse to a version that resolves this vulnerability.

    Fixed in 0.5.0
  2. Upgrade

    Upgrade ubuntu/sqlparse to a version that resolves this vulnerability.

    Fixed in 0.4.2-1ubuntu0.22.04.2
  3. Upgrade

    Upgrade ubuntu/sqlparse to a version that resolves this vulnerability.

    Fixed in 0.4.2-1ubuntu1.1
  4. Upgrade

    Upgrade ubuntu/sqlparse to a version that resolves this vulnerability.

    Fixed in 0.4.4-1ubuntu0.1
  5. Upgrade

    Upgrade ubuntu/sqlparse to a version that resolves this vulnerability.

    Fixed in 0.5.0
  6. Upgrade

    Upgrade debian/sqlparse to a version that resolves this vulnerability.

    Fixed in 0.5.1-2
  7. Upgrade

    Upgrade redhat/sqlparse to a version that resolves this vulnerability.

    Fixed in 0.5.0
  8. Upgrade

    Upgrade sqlparse to a version that resolves this vulnerability.

    Patch GHSA-2m57-hf25-phgg
  9. Upgrade

    Upgrade andialbrecht/sqlparse to a version that resolves this vulnerability.

    Patch b4a39d9850969b4e1d6940d32094ee0b42a2cf03
  10. Compensating control

    If you must accept user-supplied data, prevent abuse by limiting/validating the size/complexity of input passed to sqlparse.parse() to avoid recursion/stack exhaustion (the issue is triggered by heavily nested input).

Event History

Apr 15, 2024
Advisory Published
via GitHub·08:21 PM
Apr 30, 2024
CVE Published
via Ubuntu·12:00 AM
CVE Published
via MITRE·02:23 PM
Data Sourced
via MITRE·02:23 PM
DescriptionSeverityWeakness
Data Sourced
via NVD·03:15 PM
DescriptionSeverityWeakness
Data Sourced
via Red Hat·09:33 PM
DescriptionSeverityAffected Software
May 29, 2024
Data Sourced
via Launchpad·04:51 PM
Description

Parent advisories

This vulnerability appears in the following advisories.

Frequently Asked Questions

1

What is the severity of CVE-2024-4340?

CVE-2024-4340 has been classified as a Denial of Service vulnerability due to potential recursion errors.

2

How do I fix CVE-2024-4340?

To fix CVE-2024-4340, upgrade the sqlparse package to version 0.5.0 or later.

3

Which versions of sqlparse are affected by CVE-2024-4340?

The versions of sqlparse affected by CVE-2024-4340 are below 0.5.0.

4

What causes the vulnerability in CVE-2024-4340?

CVE-2024-4340 is caused by passing a heavily nested list to the sqlparse.parse() function, leading to a RecursionError.

5

Is there a proof of concept available for CVE-2024-4340?

Yes, a proof of concept for CVE-2024-4340 demonstrates the vulnerability by executing a specific parsing command that triggers the error.

Contact

SecAlerts Pty Ltd.
132 Wickham Terrace
Fortitude Valley,
QLD 4006, Australia
info@secalerts.co
By using SecAlerts services, you agree to our services end-user license agreement. This website is safeguarded by reCAPTCHA and governed by the Google Privacy Policy and Terms of Service. All names, logos, and brands of products are owned by their respective owners, and any usage of these names, logos, and brands for identification purposes only does not imply endorsement. If you possess any content that requires removal, please get in touch with us.
© 2026 SecAlerts Pty Ltd.
ABN: 70 645 966 203, ACN: 645 966 203