Trestle Server-Side Template Injection via Custom Jinja2 Extensions
The Trestle command-line tool is vulnerable to Server-Side Template Injection (SSTI) due to the unsafe re-evaluation of untrusted Markdown content as Jinja2 template code.
Trestle contains multiple Server-Side Template Injection (SSTI) vulnerabilities within its Jinja2 rendering pipeline. The application processes Markdown files and other data sources using custom Jinja2 extensions (MDCleanInclude and MDSectionInclude). These extensions improperly treat untrusted content as Jinja2 template source code by passing it directly to the jinja2.Parser object without adequate sanitization or sandboxing. Because the environment utilizes a standard jinja2.Environment rather than a SandboxedEnvironment, attackers can inject malicious Jinja2 expressions, such as object traversal payloads (e.g., __class__.__mro__, __globals__), to achieve arbitrary command execution via Python's os.system or subprocess modules. This pattern exists within trestle/core/jinja/tags.py and is triggered whenever a user-provided or workspace-modified Markdown file is processed by the Trestle authoring commands.
Attack Chain
- Attacker places a malicious
.mdfile containing a Jinja2 payload (e.g.,{{ ... os.system(...) }}) into the Trestle workspace. - Attacker executes the
trestle author jinjaCLI command, targeting a legitimate template that utilizes the vulnerable{% md_clean_include %}or{% mdsection_include %}tags. - The Trestle engine loads the malicious file from the filesystem via
FileSystemLoader. - The
MDCleanIncludeorMDSectionIncludetag handler processes the file content, extracting the Markdown body without sanitization. - The extracted content is passed as raw string input to the
Parserconstructor intrestle/core/jinja/tags.py. - The
Parser.parse()method triggers the evaluation of the injected Jinja2 syntax within the template context. - The injected Python payload executes with the privileges of the Trestle process, leading to full Remote Code Execution (RCE) or sensitive data exfiltration.
Impact
Successful exploitation allows for arbitrary code execution on the system running the Trestle command. If used in automated CI/CD pipelines, this can result in the compromise of build environments, leakage of environment variables (e.g., API keys, AWS credentials), or lateral movement within the infrastructure.
Recommendation
- Immediately restrict write access to all Trestle workspace directories to trusted users to prevent the introduction of malicious Markdown files.
- Patch the application code by modifying
trestle/core/jinja/tags.pyto stop re-parsing Markdown content viaParser.parse(), replacing it withnodes.TemplateDataas suggested by the security advisory. - Transition from
jinja2.Environmenttojinja2.sandbox.SandboxedEnvironmentintrestle/core/commands/author/jinja.pyto restrict access to sensitive Python object attributes. - Implement a pre-commit hook or CI scanning gate to audit all workspace files for Jinja2 syntax patterns and dangerous Python method calls (e.g.,
__globals__,os.system,subprocess).
Immediate actions
Restrict write access to Trestle workspace directories to mitigate unauthorized file injection.
Mitigations
Modify trestle/core/jinja/tags.py to remove recursive Parser.parse() calls and adopt SandboxedEnvironment.
SSTI vulnerability in MDCleanInclude and MDSectionInclude