SKY-D227 · Security in code
Inline template that turns off HTML escaping
Flask and Jinja escape every {{ value }} by default, so text a user typed is shown as text. The |safe filter and {% autoescape false %} turn that escaping off. SKY-D227 reports a template written inline in a render_template_string() call that uses either one.
- Languages
- Python
- Quality gate
- counted toward the gate limits (full and diff scans)
- Finding type
- review
- Catalog name
- Cross-site scripting: unsafe inline template
Why it matters
Whatever reaches the unescaped value is placed in the page as markup. A comment body such as <img src=x onerror=…> then runs script in the browser of everyone who views the post, with their session.
What Skylos flags
from flask import Flask, render_template_string app = Flask(__name__) POSTS = { 1: {"title": "Hello", "body": "Written by a visitor through the editor."},} @app.get("/posts/<int:post_id>")def show_post(post_id): post = POSTS[post_id] return render_template_string( "<article><h1>{{ post.title }}</h1>{{ post.body|safe }}</article>", post=post, )What Skylos leaves alone
from flask import Flask, render_template_string app = Flask(__name__) POSTS = { 1: {"title": "Hello", "body": "Written by a visitor through the editor."},} @app.get("/posts/<int:post_id>")def show_post(post_id): post = POSTS[post_id] return render_template_string( "<article><h1>{{ post.title }}</h1><p>{{ post.body }}</p></article>", post=post, )Why Skylos leaves it alone
The same inline template, but every value goes through Jinja's normal {{ … }} output, which escapes <, >, & and quotes. There is no |safe filter and no {% autoescape false %}, so nothing in the template text turns escaping off.
How Skylos decides
- Runs with
--dangeron Python files. It looks at calls whose name ends inrender_template_string, such asrender_template_string(…)orflask.render_template_string(…). - Reads only the first positional argument, and only when it is a string literal written in the call. A template held in a variable or constant, passed as
source=, or loaded from a template file is not read. - Reports HIGH when that text, lower-cased, contains
|safeor the exact text{% autoescape false %}. It does not check what values the template receives: the finding is about escaping being switched off. - Other ways to build templates, such as Jinja's
Environment().from_string()orTemplate(), are not checked by this rule. When request data becomes the template text itself, Skylos reports SKY-D349 (template injection) instead.
How to fix it
Drop |safe and let Jinja escape the value. If the field really must keep some formatting, clean it with an HTML sanitizer such as bleach.clean() that keeps only the tags you allow, wrap the result in Markup, and pass that to the template.
Added: import bleachfrom flask import Flask, render_template_stringAdded: from markupsafe import Markup app = Flask(__name__) POSTS = { 1: {"title": "Hello", "body": "Written by a visitor through the editor."},} @app.get("/posts/<int:post_id>")def show_post(post_id): post = POSTS[post_id]Added: body = Markup(bleach.clean(post["body"], tags={"p", "b", "i", "a"})) return render_template_string(Removed: "<article><h1>{{ post.title }}</h1>{{ post.body|safe }}</article>",Added: "<article><h1>{{ post.title }}</h1>{{ body }}</article>", post=post,Added: body=body, )When it can be wrong
|safeon a value your own code builds, such as rendered Markdown that was already sanitized or a constant snippet, is still reported: the rule only reads the template text.- The check is a plain text match. A custom filter whose name starts with
safe, such as{{ url|safe_url }}, is reported; a spaced filter such as{{ body | safe }}and a tag written as{%autoescape false%}are missed.
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
Put the marker on the line Skylos reports: the line where the
render_template_string(call starts.Python return render_template_string( # skylos: ignore[SKY-D227] body sanitized by bleach[tool.skylos] ignore = ["SKY-D227"]turns the rule off for the whole project.