From 3da402a8d4c71f374016ccdd11fe475b39e2bdf1 Mon Sep 17 00:00:00 2001 From: Jan Oberhauser Date: Fri, 13 Dec 2019 11:23:30 -0600 Subject: [PATCH] :sparkles: Allow to use built-in modules in Function-Nodes if configured --- docs/configuration.md | 18 ++++++++++++++++++ packages/nodes-base/nodes/Function.node.ts | 11 +++++++++-- packages/nodes-base/nodes/FunctionItem.node.ts | 11 +++++++++-- 3 files changed, 36 insertions(+), 4 deletions(-) diff --git a/docs/configuration.md b/docs/configuration.md index 51473c0ef..fb612d31f 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 b93fa233f..373411d2d 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 a3c76f7e6..d1664c7d3 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;