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.
This commit is contained in:
@@ -199,9 +199,9 @@ The :mod:`urllib.request` module defines the following functions:
|
|||||||
|
|
||||||
.. versionchanged:: next
|
.. versionchanged:: next
|
||||||
This function calls :func:`socket.gethostbyname` if the URL authority
|
This function calls :func:`socket.gethostbyname` if the URL authority
|
||||||
isn't empty or ``localhost``. If the authority resolves to a local IP
|
isn't empty, ``localhost``, or the machine hostname. If the authority
|
||||||
address then it is discarded; otherwise, on Windows a UNC path is
|
resolves to a local IP address then it is discarded; otherwise, on
|
||||||
returned (as before), and on other platforms a
|
Windows a UNC path is returned (as before), and on other platforms a
|
||||||
:exc:`~urllib.error.URLError` is raised.
|
:exc:`~urllib.error.URLError` is raised.
|
||||||
|
|
||||||
.. versionchanged:: next
|
.. versionchanged:: next
|
||||||
|
|||||||
@@ -1483,8 +1483,17 @@ class FileHandler(BaseHandler):
|
|||||||
file_open = open_local_file
|
file_open = open_local_file
|
||||||
|
|
||||||
def _is_local_authority(authority):
|
def _is_local_authority(authority):
|
||||||
|
# Compare hostnames
|
||||||
if not authority or authority == 'localhost':
|
if not authority or authority == 'localhost':
|
||||||
return True
|
return True
|
||||||
|
try:
|
||||||
|
hostname = socket.gethostname()
|
||||||
|
except (socket.gaierror, AttributeError):
|
||||||
|
pass
|
||||||
|
else:
|
||||||
|
if authority == hostname:
|
||||||
|
return True
|
||||||
|
# Compare IP addresses
|
||||||
try:
|
try:
|
||||||
address = socket.gethostbyname(authority)
|
address = socket.gethostbyname(authority)
|
||||||
except (socket.gaierror, AttributeError):
|
except (socket.gaierror, AttributeError):
|
||||||
|
|||||||
Reference in New Issue
Block a user