-
Description: An attacker crafts malicious input that, when incorporated into a Jinja template, is interpreted as Jinja syntax rather than plain text. The attacker uses Jinja delimiters (e.g.,
{{ ... }}
,{% ... %}
) to inject Python code, which is then executed on the server during template rendering. The attacker aims to execute system commands, read files, or otherwise compromise the server. -
Impact: Complete server compromise (Remote Code Execution - RCE), data exfiltration, denial of service, information disclosure.
-
Jinja Component Affected:
Environment.from_string()
: If user input is passed directly.Template.render()
: When rendering a template with unsanitized user input.render_template_string()
(Flask-specific, uses Jinja): Highly dangerous with user-provided strings.- Any function loading/rendering templates if content/variables are user-influenced.
-
Risk Severity: Critical
-
Mitigation Strategies:
- Strict Input Validation: Validate and sanitize all user input before template use. Whitelist (allow only known-good) instead of blacklist.
- Context-Aware Auto-Escaping: Enable and correctly configure Jinja's auto-escaping. Understand its limitations; manually escape where needed.
- Avoid
render_template_string
with User Input: Prefer file-based templates. If unavoidable, treat input as highly untrusted. - Sandboxing (Limited): Use
SandboxedEnvironment
, but do not rely on it solely. Bypasses are possible. - Least Privilege: Run the application with minimal necessary privileges.
- Regular Updates: Keep Jinja and dependencies updated.
-
Description: The attacker uses Jinja syntax to access and exfiltrate sensitive data available within the template's context (e.g.,
{{ config.SECRET_KEY }}
). This is a variation of SSTI, focusing on data exposure rather than code execution. -
Impact: Exposure of sensitive data (API keys, database credentials, internal data), leading to further attacks.
-
Jinja Component Affected: Same as SSTI - Code Execution. The vulnerability is in how user input is handled within any template rendering function.
-
Risk Severity: High
-
Mitigation Strategies: Same as SSTI - Code Execution. Preventing any template injection prevents both.
Threat: Using Untrusted Template Sources
-
Description: The application loads Jinja templates from an untrusted source (user-uploaded files, attacker-controlled database fields, external URLs). The attacker provides a malicious template with arbitrary Jinja code.
-
Impact: Identical to successful SSTI (code execution, data leakage).
-
Jinja Component Affected:
FileSystemLoader
: If configured to load from an untrusted directory.PackageLoader
: If the package/module path is user-influenced.DictLoader
: If the template dictionary is from untrusted data.FunctionLoader
: If the function returns untrusted content.ChoiceLoader
orPrefixLoader
: If underlying loaders use untrusted sources.Environment.from_string()
: If the template string is from an untrusted source.
-
Risk Severity: Critical
-
Mitigation Strategies:
- Load Templates from Trusted Locations Only: Use the application's filesystem, a controlled, secure directory.
- Never Load Templates from User Input: Do not allow uploads or path specifications.
- Avoid Dynamic Template Loading: Avoid loading based on user input or external data.
- Template Content Validation (Extremely Difficult): If absolutely necessary, implement extremely rigorous validation before rendering. Avoid if possible.