Interactive Playground
Experiment with the vulnerable code and security rule below. Edit the code to see how the rule detects different vulnerability patterns.
pathfinder scan --ruleset python/PYTHON-LANG-SEC-093 --project .About This Rule
Understanding the vulnerability and how it is detected
Mako is a Python template engine used by web frameworks including Pyramid and TurboGears. Unlike some template engines with sandboxed expression evaluation, Mako templates execute Python expressions in the server's Python environment with no security sandbox.
Server-Side Template Injection (SSTI) occurs when user-supplied content is rendered as part of a template's code section (between <% %> or ${} delimiters) rather than being safely HTML-escaped as data. In Mako, injected template code can access __builtins__, import modules, and execute arbitrary Python.
The primary risks are: (1) using user input as the template string itself (mako.Template(user_input)) and (2) insufficient escaping allowing template syntax characters to reach template evaluation contexts.
Security Implications
Potential attack scenarios if this vulnerability is exploited
Server-Side Template Injection (SSTI)
If user-controlled content is passed as the template string to mako.Template(), or if user content containing ${} or <% %> is rendered in an expression context, an attacker can inject Python code executed on the server. Payloads like ${__import__("os").system("id")} achieve RCE.
Information Disclosure via Template Context
Mako templates have access to all variables in the template rendering context. Injected template code can access and exfiltrate any variable passed to template.render(), including database connections, session objects, configuration dictionaries, and request objects.
Sandbox Escape via Python Built-ins
Unlike Jinja2's sandboxed environment, Mako provides no sandbox. Injected Python in a Mako template can directly access __builtins__, __import__(), and any Python object reachable from the template context without restriction.
Cross-Site Scripting (XSS) via Insufficient Escaping
Mako's ${variable} expression auto-escapes HTML by default with the h filter, but developers who use ${ variable | n } (raw output) or construct HTML manually can bypass XSS protection, enabling reflected or stored XSS.
How to Fix
Recommended remediation steps
- 1Never use mako.Template(user_input) or any pattern where user input becomes the template string; load templates from a trusted directory using TemplateLookup.
- 2Always configure TemplateLookup with a fixed templates directory that only contains developer-controlled template files.
- 3Enable default_filters=["h"] on TemplateLookup to auto-escape HTML in all ${} expressions by default.
- 4Avoid the | n (no-escape) filter unless the template variable content has been explicitly validated as safe HTML.
- 5Pass all user data as template render() arguments, never concatenate user data into template strings.
Detection Scope
How Code Pathfinder analyzes your code for this vulnerability
This rule detects calls to mako.template.Template() constructor in Python source code. All call sites are flagged to prompt review of whether the template string argument is user-controlled, which would enable SSTI.
Compliance & Standards
Industry frameworks and regulations that require detection of this vulnerability
References
External resources and documentation
Similar Rules
Explore related security rules for Python
Dangerous eval() Usage Detected
eval() executes arbitrary Python expressions from strings, enabling remote code execution when called with untrusted input.
Dangerous exec() Usage Detected
exec() executes arbitrary Python statements from strings or code objects, enabling remote code execution when called with untrusted input.
Frequently Asked Questions
Common questions about Mako Template Usage Detected
New feature
Get these findings posted directly on your GitHub pull requests
The Mako Template Usage Detected rule runs in CI and posts inline review comments on the exact lines — no dashboard, no SARIF viewer.