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

block_timeline can cause JSON parse error on dashboard when activities are saved with newline characters

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Minor
    • Resolution: Fixed
    • Affects Version/s: 3.6.3
    • Fix Version/s: 3.8
    • Component/s: Blocks

      Description

      There are a few ways to get newline characters (\r\n) saved in the database as a label or activity name. When loading the dashboard (/my/ page), the timeline block goes to display the activity list but it does not do any validation of the data it finds for the activity names, and an ajax JSON parse error will display as shown in the screenshot (block_timeline_error.png):

      JSON.parse: bad control character in string literal at line 1 column 30 of the JSON data
      

       

      Simple way to get this error on a clean moodle 3.6:

      1. Set up a test course, and enrol your user into it so that that courses activities will show up on the dashboard.
      2. In the test course, create a label. As shown in the screenshot (create_label.png), enable activity completion and set it to a date after today, and just copy and paste the following in for the label text in HTML mode (not rich text editor):

        <h4>test heading</h4>
        <p>then a paragraph</p>
        

      3. You will now see the error in the timeline block on a reload of the dashboard.

      If you don't see the error, verify in the database that you have newline characters inserted into the mdl_event table for that label (see screenshot newline_in_db.png), and purge caches. I found that the caching is quite aggressive and would have to disable activity completion, save, and then re-enable and save again for changes to be seen on the dashboard. Attached is also a picture from the network console tab in the browser that shows the newline characters. Interestingly, the browser parses the JSON correctly, but moodle doesn't (network_tab_response.png).

      You can also get newline characters to save to the database when adding a newline character in rich text editing mode, and that also causes the error.

      We probably don't care that these newline characters are inserted in the database, but there should be extra validation in the timeline block when it goes to parse and display the data that is fetched.

      We probably want to do some encoding on the newline characters, which means that it can display in ascii characters in the json, rather than being displayed as a new line. Might need to do decoding on the other end too. Looks like there is some encoding that can happen in the external_format_text method in lib/externallib.php, looking at line 958:

      *      newlines    :   If true then lines newline breaks will be converted to HTML newline breaks. Default true.
      

       

       

       

       

        Attachments

        1. before_and_after_JSONstringify.png
          before_and_after_JSONstringify.png
          8 kB
        2. block_timeline_error.png
          block_timeline_error.png
          34 kB
        3. Confirmed fixed.PNG
          Confirmed fixed.PNG
          40 kB
        4. create_label.png
          create_label.png
          56 kB
        5. network_tab_response.png
          network_tab_response.png
          117 kB
        6. newline_in_db.png
          newline_in_db.png
          12 kB

          Activity

            People

            • Votes:
              0 Vote for this issue
              Watchers:
              5 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:
                Fix Release Date:
                11/Nov/19

                Time Tracking

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