Skip to content

Enforce UTF-8 encoding of strings #1181

Closed

Description

@AtnNn

There are many inconsistencies in the treatment of strings. It should probably be standardised across drivers and reql functions, and documented.

The python driver refuses non-ascii strings unless they are unicode:

python> r.expr('\xc1').run()
  File "/Library/Python/2.7/site-packages/google/protobuf/internal/type_checkers.py", line 128, in CheckValue
    (proposed_value))
ValueError: '\xc1' has type str, but isn't in 7-bit ASCII encoding. Non-ASCII strings must be converted to unicode objects before being added.

The ruby driver lets you use strings that are not utf8:

ruby> r.expr("\xc1").run
=> "\301"

The python drivers fails badly when receiving a non-utf8 string:

ruby> r.table('foo').insert({:a=>"\xc1"}).run

python> r.table('foo').run()
  File "/Library/Python/2.7/site-packages/google/protobuf/internal/decoder.py", line 410, in DecodeField
    field_dict[key] = local_unicode(buffer[pos:new_pos], 'utf-8')
UnicodeDecodeError: 'utf8' codec can't decode byte 0xc1 in position 0: invalid start byte

r.js expects strings to be utf-8, and gets confused when they are not:

ruby> r.expr("\xc1").do(r.js('(function(x){ return x; })')).run
=> "\357\277\275"

r.match also expects utf-8:

ruby> r.expr("a").match(".").run
=> {"groups"=>[], "end"=>1, "str"=>"a", "start"=>0}

ruby> r.expr("\xc1").match(".").run
=> nil

Other reql functions that use operations from C++'s std::string (such as comparison operators) simply treat strings as arrays of bytes.

The JSON spec says that

A string is a sequence of zero or more Unicode characters, wrapped in double quotes

This affects issues such as #264, #874, #1099 and #1147.

Issue #137 complements this issue by proposing a separate binary data type.

Activity

  1. mlucy commented on Jul 18, 2013

    @mlucy
    Member

    Labeling this as a RQL proposal, since it involves standardizing RQL strings.

  2. srh commented on Mar 5, 2014

    @srh
    Contributor

    I'm relabeling this a bug. If theres an octet-string type the driver must handle it properly, not by sending garbage UTF-8.

  3. thelinuxlich commented on Apr 3, 2014

    @thelinuxlich

    I'm having a problem where I send a UTF-8 string from the browser to a Node.js server using the RethinkDB driver and it saves the string with garbage, what should I do?

  4. coffeemug commented on Apr 3, 2014

    @coffeemug
    Contributor

    Hi @thelinuxlich! What do you mean "from the browser to a Node.js server using the RethinkDB driver"? Could you post the queries here?

  5. thelinuxlich commented on Apr 3, 2014

    @thelinuxlich

    The error was on my side, sorry guys!

  6. modified the milestones: , backlog on Dec 11, 2014
  7. danielmewes commented on Dec 11, 2014

    @danielmewes
    Member

    This has been superseded by #3301 , which includes a verification step. @gchpaco has already implemented that part (currently in code review).
    Closing as duplicate.

  8. modified the milestones: duplicate, on Dec 11, 2014
  9. AtnNn commented on Dec 12, 2014

    @AtnNn
    MemberAuthor

    @danielmewes The solution you mention explicitly still allows non-utf-8 strings. Perhaps we should open another issue for those cases.

  10. 1 remaining item

  11. modified the milestones: backlog, duplicate on Dec 12, 2014
  12. gchpaco commented on Jan 8, 2015

    @gchpaco
    Contributor

    In next now, we enforce UTF-8 on input strings. When I get some other stuff done, I will be able to fix split to be safe, at which point it should not be easy to insert invalid UTF-8 strings into the database. Not included here is any form of locale support, which is the toupper/tolower stuff as well as collation; I feel that's grist for a different mill.

    @danielmewes Do you want me to close this now, or wait until we get split in?

  13. self-assigned this
    on Jan 8, 2015
  14. gchpaco commented on Jan 8, 2015

    @gchpaco
    Contributor

    Per private discussion, there's a followup ticket for that in #3531.

  15. modified the milestones: , backlog on Jan 9, 2015
  16. modified the milestones: , 1.16 on Jan 23, 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

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions