← Back to Pull requests

escape U+2028 and U+2029 in the json filter #765

Merged
zayeem06 merged 2 files (64 lines) on Oct 01, 2026 09:21 UTC
View on GitHub
zayeem06 zayeem06 commented on Sep 29, 2026 09:41 UTC
Author

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/\u2029 in write_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.

Diff: 64 lines (+62 / -2) across 2 files Reviewer: GuillaumeGomez Review duration: 2860m
All Vouch review confidence checks passed
Residual risk 0.05 = Change Risk (0.29) × (1 − Review Confidence (0.83)). Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.29 · 64 lines diff across 2 files
0.29
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.73 · 5 substantive comment(s)
0.73
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 50%
83%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size diff_size
0.250
Real PR Conversation Timeline
7 items
GuillaumeGomez GuillaumeGomez commented on code on Sep 29, 2026 12:54 UTC
Code Review
testing/tests/filters.rs:1
@@ -189,6 +189,30 @@ fn test_json() {
     );
 }
 
+// U+2028 and U+2029 are valid in JSON but are line terminators in JavaScript, so `|json|safe`
+// output embedded in a `<script>` must escape them to stay a valid JS string literal.
+#[cfg(feature = "serde_json")]
+#[test]
+fn test_json_line_separators() {
+    #[derive(Template)]
+    #[template(
+        source = r#"<script>var x = {{ v|json|safe }};</script>"#,
+        ext = "html"

Just thought: can you add a test with another escaper? Only the HTML escaper needs it, right?

zayeem06 zayeem06 commented on code on Sep 30, 2026 06:06 UTC
Code Review
testing/tests/filters.rs:1
@@ -189,6 +189,30 @@ fn test_json() {
     );
 }
 
+// U+2028 and U+2029 are valid in JSON but are line terminators in JavaScript, so `|json|safe`
+// output embedded in a `<script>` must escape them to stay a valid JS string literal.
+#[cfg(feature = "serde_json")]
+#[test]
+fn test_json_line_separators() {
+    #[derive(Template)]
+    #[template(
+        source = r#"<script>var x = {{ v|json|safe }};</script>"#,
+        ext = "html"

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.

GuillaumeGomez GuillaumeGomez commented on code on Sep 30, 2026 08:33 UTC
Code Review
testing/tests/filters.rs:1
@@ -189,6 +189,30 @@ fn test_json() {
     );
 }
 
+// U+2028 and U+2029 are valid in JSON but are line terminators in JavaScript, so `|json|safe`
+// output embedded in a `<script>` must escape them to stay a valid JS string literal.
+#[cfg(feature = "serde_json")]
+#[test]
+fn test_json_line_separators() {
+    #[derive(Template)]
+    #[template(
+        source = r#"<script>var x = {{ v|json|safe }};</script>"#,
+        ext = "html"

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?

zayeem06 zayeem06 commented on code on Oct 01, 2026 06:45 UTC
Code Review
testing/tests/filters.rs:1
@@ -189,6 +189,30 @@ fn test_json() {
     );
 }
 
+// U+2028 and U+2029 are valid in JSON but are line terminators in JavaScript, so `|json|safe`
+// output embedded in a `<script>` must escape them to stay a valid JS string literal.
+#[cfg(feature = "serde_json")]
+#[test]
+fn test_json_line_separators() {
+    #[derive(Template)]
+    #[template(
+        source = r#"<script>var x = {{ v|json|safe }};</script>"#,
+        ext = "html"

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.

GuillaumeGomez GuillaumeGomez approved these changes on Oct 01, 2026 09:10 UTC
Approved Reviewer

Approved these changes.

GuillaumeGomez GuillaumeGomez commented on Oct 01, 2026 09:21 UTC
Contributor

Thanks!

Assignees
zayeem06 zayeem06
Labels
risk: low confidence: 83.5% reviewed
Vouch Risk Breakdown
AI-powered review risk analysis
Change Risk
Change Risk · Model 1 (XGBoost)
Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
Score: 29.4% (LOW) · Higher = larger blast radius
29.4%
Review Confidence
Review Confidence · Models 2 & 3
Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
Score: 83.5% · Lower = rushed or shallow review
83.5%
Residual Risk
Residual Risk · Core Equation
Residual = Change × (1 − Confidence)
Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
Score: 4.9% · Safe to merge
4.9%