7.5high
Regular Expression Denial of Service in sqlparse
- Severity
- 7.5high CVSS:3.1 by GitHub_M
- Exploit likelihood
- 2.34% (higher than 84% of CVEs; EPSS, by FIRST)
- Published
- Last changed
- Assigned by
- GitHub_M
- Also known as
- GHSA-p5w8-wqhj-9hhfPYSEC-2021-333
Description
sqlparse is a non-validating SQL parser module for Python. In sqlparse versions 0.4.0 and 0.4.1 there is a regular Expression Denial of Service in sqlparse vulnerability. The regular expression may cause exponential backtracking on strings containing many repetitions of '\r\n' in SQL comments. Only the formatting feature that removes comments from SQL statements is affected by this regular expression. As a workaround don't use the sqlformat.format function with keyword strip_comments=True or the --strip-comments command line flag when using the sqlformat command line tool. The issues has been fixed in sqlparse 0.4.2.
Affected
| Product or package | Versions | Reported by |
|---|---|---|
| andialbrecht sqlparse | >= 0.4.0, < 0.4.2 | CVE List |
| sqlparse (PyPI) | from 0.4.0 before 0.4.2, from 1499cffcd7c4d635b4297b44d48fb4fe94cf988e before 8238a9e450ed1524e40cb3a8b0b3c00606903aeb | GitHub Advisory Database, PyPA Advisory Database |
| sqlparse_project sqlparse | from 0.4.0 before 0.4.2 | NVD |
All scores
| GitHub_M | 7.5high CVSS:3.1 | CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:N/I:N/A:H |
| GitHub | 7.5high CVSS:3.1 | CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:N/I:N/A:H |
| GitHub | high CVSS:4.0 | CVSS:4.0/AV:N/AC:L/AT:N/PR:N/UI:N/VC:N/VI:N/VA:H/SC:N/SI:N/SA:N |
| NVD | 5.0medium CVSS:2.0 | AV:N/AC:L/Au:N/C:N/I:N/A:P |
Classification
What each source says
CVE List
sqlparse is a non-validating SQL parser module for Python. In sqlparse versions 0.4.0 and 0.4.1 there is a regular Expression Denial of Service in sqlparse vulnerability. The regular expression may cause exponential backtracking on strings containing many repetitions of '\r\n' in SQL comments. Only the formatting feature that removes comments from SQL statements is affected by this regular expression. As a workaround don't use the sqlformat.format function with keyword strip_comments=True or the --strip-comments command line flag when using the sqlformat command line tool. The issues has been fixed in sqlparse 0.4.2.
GitHub Advisory Database
Impact
The formatter function that strips comments from a SQL contains a regular expression that is vulnerable to ReDoS (Regular Expression Denial of Service). The regular expression may cause exponential backtracking on strings containing many repetitions of '\r\n' in SQL comments.
Patches
The issues has been fixed in sqlparse 0.4.2.
Workarounds
Only the formatting feature that removes comments from SQL statements is affected by this regular expression. As a workaround don't use the sqlformat.format function with keyword strip_comments=True or the --strip-comments command line flag when using the sqlformat command line tool.
References
This issue was discovered by GitHub team members @erik-krogh and @yoff. It was found using a CodeQL query which identifies inefficient regular expressions. You can see the results of the query on python-sqlparse by following this link.
For more information
If you have any questions or comments about this advisory:
- Open an issue in sqlparse issue tracker
- Email us at albrecht.andi@gmail.com
NVD (es)
sqlparse es un módulo analizador de SQL no comprobable para Python. En las versiones 0.4.0 y 0.4.1 de sqlparse, se presenta una vulnerabilidad de Denegación de Servicio por Expresión regular en sqlparse. La expresión regular puede causar un retroceso exponencial en las cadenas que contienen muchas repeticiones de "\r\n" en los comentarios SQL. Sólo la función de formato que elimina los comentarios de las sentencias SQL esta afectada por esta expresión regular. Como solución, no use la función sqlformat.format con la palabra clave strip_comments=True o el indicador de línea de comandos --strip-comments cuando use la herramienta de línea de comandos sqlformat. Este problema ha sido corregido en sqlparse versión 0.4.2
References
Sources
| Source | Record | Licence |
|---|---|---|
| CVE List | CVE-2021-32839 changed 2025-11-03 | CVE Terms of Use |
| GitHub Advisory Database | GHSA-p5w8-wqhj-9hhf changed 2026-07-08 | CC-BY-4.0 |
| NVD | CVE-2021-32839 changed 2026-06-17 | Public domain |
| PyPA Advisory Database | PYSEC-2021-333 changed 2023-11-08 | CC-BY-4.0 |