Keeping untrusted data out of executable server templates
Keeping untrusted data out of executable server templates connects a security requirement to the behaviour of a real feature. The relevant controls need to remain effective across custom code, integrations and the production configuration. Understanding keeping untrusted data out of executable server templates helps establish which security property the feature needs and where that property is actually enforced.
What does this involve?
Input validation defines the structures, sizes and meanings an application expects to receive. It must happen before information influences a sensitive operation. Validation does not replace safe interpreter APIs, output encoding or permission checks, because those controls address different boundaries.
Server-Side Template Injection (SSTI) happens when a server-side template engine evaluates untrusted input as template code instead of rendering it as data (Common Weakness Enumeration CWE-1336). Engines such as Jinja2, Twig, and FreeMarker have their own expression languages, so injected syntax runs with the engine's access to application objects. Depending on the engine and its configuration, the impact ranges from reading sensitive data and files to remote code execution.
Keep templates with the application source, review changes to them like code, and never build them from untrusted data. Do not let untrusted data choose a template name, path, or include target either: map the user's choice to a fixed list of template names on the server. If users must author templates, follow the User-Supplied Templates section below.
Why it matters
Consider a realistic failure: an application lets untrusted content become the template itself instead of a safely rendered template value.
Untrusted input can change application behaviour, consume excessive resources or reach data and execution paths outside the intended feature. The practical result depends on where the value is used and which privileges are available there.
Implementation guidance should be applied to a real feature and its deployment. Framework defaults, custom handlers and integrated services all influence whether the intended protection remains effective.
How penetration testers check for it
Trace the relevant feature from its entry point to the data or operation it protects, including custom code and alternate paths.
- Trace input from every relevant entry point to its use.
- Check types, lengths, ranges and ambiguous representations.
- Compare parsing across components and alternate request formats.
- Use controlled invalid inputs to establish effects rather than assumptions.
How to fix it
Use the appropriate maintained mechanisms and verify the result in the actual production configuration.
- Input becomes template source. Untrusted data, such as request parameters, stored user content, or third-party files, is concatenated into a template string, even inside an expression like "Hello {{" + name + "}}", or passed to a string-to-template API such as Jinja2's from_string or Flask's render_template_string.
- Input chooses the template. User input selects a template name, path, or include target, which can make the engine load files it should not.
- Limit template editing to authorised roles and log template changes for audit.
- Render with the engine's sandbox or restricted configuration from the table above and keep the engine up to date.
- Reject unexpected structures before sensitive processing.
- Apply resource bounds and predictable failure handling.
Blocking a few known attack strings is not a complete validation strategy. The intended data model and downstream interpretation determine which protections are needed.
Recheck the changed behaviour against the original failure conditions and the legitimate user journey. Keep enough evidence to show which release and configuration were verified, and include related entry points that depend on the same security decision.
Related issues
- Building frontend trust boundaries around untrusted content
- Securing Next.js rendering, server actions and data boundaries
- Implementing layered defences around untrusted LLM instructions
Read about our methodology or contact pentested.com to discuss secure development and code review.