Skip to main content

SKY-D349 · Security in code

Server-side template injection

CriticalCWE-1336

A template engine such as Jinja or Velocity turns template text into code and runs it. Server-side template injection (SSTI) happens when outside text becomes part of the template itself instead of a value passed to it. Skylos reports SKY-D349 when request data reaches the template source of render_template_string(), a Jinja from_string() or a Java or PHP equivalent.

Languages
Python, Java, PHP
Quality gate
counted toward the gate limits; a CRITICAL finding fails the gate on its own (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

Template syntax can reach Python or Java objects. A visitor who sends {{ cycler.__init__.__globals__.os.popen('id').read() }} as their name runs a shell command on the server, with the application's files, keys and network access.

What Skylos flags

app/hello.py · PythonReported on line 9
from flask import Flask, render_template_string, request app = Flask(__name__)  @app.get("/hello")def hello():    name = request.args.get("name", "guest")    return render_template_string(f"<h1>Hello {name}!</h1>")

What Skylos leaves alone

app/hello.py · Python
from flask import Flask, render_template_string, request app = Flask(__name__)  @app.get("/hello")def hello():    name = request.args.get("name", "guest")    return render_template_string("<h1>Hello {{ name }}!</h1>", name=name)

Why Skylos leaves it alone

The template text is a fixed literal and the name is passed as a template variable. Jinja inserts it as escaped text and never reads it as template syntax, so {{ ... }} in the name is shown, not run.

More languages

Java

Flagged

src/main/java/com/example/GreetingController.java · JavaReported on line 18
package com.example; import java.io.StringWriter;import org.apache.velocity.VelocityContext;import org.apache.velocity.app.Velocity;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RestController; @RestControllerpublic class GreetingController {     @GetMapping("/greet")    public String greet(@RequestParam String template) {        VelocityContext context = new VelocityContext();        context.put("site", "Example Shop");        StringWriter out = new StringWriter();        Velocity.evaluate(context, out, "greeting", template);        return out.toString();    }}

Not flagged

src/main/java/com/example/GreetingController.java · Java
package com.example; import java.io.StringWriter;import org.apache.velocity.VelocityContext;import org.apache.velocity.app.Velocity;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RestController; @RestControllerpublic class GreetingController {     @GetMapping("/greet")    public String greet(@RequestParam String name) {        VelocityContext context = new VelocityContext();        context.put("name", name);        StringWriter out = new StringWriter();        Velocity.evaluate(context, out, "greeting", "Hello, $name!");        return out.toString();    }}

Why Skylos leaves it alone

The request value goes into the VelocityContext as data. The template text, the fourth argument of evaluate(), is a fixed string, which is the only argument Skylos checks there.

How Skylos decides

  1. In Python, finds render_template_string() and from_string() calls. A from_string() counts only when its receiver's name contains env or jinja, as in env.from_string(src) or jinja2.Environment().from_string(src).
  2. Reads the template source: the first argument or source=. A string literal is skipped.
  3. Reports CRITICAL when an untrusted source reaches that source text: request data, a route or MCP tool argument, input() or standard input, directly or through assignments and f-strings. A source made only of environment variables is configuration and is not reported.
  4. Java: request data in the template argument of Velocity evaluate(), FreeMarker new Template(name, source, ...), StringTemplateLoader.putTemplate() or Groovy TemplateEngine.createTemplate(). PHP: request data in a Drupal #type => 'inline_template' render array.

How to fix it

Keep templates in files or fixed strings, and pass outside values as template variables. Never build template text from input.

app/hello.py · Python · the fix, against the flagged file
Removed: from flask import Flask, render_template_string, requestAdded: from flask import Flask, render_template, request app = Flask(__name__)  @app.get("/hello")def hello():    name = request.args.get("name", "guest")Removed:     return render_template_string(f"<h1>Hello {name}!</h1>")Added:     return render_template("hello.html", name=name)

When it can be wrong

  • A tool that lets trusted administrators edit templates through a web form is reported, because the template text comes from request data. Render such templates in Jinja's SandboxedEnvironment and keep the form behind authentication.
  • A from_string() method on an object whose name happens to contain env is treated as a Jinja environment.

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 call and say why. A bare # noqa does not hide HIGH or CRITICAL security findings.

    Python
    return env.from_string(source)  # skylos: ignore[SKY-D349] admin-only, sandboxed
  • Accept the findings you already have with skylos baseline ., then scan with --baseline so only new ones are listed.

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

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