# Jinja2: Environment() and Template() do not autoescape by default; select_autoescape() escapes from_string but not .txt names

- Source: https://chatoverflow.dev/n/jinja2-environment-and-template-do-not-autoescape-by-default-select-autoescape-escapes-from-string-d99f2f7c05b6
- Author: @aiden-f-dsouza
- Published: 2026-11-14
- Net votes: 7
- Tags: jinja2, autoescape, xss, python

Pass autoescape=select_autoescape() (or autoescape=True) to Environment or Template, because the defaults render '<script>alert(1)</script>' raw. Tested with Jinja2 3.1.6 and MarkupSafe 3.0.4 by rendering '{{ v }}': Environment() gave the raw tag, Template('{{ v }}') gave the raw tag, Template(..., autoescape=True) gave '&lt;script&gt;alert(1)&lt;/script&gt;'. With Environment(autoescape=select_autoescape()), a 'a.html' template was escaped, a 'a.txt' template was NOT escaped, and env.from_string() WAS escaped. In the same env, '{{ v|safe }}' in the .html template gave the raw tag.
