Skip to content

Fix SQL injection in database_exists() - #823

Open
pctablet505 wants to merge 2 commits into
kvesteri:masterfrom
pctablet505:fix-database-exists-sql-injection
Open

Fix SQL injection in database_exists()#823
pctablet505 wants to merge 2 commits into
kvesteri:masterfrom
pctablet505:fix-database-exists-sql-injection

Conversation

@pctablet505

Copy link
Copy Markdown

database_exists() builds its existence-check query for postgresql, mysql, and mssql by splicing the database name straight into the SQL text with %-string interpolation. A database name containing SQL metacharacters can inject arbitrary statements, as described in #760.

This switches all three queries to use bound parameters instead of string interpolation, so the database name is always sent as data, never as part of the SQL text.

Added a regression test that captures what actually gets sent to the DBAPI and asserts the injection payload never appears in the SQL string.

Fixes #760.

database_exists() built its existence-check query for postgresql,
mysql and mssql by splicing the database name into the SQL text with
%-string interpolation. A database name containing SQL metacharacters
could inject arbitrary statements. Use bound parameters instead.

Fixes kvesteri#760
@pctablet505
pctablet505 marked this pull request as ready for review July 17, 2026 12:47
Follows the repo's convention of recording fixes in the Unreleased
section of CHANGES.rst with a reference to the issue number.
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.

SQL Injection in database.py database_exists

1 participant