Uploaded image for project: 'Moodle'
  1. Moodle
  2. MDL-75516

Mustache quote helper does not escape all special JSON characters

XMLWordPrintable

    • MOODLE_311_STABLE, MOODLE_39_STABLE, MOODLE_400_STABLE, MOODLE_401_STABLE
    • MOODLE_400_STABLE, MOODLE_401_STABLE
    • MDL-75516_401
    • Hide

      Test Javascript implementation:

      1. Create or edit a course so it's full name is

        foo \ bar
        

      2. Make sure the course's start date is in the past and the end date is in the future
      3. Add an assignment with a deadline for submitting
      4. Navigate to the dashboard (make sure there is a timeline block)
      5. Verify there is no popup with an error message
      6. Verify the assignment shows up on the timeline with the correct course name ("foo \ bar")

      Test PHP implementation:

      1. Download test.mustache and move it to lib/templates/test.mustache
      2. Download test.php and move it to lib/test.php
      3. Navigate to /lib/test.php in your browser
      4. Verify that you see two identical lines:

        "foo \b\f\n\r\t\"\\ bar"
        "foo \b\f\n\r\t\"\\ bar"
        

      Show
      Test Javascript implementation: Create or edit a course so it's full name is foo \ bar Make sure the course's start date is in the past and the end date is in the future Add an assignment with a deadline for submitting Navigate to the dashboard (make sure there is a timeline block) Verify there is no popup with an error message Verify the assignment shows up on the timeline with the correct course name ("foo \ bar") Test PHP implementation: Download test.mustache and move it to lib/templates/test.mustache Download test.php and move it to lib/test.php Navigate to /lib/test.php in your browser Verify that you see two identical lines: "foo \b\f\n\r\t\"\\ bar" "foo \b\f\n\r\t\"\\ bar"

      The mustache quote helper is used to escape quotes when inserting variables into JSON notation. It was introduced in MDL-52136. Unfortunately, to escape a string for use in JSON, more characters than just the double quote have to be escaped. To be exact:

      • Backspace to be replaced with \b
      • Form feed to be replaced with \f
      • Newline to be replaced with \n
      • Carriage return to be replaced with \r
      • Tab to be replaced with \t
      • Double quote to be replaced with \"
      • Backslash to be replaced with \\

      Source: https://www.tutorialspoint.com/json_simple/json_simple_escape_characters.htm

      This leads to some bugs: MDL-67640, MDL-68865 and MDL-69398 to name a few.

      In MDL-65203 and MDL-65183, some of these special characters (\t, \n and \r) were added to the list of characters that the JS implementation of the quote helper replaces. It's still incomplete, though. And the PHP implementation hasn't been updated to match the Javascript one.

      Most importantly, the backslash character is not being escaped, yet. This means that the escaping of other characters (like double quotes) can be undone easily. For example, the string

      foo\"bar

      is "escaped" to

      foo\\"bar

      by the current implementation of the quote helper. As you can see, the "escaped" version contains an unescaped double quote because of the extra backslash.

      Suggested solution

      Fortunately, there is a function in both Javascript and PHP that takes care of escaping all these special characters correctly: JSON encoding!

      Just passing the input of the quote helper to json_encode in PHP and JSON.stringify in Javascript yields a string with all special JSON characters being escaped. The resulting string also starts and ends with an extra quote, but adding quotes is exactly what the quote helper is supposed to do anyway. So we don't even have to remove them.

        1. (I) 5 Passed -- (400)MDL-75516.png
          61 kB
          Kim Jared Lucas
        2. (I) 5 Passed -- (401)MDL-75516.png
          56 kB
          Kim Jared Lucas
        3. (I) 5 Passed -- (master)MDL-75516.png
          65 kB
          Kim Jared Lucas
        4. (I) 6 Passed -- (400)MDL-75516.png
          47 kB
          Kim Jared Lucas
        5. (I) 6 Passed -- (401)MDL-75516.png
          38 kB
          Kim Jared Lucas
        6. (I) 6 Passed -- (master)MDL-75516.png
          14 kB
          Kim Jared Lucas
        7. (II) 4 Passed -- (400)MDL-75516.png
          9 kB
          Kim Jared Lucas
        8. (II) 4 Passed -- (401)MDL-75516.png
          9 kB
          Kim Jared Lucas
        9. (II) 4 Passed -- (master)MDL-75516.png
          10 kB
          Kim Jared Lucas
        10. php_template_success.png
          17 kB
          David Woloszyn
        11. test.mustache
          0.9 kB
          Lars Bonczek
        12. test.php
          0.3 kB
          Lars Bonczek
        13. timeline_no_errors.png
          26 kB
          David Woloszyn
        14. unit_test_passed.png
          46 kB
          David Woloszyn

            bonczek Lars Bonczek
            bonczek Lars Bonczek
            David Woloszyn David Woloszyn
            Ilya Tregubov Ilya Tregubov
            Kim Jared Lucas Kim Jared Lucas
            Votes:
            4 Vote for this issue
            Watchers:
            10 Start watching this issue

              Created:
              Updated:
              Resolved:

                Estimated:
                Original Estimate - 0 minutes
                0m
                Remaining:
                Remaining Estimate - 0 minutes
                0m
                Logged:
                Time Spent - 2 hours, 25 minutes
                2h 25m

                  Error rendering 'clockify-timesheets-time-tracking-reports:timer-sidebar'. Please contact your Jira administrators.