The vulnerability is a Remote Code Execution (RCE) in n8n's expression evaluation mechanism, caused by an insufficient sandbox. The analysis of the provided patches shows that the core of the fix lies in the PrototypeSanitizer function located in packages/workflow/src/expression-sandboxing.ts.
This function operates on the Abstract Syntax Tree (AST) of the user-provided expression to detect and remove potentially malicious code before it gets executed. The patches harden this function by adding new security controls:
-
Disallowing with statements: A visitWithStatement method was added to the AST visitor within PrototypeSanitizer. This method throws an error whenever a with statement is encountered. The with statement in JavaScript can be abused to manipulate the scope chain, which was the vector for the RCE in this case. The test cases added in the patch confirm this with an exploit payload.
-
Preventing variable shadowing: The patch introduces checks within visitVariableDeclarator, visitFunction, and visitCatchClause to prevent the declaration of variables with reserved names like ___n8n_data and __sanitize. This prevents attackers from overwriting critical variables used by the sandboxing environment itself.
Before these changes, the PrototypeSanitizer function was inadequate, allowing malicious expressions to pass through the sandbox, leading to the RCE. Therefore, PrototypeSanitizer is the key function that was modified to mitigate the vulnerability and would be present in the execution stack when a malicious expression is evaluated.