Skip to main content

SKY-D355 · Security in code

Jinja environment without autoescape

High

Autoescaping makes a template engine turn <, >, & and quotes in inserted values into harmless text. Jinja's own Environment has it off unless you turn it on (Flask turns it on for you, plain Jinja does not). SKY-D355 reports a jinja2.Environment created without autoescape, or with autoescape=False.

Languages
Python
Quality gate
counted toward the gate limits (full and diff scans)
Finding type
review
Wrong result? Report itA false positive, a missed case or a confusing message: it goes privately to the founder.

Why it matters

Every {{ value }} in such a template is pasted into the HTML as is. A user who sets their display name to <script>...</script> runs JavaScript in the browser of everyone who views their profile, which can steal sessions or act as the viewer.

What Skylos flags

app/render.py · PythonReported on line 3
from jinja2 import Environment, FileSystemLoader env = Environment(loader=FileSystemLoader("templates"))  def render_profile(user):    return env.get_template("profile.html").render(user=user)

What Skylos leaves alone

app/render.py · Python
from jinja2 import Environment, FileSystemLoader, select_autoescape env = Environment(    loader=FileSystemLoader("templates"),    autoescape=select_autoescape(["html", "xml"]),)  def render_profile(user):    return env.get_template("profile.html").render(user=user)

Why Skylos leaves it alone

select_autoescape() turns escaping on for templates whose names end in .html or .xml. Skylos reports only a missing autoescape or a literal False or None; any other value is left alone.

How Skylos decides

  1. Finds calls that resolve through imports to jinja2.Environment or Jinja's SandboxedEnvironment. A local class with the same name does not match.
  2. Reads autoescape=, or the seventeenth positional argument, which is where Environment takes it.
  3. Reports HIGH when autoescape is missing and the call passes no **options or *args, or when it is a literal False or None.
  4. Leaves True, select_autoescape(...), a function and any value it cannot read as a literal alone.

How to fix it

Pass autoescape=select_autoescape() (or autoescape=True when the environment only renders HTML). Mark values that really are trusted HTML with Markup instead of turning escaping off.

app/render.py · Python · the fix, against the flagged file
from jinja2 import Environment, FileSystemLoader Removed: env = Environment(loader=FileSystemLoader("templates"))Added: env = Environment(loader=FileSystemLoader("templates"), autoescape=True)  def render_profile(user):    return env.get_template("profile.html").render(user=user)

When it can be wrong

  • An environment that only renders plain-text emails, YAML or other non-HTML output, where HTML escaping would be wrong, is reported, even with an explicit autoescape=False.
  • Options passed as Environment(**settings) are not read, so such a call is never reported, whatever settings holds.

Seeing a case that isn't listed here? Report the wrong result so the rule can be fixed. Don't paste company code: describe the shape of it.

How to silence it

  • Mark a reviewed line and say why, for example for a plain-text environment.

    Python
    env = Environment(loader=loader)  # skylos: ignore[SKY-D355] plain-text emails only
  • Accept the findings you already have with skylos baseline ., then scan with --baseline so only new ones are listed.

  • [tool.skylos] ignore = ["SKY-D355"] turns the rule off for the whole project.

From the Skylos rule handbook, Skylos 5.8.0 (commit 5b880b0). All rules.