2023-11-29 03:13:55 -08:00
|
|
|
import { NodeVM, makeResolverFromLegacyOptions, type Resolver } from '@n8n/vm2';
|
2023-08-01 08:47:43 -07:00
|
|
|
import type { IExecuteFunctions, INodeExecutionData } from 'n8n-workflow';
|
2023-05-04 11:00:00 -07:00
|
|
|
|
|
|
|
import { ValidationError } from './ValidationError';
|
|
|
|
import { ExecutionError } from './ExecutionError';
|
|
|
|
import type { SandboxContext } from './Sandbox';
|
|
|
|
import { Sandbox } from './Sandbox';
|
2024-10-07 11:18:32 -07:00
|
|
|
import {
|
|
|
|
mapItemNotDefinedErrorIfNeededForRunForEach,
|
|
|
|
mapItemsNotDefinedErrorIfNeededForRunForAll,
|
|
|
|
validateNoDisallowedMethodsInRunForEach,
|
|
|
|
} from './JsCodeValidator';
|
2023-05-04 11:00:00 -07:00
|
|
|
|
|
|
|
const { NODE_FUNCTION_ALLOW_BUILTIN: builtIn, NODE_FUNCTION_ALLOW_EXTERNAL: external } =
|
|
|
|
process.env;
|
|
|
|
|
2023-05-25 03:55:53 -07:00
|
|
|
export const vmResolver = makeResolverFromLegacyOptions({
|
|
|
|
external: external
|
|
|
|
? {
|
|
|
|
modules: external.split(','),
|
|
|
|
transitive: false,
|
2024-03-26 06:22:57 -07:00
|
|
|
}
|
2023-05-25 03:55:53 -07:00
|
|
|
: false,
|
|
|
|
builtin: builtIn?.split(',') ?? [],
|
|
|
|
});
|
|
|
|
|
2023-05-04 11:00:00 -07:00
|
|
|
export class JavaScriptSandbox extends Sandbox {
|
2023-08-01 08:47:43 -07:00
|
|
|
private readonly vm: NodeVM;
|
2023-05-04 11:00:00 -07:00
|
|
|
|
|
|
|
constructor(
|
|
|
|
context: SandboxContext,
|
|
|
|
private jsCode: string,
|
|
|
|
helpers: IExecuteFunctions['helpers'],
|
2023-11-29 03:13:55 -08:00
|
|
|
options?: { resolver?: Resolver },
|
2023-05-04 11:00:00 -07:00
|
|
|
) {
|
|
|
|
super(
|
|
|
|
{
|
|
|
|
object: {
|
|
|
|
singular: 'object',
|
|
|
|
plural: 'objects',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
helpers,
|
|
|
|
);
|
2023-08-01 08:47:43 -07:00
|
|
|
this.vm = new NodeVM({
|
|
|
|
console: 'redirect',
|
|
|
|
sandbox: context,
|
2023-11-29 03:13:55 -08:00
|
|
|
require: options?.resolver ?? vmResolver,
|
2023-09-06 07:11:39 -07:00
|
|
|
wasm: false,
|
2023-08-01 08:47:43 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
this.vm.on('console.log', (...args: unknown[]) => this.emit('output', ...args));
|
2023-05-04 11:00:00 -07:00
|
|
|
}
|
|
|
|
|
2024-10-07 11:18:32 -07:00
|
|
|
async runCode<T = unknown>(): Promise<T> {
|
2023-11-29 03:13:55 -08:00
|
|
|
const script = `module.exports = async function() {${this.jsCode}\n}()`;
|
|
|
|
try {
|
2024-10-07 11:18:32 -07:00
|
|
|
const executionResult = (await this.vm.run(script, __dirname)) as T;
|
2023-11-29 03:13:55 -08:00
|
|
|
return executionResult;
|
|
|
|
} catch (error) {
|
|
|
|
throw new ExecutionError(error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async runCodeAllItems(options?: {
|
|
|
|
multiOutput?: boolean;
|
|
|
|
}): Promise<INodeExecutionData[] | INodeExecutionData[][]> {
|
2023-05-04 11:00:00 -07:00
|
|
|
const script = `module.exports = async function() {${this.jsCode}\n}()`;
|
|
|
|
|
2023-11-29 03:13:55 -08:00
|
|
|
let executionResult: INodeExecutionData | INodeExecutionData[] | INodeExecutionData[][];
|
2023-05-04 11:00:00 -07:00
|
|
|
|
|
|
|
try {
|
|
|
|
executionResult = await this.vm.run(script, __dirname);
|
|
|
|
} catch (error) {
|
|
|
|
// anticipate user expecting `items` to pre-exist as in Function Item node
|
2024-10-07 11:18:32 -07:00
|
|
|
mapItemsNotDefinedErrorIfNeededForRunForAll(this.jsCode, error);
|
2023-05-04 11:00:00 -07:00
|
|
|
|
|
|
|
throw new ExecutionError(error);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (executionResult === null) return [];
|
|
|
|
|
2023-11-29 03:13:55 -08:00
|
|
|
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[],
|
|
|
|
);
|
2023-05-04 11:00:00 -07:00
|
|
|
}
|
|
|
|
|
2024-10-07 11:18:32 -07:00
|
|
|
async runCodeEachItem(itemIndex: number): Promise<INodeExecutionData | undefined> {
|
2023-05-04 11:00:00 -07:00
|
|
|
const script = `module.exports = async function() {${this.jsCode}\n}()`;
|
|
|
|
|
2024-10-07 11:18:32 -07:00
|
|
|
validateNoDisallowedMethodsInRunForEach(this.jsCode, itemIndex);
|
2023-05-04 11:00:00 -07:00
|
|
|
|
|
|
|
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
|
2024-10-07 11:18:32 -07:00
|
|
|
mapItemNotDefinedErrorIfNeededForRunForEach(this.jsCode, error);
|
2023-05-04 11:00:00 -07:00
|
|
|
|
2024-10-07 11:18:32 -07:00
|
|
|
throw new ExecutionError(error, itemIndex);
|
2023-05-04 11:00:00 -07:00
|
|
|
}
|
|
|
|
|
2024-10-07 11:18:32 -07:00
|
|
|
if (executionResult === null) return undefined;
|
2023-05-04 11:00:00 -07:00
|
|
|
|
2024-10-07 11:18:32 -07:00
|
|
|
return this.validateRunCodeEachItem(executionResult, itemIndex);
|
2023-05-04 11:00:00 -07:00
|
|
|
}
|
|
|
|
}
|