CVE-2026-59894 Overview
CVE-2026-59894 is a code injection vulnerability [CWE-94] in sqlparse, a non-validating SQL parser module for Python. The flaw exists in sqlparse/filters/output.py in versions prior to 0.6.0. The formatter fails to escape existing backslashes before quotes when rendering SQL to output_format='python' or output_format='php', and in the equivalent sqlformat -l modes. Attackers can craft SQL input that terminates the generated string literal and injects arbitrary Python or PHP code. The injected code executes when a downstream consumer runs or imports the generated source file.
Critical Impact
Crafted SQL processed through sqlparse.format can inject executable Python or PHP into generated source code, leading to arbitrary code execution in downstream consumers.
Affected Products
- sqlparse Python module versions prior to 0.6.0
- Applications invoking sqlparse.format with output_format='python' or output_format='php'
- Tooling that uses sqlformat -l python or sqlformat -l php on untrusted SQL
Discovery Timeline
- 2026-08-17 - CVE-2026-59894 published to NVD
- 2026-08-17 - Last updated in NVD database
Technical Details for CVE-2026-59894
Vulnerability Analysis
The sqlparse library converts SQL statements into string literals embedded in Python or PHP source code when the output_format option targets those languages. The output filter quotes SQL fragments but does not escape backslash characters that already exist in the SQL text. An attacker who controls SQL passed to sqlparse.format can supply a trailing backslash followed by a quote to break out of the generated string literal. Once outside the literal, appended tokens are interpreted as code by the target language parser. When a downstream workflow imports or executes the generated file, the injected payload runs with the privileges of that process.
Root Cause
The root cause is improper neutralization of special elements in code output [CWE-94]. The formatter in sqlparse/filters/output.py escapes quotation marks but treats backslashes as literal characters. In Python and PHP string literals, a backslash preceding a quote produces an escaped quote inside the literal, which shifts the string terminator to a later position and exposes subsequent bytes as executable syntax.
Attack Vector
Exploitation requires a workflow that accepts attacker-influenced SQL and emits generated Python or PHP source through sqlparse. The attacker embeds a backslash-quote sequence followed by language-specific code inside the SQL. When the generated file is later imported, executed, or evaluated, the injected code runs. User interaction is required because the generated artifact must be consumed by a downstream process. No verified public proof-of-concept code is available; see the GitHub Security Advisory GHSA-3496-9g83-7v6x for advisory details.
Detection Methods for CVE-2026-59894
Indicators of Compromise
- Generated Python or PHP files containing unexpected statements after string literals derived from SQL input
- Presence of backslash-quote sequences (\" or \') inside SQL passed to sqlparse.format
- Unexplained imports, subprocess calls, or network activity originating from build tooling that consumes sqlparse output
Detection Strategies
- Audit application inventories for sqlparse package versions below 0.6.0 in Python virtual environments and container images
- Static-analysis review of code paths that call sqlparse.format with output_format set to python or php
- Compare hashes of generated source files against expected baselines in CI/CD pipelines
Monitoring Recommendations
- Log all invocations of sqlformat -l python and sqlformat -l php in build and reporting pipelines
- Alert on anomalous child processes spawned by scripts that import sqlparse-generated code
- Track file writes from tooling that emits Python or PHP source based on SQL input
How to Mitigate CVE-2026-59894
Immediate Actions Required
- Upgrade sqlparse to version 0.6.0 or later across all Python environments
- Rebuild container images and reinstall dependencies that pin vulnerable sqlparse releases
- Review any previously generated Python or PHP files produced from untrusted SQL for injected content
Patch Information
The issue is fixed in sqlparse version 0.6.0. The maintainers corrected the output filter to escape existing backslashes before applying quote escaping. Details are published in the GitHub Security Advisory GHSA-3496-9g83-7v6x.
Workarounds
- Avoid passing untrusted SQL to sqlparse.format with output_format='python' or output_format='php'
- Sanitize SQL input to strip or escape backslash characters before invoking the formatter
- Restrict execution or import of generated source files to trusted, reviewed artifacts only
Disclaimer: This content was generated using AI. While we strive for accuracy, please verify critical information with official sources.

