xo / usql

Universal command-line interface for SQL databases
MIT License
8.82k stars 346 forks source link

Using password with special characters #409

Closed maretodoric closed 4 months ago

maretodoric commented 1 year ago

Hello,

I've been using usql by running it via python subprocess on Windows Server, forming DSN URL like

mssql://{sqluser}:{sqlpass}@localhost:{listener_port}?database={sqldb}

However when password contains special characters, specifically "#", windows probably interprets it as a comment so usql complains about wrong DSN. So i wanted to add that password into .usqlrc file (by also creating it in python) but, as you guessed, it's also interpreted as a comment and then usql complains about having an empty field. How would i escape hash/comment characted if it's part of a password? I tried URL encoding it with %23 but it didn't work.

nineinchnick commented 1 year ago

URL encoding is the right way,maybe there are other characters? If you only tried the encoded pw in usqlrc, see if the entry gets matched correctly. You need to provide the right proto, hostname, port, and username

maretodoric commented 1 year ago

Nope, all other characters are alfanumeric. Only two octothorpes/hashes are in password. Proto, hostname is all *, example:

mssql:*:*:*:*:%23alfanumeric%23alfanumeric

And for DSN i use:

mssql://sa@localhost:62275?database=dbname

And i get 'Login failed for user 'sa'.

If i use same DSN - different port for different DB and different password entry in usqlrc file with password without # but with + which is also urlencodable but I've added it just plain '+' without encoding it - it will work. So wondering if encoding is even needed/honored from usqlrc ?

kenshaw commented 1 year ago

@maretodoric please try connecting directly without the .usqlpass file. Are you able to connect? Display what your actual connection string is from within usql by using the \conninfo meta command.

maretodoric commented 1 year ago

Yup, connecting directly (with urlencoding hashes) worked. So urlencoding it in .usqlpass doesn't work for some reason. \conninfo displays something like:

Connected with driver sqldriver (sqlserver://sa:%23password%23password@localhost:...
nineinchnick commented 1 year ago

Can you try using sqlserver in usqlpass? Mssql is an alias.

thiago-scherrer commented 10 months ago

Yup, connecting directly (with urlencoding hashes) worked. So urlencoding it in .usqlpass doesn't work for some reason.

@maretodoric same here I am using PostgreSQL.

azinsharaf commented 4 months ago

it seems the password can't include : either. I am trying to connect to sql server and parsing the string fails.

usql ms://admin_username:pass:Tord@host/instance/dbname
invalid port ":T" after host. 
kenshaw commented 4 months ago

You need to URL escape the password:

$ usql ms://admin_username:pass%3ATord@host/instance/dbname
azinsharaf commented 4 months ago

thanks. i am getting login failed error now.

MichalisDBA commented 3 months ago

I too have login failed in mssql connection string when password contains this symbol %