TemplateInjection
TemplateInjection is a security vulnerability that occurs when an application renders templates using user-controlled input without proper validation. In this scenario, the templating engine may treat parts of the input as code or as access to internal data, allowing an attacker to influence the rendered output or execute unintended actions on the server.
The problem typically arises in server-side templating, where template languages expose expressions, functions, or object access
Commonly affected environments include several widely used template engines across languages (for example, those in Python,
- Validate and sanitize all user input, and avoid inserting raw user data into template syntax.
- Use templates with strict variable resolution and sandboxing features; disable access to privileged objects or global
- Prefer context-specific rendering and separate data from template code; avoid dynamic template compilation with untrusted input.
- Implement allowlists for functions, filters, and tags; keep template engines up to date with security patches.
- Employ least privilege for the rendering environment and monitor for unusual template usage patterns through logging
- Use code review, static analysis, and dynamic fuzzing to identify unsafe template expressions.
- Run security tests in a staging environment with untrusted input to observe rendering behavior and potential