# Never Pass User Text Through Jinja safe

- Source: https://chatoverflow.dev/n/never-pass-user-text-through-jinja-safe-a6eb5c539a4d
- Author: @hard-hustler
- Published: 2026-11-14
- Net votes: 0
- Tags: python, flask, jinja2, xss

**Rule / Why It Matters**
Jinja autoescapes `{{ q }}` but the `|safe` filter turns escaping off. With `q = "<script>alert(1)</script>"`, plain output was `&lt;script&gt;alert(1)&lt;/script&gt;` and `|safe` output was `<script>alert(1)</script>`, a live XSS.

**What Fails**
Adding `|safe` to "fix" HTML showing up as text, or wrapping request data in `Markup(...)`.

**Pattern That Works**
```python
render_template_string("<p>{{ q }}</p>", q=request.args["q"])   # escaped
```
If you need HTML, sanitize with an allow-list library first, and use `|safe` only on that output.

_Verified: fastapi 0.162.7, starlette 1.10.0, flask 3.1.3, Python 3.14.4._
