Uploaded image for project: 'MariaDB Server'
  1. MariaDB Server
  2. MDEV-41796

JSON_NORMALIZE emits invalid JSON or changes object-member names containing escaped characters

    XMLWordPrintable

Details

    Description

      JSON_NORMALIZE incorrectly serializes decoded object-member names containing a quote, backslash, or newline when NO_BACKSLASH_ESCAPES is enabled. In MariaDB 12.3.3 on Linux/amd64, valid inputs containing escaped quotes and newlines become invalid JSON, while a key containing an escaped backslash remains valid but changes its decoded bytes.

      Reproducer

      SET SESSION sql_mode = 'NO_BACKSLASH_ESCAPES';
       
      SET @quote_key =
        CONVERT(0x7B22615C753030323262223A317D USING utf8mb4);
      SET @backslash_key =
        CONVERT(0x7B22615C753030354362223A317D USING utf8mb4);
      SET @newline_key =
        CONVERT(0x7B22615C753030304162223A317D USING utf8mb4);
       
      SELECT 'quote_key' AS case_name,
             JSON_VALID(@quote_key) AS input_valid,
             HEX(JSON_NORMALIZE(@quote_key)) AS normalized_hex,
             JSON_VALID(JSON_NORMALIZE(@quote_key)) AS output_valid,
             HEX(JSON_UNQUOTE(JSON_EXTRACT(JSON_KEYS(@quote_key), '$[0]'))) AS input_key_hex,
             HEX(JSON_UNQUOTE(JSON_EXTRACT(JSON_KEYS(JSON_NORMALIZE(@quote_key)), '$[0]'))) AS output_key_hex;
      SELECT 'backslash_key' AS case_name,
             JSON_VALID(@backslash_key) AS input_valid,
             HEX(JSON_NORMALIZE(@backslash_key)) AS normalized_hex,
             JSON_VALID(JSON_NORMALIZE(@backslash_key)) AS output_valid,
             HEX(JSON_UNQUOTE(JSON_EXTRACT(JSON_KEYS(@backslash_key), '$[0]'))) AS input_key_hex,
             HEX(JSON_UNQUOTE(JSON_EXTRACT(JSON_KEYS(JSON_NORMALIZE(@backslash_key)), '$[0]'))) AS output_key_hex;
      SELECT 'newline_key' AS case_name,
             JSON_VALID(@newline_key) AS input_valid,
             HEX(JSON_NORMALIZE(@newline_key)) AS normalized_hex,
             JSON_VALID(JSON_NORMALIZE(@newline_key)) AS output_valid,
             HEX(JSON_UNQUOTE(JSON_EXTRACT(JSON_KEYS(@newline_key), '$[0]'))) AS input_key_hex,
             HEX(JSON_UNQUOTE(JSON_EXTRACT(JSON_KEYS(JSON_NORMALIZE(@newline_key)), '$[0]'))) AS output_key_hex;
       
      SET @quote_key = NULL;
      SET @backslash_key = NULL;
      SET @newline_key = NULL;

      The statements return:

      case_name	input_valid	normalized_hex	output_valid	input_key_hex	output_key_hex
      quote_key	1	7B22612262223A312E3045307D	0	612262	NULL
      case_name	input_valid	normalized_hex	output_valid	input_key_hex	output_key_hex
      backslash_key	1	7B22615C62223A312E3045307D	1	615C62	6108
      case_name	input_valid	normalized_hex	output_valid	input_key_hex	output_key_hex
      newline_key	1	7B22610A62223A312E3045307D	0	610A62	NULL

      For every case, both input_valid and output_valid should be 1, and input_key_hex should equal output_key_hex. Normalizing valid JSON must preserve both JSON validity and the decoded object-member name. Instead, the quote and newline cases become invalid JSON, and the backslash case changes the decoded key bytes from 615C62 to 6108.

      The evidence indicates that object-member escapes are decoded before serialization, but the decoded characters are not JSON-escaped again.

      This occurs when JSON_NORMALIZE serializes object-member names whose JSON spelling uses escapes for a quote, backslash, or control character.

      Attachments

        Issue Links

          Activity

            People

              rucha174 Rucha Deodhar
              faner fan
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:

                Time Tracking

                  Estimated:
                  Original Estimate - 0d
                  0d
                  Remaining:
                  Remaining Estimate - 2.5d
                  2.5d
                  Logged:
                  Time Spent - Not Specified
                  Not Specified

                  Git Integration

                    Error rendering 'com.xiplink.jira.git.jira_git_plugin:git-issue-webpanel'. Please contact your Jira administrators.