Summary
In affected versions of n8n, the Merge node's SQL query mode allows an authenticated workflow creator or editor to craft queries that lead to arbitrary code execution and arbitrary file writes on the n8n server.
This becomes risky in any deployment where users with workflow creation or modification rights are not fully trusted, effectively turning workflow configuration into a code execution primitive on the backend.
Affected context
This vulnerability arises in n8n instances where:
- The Merge node is available and its SQL query mode is enabled in workflows.
- Users are granted permissions to create or modify workflows, including adding or configuring Merge nodes.
- The n8n server has access to underlying system resources (filesystem, OS, and potentially network) typical for its runtime.
The risk is pronounced in multi-tenant or shared environments where workflow authors are not strictly trusted or where internal accounts can be phished or taken over.
Exploit preconditions
- Attack surface: n8n application interface or API used to create or update workflows that include the Merge node.
- Required access: Valid authenticated account with permission to create or modify workflows (no need for direct system shell or DB access).
- Trigger point: Execution of a maliciously configured workflow that uses the Merge node in SQL query mode.
- Why exploitation is possible: The Merge node's SQL query mode fails to sufficiently constrain or sanitize query construction, enabling crafted inputs that break out of expected query logic and escalate to arbitrary code execution and file writes (CWE-89 and CWE-94).
Why this happens
The underlying issue maps to a combination of CWE-89: Improper Neutralization of Special Elements used in an SQL Command ('SQL Injection') and CWE-94: Improper Control of Generation of Code ('Code Injection').
Within the Merge node's SQL query mode, user-controlled workflow configuration is effectively treated as trusted SQL and/or execution logic without robust parameterization or isolation. This allows a malicious workflow author to inject additional SQL or execution constructs that the backend interprets as legitimate instructions.
Conceptually, the bug class is:
- Treating workflow configuration values as safe query/program fragments instead of data.
- Allowing those fragments to influence control flow and low-level database or execution primitives.
- Executing the resulting queries or code in the security context of the n8n server process, which typically has access to the filesystem and other sensitive resources.
Potential impact
- An authenticated user may execute arbitrary code on the n8n server.
- Attackers can write arbitrary files to the server's filesystem, potentially planting backdoors or modifying application behavior.
- Compromise of all workflows, credentials, and secrets stored or accessible via the n8n instance.
- Lateral movement into connected systems (databases, APIs, internal services) that n8n has permissions to access.
- Full takeover of the n8n host if the process privileges and environment allow escalation.
General mitigation guidance
Effective mitigation centers on preventing user-controlled workflow configuration from becoming executable SQL or code:
- Use strict parameterization for all database and query-building operations within workflow nodes; treat configuration values as data, never as query fragments.
- Introduce clear separation between "data inputs" and "execution logic" in node configuration, disallowing arbitrary expressions in query strings where possible.
- Restrict powerful node capabilities (such as SQL execution or file writes) behind permission scopes or role-based access control aligned to least privilege.
- Enforce server-side validation of workflow definitions to detect and reject unsafe query patterns or dynamic code injection constructs.
- Run the n8n server with reduced OS privileges, isolated service accounts, and minimal filesystem write access to limit blast radius.
- Where feasible, sandbox execution contexts for nodes that interact with external databases or the filesystem so that injected logic cannot escape its boundary.
How teams detect issues like this
Detection typically involves reviewing both code and workflow configurations for unsafe patterns:
- Static analysis and code review:
- Flag any node implementation that concatenates user-supplied strings into SQL queries or other interpretable command/code formats.
- Search for generic "query" fields that are passed directly to database or execution APIs without parameterization.
- Examine file-writing utilities and ensure they cannot be fully controlled via workflow configuration without checks.
- Workflow configuration review:
- Periodically scan stored workflows for suspicious Merge node configurations, such as queries containing unexpected control keywords, system-specific functions, or file paths.
- Implement policy-based checks in CI/CD or deployment pipelines when workflows are defined as code, rejecting risky query patterns.
- Runtime monitoring:
- Instrument logging for Merge node usage and correlate with anomalous database queries, file writes, or OS-level alerts.
- Use host-based intrusion detection or EDR solutions to detect abnormal processes, file modifications, or command execution originating from the n8n process.
Automated SAST/IAST tools and configuration linters can assist by enforcing rules around query construction and privileged node usage.

