Server-Side Template Injection

User input is placed into a server-side template's source rather than passed to it as data, so the template engine evaluates the attacker's template syntax.

SeverityCritical
CVSS 3.19.8 CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:H/I:H/A:H Open in the calculator
CVSS 4.09.3 CVSS:4.0/AV:N/AC:L/AT:N/PR:N/UI:N/VC:H/VI:H/VA:H/SC:N/SI:N/SA:N Open in the calculator
CWECWE-1336: Improper Neutralization of Special Elements Used in a Template Engine
MITRE ATT&CKT1190 Exploit Public-Facing Application, T1059 Command and Scripting Interpreter
CategoryInjection

Description

The application uses a server-side template engine, such as Jinja2, Twig, FreeMarker, Velocity, Thymeleaf, Smarty or ERB, and builds part of a template's source from values supplied by the user, for example by concatenating a name into a greeting template or by letting users edit email or page templates. The engine then evaluates any template syntax in that input.

Because template languages can call methods on the objects passed to them, an attacker can often move from those objects to the language's runtime, and in most template engines can run arbitrary code on the server with the privileges of the application, giving them its data, configuration secrets and access to the underlying host. Where the engine runs in a sandbox or is logic-less, the impact may be limited to reading the objects and variables passed to the template, which can still include sensitive data, and sandbox escapes are published regularly. No user interaction is needed, although some affected features, such as those that let users customise templates, require an account.

Server-side template injection typically shows itself on a test when a template expression in a parameter is evaluated, for example ${7*7} or its double curly-brace equivalent returning 49 in the response, or when a malformed expression produces an error naming the engine. Further expressions identify the engine, after which known payloads for that engine confirm the impact. Burp Suite and OWASP ZAP report the weakness as server-side template injection, and tools such as SSTImap automate its detection and exploitation. It should be distinguished from cross-site scripting: the input is evaluated on the server, so a payload that appears as harmless text in the browser may still be running code on the host.

Remediation

Keep user input out of template source:

  • Load templates only from fixed files or strings defined by the application, and pass user input to them as context variables. Never concatenate or format user input into a template before it is rendered.
  • Where users must be able to customise content, offer a limited set of placeholders that the application substitutes itself, or use a logic-less template language that cannot call methods or reach application objects.
  • If a full template engine must process templates written by users, enable its sandbox (for example Jinja2's sandboxed environment or Twig's sandbox extension) with a strict allowlist, pass only simple data objects into the context, and render in an isolated, low-privilege process or container.
  • Keep the template engine and framework up to date, as sandbox escapes are fixed in new releases.

As an interim measure, disable any feature that lets users edit templates. Confirm the fix by repeating the original expressions and checking that they appear in the response as literal text.

References


Add This Finding to Your Library

In Pental this write-up is in Settings, Library. Add to my library copies it into your own, rated in your firm's CVSS version, ready to put in your own words.

Free trial  ·  No card required

Only need the reporting? Our reporting tool is free and open source, with the same library.


More Web Applications Findings

Every Web Applications finding