Skip to content

GH-123599: Match file: URL hostname against machine hostname in urllib #132523

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Doc/library/urllib.request.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions Lib/urllib/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
Loading