core icon indicating copy to clipboard operation
core copied to clipboard

Content Types: Key/Value fields values are not escaped properly

Open joseorsini opened this issue 9 years ago • 7 comments

Depending on the values you would like to store in a Key/Value field, Velocity escaping is applied before the entire field value is stored on a JSON object on the DB.

Expected Behavior

Escaping is properly applied, so no data loss takes place.

Current Behavior

Combination of backslashes and certain characters are not escaped properly, or even we face data loss once the content is saved.

Possible Solution

Apply a better way to handle data before it's stored as a JSON Object. Class in conflict is ESMappingAPIImpl.java, specifically on the loadFields() method and how Key/Value fields are handled IF they're not the Metadata field for a file type content type.

Known Workarounds

None.

Steps to Reproduce (for bugs)

  1. Add a new Key/Value field to File Asset content type. For testing purposes, let's call this field "Other Metadata Field"

  2. Go to any folder in Website Browser and attempt to add a new file. For testing purposes, file is called "form.xlsx".

  3. Under "Other Metadata Field", submit the following key/value:

    • Key: fullfilename.
    • Value: \WIN-L9IE0C5QN6Iuserdataform.xlsx
  4. Save the content.

  5. Open the content again and check the "Other Metadata Field" value. As you can see, we go from this:

\WIN-L9IE0C5QN6Iuserdataform.xlsx

To this

WIN-L9IE0C5QN6I${esc.b}userdataorm.xlsx

Screenshot: screen shot 2017-01-25 at 12 46 59 pm 2

Context

Regardless of the current output, we should not incur in data loss. Reported by customer: https://my.dotcms.com/tickets/detail.dot?id=14c786c5-8a57-4fa1-b04d-e67d646b2176

Your Environment

  • dotCMS 3.6.0 and 3.6.2.
  • Google Chrome Browser.
  • Windows Server 2016, macOS Sierra 10.12.
  • Tomcat 8.0.18.
  • Oracle JDK 8.
  • Postgres 9.4.1, SQL Server 2012.

joseorsini avatar Jan 25 '17 18:01 joseorsini

Here’s what I’ve done so far:

  1. I removed the call that replaces the \u with the {$enc.b} token. After evaluating the key/value without the replaced token, the page throws an error. I have attached the StackTrace Here. It is interesting the error it throws is a java.lang.NumberFormatException: \userd
  2. I proceded to do a research on Javascript escape characters and found some interesting data (description bellow).
  3. After the research I’ve created replacements for any occurrences of a back slash () with a particular token, so it could render in the page accordingly. Then tried to parse the output so it could replace the token with the correct character. I struggled a bit locating the place to do this change. So…

…to solve this issue I may need some guidance and feedback. Based on my research I found out the way dotCMS renders the JSON contents of a key/value field is not what we expect as main result. The previous statement is based on the steps to reproduce the current issue, but this also applies for any other field/content combination.

First, I need to mention that in Javascript the escape characters can be used to insert other special characters in a string. For instance: \b Backspace \r Carriage Return \f Form Feed \t Horizontal Tabulator \v Vertical Tabulator

Also we have the \u which applies for any unicode value representation.

In this particular scenario the page fetches the stored value and renders it, but since it has slashes and particular combinations of \u and \f those values get “ignored”. In fact what happens is that Javascript tries to apply the escape character values leading to errors or weird output. So any String that contains any of the previous escape characters will show incorrectly on screen but saved correctly in the DB.

My suggestions:

  1. Implement a simple character exchange in which you switch any backslash (\)* with a forward slash (/).
  2. Create a parser that given any of occurrence of a backslash it gets replaced by a token. Then on the JSP or the rendered output a call needs to be made to a parse engine that replaces the tokens with the correct character. This may be harder to do since I ignore how many places this can occur and could lead to a lot of code changes
  3. Find a way to escape the characters correctly
  4. Wait for feedback.

*when I edited this comment I had to put two backslashes so it renders it as one but not for the \u, \b, etc. Interesting...

You can check the following URLs for additional details: https://mathiasbynens.be/notes/javascript-unicode https://www.w3schools.com/js/js_strings.asp http://arpan-khandelwal-tech.blogspot.mx/2008/08/this-article-is-related-to-escaping-of.html

danlaguna avatar Mar 31 '17 22:03 danlaguna

I added some code to use the javascript escaped characters (\) instead of the plain backslash. This fixes the issue on both the key/value and also on editing a container.

danlaguna avatar Apr 03 '17 23:04 danlaguna

@danlaguna @jdotcms please look at this new issue https://github.com/dotCMS/core/issues/11277. Please test with latest PR to see if this is fixed

dsilvam avatar Apr 07 '17 22:04 dsilvam

Started reviewing, will let you know ASAP.

danlaguna avatar Apr 10 '17 21:04 danlaguna

Based on local test against the TimeMachine feature and the CronJobs, the latest fix causes this process not to crash. I'm currently testing other features on dotCMS to check if there are some more issues.

danlaguna avatar Apr 11 '17 15:04 danlaguna

Needs work. Even though the value field holds the correct data (as you can see below) image

we still see errors in the logs related to NumberFormatException: \userd https://gist.github.com/shaydotcms/c596b161e6c1914c3fba8fa9af4cdc18

shaydotcms avatar Apr 18 '17 13:04 shaydotcms

Passed QA. Exception related to NumberFormatException: \userd is not showing up anymore Tested on master-4.1 / commit f0bc1401ba5a404ce078f934aa23fa2db7a052af Mac OS 10.10 / MySQL 5.6.35 / Firefox v45.0.1 / Tomcat 8 / Java 8

shaydotcms avatar Apr 26 '17 15:04 shaydotcms