[issue42495] socket.gethostbyaddr raises error if invalid unicode in hosts
Peter Hunt
report at bugs.python.org
Sat Nov 28 21:08:11 EST 2020
New submission from Peter Hunt <peter at huntfx.uk>:
If the hosts file contains invalid unicode, then the socket module will break when attempting to get the list of hosts. This renders modules such as Flask and Django unusable.
Background:
I had a mapping to localghost (https://twitter.com/rfreebern/status/1214560971185778693), and Docker incorrectly rewrote the hosts file during installation, turning it from "127.0.0.1 xn--9q8h" to "127.0.0.1 xn-9q8h".
The socket module was not able to handle that, and was failing with a UnicodeDecodeError in Python 3.6+ as it attempted to list the addresses for "127.0.0.1".
How to reproduce:
# Add "127.0.0.1 xn-9q8h" to C:/Windows/System32/drivers/etc/hosts
>>> socket.gethostbyaddr('127.0.0.1')
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x96 in position 2: invalid start byte
Expected:
In Python 2.7 - 3.5, it returns 'xn\u20139q8h'.
I feel returning "xn-9q8h" as a string would be best, but ignoring it could be an option.
An alternative would be to raise socket.error.
Full traceback from Flask as an example:
Traceback (most recent call last):
File "__init__.py", line 285, in <module>
app.run()
File "C:\Users\Peter\AppData\Roaming\Python\Python37\site-packages\flask\app.py", line 990, in run
run_simple(host, port, self, **options)
File "C:\Users\Peter\AppData\Roaming\Python\Python37\site-packages\werkzeug\serving.py", line 1052, in run_simple
inner()
File "C:\Users\Peter\AppData\Roaming\Python\Python37\site-packages\werkzeug\serving.py", line 1005, in inner
fd=fd,
File "C:\Users\Peter\AppData\Roaming\Python\Python37\site-packages\werkzeug\serving.py", line 848, in make_server
host, port, app, request_handler, passthrough_errors, ssl_context, fd=fd
File "C:\Users\Peter\AppData\Roaming\Python\Python37\site-packages\werkzeug\serving.py", line 740, in __init__
HTTPServer.__init__(self, server_address, handler)
File "C:\Program Files\Python37\lib\socketserver.py", line 452, in __init__
self.server_bind()
File "C:\Program Files\Python37\lib\http\server.py", line 139, in server_bind
self.server_name = socket.getfqdn(host)
File "C:\Program Files\Python37\lib\socket.py", line 676, in getfqdn
hostname, aliases, ipaddrs = gethostbyaddr(name)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x96 in position 2: invalid start byte
----------
components: Library (Lib), Unicode, Windows
messages: 382029
nosy: Peter92, ezio.melotti, paul.moore, steve.dower, tim.golden, vstinner, zach.ware
priority: normal
severity: normal
status: open
title: socket.gethostbyaddr raises error if invalid unicode in hosts
type: behavior
versions: Python 3.10, Python 3.6, Python 3.7, Python 3.8, Python 3.9
_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue42495>
_______________________________________
More information about the Python-bugs-list
mailing list