SKY-D355 · Security in code
Jinja environment without autoescape
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
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
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
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
- Finds calls that resolve through imports to
jinja2.Environmentor Jinja'sSandboxedEnvironment. A local class with the same name does not match. - Reads
autoescape=, or the seventeenth positional argument, which is whereEnvironmenttakes it. - Reports HIGH when
autoescapeis missing and the call passes no**optionsor*args, or when it is a literalFalseorNone. - 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.
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, whateversettingsholds.
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 onlyAccept the findings you already have with
skylos baseline ., then scan with--baselineso only new ones are listed.[tool.skylos] ignore = ["SKY-D355"]turns the rule off for the whole project.