]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
GH-123599: Match `file:` URL hostname against machine hostname in urllib (#132523)
authorBarney Gale <barney.gale@gmail.com>
Tue, 15 Apr 2025 00:05:06 +0000 (01:05 +0100)
committerGitHub <noreply@github.com>
Tue, 15 Apr 2025 00:05:06 +0000 (01:05 +0100)
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.

Doc/library/urllib.request.rst
Lib/urllib/request.py

index a5f1b9b292a85acabd70ed41057385bc9b74f33e..b7c0c7d5099806506811a8bd4ca2d86e72624607 100644 (file)
@@ -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
index 2c9c7b6ca5394d4d653728d79f1aad66bf8a8f2c..9a6b29a90a2968294f6842aa0e99654d0493f82a 100644 (file)
@@ -1483,8 +1483,17 @@ class FileHandler(BaseHandler):
     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):