Advertisement
Guest User

How not to safely escape JSON

a guest
Jan 13th, 2015
2,178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.42 KB | None | 0 0
  1. def json_safedumps(content):
  2.     return json.dumps(content) \
  3.         .replace('\\\\', '__literal_slash__') \
  4.         .replace('\\n', '') \
  5.         .replace('\\r', '') \
  6.         .replace('\\"', '\\\\"') \
  7.         .replace("'", "\\'") \
  8.         .replace('__literal_slash__', '\\\\\\\\')
  9.  
  10. """DESCRIPTION:
  11.  
  12. This function "safely" dumps a JSON string that could be injected into a front-end template inside a JavaScript quoted literal, i.e.
  13.  
  14. <script>
  15. var data = JSON.parse("{{ encoded_data }}");
  16. </script>
  17.  
  18. So, quotes in the JSON needed to be escaped to not conflict with the string delimiters, newlines had to be removed or they'd cause a JavaScript syntax error, and so-on.
  19.  
  20. - - -
  21.  
  22. Originally, the two __literal_slash__ lines didn't exist, and it would fall apart if some of the text had a literal "\n" sequence written out in text, i.e. "\\n", as in this example:
  23.  
  24. >>> data = {"message": "Hello\\nworld!"}
  25. >>> json.dumps(data)
  26. {"message": "Hello\\nworld!"}
  27.  
  28. What would happen was that the "\\n" substitution would end up matching the "\n" from "\\n" and removing it, leaving an orphaned, single "\" character behind. If that character then ended up touching another letter and it didn't form a valid JSON escape sequence (for example, "\a"), this would cause a JSON parse error in the JavaScript.
  29.  
  30. So, they first rename literal \ characters to __literal_slash__, do all the other substitutions, and then rename it back.
  31. """
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement