escape U+2028 and U+2029 in the json filter #765
Just thought: can you add a test with another escaper? Only the HTML escaper needs it, right?
Added one. The escaping happens in the filter's own write_escaped_str, not in the escaper, so it applies whatever escaper is active. The new case renders through the txt escaper and the two separators still come out escaped.
Then it seems like the fix is wrong, no? If it's only an issue for javscript (through HTML), then it shouldn't be escaped for txt, no?
Sorry, my earlier reply was misleading on that. The filter already escapes <>&' unconditionally regardless of the active escaper (it's noted in the doc comment at the top of the file), because the documented use is {{ data|json|safe }} and |safe turns the escaper off, so the filter has to produce script-safe output on its own. U+2028/2029 belongs in that same set: without escaping them, |json|safe in a <script> is an invalid JS string literal.
Escaping them in the txt case is harmless the same way \u003c is: it's still valid JSON and decodes to the identical string. Making it conditional on the HTML escaper wouldn't cover the documented case anyway, since with |safe there's no escaper running at that point.
Approved these changes.
Thanks!
GuillaumeGomez
zayeem06
The json filter escapes
&'<>for HTML but forwards U+2028 and U+2029 verbatim like serde_json does, and those are line terminators in JavaScript even though they are valid inside JSON strings, so the documented{{ data|json|safe }}inside a<script>produces an invalid JS string literal once the data contains one of them. This escapes them as\u2028/\u2029inwrite_escaped_str, the same handling Go's text/template, Rails and Django apply to JSON embedded in HTML. Worth verifying during review: the two three-byte sequences are detected on their trailing continuation byte so the existing ASCII fast path stays as it was, and I added a unit test plus an end-to-end<script>template regression test.