Repository navigation
Escape values in the SQL Server ODBC connection string - #13
Open
maharanay22 wants to merge 1 commit into
Open
maharanay22 wants to merge 1 commit into
maharanay22 wants to merge 1 commit into
Conversation
The ODBC connection string was built by pasting the connection values
between "KEY=" and ";" with no escaping. A password such as
"Xy;9}Encrypt=no" ended the PWD attribute at the first ';', so the
driver got a truncated password and a stray "9}Encrypt=no" attribute
(the Windows SQL Server ODBC driver reports "Invalid connection string
attribute"), and a value could add or override attributes such as
Encrypt.
Wrap values that contain ';', '{' or '}', or that start or end with a
space, in braces and double every '}', following the ODBC connection
string rules. Simple values are written as before.
This branch has not been deployed
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.
Description
When the SQL Server handler connects through ODBC (
use_odbc: trueor adriverparameter),_connect_odbc()builds the connection string by putting each value betweenKEY=and;with no escaping:In an ODBC connection string,
;ends an attribute. So a password (or user or database name) that contains;,{or}breaks the string. For example, with the passwordXy;9}Encrypt=no, the driver getsPWD=Xyas the password plus a stray9}Encrypt=noattribute. The login fails with a confusing error even though the credentials are right, and on Windows the SQL Server ODBC driver also reports "Invalid connection string attribute". A value can also add or override other attributes, such asEncrypt, which shouldn't be possible from a password fieldThis change escapes values the way the ODBC connection string rules require. A value that contains
;,{or}, or that starts or ends with a space, is wrapped in{...}and every}inside it is doubled. This coversSERVER,DATABASE,UID,PWD,EncryptandTrustServerCertificate.DRIVERwas already wrapped in braces, and now its}characters are doubled too. Simple values are written exactly as before, so existing connection strings don't change.connection_string_argsis still appended as is, since it is meant to be a raw piece of the connection stringFixes: no linked issue, because issues are turned off for this repository. The bug is described in full above
Type of change
Verification Process
To ensure the changes are working as expected:
tests/unit/handlers/test_mssql.py, new testTestMSSQLHandlerODBC::test_odbc_connection_string_escapes_special_charactersXy;9}Encrypt=no(withpyodbc.connectmocked), then pass it to a real ODBC driver (the built-in Windows "SQL Server" driver) and an unreachable server, so the only difference between runs is how the connection string is parsedBefore (current main):
After:
With the fix, the special password is parsed as one value and gives the same result as a plain password. The server error is expected because there is no server on that port
Unit tests. The new test fails on main and passes with the fix, and the existing tests (including the ones that check plain values like
PWD=example_pass) still pass:ruff checkandruff format --check(0.11.11) pass on the changed files. No dependencies changedAdditional Media:
I didn't record a video or take screenshots. The before and after terminal output above shows the change instead
Checklist:
No documentation change is needed
I used an AI assistant to help investigate this and draft the change and tests, and I reviewed and tested it myself