diff --git a/docs/configuration.md b/docs/configuration.md index 51473c0ef1..fb612d31fc 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -100,6 +100,24 @@ export N8N_CUSTOM_EXTENSIONS="/home/jim/n8n/custom-nodes;/data/n8n/nodes" ``` +## Use built-in modules in Function-Nodes + +By default is it for security reasons not allowed to import modules in Function-Nodes. +It is, however, possible to lift that restriction for built-in modules by setting the +environment variable `NODE_FUNCTION_ALLOW_BUILTIN`. + +```bash +# Allows usage of all builtin modules +export NODE_FUNCTION_ALLOW_BUILTIN=* + +# Allows usage of only crypto +export NODE_FUNCTION_ALLOW_BUILTIN=crypto + +# Allows usage of only crypto and fs +export NODE_FUNCTION_ALLOW_BUILTIN=crypto,fs +``` + + ## Timezone The timezone is set by default to "America/New_York". It gets for example used by the diff --git a/packages/nodes-base/nodes/Function.node.ts b/packages/nodes-base/nodes/Function.node.ts index b93fa233f9..373411d2d5 100644 --- a/packages/nodes-base/nodes/Function.node.ts +++ b/packages/nodes-base/nodes/Function.node.ts @@ -59,14 +59,21 @@ export class Function implements INodeType { // By default use data from first item Object.assign(sandbox, sandbox.$item(0)); - const vm = new NodeVM({ + const options = { console: 'inherit', sandbox, require: { external: false, + builtin: [] as string[], root: './', } - }); + }; + + if (process.env.NODE_FUNCTION_ALLOW_BUILTIN) { + options.require.builtin = process.env.NODE_FUNCTION_ALLOW_BUILTIN.split(','); + } + + const vm = new NodeVM(options); // Get the code to execute const functionCode = this.getNodeParameter('functionCode', 0) as string; diff --git a/packages/nodes-base/nodes/FunctionItem.node.ts b/packages/nodes-base/nodes/FunctionItem.node.ts index a3c76f7e6e..d1664c7d30 100644 --- a/packages/nodes-base/nodes/FunctionItem.node.ts +++ b/packages/nodes-base/nodes/FunctionItem.node.ts @@ -64,14 +64,21 @@ export class FunctionItem implements INodeType { const dataProxy = this.getWorkflowDataProxy(); Object.assign(sandbox, dataProxy); - const vm = new NodeVM({ + const options = { console: 'inherit', sandbox, require: { external: false, + builtin: [] as string[], root: './', } - }); + }; + + if (process.env.NODE_FUNCTION_ALLOW_BUILTIN) { + options.require.builtin = process.env.NODE_FUNCTION_ALLOW_BUILTIN.split(','); + } + + const vm = new NodeVM(options); // Get the code to execute const functionCode = this.getNodeParameter('functionCode') as string;