fix(mssql): escape special characters in passwords#10437
fix(mssql): escape special characters in passwords#10437gforsyth merged 2 commits intoibis-project:mainfrom
Conversation
|
I have no idea how to make it be tested in the test suite. If you have any ideas, please let me know. Thanks. |
|
Hey @grieve54706 -- thanks for putting this in. What would happen if a user passed in a properly escaped password directly? We should make sure that this works in that case, too. As for testing, I would recommend a test just of the escaping function, ensuring that unescaped strings are properly escaped, and properly escaped strings are left untouched |
|
Hi @gforsyth, thanks for your point. I tested the normal password and the escaped password by testcontainers. from testcontainers.mssql import SqlServerContainer
import pyodbc
def test_password_with_special_characters():
passwords = [
"1bis_Testing!",
"{1bis_Testing!",
"1bis_Testing!}",
"{1bis_Testing!}",
"1bis}Testing!",
"{R;3G1/8Al2AniRye",
"{R;3G1/8Al2AniRye}",
]
for pwd in passwords:
with SqlServerContainer(
mssql_image,
dialect="mssql+pyodbc",
password=pwd,
) as mssql:
pyodbc.connect(
user=mssql.username,
server=f"{mssql.get_container_host_ip()},{mssql.get_exposed_port(mssql.port)}",
password=_escape_special_characters(pwd),
database=mssql.dbname,
driver="FreeTDS",
)
def _escape_special_characters(value: str) -> str:
return "{" + value.replace("}", "}}") + "}"They are all good. |
|
Nice, thanks for the update, @grieve54706 ! This looks good to me -- one last thing I'm unsure of here -- do left curly-braces also need to be escaped? e.g. should there also be a |
gforsyth
left a comment
There was a problem hiding this comment.
Nice! Thanks for working on this @grieve54706 , and for providing useful reference info for the pyodbc escaping conventions.
Description of changes
I found the error
Because the password of mssql includes special characters like
{R;3G1/8Al2AniRyethat start with{or include;.It should be covered by
{and}and replace}with}}.Reference:
https://github.com/mkleehammer/pyodbc/wiki/Connecting-to-databases
https://stackoverflow.com/questions/78531086/pyodbc-connection-string-correctly-escaping-password-with-special-characters/78532507#78532507