You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
It is possible to get valid ParseResult from the urlparse function even for a non-numeric port value. Only by requesting the port it fails[1].
Would it be an improvement if _checknetloc[2] validates the value of port properly?
// code snippet
Python 3.8.5 (default, Jan 27 2021, 15:41:15)
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from urllib.parse import urlparse
>>> uri ='xx://foo:bar'
>>> uri_parts = urlparse(uri)
>>> uri_parts.netloc
'foo:bar'
>>> uri_parts.port
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.8/urllib/parse.py", line 174, in port
raise ValueError(message) from None
ValueError: Port could not be cast to integer value as 'bar'
// code snippet
Activity
p-alik commentedon Apr 16, 2021
It is possible to get valid ParseResult from the urlparse function even for a non-numeric port value. Only by requesting the port it fails[1].
Would it be an improvement if _checknetloc[2] validates the value of port properly?
[1]
cpython/Lib/urllib/parse.py
Line 172 in e1903e1
[2]
cpython/Lib/urllib/parse.py
Line 416 in e1903e1
tirkarthi commentedon Apr 16, 2021
I guess moving port validation logic to parsing time is done as part of #16780
orsenthil commentedon Apr 16, 2021
Treating this as bug in itself might be a better idea than waiting for a ipv6 scope introduction, which had few caveats.
Yes, we could check if it is an int. That should be sufficient.
p-alik commentedon Apr 17, 2021
Thank you for your swift response and your willingness to add port validation to _checknetloc.
I think the validation itself should compound both exceptional branches implemented in port[3]
[3]
cpython/Lib/urllib/parse.py
Lines 173 to 178 in e1903e1
miguendes commentedon May 1, 2021
I also think the validation logic should be ran as early as possible.
I gave it a shot and implemented it.
I appreciate any reviews: #25774
Got some ideas from #16780