Summary
A flaw in n8n's expression compiler sanitizer allows a specially crafted class field named __sanitize to rebind the sanitizer and reach the JavaScript Function constructor. Depending on the execution path and available privileges, this may enable backend code execution and JavaScript execution in the editor preview.
Affected context
The issue occurs in n8n expression compilation, specifically in the PrototypeSanitizer AST hook within the workflow expression-sandboxing logic. It becomes risky when attacker-influenced expressions or class structures are compiled through the affected sanitizer and can define reserved member names that alter how sanitization is resolved. The practical exposure depends on who can create or modify workflows and which execution or preview paths are enabled.
Exploit preconditions
- Attack surface: n8n workflow expressions and editor-preview evaluation
- Required access: The ability to supply or modify expression content or related class structures processed by the compiler
- Trigger point: Expression compilation or execution involving the affected sanitizer
- Why exploitation is possible: Dynamic resolution through
thisallows a reserved class member named__sanitizeto rebind the sanitizer and expose theFunctionconstructor
Why this happens
The vulnerability results from an expression sanitizer that resolves through dynamically scoped this without rejecting reserved class member names. This permits attacker-controlled class structure to replace or influence the sanitizer callback, allowing compilation behavior to reach a dynamic code-construction primitive. The issue maps to CWE-94: Improper Control of Generation of Code, because untrusted input can influence the generation or execution of JavaScript code.
Potential impact
- An attacker could execute arbitrary code in the n8n backend process
- An attacker could execute JavaScript in an editor-preview context
- Workflow credentials, stored data, or connected service access could be exposed
- The n8n host or surrounding environment could be compromised, depending on process privileges
- Workflows or automation data could be modified or abused
General mitigation guidance
Mitigation focuses on preventing attacker-controlled syntax from influencing sanitizer bindings or dynamic code-generation paths.
Common defensive patterns include:
- Rejecting reserved and security-sensitive member names during parsing and AST validation
- Resolving sanitizer functions through fixed lexical bindings rather than dynamically scoped
this - Eliminating access to the
Functionconstructor and comparable dynamic evaluation primitives - Applying allowlisted expression grammars and strict AST validation before compilation
- Separating editor-preview execution from backend privileges and sensitive runtime capabilities
- Running workflow processes with least privilege and restricting access to credentials and host resources
How teams detect issues like this
Organizations can look for this class of issue through:
- Static analysis rules identifying dynamic
thisresolution in sanitizers or security hooks - Searches for reachable use of
Function,eval, or equivalent dynamic code-generation APIs - Code review checks for missing rejection of reserved class member names
- Tests that compile adversarial AST structures and verify sanitizer bindings cannot be replaced
- Policy enforcement requiring allowlisted expression grammars and isolated preview execution
- Runtime monitoring for unexpected child processes, filesystem access, or outbound connections from n8n workers
Automated security review tools may help identify dynamic evaluation and unsafe AST-handling patterns, but should be supplemented with targeted sandbox tests.

