Skip to content

r.http should do utf-8 validation before returning a string #2950

Open

Description

@deontologician

Currently, r.http with result_format='auto' looks for some known binary content-types and returns a String if the content-type of the page isn't among them. The issue with this is that we don't allow Strings to have null bytes in them. It's possible for a content-type to miss our heuristic for finding binary files and to be returned as a string improperly. This throws an avoidable exception and prevents the user from doing what they want to do.

Following the discussion on rethinkdb/docs#467, I think we should do one of these two things:

  1. Validate the page content as utf-8, and only if it validates return a String (otherwise return a Binary).
  2. If utf-8 validation turns out to be too slow in practice, switch from white-listing binary content-types to white-listing content-types that are utf-8. The default return type of r.http would become a Binary, and only white-listed content-types would return a String.

The downside of approach 1 is that we'd need to add code to do utf-8 validation, and that it may be slow. The downside of 2 is that it may be surprising to users that some content type comes back as a binary because we didn't explicitly white-list it.

Activity

  1. added this to the subsequent milestone on Aug 22, 2014
  2. gchpaco commented on Jan 9, 2015

    @gchpaco
    Contributor

    So now that we have UTF-8 validation (which is easy) this is possible. There's an issue that comes up with this, which is as follows:

    • I issue r.http('http://capcom.co.jp/').
    • in the headers, it advertises itself as Content-Type: text/html with no encoding given. In HTML 4, the default character set is ISO 8859-1. In HTML 5 the default character set is UTF-8. In both cases, the Content-Type can be further clarified in a <meta> field in the <head> section of the HTML. We can't deal with this unless we are actually trying to parse the HTML; we have to assume UTF-8 and if it doesn't work give a binary, which is fine.
    • A different sort of failure comes from r.http('http://timecube.com') which advertises itself as, again, Content-Type: text/html with no encoding given but is actually (as clarified from the <meta> field in the <head> section ISO 8859-1 (mostly literal encoding of non-breaking spaces). In this case, r.http should probably give a binary.
  3. deontologician commented on Jan 9, 2015

    @deontologician
    ContributorAuthor

    So, if you have an ISO 8859-1 document, how likely is it (if it uses non-ascii characters) to validate as utf-8 accidentally?

  4. gchpaco commented on Jan 9, 2015

    @gchpaco
    Contributor

    So the current state is that if I try to load a web page that isn't UTF-8 but is a text/* format, it will force it to a string and that string may be invalid. If I r.http a URL that purports to be JSON but is actually not encoded in UTF-8, that will fail with a poor error message and quote the (invalid) string in the reply, which is kinda bad.

    @danielmewes has proposed that we always give binaries by default, particularly because parsing the HTML <head> so that we can know what encoding the body uses is really not trivial. I am not so sure about this, because a) we lose all the header information when we do that, particularly the Content-Type which is important b) users may have grown to expect strings from r.http and this may be a bit of a surprise to them.

    When we have the improved UTF-8 parser needed for #2518 we can properly 'escape' the invalid body, which is a separate issue.

    Possibly we should add a new data type that includes all the headers as well, as those could be useful. But that may not be a good idea to run from r.http, maybe call it something else. ICU does have methods for charset conversion.

  5. deontologician commented on Jan 9, 2015

    @deontologician
    ContributorAuthor

    Another consideration is why you might be fetching a non-json document with r.http. It may be best to return a binary just because if you're doing something like this, reql can't really do much meaningful processing on it anyway, and it's likely you just want to store it as it is.

  6. gchpaco commented on Jan 9, 2015

    @gchpaco
    Contributor

    ISO 8859-1 and UTF-8 are identical in the 7-bit zone that is ASCII. It is possible to accidentally issue a sequence of 8859-1 characters that happen to correspond to valid UTF-8 sequences; "êté" in UTF-8 has the same byte sequence as "êté" in ISO 8859-1, although that particular character sequence is pretty goofy.

    In practice in actual documents the chance of accidentally making a document in 8859-1 that is semantically meaningful and also legal UTF-8 is minimal, I would guess sub 0.01%.

  7. gchpaco commented on Jan 9, 2015

    @gchpaco
    Contributor

    That's what @danielmewes was advocating, the binary solution. I don't like it because we lose the header. If we didn't lose the header, I would be okay with it. However I don't know if our users have come to expect to be able to download random websites with r.http in the auto processing mode.

  8. deontologician commented on Jan 9, 2015

    @deontologician
    ContributorAuthor

    given the low likelihood of accidental collisions like this, could we use the "if it validates as utf-8, we assume it's utf-8" strategy?

  9. danielmewes commented on Jan 9, 2015

    @danielmewes
    Member

    More precisely, my suggestion is to

    • Make the default return format for r.http binary
    • Keep automatically parsing things where the Content-Type is one of the JSON types
    • If the user specified {result_format: "test"}, return a string but fail if it's not valid UTF-8
    • Allow r.json to accept binary data, so that a user can still easily wrap an r.json around the r.http.

    I'm opposed to returning a string by default and falling back to binary only if the string isn't valid UTF-8 because user's will than have to handle two different cases in their queries which are going to depend on the contents of the retrieved data.

  10. gchpaco commented on Jan 9, 2015

    @gchpaco
    Contributor

    We could, yeah, and for result_format='text' that is what we should do. auto is stickier because the same r.http call could return multiple types depending on something totally out of your control (imagine if Gene Ray hit Ctrl+S on his web page again and Word converted it to UTF-8).

  11. deontologician commented on Jan 9, 2015

    @deontologician
    ContributorAuthor

    I like that proposal personally. As far as headers, we could always implement #3192 and save metadata in the binary itself

  12. danielmewes commented on Jan 9, 2015

    @danielmewes
    Member

    Regarding HTTP headers: I think that's a separate question. We could add a return_headers boolean opt arg to r.http so that it would return an object {headers: {...}, data: r.binary(...)}.

    Edit: Or what @deontologician said, i.e. #3192

    (also mentioning @Tryneus)

  13. modified the milestones: 2.1, subsequent on Mar 23, 2015
  14. gchpaco commented on Mar 31, 2015

    @gchpaco
    Contributor

    I can live with a return_headers Boolean, FWIW.

  15. modified the milestones: subsequent, 2.1 on Apr 20, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions