Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

url parsing struggle with username containing @ char #483

Closed
victornoel opened this issue Oct 7, 2019 · 8 comments
Closed

url parsing struggle with username containing @ char #483

victornoel opened this issue Oct 7, 2019 · 8 comments

Comments

@victornoel
Copy link

  • asyncpg version: 0.18.3

I am using asyncpg to connect to a Azure PostgreSQL database, and Azure imposes the use of a username of the form name@db, so the full connection url looks like:

postgresql://my-user@my-db-server:[email protected]:5432/some-db

When using such an url to connect with asyncpg, I get the following error:

File "/home/victor/.local/share/virtualenvs/datapipe-3ZEOKy8g/lib/python3.7/site-packages/asyncpg/connect_utils.py", line 187, in _parse_hostlist
    hostlist_ports.append(int(hostspec_port))
ValueError: invalid literal for int() with base 10: '[email protected]:5432'

I see two problems here:

  • the password leak in the logs (even tough I'm not sure how to avoid this in a consistent manner…)
  • the fact that asyncpg incorrectly split the netloc part of the uri by splitting at the first @ intead of the last one.

The ticket is about the second problem:

  • I think it should be safe to assume that everything before the last @ in the netloc is for authentication.
  • Also by the way, it should also be safe to assume that everything after the first : in the auth part to be the password.

For the record, when using sqlalchemy + psycogs2, I have no problem using this kind of url to connect to my database.

@victornoel
Copy link
Author

One more point: I tried using %40 instead of the @ but then it is sent directly as-is to the server which does not recognize the name.

@elprans
Copy link
Member

elprans commented Oct 7, 2019

This is already fixed in #472 and will be in the next release soon.

@elprans elprans closed this as completed Oct 7, 2019
@victornoel
Copy link
Author

@elprans great, thank you? And do you have an ETA for this next release btw?

@elprans
Copy link
Member

elprans commented Oct 7, 2019

Waiting on #295 to land, and also need to do some PostgreSQL 12 testing.
The release should happen in a few days.

@victornoel
Copy link
Author

@elprans excellent, thx :)

@KrunchMuffin
Copy link

KrunchMuffin commented Aug 18, 2020

I appear to be getting this because asyncpg thinks there should be a port after the first colon, but it's the username:password colon.

    addrs, params = _parse_connect_dsn_and_args(
  File "C:\venv\lib\site-packages\asyncpg\connect_utils.py", line 237, in _parse_connect_dsn_and_args
    host, port = _parse_hostlist(dsn_hostspec, port, unquote=True)
  File "C:\venv\lib\site-packages\asyncpg\connect_utils.py", line 197, in _parse_hostlist
    hostlist_ports.append(int(hostspec_port))
ValueError: invalid literal for int() with base 10: 'abcde'

@nikolalazovski
Copy link

I am getting the same error for parsing postgresql://[email protected]:anystring@localhost:5432/app- ValueError: invalid literal for int() with base 10: 'anystring@localhost:5432', Also I tested with the connection string from the first post with all the asyncpg versions from 0.19-0.25 but I was getting the same error. Can anyone point what is the right version so that the connection string is interpreted well? Thanks

@elprans
Copy link
Member

elprans commented Mar 3, 2022

Those aren't valid DSNs, you must urllib.parse.quote the parts properly.

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

No branches or pull requests

4 participants