Skip to content

Escape dollar signs in PHP output (CVE-2026-112307) - #937

Open
cve-zh00 wants to merge 1 commit into
andialbrecht:masterfrom
cve-zh00:fix/php-output-dollar-escape
Open

cve-zh00 wants to merge 1 commit into
andialbrecht:masterfrom
cve-zh00:fix/php-output-dollar-escape

Conversation

@cve-zh00

Copy link
Copy Markdown

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
  • PostgreSQL dollar quoting ($body$...$body$)
  • double-quoted identifiers and comments containing $
  • existing backslash before $
  • multi-line output and unchanged $ in the generated variable names
  • python output 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).

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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant