first commit
Security: Sync from Public / sync-from-public (push) Has been cancelled
Test: Benchmark Nightly / build (push) Has been cancelled
Test: Benchmark Nightly / Notify Cats on failure (push) Has been cancelled
CI: Python / Checks (push) Has been cancelled
Test: Evals Python / Workflow Comparison Python (push) Has been cancelled
Util: Check Docs URLs / check-docs-urls (push) Has been cancelled
Test: Visual Storybook / Cloudflare Pages (push) Has been cancelled
Test: E2E Performance / build-and-test-performance (push) Has been cancelled
Test: Workflows Nightly / Run Workflow Tests (push) Has been cancelled
Util: Cleanup CI Docker Images / Delete stale CI images (push) Has been cancelled
Test: Benchmark Destroy Env / build (push) Has been cancelled
Util: Update Node Popularity / update-popularity (push) Has been cancelled
Test: E2E Coverage Weekly / Coverage Tests (push) Has been cancelled
Security: Sync from Public / sync-from-public (push) Has been cancelled
Test: Benchmark Nightly / build (push) Has been cancelled
Test: Benchmark Nightly / Notify Cats on failure (push) Has been cancelled
CI: Python / Checks (push) Has been cancelled
Test: Evals Python / Workflow Comparison Python (push) Has been cancelled
Util: Check Docs URLs / check-docs-urls (push) Has been cancelled
Test: Visual Storybook / Cloudflare Pages (push) Has been cancelled
Test: E2E Performance / build-and-test-performance (push) Has been cancelled
Test: Workflows Nightly / Run Workflow Tests (push) Has been cancelled
Util: Cleanup CI Docker Images / Delete stale CI images (push) Has been cancelled
Test: Benchmark Destroy Env / build (push) Has been cancelled
Util: Update Node Popularity / update-popularity (push) Has been cancelled
Test: E2E Coverage Weekly / Coverage Tests (push) Has been cancelled
This commit is contained in:
@@ -0,0 +1,123 @@
|
||||
import { NodeVM, makeResolverFromLegacyOptions, type Resolver } from 'vm2';
|
||||
import type { IExecuteFunctions, INodeExecutionData } from 'n8n-workflow';
|
||||
|
||||
import { ExecutionError } from './ExecutionError';
|
||||
import {
|
||||
mapItemNotDefinedErrorIfNeededForRunForEach,
|
||||
mapItemsNotDefinedErrorIfNeededForRunForAll,
|
||||
validateNoDisallowedMethodsInRunForEach,
|
||||
} from './JsCodeValidator';
|
||||
import type { SandboxContext } from './Sandbox';
|
||||
import { Sandbox } from './Sandbox';
|
||||
import { ValidationError } from './ValidationError';
|
||||
|
||||
const { NODE_FUNCTION_ALLOW_BUILTIN: builtIn, NODE_FUNCTION_ALLOW_EXTERNAL: external } =
|
||||
process.env;
|
||||
|
||||
export const vmResolver = makeResolverFromLegacyOptions({
|
||||
external: external
|
||||
? {
|
||||
modules: external.split(','),
|
||||
transitive: false,
|
||||
}
|
||||
: false,
|
||||
builtin: builtIn?.split(',') ?? [],
|
||||
});
|
||||
|
||||
export class JavaScriptSandbox extends Sandbox {
|
||||
private readonly vm: NodeVM;
|
||||
|
||||
constructor(
|
||||
context: SandboxContext,
|
||||
private jsCode: string,
|
||||
helpers: IExecuteFunctions['helpers'],
|
||||
options?: { resolver?: Resolver },
|
||||
) {
|
||||
super(
|
||||
{
|
||||
object: {
|
||||
singular: 'object',
|
||||
plural: 'objects',
|
||||
},
|
||||
},
|
||||
helpers,
|
||||
);
|
||||
this.vm = new NodeVM({
|
||||
console: 'redirect',
|
||||
sandbox: context,
|
||||
require: options?.resolver ?? vmResolver,
|
||||
wasm: false,
|
||||
});
|
||||
|
||||
this.vm.on('console.log', (...args: unknown[]) => this.emit('output', ...args));
|
||||
}
|
||||
|
||||
async runCode<T = unknown>(): Promise<T> {
|
||||
const script = `module.exports = async function() {${this.jsCode}\n}()`;
|
||||
try {
|
||||
const executionResult = (await this.vm.run(script, __dirname)) as T;
|
||||
return executionResult;
|
||||
} catch (error) {
|
||||
throw new ExecutionError(error);
|
||||
}
|
||||
}
|
||||
|
||||
async runCodeAllItems(options?: {
|
||||
multiOutput?: boolean;
|
||||
}): Promise<INodeExecutionData[] | INodeExecutionData[][]> {
|
||||
const script = `module.exports = async function() {${this.jsCode}\n}()`;
|
||||
|
||||
let executionResult: INodeExecutionData | INodeExecutionData[] | INodeExecutionData[][];
|
||||
|
||||
try {
|
||||
executionResult = await this.vm.run(script, __dirname);
|
||||
} catch (error) {
|
||||
// anticipate user expecting `items` to pre-exist as in Function Item node
|
||||
mapItemsNotDefinedErrorIfNeededForRunForAll(this.jsCode, error);
|
||||
|
||||
throw new ExecutionError(error);
|
||||
}
|
||||
|
||||
if (executionResult === null) return [];
|
||||
|
||||
if (options?.multiOutput === true) {
|
||||
// Check if executionResult is an array of arrays
|
||||
if (!Array.isArray(executionResult) || executionResult.some((item) => !Array.isArray(item))) {
|
||||
throw new ValidationError({
|
||||
message: "The code doesn't return an array of arrays",
|
||||
description:
|
||||
'Please return an array of arrays. One array for the different outputs and one for the different items that get returned.',
|
||||
});
|
||||
}
|
||||
|
||||
return executionResult.map((data) => {
|
||||
return this.validateRunCodeAllItems(data);
|
||||
});
|
||||
}
|
||||
|
||||
return this.validateRunCodeAllItems(
|
||||
executionResult as INodeExecutionData | INodeExecutionData[],
|
||||
);
|
||||
}
|
||||
|
||||
async runCodeEachItem(itemIndex: number): Promise<INodeExecutionData | undefined> {
|
||||
const script = `module.exports = async function() {${this.jsCode}\n}()`;
|
||||
|
||||
validateNoDisallowedMethodsInRunForEach(this.jsCode, itemIndex);
|
||||
|
||||
let executionResult: INodeExecutionData;
|
||||
|
||||
try {
|
||||
executionResult = await this.vm.run(script, __dirname);
|
||||
} catch (error) {
|
||||
// anticipate user expecting `item` to pre-exist as in Function Item node
|
||||
mapItemNotDefinedErrorIfNeededForRunForEach(this.jsCode, error);
|
||||
|
||||
throw new ExecutionError(error, itemIndex);
|
||||
}
|
||||
|
||||
if (executionResult === null) return undefined;
|
||||
|
||||
return this.validateRunCodeEachItem(executionResult, itemIndex);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user