Repository navigation
Conversation
PHP interpolates $var, ${expr} and {$expr} inside double-quoted strings,
so SQL text containing them could inject PHP code into the snippet
generated by output_format='php'. Escape "$" as "\$" after the existing
backslash and double-quote escaping.
Adds regression tests for SQL literals, ${expr}/{${expr}} payloads,
PostgreSQL dollar quoting, quoted identifiers, comments and a preceding
backslash.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
OutputPHPFilter(output_format='php') wraps SQL in a double-quoted PHP string and escapes\and", but not$. PHP interpolates$var,${expr}and{$expr}inside double-quoted strings, so SQL text such as${expr}or{${expr}}lets an attacker inject PHP expressions into the generated snippet (CVE-2026-112307, CWE-94).Fix
Escape
$as\$after the existing backslash/double-quote escaping. Order matters: a pre-existing backslash before a$is doubled first, so it cannot neutralize the new escape. The generated assignment target ($sql,$sql2, ...) is emitted separately and stays unescaped.Tests
Regression tests in
tests/test_format.py(9 of them fail without the fix):$name,${expr},{${expr}},{$obj->prop}inside SQL literals$body$...$body$)$$$in the generated variable namespythonoutput unchanged ($is not special there)Also verified end-to-end with PHP:
${print("PWNED")}is now printed literally instead of executed.pytest tests/: 517 passed, 2 xfailed, 1 xpassed.ruff check sqlparse/passes.Related: #930 (same root cause and same escaping approach; this PR additionally covers
{$expr}/{${expr}}payloads and multi-line output).