CVE-2026-59894: sqlparse: Generated Python and PHP snippets allow SQL string breakout through unescaped backslashes

Published Aug 17, 2026
·
Updated

Summary

The documented Python and PHP output modes generate source-code snippets from caller-supplied SQL. Their output filters escape quote characters without first escaping existing backslashes. Crafted SQL can therefore neutralize the generated quote escape, terminate the intended language string, and place attacker-controlled code into the generated snippet. If a downstream consumer executes or imports that generated source, the injected code runs in the consumer's environment.

Details

The Python output filter places SQL in a single-quoted string and replaces each single quote with an escaped quote. The PHP output filter performs the equivalent operation for a double-quoted string. Neither transformation escapes pre-existing backslashes before escaping quotes. A backslash supplied immediately before a quote causes the generated backslash to be escaped instead of the quote, allowing the quote to close the string.

The affected modes are exposed through sqlparse.format(..., outputformat='python'), sqlparse.format(..., outputformat='php'), and the corresponding sqlformat -l options. Formatting produces the injected source but does not itself execute it; code execution occurs when a downstream workflow treats the generated snippet as Python or PHP code.

Relevant code locations:

- sqlparse/formatter.py:193 — selection of the output-language filters - sqlparse/filters/output.py:45 — opening of the generated Python string - sqlparse/filters/output.py:65 — incomplete Python quote escaping - sqlparse/filters/output.py:91 — opening of the generated PHP string - sqlparse/filters/output.py:114 — incomplete PHP quote escaping

PoC

A complete validated reproduction is attached as outputformatsnippetinjection-poc.zip. The archive contains reproduction/ at its root, uses Git and Docker, and validates the Python output path by generating and executing a snippet containing a controlled marker-file write.

Extract the archive beside this report, then run:

console ./reproduction/run.sh

Observed result:

The generated Python snippet placed the attacker-controlled pathlib.Path(...).writetext(...) expression outside the intended SQL string. Executing the snippet wrote the expected proof marker, emitted EVOHUNTOUTPUTFORMATINJECTIONVERIFIED, and completed successfully.

Verification method:

The verification helper calls sqlparse.format(..., outputformat='python'), executes the generated snippet, and fails unless the injected Python expression writes the exact proof marker file.

Limitations:

No reproduction blocker was recorded. The attached harness directly verifies the Python output path; exploitation also requires a downstream consumer to execute or import the generated source.

Impact

This is source-code injection in the opt-in Python and PHP snippet-generation modes. An attacker who controls SQL converted by one of these modes can inject language code into the generated artifact. If that artifact is subsequently executed, the attacker can run code with the permissions and access of the downstream Python or PHP process.

The demonstrated end-to-end result is code execution through a generated Python snippet. Formatting the SQL alone does not execute the payload, and ordinary parsing, splitting, or formatting without these output modes is not shown to be affected.

Other sources

sqlparse is a non-validating SQL parser module for Python. Prior to 0.6.0, sqlparse/filters/output.py fails to escape existing backslashes before quotes in sqlparse.format outputformat='python' and outputformat='php' and the corresponding sqlformat -l modes, allowing crafted SQL to terminate the generated string and inject Python or PHP code when a downstream consumer executes or imports the generated source. This issue is fixed in version 0.6.0.

— MITRE

Affected Software

2 affected componentsFixes available
pypi/sqlparse<0.6.0
pip/sqlparse<=0.5.5
0.6.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.6.0
  2. Upgrade

    Upgrade sqlparse to a version that resolves this vulnerability.

    Fixed in 0.6.0

Event History

Aug 17, 2026
CVE Published
via MITRE·05:14 PM
Data Sourced
via MITRE·05:14 PM
DescriptionWeakness
Advisory Published
via GitHub·05:20 PM
Data Sourced
via GitHub·05:20 PM
DescriptionWeaknessAffected Software
Data Sourced
via NVD·06:17 PM
DescriptionSeverityWeakness

Frequently Asked Questions

1

What is the severity of CVE-2026-59894?

The severity of CVE-2026-59894 is rated at 55, indicating a notable risk level.

2

How do I fix CVE-2026-59894?

To fix CVE-2026-59894, update the sqlparse module to version 0.6.0 or later.

3

What systems are affected by CVE-2026-59894?

CVE-2026-59894 affects the sqlparse module used in Python prior to version 0.6.0.

4

What kind of vulnerability is CVE-2026-59894?

CVE-2026-59894 is classified as a code injection vulnerability.

5

What components of sqlparse are vulnerable in CVE-2026-59894?

The vulnerability exists in sqlparse/filters/output.py, which fails to properly escape backslashes.

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