Skip to content

Improper reliance on local encoding of remote resource #1546

Open

Description

@PraxisSpace

This is in response to issue #1402 which was addressed with commit 1a32c50.

The issue there was regarding platforms where UTF-8 is not the default encoding, for example, Windows, where cp1252 may be the default.

This only occurs with python3 as only there is _binary_to_string_handle needed.

The solution to that issue was to add the following to _binary_to_string_handle:

+        try:
 +            # If this is a network handle from urllib,
 +            # the HTTP headers may tell us the encoding.
 +            encoding = handle.headers.get_content_charset()
 +        except AttributeError:
 +            encoding = locale.getpreferredencoding(False)
 +        wrapped = io.TextIOWrapper(io.BufferedReader(handle), encoding=encoding)

This has a number of problems:

  1. encoding = handle.headers.get_content_charset() may return None rather than raise an AttributeError. In this instance, the call to io.TextIOWrapper with encoding=None will default to the system's default encoding, which on Windows may be cp1252, which is a revert back to the initial issue.
  2. A reliance on locale.getpreferredencoding(False) is reverting to the default encoding of the user's system, which has no relation to the encoding of a remote resource. According to the HTTP protocol parameters from W3C:

When no explicit charset parameter is provided by the sender, media subtypes of the "text" type are defined to have a default charset value of "ISO-8859-1" when received via HTTP.

ISO-8859-1 is also known as latin-1. Therefore, when no encoding information is available, the default should revert to latin-1 and not the user's preferred encoding or the user's default encoding.

Activity

  1. fabianegli commented on Jan 21, 2019

    @fabianegli
    Contributor

    Assuming text transfers to be ISO-8859-1 a.k.a. latin-1 encoded seems to be obsolete for HTTP 1.1 (contrary to HTTP 1.0).

    From https://tools.ietf.org/html/rfc7231 Appendix B. Changes from RFC 2616:

    The default charset of ISO-8859-1 for text media types has been
    removed; the default is now whatever the media type definition says.
    Likewise, special treatment of ISO-8859-1 has been removed from the
    Accept-Charset header field. (Section 3.1.1.3 and Section 5.3.3)

  2. peterjc commented on Jan 21, 2019

    @peterjc
    Member

    We can add:

        if encoding is None:
    

    doing the same thing as the AttributeError handler.

    So what is the default encoding now if the headers don't say? Is latin-1 a better choice than the local system default?

  3. fabianegli commented on Jan 21, 2019

    @fabianegli
    Contributor

    Being used on a staggering 92.9% (as of 01/21/2019) of websites, the UTF-8 encoding permeates the web. See https://w3techs.com/technologies/details/en-utf8/all/all for current numbers. I think the following is the most reasonable and robust approach.
    Choose the first that holds true for documents downloaded from the web:

    1. believe the header
    2. guess the encoding
      a) sniff & test the encoding (Because sniffing may fail - processing needs error handling & sensible error message.)
      b) assume UTF-8 encoding (Because this may fail - processing needs error handling & sensible error message.)

    Step 2. a) and 2. b) are arbitrarily ordered. I don't know if there is a clear benefit to putting one or the other first in the control flow as long as the first failing triggers the other.

  4. peterjc commented on Jan 22, 2019

    @peterjc
    Member

    Sniffing is fiddly, we'd need to buffer what is typically read-once handle, so I prefer assuming URF-8 for online resources.

    Can you test locally with something like this?:

            try:
                # If this is a network handle from urllib,
                # the HTTP headers may tell us the encoding.
                encoding = handle.headers.get_content_charset()
                if encoding is None:
                    # Had a header, so should be an online resource, assume UTF-8
                    encoding ="utf8"
            except AttributeError:
                # No header attribute, probably not an online resource, assume local
                encoding = locale.getpreferredencoding(False)
            wrapped = io.TextIOWrapper(io.BufferedReader(handle), encoding=encoding)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions