Skip to content

Commit 0879ebc

Browse files
authored
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.
1 parent 102f825 commit 0879ebc

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

Diff for: Doc/library/urllib.request.rst

+3-3
Original file line numberDiff line numberDiff line change
@@ -199,9 +199,9 @@ The :mod:`urllib.request` module defines the following functions:
199199

200200
.. versionchanged:: next
201201
This function calls :func:`socket.gethostbyname` if the URL authority
202-
isn't empty or ``localhost``. If the authority resolves to a local IP
203-
address then it is discarded; otherwise, on Windows a UNC path is
204-
returned (as before), and on other platforms a
202+
isn't empty, ``localhost``, or the machine hostname. If the authority
203+
resolves to a local IP address then it is discarded; otherwise, on
204+
Windows a UNC path is returned (as before), and on other platforms a
205205
:exc:`~urllib.error.URLError` is raised.
206206

207207
.. versionchanged:: next

Diff for: Lib/urllib/request.py

+9
Original file line numberDiff line numberDiff line change
@@ -1483,8 +1483,17 @@ def open_local_file(self, req):
14831483
file_open = open_local_file
14841484

14851485
def _is_local_authority(authority):
1486+
# Compare hostnames
14861487
if not authority or authority == 'localhost':
14871488
return True
1489+
try:
1490+
hostname = socket.gethostname()
1491+
except (socket.gaierror, AttributeError):
1492+
pass
1493+
else:
1494+
if authority == hostname:
1495+
return True
1496+
# Compare IP addresses
14881497
try:
14891498
address = socket.gethostbyname(authority)
14901499
except (socket.gaierror, AttributeError):

0 commit comments

Comments
 (0)