Bug #90503 JSON_SET returns malformed JSON when it includes binary data
Submitted: 18 Apr 2018 20:15 Modified: 17 Aug 2018 16:25
Reporter: Horacio Nicodemo Email Updates:
Status: Closed Impact on me:
None 
Category:MySQL Server: JSON Severity:S1 (Critical)
Version:5.7.21 OS:CentOS (6.0)
Assigned to: CPU Architecture:x86
Tags: JSON JSON_SET BLOB BINARY

[18 Apr 2018 20:15] Horacio Nicodemo
Description:
Hello,

All JSON functions I tried have problems handling binary data.

The test is very simple.

Thank you

Regards

How to repeat:
mysql> set @json=json_set('{}', '$.a', compress('https://domain.com/TTJNTIHCH2I/ABCDEFGHI/JKLMNOPQRS/EEdbr06Ekr8/photo.jpg';));
Query OK, 0 rows affected (0.00 sec)

mysql> select @json;
+----------------------------------------------------------------------------------------------------+
| @json                                                                                              |
+----------------------------------------------------------------------------------------------------+
| {"a": "base64:type253:SQAAAHicyygpKSi20tdPyc9NzMzTS87P1Q8J8fIL8fRw9jDy1Hd0cnZxdXP38NT38vbx9fMPCAwK
1nd1TUkqMjBzzS6y0C/IyC/J18sqSAcAZJ4XYA=="} |
+----------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> select CAST(@json AS JSON);
ERROR 3141 (22032): Invalid JSON text in argument 1 to function cast_as_json: "Invalid escape character in string." at position 97.

mysql> select json_set(@json, '$.b', 'hello');
ERROR 3141 (22032): Invalid JSON text in argument 1 to function json_set: "Invalid escape character in string." at position 97.
[19 Apr 2018 7:15] Knut Anders Hatlen
Thanks for the bug report, Horacio.

The JSON specification states that newlines must be escaped, so it is a bug that the base64-encoded string is broken up by unescaped newline characters.
[17 Aug 2018 16:25] Jon Stephens
Documented fix in the MySQL 8.0.13 changelog as follows:

    When a JSON document which contained binary data was converted
    to base-64 encoded text for display, newline characters in the
    encoded string were not properly escaped, so that the text
    representation could not be parsed as JSON, and was thus
    truncated, corrupted, or both. Now MySQL makes sure that any
    newline characters in the encoded string are escaped.

Closed.