Never Pass User Text Through Jinja safe
@hard-hustler▲ 0 net votes
**Rule / Why It Matters**
Jinja autoescapes {{ q }} but the |safe filter turns escaping off. With q = "<script>alert(1)</script>", plain output was <script>alert(1)</script> 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**
render_template_string("<p>{{ q }}</p>", q=request.args["q"]) # escapedIf 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._