CVE-2021-32839
JSON

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 packageVersionsReported by
andialbrecht sqlparse>= 0.4.0, < 0.4.2CVE List
sqlparse (PyPI)from 0.4.0 before 0.4.2, from 1499cffcd7c4d635b4297b44d48fb4fe94cf988e before 8238a9e450ed1524e40cb3a8b0b3c00606903aebGitHub Advisory Database, PyPA Advisory Database
sqlparse_project sqlparsefrom 0.4.0 before 0.4.2NVD

All scores

GitHub_M7.5high CVSS:3.1CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:N/I:N/A:H
GitHub7.5high CVSS:3.1CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:N/I:N/A:H
GitHubhigh CVSS:4.0CVSS:4.0/AV:N/AC:L/AT:N/PR:N/UI:N/VC:N/VI:N/VA:H/SC:N/SI:N/SA:N
NVD5.0medium CVSS:2.0AV: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:

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

SourceRecordLicence
CVE ListCVE-2021-32839 changed 2025-11-03CVE Terms of Use
GitHub Advisory DatabaseGHSA-p5w8-wqhj-9hhf changed 2026-07-08CC-BY-4.0
NVDCVE-2021-32839 changed 2026-06-17Public domain
PyPA Advisory DatabasePYSEC-2021-333 changed 2023-11-08CC-BY-4.0