Summary
RAGFlow's Agent workflow Text Processing (StringTransform) and Message components render user-supplied templates using an unsandboxed Jinja2 engine, leading to server-side template injection.
Any authenticated user who can define or edit these templates may execute arbitrary operating system commands on the RAGFlow server.
Affected context
This vulnerability occurs in RAGFlow deployments where:
- The Agent workflow's Text Processing (StringTransform) and Message components are enabled and used.
- Authenticated users are allowed to create, edit, or otherwise control template strings for these components.
- Templates are rendered on the server using Python's
jinja2.Templatewithout sandboxing or strict filters.
The risk increases in multi-user or shared environments where less-trusted users can define workflows or text processing steps that are executed by a central RAGFlow backend.
Exploit preconditions
- Attack surface: RAGFlow web UI, API, or any interface that allows users to configure Agent workflows or template-based text transformations.
- Required access: Valid authenticated account with permissions to create or modify workflows that use the Text Processing (StringTransform) or Message components.
- Trigger point: Rendering of a maliciously crafted template string by the backend Jinja2 engine during workflow execution.
- Why exploitation is possible: The system passes user-controlled template strings directly into an unsandboxed
jinja2.Template, enabling access to Python internals and shell execution primitives, which maps to server-side template injection and command execution.
Why this happens
The core issue is the direct use of Jinja2's template rendering (jinja2.Template) on user-supplied strings without sandboxing, whitelisting, or restricting the available template environment.
This maps to:
- CWE-1336: Improper Neutralization of Special Elements Used in a Template Engine — user-provided template content is treated as trusted code.
- CWE-94: Improper Control of Generation of Code — template expressions can resolve to executable Python constructs.
- CWE-78: Improper Neutralization of Special Elements used in an OS Command — once the template can reach OS command APIs, shell commands can be issued.
- CWE-20: Improper Input Validation — there is no validation or constraint on the structure or capabilities of the templates.
Conceptually, the design assumes templates are benign "format strings," but Jinja2 templates are powerful mini-programs; exposing that power to untrusted users without sandboxing results in code execution.
Potential impact
- Execution of arbitrary operating system commands under the RAGFlow server's runtime account.
- Reading, modifying, or deleting data accessible to the application, including model prompts, documents, or workflow definitions.
- Pivoting to other internal services reachable from the RAGFlow server (e.g., databases, vector stores, or internal APIs).
- Interference with RAG pipelines, such as data poisoning, prompt tampering, or sabotaging retrieval logic.
- Potential full server compromise if the RAGFlow process holds sensitive credentials or elevated privileges.
General mitigation guidance
Defensive strategies should focus on reducing template engine capabilities, shrinking trust boundaries, and isolating execution:
- Prefer safe, limited template engines or formatting mechanisms for user-supplied content (e.g., basic variable substitution without logic or arbitrary attribute access).
- If Jinja2 must be used, configure a sandboxed environment that:
- Disallows access to Python internals, file system, and OS interfaces.
- Restricts filters, tests, and globals to a minimal, audited set.
- Implement strict template validation and normalization:
- Allow only specific tags and expressions (e.g., no function calls, imports, or attribute chains beyond approved objects).
- Reject or strip dangerous constructs as a class, not based on blacklists of known payloads.
- Apply principle of least privilege on the RAGFlow server:
- Run the service under a non-privileged account with minimal system access.
- Restrict access to secrets, configuration files, and system utilities not required for core functionality.
- Introduce workflow trust separation:
- Treat user-authored templates from less-trusted users as untrusted data, and avoid executing them with powerful template environments.
- Use separate execution environments or worker roles for executing user-defined workflows.
How teams detect issues like this
Detection can combine code-focused and behavior-focused approaches:
- Static analysis / code scanning:
- Flag direct use of
jinja2.TemplateorEnvironment.from_stringon user-controlled input. - Highlight templating calls where the environment includes dangerous builtins, filters, or unrestricted globals.
- Secure code review:
- Look for workflow or text processing components that accept arbitrary template strings.
- Verify that any template rendering paths are sandboxed and that inputs are constrained to a safe subset of functionality.
- Runtime monitoring and logging:
- Enable detailed logging for workflow executions that include template rendering, capturing unusual template content or errors.
- Monitor for suspicious command invocations, process spawns, or file access originating from the RAGFlow service account.
- Policy and pipeline enforcement:
- Enforce security checks in CI/CD that disallow unsafe template patterns and require use of sandboxed template environments.
- Optionally use automated security tools that specialize in detecting template injection patterns in web and workflow engines.

