Open Vishnu-priyan opened 4 years ago
Agreed, our behaviour seems wrong here. That behviour of urllib is suprising. We should do some form of fuzz testing for the username/password to ensure we parse it correctly.
Alright. What could be the fix for it?, Can we modify sqlalchemy's URL parsing logic here for tortoise orm? Or any other way?. Urllib parsing will throw this error, couldn't find a way to ignore that netloc check. It raises ValueError. Users might not be aware of this behaviour and might find their production app causing error, but cause being a dB password. (This what happened for me).
Describe the bug
tortoise-orm's expand_db_url function in config_generator.py throws ValueError("Invalid IPv6 URL") when trying to parse db_url.
expand_db_url function has,
url = urlparse.urlparse(db_url)
Source code of urlparse (which uses urlsplit, see this line of code)
When given a db url with either one of these special characters '[' or ']', urllib's parse tries to parse and raises it's invalid IPv6 URL error.
To Reproduce
Expected behavior Expected is to allow this password with special characters.
https://github.com/tortoise/tortoise-orm/blob/72f84f0848dc68041157f03e60cd1c92b0ee5137/tortoise/backends/base/config_generator.py#L62 https://github.com/tortoise/tortoise-orm/blob/72f84f0848dc68041157f03e60cd1c92b0ee5137/tortoise/backends/base/config_generator.py#L63
This shouldn't be happening. I tried the same with sqlalchemy ORM please have a look at the below code, mydatabase.py
models.py
Terminal:
Above works without any issue.
After digging through the source code for sqlalchemy they use this make_url following RFC 1738.
Ideally the above password should be allowed, it is an unexpected error here. Sqlalchemy connects with db, whereas when using tortoise orm error occurs.
I'm working on fixing this db url parsing logic for tortoise orm (if it is an error correct me otherwise). or is this error expected in tortoise orm and let it be. Please advice. :)
Python 3 for urllib say
Additional context Please note, I understand that this is because of the password in db url and just posting this for further discussion. Also somewhere down the line, somebody hopefully stumbles across this discussion when they face this IPV6 error using tortoise orm.