You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
r.http should do utf-8 validation before returning a string #2950
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:
Validate the page content as utf-8, and only if it validates return a String (otherwise return a Binary).
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.
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.
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.
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.
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%.
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.
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.
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).
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(...)}.
Activity
gchpaco commentedon Jan 9, 2015
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:
r.http('http://capcom.co.jp/').Content-Type: text/htmlwith 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, theContent-Typecan 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.r.http('http://timecube.com')which advertises itself as, again,Content-Type: text/htmlwith 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.httpshould probably give a binary.deontologician commentedon Jan 9, 2015
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?
gchpaco commentedon Jan 9, 2015
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 Ir.httpa 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 theContent-Typewhich is important b) users may have grown to expect strings fromr.httpand 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.deontologician commentedon Jan 9, 2015
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.gchpaco commentedon Jan 9, 2015
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%.
gchpaco commentedon Jan 9, 2015
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.httpin the auto processing mode.deontologician commentedon Jan 9, 2015
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?
danielmewes commentedon Jan 9, 2015
More precisely, my suggestion is to
r.httpbinaryContent-Typeis one of the JSON types{result_format: "test"}, return a string but fail if it's not valid UTF-8r.jsonto acceptbinarydata, so that a user can still easily wrap anr.jsonaround ther.http.I'm opposed to returning a string by default and falling back to
binaryonly 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.gchpaco commentedon Jan 9, 2015
We could, yeah, and for
result_format='text'that is what we should do.autois stickier because the samer.httpcall 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).deontologician commentedon Jan 9, 2015
I like that proposal personally. As far as headers, we could always implement #3192 and save metadata in the binary itself
danielmewes commentedon Jan 9, 2015
Regarding HTTP headers: I think that's a separate question. We could add a
return_headersboolean opt arg tor.httpso that it would return an object{headers: {...}, data: r.binary(...)}.Edit: Or what @deontologician said, i.e. #3192
(also mentioning @Tryneus)
gchpaco commentedon Mar 31, 2015
I can live with a
return_headersBoolean, FWIW.