comparison mercurial/templatefilters.py @ 21867:829f2dd99f5c stable

templates: escape NUL bytes in jsonescape (issue4303) It's currently possible for various fields to contain NUL bytes, which are disallowed in JSON.
author Matt Mackall <mpm@selenic.com>
date Mon, 14 Jul 2014 12:44:45 -0500
parents e828975722c8
children cf599f8a2da8
comparison
equal deleted inserted replaced
21855:1eda3e556845 21867:829f2dd99f5c
213 return '\\u%04x' % ord(c) 213 return '\\u%04x' % ord(c)
214 214
215 _escapes = [ 215 _escapes = [
216 ('\\', '\\\\'), ('"', '\\"'), ('\t', '\\t'), ('\n', '\\n'), 216 ('\\', '\\\\'), ('"', '\\"'), ('\t', '\\t'), ('\n', '\\n'),
217 ('\r', '\\r'), ('\f', '\\f'), ('\b', '\\b'), 217 ('\r', '\\r'), ('\f', '\\f'), ('\b', '\\b'),
218 ('<', '\\u003c'), ('>', '\\u003e') 218 ('<', '\\u003c'), ('>', '\\u003e'), ('\0', '\\u0000')
219 ] 219 ]
220 220
221 def jsonescape(s): 221 def jsonescape(s):
222 for k, v in _escapes: 222 for k, v in _escapes:
223 s = s.replace(k, v) 223 s = s.replace(k, v)