FoF Pretty Mail 1.1.2 - Server Side Template Injection (SSTI)
Exploit Title: FoF Pretty Mail 1.1.2 - Server Side Template Injection (SSTI)
Date: 03/28/2024
Exploit Author: Chokri Hammedi
Vendor Homepage: https://flarum.org/
Software Link: https://github.com/FriendsOfFlarum/pretty-mail
Version: 1.1.2
Tested on: Windows XP
CVE: N/A
Description:
The FoF Pretty Mail extension for Flarum is vulnerable to Server-Side
Template Injection (SSTI) due to the unsafe handling of template variables.
An attacker with administrative access can inject malicious code into the
email template, leading to arbitrary code execution on the server.
Steps to Reproduce:
- Log in as an administrator on the Flarum forum.
- Navigate to the FoF Pretty Mail extension settings.
- Edit the email default template and insert the following payload:
{{ 7*7 }}
{{ system('id') }}
{{ system('echo "Take The Rose"') }}
- Save the changes to the email template.
- Trigger any action that sends an email, such as user registration or password reset.
- The recipient of the email will see the result of the injected expressions (e.g., "49" for {{ 7*7 }}, the output of the id command for {{ system('id') }}, and the output of the echo "Take The Rose" command for {{ system('echo"Take The Rose"') }}) in the email content. FoF Pretty Mail 1.1.2 — Server-Side Template Injection (SSTI)
This article analyzes a reported Server-Side Template Injection (SSTI) vulnerability in the FoF Pretty Mail extension for Flarum (version 1.1.2). It explains the vulnerability class, likely root causes, risk and impact, non-actionable detection techniques, and robust mitigation and remediation strategies for developers and administrators. The goal is to inform defenders and maintainers so they can eliminate the issue and harden deployments.
What is Server-Side Template Injection (SSTI)?
SSTI occurs when user-controllable input is interpreted by a server-side template engine in a way that allows evaluation of arbitrary expressions or code. Template engines are used to generate dynamic output (HTML, email bodies, documents), and many support expression evaluation or filters. If untrusted content is injected into template source or template variables without proper sanitization or sandboxing, an attacker may cause the engine to evaluate expressions leading to information disclosure or remote code execution.
Why FoF Pretty Mail can be affected
- FoF Pretty Mail allows administrators to edit email templates that are rendered on the server and sent to users.
- If the template rendering path does not restrict the template language features (for example, by disabling unsafe functions or employing a sandbox) or accepts administrator-supplied template source without validation, a malicious template can evaluate expressions beyond simple formatting.
- Because templates are rendered on the server, evaluated expressions can leak system information or, in poorly protected environments, execute arbitrary commands.
High-level, non-actionable vulnerability summary
In affected versions, an administrative user can insert expressions into the configurable email template that are evaluated by the server-side template engine. Evaluated expressions may appear in outgoing emails, and in some configurations could reveal system data or enable code execution. The core issue is unsafe handling of template variables and lack of adequate sandboxing or restrictions on evaluated expressions.
Impact and risk
| Risk | Impact |
|---|---|
| Confidentiality | Exfiltration of system or environment information into email content (credentials, host details, file contents) |
| Integrity | Altered outgoing messages or template contents; potential for template manipulation |
| Availability | Possible execution of resource-intensive operations if expressions are unbounded |
| Authentication/Authorization | Requires administrative access to template editing in the reported case; however privilege escalation paths may increase exposure |
Detection and monitoring (defensive, non-actionable)
- Audit admin actions: monitor changes to extension settings and template edits. Alert on unexpected edits from admin accounts or new admin accounts.
- Inspect outgoing email content: look for unexpected outputs that look like evaluated expressions or system command output in the body of automated messages.
- Log template rendering errors and warnings. Template engines that encounter unexpected constructs may log stack traces or warnings — collect and review these logs.
- Search server logs for unusual subprocess invocations, shell usage, or program calls originating from the application process.
- Review file modification timestamps in the webroot and config directories to ensure templates or engine code weren't modified.
Mitigation and remediation
Defenders should prioritize eliminating the root cause and applying configuration changes that prevent template evaluation of arbitrary expressions. Recommended actions in order of priority:
- Upgrade: install the vendor's patch or upgrade to a fixed version when available. Check the extension's repository and vendor advisories first.
- Restrict privileges: limit which users can edit templates. Use least privilege — only trusted administrators should have template editing rights.
- Sandbox the template engine: configure the template renderer to run in a sandbox mode where functions, methods, and global objects are whitelisted rather than blacklisted.
- Disable arbitrary expression evaluation: avoid rendering raw admin-provided template source if evaluation is unnecessary. Prefer parameterized templates where admin input is only used as data, not as template code.
- Input validation and escaping: validate template inputs and escape user-supplied variables when inserting into templates so that they cannot be interpreted as expressions by the rendering engine.
- Runtime restrictions: drop privileges of the process that performs rendering, run in containerized or chrooted environments, and apply OS-level controls (AppArmor/SELinux) to limit impact if code execution occurs.
- Logging and alerting: centralize logs, create alerts for sensitive template edits, unexpected render outputs, or unusual process activity.
Safe implementation patterns (examples)
The following examples are defensive patterns. They do not demonstrate exploit payloads. They show how to render templates safely by separating template code from data and by using a sandbox where supported.
// Pseudocode: Use a safe, precompiled template and pass admin content as data,
// rather than compiling admin-supplied template source at runtime.
// Unsafe (do not use): compiling arbitrary admin template source
$template_source = get_admin_template(); // admin-provided template body
$renderer->compileAndRender($template_source, $context);
// Safer: use a predefined template with placeholders and treat admin input as plain text
$base_template = load_predefined_template('email_base'); // developer-controlled template
$admin_text = get_admin_text(); // content that should be treated as data
$context['custom_text'] = escape_for_email($admin_text);
$renderer->render($base_template, $context);
Explanation: The safer approach uses a developer-maintained template and inserts admin-provided content only as data (escaped for the email context). This prevents the renderer from interpreting admin content as executable template code.
Sandboxing example (conceptual)
Many template engines provide sandboxing or configuration options to limit available functions, filters and objects. Here is a conceptual example showing the idea of restricting features (this is an illustration — adapt to the specific template engine and platform in use):
// Conceptual: initialize renderer with a restricted environment
$sandbox = new TemplateSandbox();
$sandbox->allowFilters(['escape', 'upper', 'lower']); // only safe filters
$sandbox->allowFunctions([]); // disallow all functions by default
$sandbox->allowMethodsOnTypes(['User' => ['getDisplayName']]); // whitelist safe methods
$renderer = new TemplateRenderer(['sandbox' => $sandbox]);
// Render only developer-defined templates
$template = $renderer->loadTemplate('standard_notification');
$renderer->render($template, $context);
Explanation: The sandbox is configured to deny arbitrary functions and to only permit a minimal set of filters/methods. Templates are then rendered within this constrained environment so that even if an admin-supplied string contains template-like constructs, it cannot invoke unsafe functionality.
Operational response and responsible disclosure
- If you identify a vulnerable instance, immediately limit access to template editing (temporarily revoke admin privileges if possible) and enable additional monitoring.
- Collect evidence (logs, changed templates, email samples) and preserve timestamps for forensic analysis.
- Follow responsible disclosure: contact the extension maintainers or vendor privately with a clear, reproducible description for administrators and operators only. Share proof-of-concept with maintainers under a non-public channel.
- Coordinate patch deployment and communicate with your user base if sensitive data may have been exposed.
Hardening checklist for Flarum + extensions
- Keep Flarum core and all extensions up to date. Subscribe to vendor advisories.
- Limit administrative accounts and use strong, multi-factor authentication.
- Apply principle of least privilege to extension settings and admin features.
- Use content sanitization and escaping for any content that is inserted into rendered outputs.
- Run web applications in a hardened runtime environment (containers, minimal OS, AppArmor/SELinux).
- Regularly audit installed extensions for unsafe patterns such as runtime evaluation of user-supplied templates.
Conclusion
Server-Side Template Injection in email template features is a serious class of vulnerability because it bridges application logic and server-side evaluation. For FoF Pretty Mail or similar extensions, the reliable defenses are: avoid compiling admin-supplied template source, sandbox template rendering, restrict template editing privileges, and promptly apply vendor patches. Combining these controls with logging and runtime containment significantly reduces the likelihood and impact of SSTI exploits.
References and further reading
- OWASP — Server-Side Template Injection cheat sheet
- Official extension repository and changelogs — check vendor advisories for fixed versions
- Template engine documentation — consult sandboxing or security sections for your specific renderer