Skip to main content

SKY-D227 · Security in code

Inline template that turns off HTML escaping

HighCWE-79

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
Wrong result? Report itA false positive, a missed case or a confusing message: it goes privately to the founder.

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

app/posts.py · PythonReported on line 13
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

app/posts.py · Python
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

  1. Runs with --danger on Python files. It looks at calls whose name ends in render_template_string, such as render_template_string(…) or flask.render_template_string(…).
  2. 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.
  3. Reports HIGH when that text, lower-cased, contains |safe or the exact text {% autoescape false %}. It does not check what values the template receives: the finding is about escaping being switched off.
  4. Other ways to build templates, such as Jinja's Environment().from_string() or Template(), 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.

app/posts.py · Python · the fix, against the flagged file
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

  • |safe on 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.

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