https://github.com/python/cpython/commit/0879ebc953fa7372a4d99f3f79889093f04cac67 commit: 0879ebc953fa7372a4d99f3f79889093f04cac67 branch: main author: Barney Gale <barney.g...@gmail.com> committer: barneygale <barney.g...@gmail.com> date: 2025-04-15T01:05:06+01:00 summary:
GH-123599: Match `file:` URL hostname against machine hostname in urllib (#132523) In `_is_local_authority()`, return early if the authority matches the machine hostname from `socket.gethostname()`, rather than resolving the names and matching IP addresses. files: M Doc/library/urllib.request.rst M Lib/urllib/request.py diff --git a/Doc/library/urllib.request.rst b/Doc/library/urllib.request.rst index a5f1b9b292a85a..b7c0c7d5099806 100644 --- a/Doc/library/urllib.request.rst +++ b/Doc/library/urllib.request.rst @@ -199,9 +199,9 @@ The :mod:`urllib.request` module defines the following functions: .. versionchanged:: next This function calls :func:`socket.gethostbyname` if the URL authority - isn't empty or ``localhost``. If the authority resolves to a local IP - address then it is discarded; otherwise, on Windows a UNC path is - returned (as before), and on other platforms a + isn't empty, ``localhost``, or the machine hostname. If the authority + resolves to a local IP address then it is discarded; otherwise, on + Windows a UNC path is returned (as before), and on other platforms a :exc:`~urllib.error.URLError` is raised. .. versionchanged:: next diff --git a/Lib/urllib/request.py b/Lib/urllib/request.py index 2c9c7b6ca5394d..9a6b29a90a2968 100644 --- a/Lib/urllib/request.py +++ b/Lib/urllib/request.py @@ -1483,8 +1483,17 @@ def open_local_file(self, req): file_open = open_local_file def _is_local_authority(authority): + # Compare hostnames if not authority or authority == 'localhost': return True + try: + hostname = socket.gethostname() + except (socket.gaierror, AttributeError): + pass + else: + if authority == hostname: + return True + # Compare IP addresses try: address = socket.gethostbyname(authority) except (socket.gaierror, AttributeError): _______________________________________________ Python-checkins mailing list -- python-checkins@python.org To unsubscribe send an email to python-checkins-le...@python.org https://mail.python.org/mailman3/lists/python-checkins.python.org/ Member address: arch...@mail-archive.com