Restricting the n8n Code node modules and environment access for self-hosted instances
Restrict what Code node scripts can load and read before a self-hosted n8n instance holds production credentials. The Code node runs arbitrary JavaScript or Python written by whoever edits a workflow, so anything the n8n process can import or read is reachable from a script. Two levers matter most. NODE_FUNCTION_ALLOW_BUILTIN controls which Node.js built-in modules scripts may import, and NODE_FUNCTION_ALLOW_EXTERNAL controls which external packages installed on the instance may be loaded. Blocked access to environment variables applies from version 2.0, so a script on that version cannot simply read secrets held in the process environment. Confirm the exact behavior for your version in the n8n documentation and on a staging instance before you rely on it.
Teams need these controls in several common situations: any instance that stores production credentials, any instance where several people can edit workflows, and any instance that installs community nodes or custom packages, since those add modules a script can reach. Treat the allowlists as one layer of control. The Code node is not a sandbox on its own, because an allowed module can still act within the permissions it grants, and a script that reaches the database or the network can act there too. Isolating script execution is covered in the task runner section, and outbound request and webhook controls appear elsewhere in this group.
Start with an inventory. Search existing workflows for Code nodes that call require or import, list each module they use, then decide which built-ins and packages the business needs and set the two variables to that list. The allowlists apply to the process that runs the Code node, so in queue mode or with task runners, set them where those processes run and check each worker. Confirm the setting names and defaults against the release you run, test a known-good workflow and a deliberately blocked import on staging, and repeat the check after every upgrade, since a version change can change behavior.
Where the module and environment rules apply in a Code node run
A script starts with a workflow author in the editor and reaches the Code node inside the n8n main process. Each import request passes the builtin allowlist first, then the external allowlist if it is not a builtin, and any module not listed ends in a blocked-call error. Environment reads pass the environment access rule before the run continues. The last hand-off decides whether the script executes on the main instance or on a separate task runner, so check both allowlists and the run location on every instance that holds real credentials.
Code node import and environment checks, with the trust boundary between editor, main process and runner.
Which configuration option fits each environment
Choose this option when Code node scripts only need to transform data that already exists in the workflow. Blocking external module access keeps scripts from loading packages from the instance, which removes the largest supply chain and file access surface in the node. It fits instances that hold production credentials, serve several teams, or process regulated data.
- Smallest attack surface: Scripts cannot pull in third-party code, so a compromised or vulnerable package cannot run inside a workflow.
- What breaks for authors: Any script that calls
requireon a package stops running after the change, and the error appears in the execution log. - Migration effort: Authors need to move that logic to expressions, built-in nodes or an HTTP Request node that calls a separate service.
- Ownership: Someone must review workflows that depend on packages before the restriction goes live, or those workflows will fail in production.
Before relying on this setting, confirm in the documentation for your n8n version how the variable is named, what it accepts and what the failure looks like, and test a workflow that imports a package to confirm it fails.
Choose this option when teams have a legitimate need for specific libraries, such as a parser or a formatting helper, and you want that need documented and reviewed. An explicit list of packages turns an open-ended capability into a short, approved set. It fits instances with a platform team that can review each package before it is added.
- Reviewed list: Each permitted package should have an owner, a version or range, a documented purpose and a review date.
- Installation location: Confirm where packages must be installed for your version and deployment model, since a package that is allowed but missing will fail at runtime.
- What breaks for authors: Packages not on the list fail, and transitive dependencies may need to be considered when a listed package pulls in more code.
- Change control: Adding a package should follow the same review path as other changes to workflow infrastructure, not a one-person decision.
- Recurring work: Each package update is a change that needs a test run, so the list adds ongoing maintenance.
Verify by running each approved package in a test workflow, then confirm that an unlisted package fails in the same way. Check the list against the version of n8n you run, since the behavior of the setting can change between releases.
Choose this option when scripts need some Node.js core functionality but not the most sensitive parts. Built-in modules give scripts access to the runtime itself, so limiting them is a separate decision from limiting external packages. It fits instances where authors write moderately complex logic and you have mapped which core modules that logic uses.
- Sensitive modules first: Modules that read the file system or start child processes carry the most risk, and they should be the first to be restricted.
- What breaks for authors: Scripts that depend on core functions for hashing, encoding or other utilities may stop working, so inventory existing scripts before enabling the restriction.
- Interaction with packages: A permitted external package may itself depend on a built-in module, so testing the two settings together matters.
- Environment access: Since n8n 2.0, the Code node blocks access to environment variables. Confirm in the release notes and docs exactly what this covers in your version, so you do not assume a value is hidden when it is not.
- Review cadence: Revisit the built-in list after each upgrade, because the set of permitted modules can change.
Verify with test scripts that try to read a local file, start a process and read an environment variable, and confirm each attempt is blocked as your documentation describes.
Choose this option by first confirming which process executes Code node scripts in your topology. A restriction only protects the process where it is applied, so the placement decision matters as much as the choice of allowlist. It fits any instance with real credentials, and it is especially important in queue mode, where executions run on separate worker processes.
- Where scripts run: Confirm whether Code node execution happens on the main instance, a task runner or a worker in your version and setup.
- Consistency: Settings applied to the main instance may not reach a separately configured runner, so each process that runs scripts needs its own verified configuration.
- Queue mode: Workers and webhook processors must share the encryption key to read credentials, so the same care applies to any setting that changes what they can access.
- What breaks for authors: A script that works in the editor can fail on a worker, so behavior can differ between manual and production runs.
- Operations: Document which process holds the restriction, so later changes to runners or scaling do not remove it unnoticed.
Verify by running the same test scripts through the editor, a manual execution, a webhook path and a worker, and confirm the restriction holds on every route. The configuration reference for your version is the authoritative source for how each setting applies.
Configuration options and when each fits
This table compares the four settings that control what Code node scripts can import and read. Read each row with one question in mind: who can write or edit a script, because that person can use any module or variable the runtime allows. The main takeaway is that module allowlists and environment blocking limit damage most when scripts run in a separate, locked-down task runner.
| Setting | What it controls | When to use it | What to confirm |
|---|---|---|---|
NODE_FUNCTION_ALLOW_BUILTIN | Node.js built-in modules, such as file system or child process, that Code node scripts may require | Keep the list empty or minimal on instances holding real credentials, adding only modules a workflow needs | Accepted list format, and whether the setting applies inside task runners in your version |
NODE_FUNCTION_ALLOW_EXTERNAL | Installed npm packages that Code node scripts may import | Allow only vetted packages your team maintains, since each one runs with workflow access | Which packages must be installed in the runtime, and who approves and updates them |
| Environment variable access | Whether Code node scripts can read process environment variables, where keys and tokens often sit | Blocked in 2.0 and later, so pass needed values through credentials or inputs instead of the environment | Exact behavior on your version and whether any setting re-enables access, per the release notes |
| Task runner placement | Where Code node execution happens: inside the main process or in a separate runner | Use a separate task runner for any instance with real credentials, especially in queue mode | Runner image and version you deploy, and that your allowlists apply to that runner |
How to set the restrictions and verify them on a running instance
This sequence moves Code node restrictions from a decision on paper to a verified setting on a live n8n instance. It inventories existing scripts and packages, agrees allowlists with workflow owners, applies the variables, and proves the result with a test script before the change reaches production.
Inventory existing Code node scripts and packages
The platform or automation lead lists every workflow that contains a Code node, the modules each script imports, and any community packages installed on the instance. Export the workflows from the editor or query the database, and record the workflow owner for each entry. The step is done when every import in every Code node appears in a single inventory sheet with an owner beside it, so no allowlist decision is made from memory.
Agree builtin and external allowlists with owners
Walk through the inventory with each workflow owner and decide which Node.js built-in modules and which external npm packages their scripts may import. Record the decision as the exact values for NODE_FUNCTION_ALLOW_BUILTIN and NODE_FUNCTION_ALLOW_EXTERNAL, with a short reason for each entry. Anything not agreed stays out of the list. The step is done when the security owner and each workflow owner have approved the lists in writing and the lists are stored in version control.
Set the variables on staging first
The platform engineer sets NODE_FUNCTION_ALLOW_BUILTIN and NODE_FUNCTION_ALLOW_EXTERNAL on a staging copy of the instance, matching the production deployment method. Where the instance uses external task runners, the same values must also be applied to the runner environment, because the runner executes the Code node. Check the staging copy against the approved list before any production change, and confirm the change is recorded in your change management process. Production changes should follow a successful staging run.
Apply the variables on the main instance or task runners
Once staging passes, the platform engineer applies the same variables to the production main instance or to the task runner configuration, depending on where Code nodes execute in your topology. Confirm the variables are set in the running environment rather than only in a file, for example by inspecting the container or process environment. The step is done when the production values match the approved lists character for character.
Restart and confirm the new settings are loaded
Restart the affected n8n processes and any runners so the new variables take effect. Then open an existing workflow that uses an approved module and run it, confirming it still completes. Check the logs for startup errors. The step is done when an approved workflow runs successfully after the restart and no startup warnings relate to the Code node configuration.
Test blocked imports and environment reads
Create a test workflow, clearly named as a test and kept out of production, with a Code node that attempts to import a built-in module that is not on the allowlist and another that attempts to import an external package that is not approved. Add a third script that tries to read an environment variable holding a non-secret test value, then a fourth that reads a variable containing a placeholder secret. Each attempt should fail as expected under the current configuration. Record the error text for each case.
Retest after every change and keep the evidence
Repeat the test workflow after each change to the allowlists, after every upgrade, and after any change to the runner image or environment. Keep the test results, the approved lists and the change record together so the security team and any assessor can see what was set, when, and how it was verified. The step is complete when a dated record exists for the current configuration and the test workflow is documented as a regression check.
Security gaps that remain after the module rules are set
Blocking modules and environment variables narrows what a Code node can reach, but it does not make the Code node a safe place for untrusted logic. The cards below cover the risks that stay open after those rules are in place, and each one points to a separate control or check.
Allowed modules still do work
An allowlisted module such as a file, network or crypto library can be turned against your instance if a script uses it in an unexpected way. Allowing a module is a trust decision about its full behavior, not only its import name. Review each entry in NODE_FUNCTION_ALLOW_BUILTIN and NODE_FUNCTION_ALLOW_EXTERNAL against what the module can read, write or send, and keep the list as short as your workflows need.
Code writers may hold credentials
Anyone who can create or edit a workflow with a Code node can often reach credentials through the nodes around it, even when environment variables are blocked. Module rules do not change that. Check which roles can edit workflows, which projects hold production credentials, and whether those users need the Code node at all.
Task runner isolation is separate
Restricting modules and environment access does not isolate the process that runs Code node scripts. Task runner isolation is a separate control with its own placement, user and filesystem settings. Read the sibling topic on isolating the Code node with external task runners before you treat module rules as sufficient for any instance that holds real credentials.
Workers need the encryption key
In queue mode, every worker and webhook processor must hold the main instance's encryption key so it can decrypt stored credentials. A Code node that runs on a worker therefore sits in a process that can read that key's effects. Confirm where workers run, who can access their hosts and environment, and how the key is stored in each location.
Audit the instance for exposure
Module rules do not reveal which workflows already contain risky Code node patterns, unused credentials or unprotected webhooks. Run the n8n security audit before and after you change the rules, and record which findings you accepted and why. The report gives you a baseline to compare against after each change.
Retest after every upgrade
Module and environment behavior can change between releases, so an allowlist that worked on one version may behave differently on the next. Retest the blocked imports, blocked variables and task runner settings after each upgrade and before you move it to production. Track the release notes for each minor version you adopt, since n8n ships minor releases frequently.
- Minor release cadence
- Weekly
Questions teams ask before restricting Code node modules
How long does restricting Code node modules take?
The configuration change itself is small, but the time it takes depends on how many workflows already import modules or read environment variables. The main drivers are the number of Code nodes in production, how many distinct modules they use, how many of those workflows have owners who must review changes, and whether you run task runners that need the same settings applied. A quote should break the work into inventory, allowlist decisions, staging tests, owner review and production rollout, each with its own dependencies. Ask the provider to state which of these steps are included and which depend on your own people being available.
Can our in-house team restrict the Code node without a partner?
Yes, if you have someone who can change the instance environment, read n8n's configuration reference for your version, and test workflows in a staging instance. The technical change is a few environment variables, so the harder part is the inventory and the decisions behind it. Your team must know which Code nodes exist, which modules they need, and which secrets they read. Teams with a single engineer and many unowned workflows often find the review work takes longer than the configuration. A partner adds value mainly where the inventory is large or where evidence for an audit must be produced.
What does the work need from our team?
Three things are needed before any change reaches production. First, an inventory of every workflow with a Code node, the modules it imports and the environment variables it reads. Second, a named owner for each business process who signs off that the workflow still works under the new allowlist. Third, a staging instance that mirrors production's version, database and task runner setup, so breakage shows up before users see it. Without the staging instance, you will learn about blocked modules from failed production executions.
What should a partner quote depend on?
A quote should follow from a written scope that names the instances covered, the number of Code nodes to review, the staging environment available, the owners who must approve changes and the documentation expected at the end. Ask whether the price covers the inventory, the allowlist design, the retest of affected workflows and any follow-up after rollout. DeployOpen agrees scope, deliverables and timelines in writing before work starts, and does not publish a price list. Confirm with any provider which items are fixed and which change if the inventory turns up more workflows than expected.
Do these restrictions replace external task runners?
No. The module and environment settings limit what a Code node may import and read in the main process. External task runners move Code node execution into a separate sidecar with its own user, filesystem and resource limits, which is a stronger isolation boundary. The two controls address different risks and are usually used together. If you run task runners, the allowlist settings must also be applied to the runner configuration, otherwise scripts may behave differently depending on where they execute. Confirm the exact behavior for your n8n version before relying on either control alone.
What happens to existing workflows that import blocked modules?
Those workflows will fail at execution once the restriction is active, because the import is refused at runtime. This is the intended effect, but it can break business processes silently if no one is watching failures. Before the change, list each affected workflow and decide whether to replace the module with a built-in node or approved function, add the module to the allowlist with a documented reason, or retire the workflow. Test each fix in staging with realistic input and confirm the owner accepts the result. Plan for a rollback path, and tell owners when the change lands so they can watch their executions.
Does the restriction need ongoing work after rollout?
Yes, though the load is usually smaller than the initial review. New workflows, new modules required by a team, and n8n upgrades can each change what the Code node needs, so the allowlist should have an owner and a review step for additions. Keep the inventory current and check failed executions for blocked-module errors after each upgrade. Retainers for ongoing support and upgrades are available from DeployOpen, and an internal owner can run the same checks without one.
What DeployOpen delivers for n8n hardening
DeployOpen plans and applies the Code node restrictions inside your own infrastructure, starting with an inventory rather than a configuration change. We first list every workflow that uses a Code node, which modules those scripts import, which environment variables they read, and which credentials they can reach. That inventory decides whether NODE_FUNCTION_ALLOW_BUILTIN and NODE_FUNCTION_ALLOW_EXTERNAL can be tightened to a short allowlist, and which teams need a documented exception before the change lands. The work covers the main instance, any worker or webhook processor that runs Code nodes, and the task runner configuration, so the restriction applies wherever a script executes.
Changes are made first in a staging copy of your instance that mirrors production's version, database and queue layout. We verify each allowed module, confirm that blocked environment access fails in the way you expect, and rerun representative workflows to catch scripts that break under the new settings. Once staging passes, the configuration is promoted to production through your change process, with the final settings written to a runbook that names each variable, its value, where it is set and who approves changes to it. Your security team receives that runbook, the staging test results and a list of open exceptions with owners.
Engagements are fixed-scope, and the scope, deliverables and timeline are agreed in writing before work starts. Ask for the written scope to name the instances, runner placements and workflow count covered, and whether staging is provided by you or by DeployOpen. Ongoing retainers cover n8n upgrades, which can change how Code node settings behave, and periodic retesting after new modules or workflows are added. Confirm the current behavior of these variables against the version you run, since the blocked environment access change arrived in 2.0 and your release notes are the authority for your build.
How DeployOpen works on this
DeployOpen starts by mapping which Code node scripts exist, which modules they need and what environment values they can reach today. The restriction settings are then applied on the instance or task runners you control, tested against real workflows, and handed over with written runbooks so your team can maintain the policy.
How we scope
- Inventory every workflow with a Code node and list the modules each script imports.
- Agree in writing which built-in and external modules stay allowed and which are blocked.
- Confirm your n8n version and whether Code node execution runs in-process or on task runners.
How we build
- Set NODE_FUNCTION_ALLOW_BUILTIN and NODE_FUNCTION_ALLOW_EXTERNAL to the approved list, then apply the same policy to task runners.
- Confirm environment variables and secrets are unreadable from Code node scripts on your version.
- Test allowed and blocked imports in a staging instance before any change reaches production.
How we hand over
- Deliver the approved module list, configuration values and change record for your review.
- Document how to request a new module through your own change process.
- Provide a runbook covering rollback, task runner configuration and checks after each n8n upgrade.
How we support
- Ongoing support, upgrades and continuous testing are available as retainers.
- Retest the Code node policy after each n8n upgrade or new module request, if your retainer covers it.
- Confirm with DeployOpen during scoping which upgrade and retest tasks are included in the quote.

