2023-01-27 03:22:44 -08:00
|
|
|
|
import { NodeVM } from 'vm2';
|
2022-10-13 05:28:02 -07:00
|
|
|
|
import { ValidationError } from './ValidationError';
|
|
|
|
|
import { ExecutionError } from './ExecutionError';
|
2023-01-27 03:22:44 -08:00
|
|
|
|
import { isObject, REQUIRED_N8N_ITEM_KEYS } from './utils';
|
2022-10-13 05:28:02 -07:00
|
|
|
|
|
2023-02-27 19:39:43 -08:00
|
|
|
|
import type {
|
|
|
|
|
IDataObject,
|
|
|
|
|
IExecuteFunctions,
|
|
|
|
|
INodeExecutionData,
|
|
|
|
|
IWorkflowDataProxyData,
|
|
|
|
|
WorkflowExecuteMode,
|
|
|
|
|
} from 'n8n-workflow';
|
2022-10-13 05:28:02 -07:00
|
|
|
|
|
2023-04-19 04:09:46 -07:00
|
|
|
|
interface SandboxContext extends IWorkflowDataProxyData {
|
|
|
|
|
$getNodeParameter: IExecuteFunctions['getNodeParameter'];
|
|
|
|
|
$getWorkflowStaticData: IExecuteFunctions['getWorkflowStaticData'];
|
|
|
|
|
helpers: IExecuteFunctions['helpers'];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const { NODE_FUNCTION_ALLOW_BUILTIN: builtIn, NODE_FUNCTION_ALLOW_EXTERNAL: external } =
|
|
|
|
|
process.env;
|
2022-12-02 12:54:28 -08:00
|
|
|
|
|
2023-04-19 04:09:46 -07:00
|
|
|
|
export class Sandbox extends NodeVM {
|
2022-10-13 05:28:02 -07:00
|
|
|
|
private itemIndex: number | undefined = undefined;
|
|
|
|
|
|
|
|
|
|
constructor(
|
2023-04-19 04:09:46 -07:00
|
|
|
|
context: SandboxContext,
|
|
|
|
|
private jsCode: string,
|
2022-10-13 05:28:02 -07:00
|
|
|
|
workflowMode: WorkflowExecuteMode,
|
2023-03-09 09:13:15 -08:00
|
|
|
|
private helpers: IExecuteFunctions['helpers'],
|
2022-10-13 05:28:02 -07:00
|
|
|
|
) {
|
2023-04-19 04:09:46 -07:00
|
|
|
|
super({
|
2022-10-13 05:28:02 -07:00
|
|
|
|
console: workflowMode === 'manual' ? 'redirect' : 'inherit',
|
|
|
|
|
sandbox: context,
|
|
|
|
|
require: {
|
|
|
|
|
builtin: builtIn ? builtIn.split(',') : [],
|
|
|
|
|
external: external ? { modules: external.split(','), transitive: false } : false,
|
|
|
|
|
},
|
2023-04-19 04:09:46 -07:00
|
|
|
|
});
|
2022-10-13 05:28:02 -07:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-19 04:09:46 -07:00
|
|
|
|
async runCodeAllItems(): Promise<INodeExecutionData[]> {
|
2022-10-13 05:28:02 -07:00
|
|
|
|
const script = `module.exports = async function() {${this.jsCode}\n}()`;
|
|
|
|
|
|
2023-04-19 04:09:46 -07:00
|
|
|
|
let executionResult: INodeExecutionData | INodeExecutionData[];
|
2022-10-13 05:28:02 -07:00
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
executionResult = await this.run(script, __dirname);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
// anticipate user expecting `items` to pre-exist as in Function Item node
|
|
|
|
|
if (error.message === 'items is not defined' && !/(let|const|var) items =/.test(script)) {
|
|
|
|
|
const quoted = error.message.replace('items', '`items`');
|
2023-01-13 09:11:56 -08:00
|
|
|
|
error.message = (quoted as string) + '. Did you mean `$input.all()`?';
|
2022-10-13 05:28:02 -07:00
|
|
|
|
}
|
|
|
|
|
|
2023-02-27 19:39:43 -08:00
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
|
2022-10-13 05:28:02 -07:00
|
|
|
|
throw new ExecutionError(error);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (executionResult === null) return [];
|
|
|
|
|
|
|
|
|
|
if (executionResult === undefined || typeof executionResult !== 'object') {
|
|
|
|
|
throw new ValidationError({
|
|
|
|
|
message: "Code doesn't return items properly",
|
|
|
|
|
description:
|
|
|
|
|
'Please return an array of objects, one for each item you would like to output',
|
|
|
|
|
itemIndex: this.itemIndex,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (Array.isArray(executionResult)) {
|
2022-11-22 05:49:13 -08:00
|
|
|
|
/**
|
|
|
|
|
* If at least one top-level key is an n8n item key (`json`, `binary`, etc.),
|
|
|
|
|
* then require all item keys to be an n8n item key.
|
|
|
|
|
*
|
|
|
|
|
* If no top-level key is an n8n key, then skip this check, allowing non-n8n
|
|
|
|
|
* item keys to be wrapped in `json` when normalizing items below.
|
|
|
|
|
*/
|
|
|
|
|
const mustHaveTopLevelN8nKey = executionResult.some((item) =>
|
2023-02-27 19:39:43 -08:00
|
|
|
|
Object.keys(item as IDataObject).find((key) => REQUIRED_N8N_ITEM_KEYS.has(key)),
|
2022-11-22 05:49:13 -08:00
|
|
|
|
);
|
|
|
|
|
|
2022-10-13 05:28:02 -07:00
|
|
|
|
for (const item of executionResult) {
|
2022-11-22 05:49:13 -08:00
|
|
|
|
if (mustHaveTopLevelN8nKey) {
|
2023-04-19 04:09:46 -07:00
|
|
|
|
this.validateTopLevelKeys(item);
|
2022-10-13 05:28:02 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-19 04:09:46 -07:00
|
|
|
|
const returnData = this.helpers.normalizeItems(executionResult);
|
|
|
|
|
returnData.forEach((item) => this.validateResult(item));
|
|
|
|
|
return returnData;
|
2022-10-13 05:28:02 -07:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-19 04:09:46 -07:00
|
|
|
|
async runCodeEachItem(itemIndex: number): Promise<INodeExecutionData | undefined> {
|
|
|
|
|
this.itemIndex = itemIndex;
|
2022-10-13 05:28:02 -07:00
|
|
|
|
const script = `module.exports = async function() {${this.jsCode}\n}()`;
|
|
|
|
|
|
|
|
|
|
const match = this.jsCode.match(/\$input\.(?<disallowedMethod>first|last|all|itemMatching)/);
|
|
|
|
|
|
|
|
|
|
if (match?.groups?.disallowedMethod) {
|
|
|
|
|
const { disallowedMethod } = match.groups;
|
|
|
|
|
|
|
|
|
|
const lineNumber =
|
|
|
|
|
this.jsCode.split('\n').findIndex((line) => {
|
|
|
|
|
return line.includes(disallowedMethod) && !line.startsWith('//') && !line.startsWith('*');
|
|
|
|
|
}) + 1;
|
|
|
|
|
|
|
|
|
|
const disallowedMethodFound = lineNumber !== 0;
|
|
|
|
|
|
|
|
|
|
if (disallowedMethodFound) {
|
|
|
|
|
throw new ValidationError({
|
|
|
|
|
message: `Can't use .${disallowedMethod}() here`,
|
|
|
|
|
description: "This is only available in 'Run Once for All Items' mode",
|
|
|
|
|
itemIndex: this.itemIndex,
|
|
|
|
|
lineNumber,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-19 04:09:46 -07:00
|
|
|
|
let executionResult: INodeExecutionData;
|
2022-10-13 05:28:02 -07:00
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
executionResult = await this.run(script, __dirname);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
// anticipate user expecting `item` to pre-exist as in Function Item node
|
|
|
|
|
if (error.message === 'item is not defined' && !/(let|const|var) item =/.test(script)) {
|
|
|
|
|
const quoted = error.message.replace('item', '`item`');
|
2023-01-13 09:11:56 -08:00
|
|
|
|
error.message = (quoted as string) + '. Did you mean `$input.item.json`?';
|
2022-10-13 05:28:02 -07:00
|
|
|
|
}
|
|
|
|
|
|
2023-02-27 19:39:43 -08:00
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
|
2022-10-13 05:28:02 -07:00
|
|
|
|
throw new ExecutionError(error, this.itemIndex);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (executionResult === null) return;
|
|
|
|
|
|
|
|
|
|
if (executionResult === undefined || typeof executionResult !== 'object') {
|
|
|
|
|
throw new ValidationError({
|
|
|
|
|
message: "Code doesn't return an object",
|
|
|
|
|
description: `Please return an object representing the output item. ('${executionResult}' was returned instead.)`,
|
|
|
|
|
itemIndex: this.itemIndex,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-19 04:09:46 -07:00
|
|
|
|
if (Array.isArray(executionResult)) {
|
|
|
|
|
const firstSentence =
|
|
|
|
|
executionResult.length > 0
|
|
|
|
|
? `An array of ${typeof executionResult[0]}s was returned.`
|
|
|
|
|
: 'An empty array was returned.';
|
|
|
|
|
|
|
|
|
|
throw new ValidationError({
|
|
|
|
|
message: "Code doesn't return a single object",
|
|
|
|
|
description: `${firstSentence} If you need to output multiple items, please use the 'Run Once for All Items' mode instead`,
|
|
|
|
|
itemIndex: this.itemIndex,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const [returnData] = this.helpers.normalizeItems([executionResult]);
|
|
|
|
|
this.validateResult(returnData);
|
|
|
|
|
|
|
|
|
|
// If at least one top-level key is a supported item key (`json`, `binary`, etc.),
|
|
|
|
|
// and another top-level key is unrecognized, then the user mis-added a property
|
|
|
|
|
// directly on the item, when they intended to add it on the `json` property
|
|
|
|
|
this.validateTopLevelKeys(returnData);
|
|
|
|
|
|
|
|
|
|
return returnData;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private validateResult({ json, binary }: INodeExecutionData) {
|
|
|
|
|
if (json === undefined || !isObject(json)) {
|
2022-10-13 05:28:02 -07:00
|
|
|
|
throw new ValidationError({
|
|
|
|
|
message: "A 'json' property isn't an object",
|
|
|
|
|
description: "In the returned data, every key named 'json' must point to an object",
|
|
|
|
|
itemIndex: this.itemIndex,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-19 04:09:46 -07:00
|
|
|
|
if (binary !== undefined && !isObject(binary)) {
|
2022-10-13 05:28:02 -07:00
|
|
|
|
throw new ValidationError({
|
|
|
|
|
message: "A 'binary' property isn't an object",
|
|
|
|
|
description: "In the returned data, every key named 'binary’ must point to an object.",
|
|
|
|
|
itemIndex: this.itemIndex,
|
|
|
|
|
});
|
|
|
|
|
}
|
2023-04-19 04:09:46 -07:00
|
|
|
|
}
|
2022-10-13 05:28:02 -07:00
|
|
|
|
|
2023-04-19 04:09:46 -07:00
|
|
|
|
private validateTopLevelKeys(item: INodeExecutionData) {
|
|
|
|
|
Object.keys(item).forEach((key) => {
|
2022-12-05 01:59:26 -08:00
|
|
|
|
if (REQUIRED_N8N_ITEM_KEYS.has(key)) return;
|
2022-10-13 05:28:02 -07:00
|
|
|
|
throw new ValidationError({
|
|
|
|
|
message: `Unknown top-level item key: ${key}`,
|
|
|
|
|
description: 'Access the properties of an item under `.json`, e.g. `item.json`',
|
|
|
|
|
itemIndex: this.itemIndex,
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-19 04:09:46 -07:00
|
|
|
|
export function getSandboxContext(this: IExecuteFunctions, index?: number): SandboxContext {
|
|
|
|
|
return {
|
2022-11-10 07:29:41 -08:00
|
|
|
|
// from NodeExecuteFunctions
|
|
|
|
|
$getNodeParameter: this.getNodeParameter,
|
|
|
|
|
$getWorkflowStaticData: this.getWorkflowStaticData,
|
|
|
|
|
helpers: this.helpers,
|
|
|
|
|
|
|
|
|
|
// to bring in all $-prefixed vars and methods from WorkflowDataProxy
|
2023-04-19 04:09:46 -07:00
|
|
|
|
...this.getWorkflowDataProxy(index ?? 0),
|
2022-11-10 07:29:41 -08:00
|
|
|
|
};
|
2022-10-13 05:28:02 -07:00
|
|
|
|
}
|