Remote Code Execution via SSTI in mcp-contextforge-gateway
An authenticated Server-Side Template Injection (SSTI) vulnerability in mcp-contextforge-gateway version 0.9.0 and earlier allows attackers to achieve Remote Code Execution via unsandboxed Jinja2 template rendering.
The mcp-contextforge-gateway service, up to version 0.9.0, is vulnerable to Server-Side Template Injection (SSTI) due to the use of a plain, unsandboxed Jinja2 Environment for rendering user-supplied prompt templates. The vulnerability exists within mcpgateway/services/prompt_service.py in the _render_template method. By registering or updating a prompt template via the gateway's REST API, an authenticated user can inject arbitrary Jinja2 syntax. Because the environment does not restrict attribute traversal or function calls, an attacker can access Python built-ins such as __builtins__.__import__ to execute arbitrary commands. This allows for full host compromise, including access to environment variables, credentials, and persistent modifications to the gateway host. This issue was resolved in version 1.0.0 by migrating to a jinja2.sandbox.SandboxedEnvironment.
Attack Chain
- Attacker obtains valid credentials for the mcp-contextforge-gateway service with permissions to register or update prompt templates.
- Attacker crafts a malicious Jinja2 payload utilizing the
self.__init__.__globals__.__builtins__chain to reach theosmodule. - Attacker submits the crafted template content to the gateway via the
POST /promptsorPUT /prompts/{id}REST API endpoint. - The gateway stores the malicious template string in its backend database.
- The attacker or another process triggers the
prompts/getflow through the gateway. - The
PromptService.get_promptmethod calls_render_templatewith the stored, malicious template. - The unsandboxed
jinja2.Environmentinterprets and executes the embedded Python commands at render-time. - The attacker achieves Remote Code Execution with the permissions of the gateway service process.
Impact
Successful exploitation results in full host compromise under the security context of the gateway process. Attackers gain the ability to read or modify files on the gateway host, exfiltrate sensitive environment variables (e.g., API keys, database credentials), and move laterally within the network. In multi-tenant environments, a single compromised tenant account can lead to a complete takeover of the gateway, affecting all other tenants.
Recommendation
- Upgrade all instances of
mcp-contextforge-gatewayto version 1.0.0 or higher immediately to address the use of the unsandboxed Jinja2 environment. - Audit prompt templates currently stored in the gateway database for suspicious Jinja2 syntax or external references.
- Review access logs for the
POST /promptsandPUT /prompts/{id}endpoints to identify unauthorized or anomalous template registration activity. - Restrict permissions for registering or updating prompt templates to a limited set of trusted users to reduce the attack surface.
Immediate actions
Upgrade mcp-contextforge-gateway to >=1.0.0
Deploy SSTI detection rule
Threat Hunt
Search logs for registration of templates containing __builtins__ or os.popen
Data: Web server logs
Mitigations
Update to version 1.0.0
mcp-contextforge-gateway < 1.0.0
Detection coverage 1
Detect Exploitation of SSTI in mcp-contextforge-gateway
highDetects potential SSTI exploitation attempts by monitoring for malicious Jinja2 template syntax in POST requests to the prompts API endpoint
Detection queries are available on the platform. Get full rules →