Permissions in Node.js

The Node.js Permission Model provides a mechanism to restrict process access to system resources. It acts as a "seat belt," preventing trusted code from unintentionally modifying files or using resources without explicit permission. However, it does not guarantee protection against malicious code, which can bypass restrictions. Developers can configure permissions at startup or check them at runtime.

--permission flagprocess.permission.has()--allow-fs-read / --allow-fs-write--allow-net / --allow-worker / --allow-addons / --allow-wasi

~2 min read · Updated Dec 30, 2025

1. Introduction


By starting Node.js with the --permission flag, access to resources such as the file system, network, child processes, worker threads, native addons, WASI, and the inspector is restricted.


$ node --permission index.js
Error: Access to this API has been restricted

2. Runtime API


When the Permission Model is enabled, the process.permission property is available with the has() method to check permissions:


process.permission.has('fs.write'); // true
process.permission.has('fs.read', '/home/protected'); // false

3. File System Permissions


By default, file system access is restricted. To allow read or write operations, use:


  • --allow-fs-read=*: Allow all read operations.
  • --allow-fs-write=/tmp/: Allow write access to /tmp.
  • Supports wildcards: --allow-fs-read=/home/test*.

4. Using with npx


Enable the Permission Model when running scripts via npx using --node-options:


npx --node-options="--permission" package-name

To avoid FileSystemRead errors, grant read access to global node_modules or npm cache directories.


5. Permission Model Constraints


  • Permissions do not inherit to worker threads.
  • Restricted features include native modules, network, child processes, file system, WASI, and inspector.
  • Certain flags like --env-file or --openssl-config bypass the model since they run before initialization.
  • Existing file descriptors can bypass restrictions.

6. Limitations and Known Issues


  • Symbolic links are followed even outside allowed paths.
  • Relative symlinks may grant access to arbitrary files and directories.

Conclusion


The Node.js Permission Model is a useful tool for controlling resource access at runtime. While it prevents unintended actions, it does not fully protect against malicious code. Developers must carefully configure allowed paths and be aware of constraints and known issues.


Written & researched by Dr. Shahin Siami

Related Articles

Comprehensive Guide to the Node.js VM Module (node:vm)

The node:vm module allows you to compile and execute JavaScript code inside isolated V8 contexts — essentially creating lightweight sandboxes within your Node.js application. These contexts have their own global scope and can run code independently from the main environment. However, vm is NOT a security sandbox. It is powerful for dynamic code execution, template engines, REPLs, plugin systems, and controlled module execution, but it must never be used to run untrusted code.

Continue

Comprehensive Guide to the V8 Module in Node.js (node:v8)

The node:v8 module exposes low-level APIs that interact directly with the V8 JavaScript engine embedded in Node.js. . These APIs provide access to heap statistics, heap snapshots, coverage tools, serialization mechanisms, V8 flags, object queries, and promise lifecycle hooks. The module is essential for performance analysis, memory debugging, tooling, and advanced Node.js internals work.

Continue

Comprehensive Guide to Node.js Worker Threads (node:worker_threads)

The node:worker_threads module enables true multithreading in Node.js by running JavaScript in separate threads. While Node.js is traditionally single‑threaded, worker threads allow CPU‑intensive tasks to run in parallel without blocking the event loop. They support shared memory, zero‑copy transfers, worker pools, resource limits, and advanced synchronization APIs. Worker threads are ideal for heavy computation, data processing, and parallel workloads—while async I/O remains best handled by the main thread.

Continue

Comprehensive Guide to the Node.js util Module (node:util)

The node:util module provides a powerful collection of helper functions used throughout Node.js core and extremely useful for application developers. These utilities support debugging, inspection, formatting, type checking, callback/Promise conversions, argument parsing, text encoding, MIME handling, and more. It is one of the most versatile and essential toolkits in the Node.js ecosystem.

Continue

Comprehensive Guide to the URL Module in Node.js

The node:url module provides tools for parsing, constructing, and manipulating URLs. Node.js supports two URL APIs: WHATWG URL API — modern, browser‑compatible, standards‑based. Legacy Node.js URL API — older, Node‑specific, now discouraged. The WHATWG API is the recommended approach for all modern applications. It provides a clean, consistent interface for working with URL components, query parameters, and structured URL patterns.

Continue

راهنمای جامع UDP / Datagram Sockets در Node.js

ماژول node:dgram پیاده‌سازی کامل سوکت‌های UDP را در Node.js فراهم می‌کند. UDP یک پروتکل سبک، بدون اتصال (connectionless) و مناسب برای برنامه‌های بلادرنگ مانند VoIP، بازی‌ها، IoT، سیستم‌های پخش (broadcast) و چندپخشی (multicast) است. این ماژول امکان ساخت سوکت، ارسال و دریافت دیتاگرام، مدیریت TTL، عضویت در گروه‌های multicast، کنترل بافرها، و مدیریت رفتار سطح پایین شبکه را فراهم می‌کند.

Continue