This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author Peter92
Recipients Peter92, ezio.melotti, paul.moore, steve.dower, tim.golden, vstinner, zach.ware
Date 2020-11-29.02:08:09
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1606615691.16.0.723676865629.issue42495@roundup.psfhosted.org>
In-reply-to
Content
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
History
Date User Action Args
2020-11-29 02:08:11Peter92setrecipients: + Peter92, paul.moore, vstinner, tim.golden, ezio.melotti, zach.ware, steve.dower
2020-11-29 02:08:11Peter92setmessageid: <1606615691.16.0.723676865629.issue42495@roundup.psfhosted.org>
2020-11-29 02:08:10Peter92linkissue42495 messages
2020-11-29 02:08:09Peter92create