diff options
author | Ben Kallus <49924171+kenballus@users.noreply.github.com> | 2022-10-20 21:00:56 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-10-20 21:00:56 (GMT) |
commit | 6f15ca8c7afa23e1adc87f2b66b958b721f9acab (patch) | |
tree | 161c8fb4abb3bb7172df1b99d7cabca993e5d73c /Lib/urllib | |
parent | 4ec9ed8fde8e285a3eea97c199f7bbf7c95c8881 (diff) | |
download | cpython-6f15ca8c7afa23e1adc87f2b66b958b721f9acab.zip cpython-6f15ca8c7afa23e1adc87f2b66b958b721f9acab.tar.gz cpython-6f15ca8c7afa23e1adc87f2b66b958b721f9acab.tar.bz2 |
gh-96035: Make urllib.parse.urlparse reject non-numeric ports (#98273)
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
Diffstat (limited to 'Lib/urllib')
-rw-r--r-- | Lib/urllib/parse.py | 17 |
1 files changed, 8 insertions, 9 deletions
diff --git a/Lib/urllib/parse.py b/Lib/urllib/parse.py index 3734c73..9a3102a 100644 --- a/Lib/urllib/parse.py +++ b/Lib/urllib/parse.py @@ -167,12 +167,11 @@ class _NetlocResultMixinBase(object): def port(self): port = self._hostinfo[1] if port is not None: - try: - port = int(port, 10) - except ValueError: - message = f'Port could not be cast to integer value as {port!r}' - raise ValueError(message) from None - if not ( 0 <= port <= 65535): + if port.isdigit() and port.isascii(): + port = int(port) + else: + raise ValueError(f"Port could not be cast to integer value as {port!r}") + if not (0 <= port <= 65535): raise ValueError("Port out of range 0-65535") return port @@ -1132,15 +1131,15 @@ def splitnport(host, defport=-1): def _splitnport(host, defport=-1): """Split host and port, returning numeric port. Return given default port if no ':' found; defaults to -1. - Return numerical port if a valid number are found after ':'. + Return numerical port if a valid number is found after ':'. Return None if ':' but not a valid number.""" host, delim, port = host.rpartition(':') if not delim: host = port elif port: - try: + if port.isdigit() and port.isascii(): nport = int(port) - except ValueError: + else: nport = None return host, nport return host, defport |