Permissions | Node.js v23.11.0 Documentation (original) (raw)

Source Code: src/permission.cc

Permissions can be used to control what system resources the Node.js process has access to or what actions the process can take with those resources.

The permission model implements a "seat belt" approach, which prevents trusted code from unintentionally changing files or using resources that access has not explicitly been granted to. It does not provide security guarantees in the presence of malicious code. Malicious code can bypass the permission model and execute arbitrary code without the restrictions imposed by the permission model.

If you find a potential security vulnerability, please refer to ourSecurity Policy.

Process-based permissions#

Permission Model#

The Node.js Permission Model is a mechanism for restricting access to specific resources during execution. The API exists behind a flag --permission which when enabled, will restrict access to all available permissions.

The available permissions are documented by the --permissionflag.

When starting Node.js with --permission, the ability to access the file system through the fs module, spawn processes, use node:worker_threads, use native addons, use WASI, and enable the runtime inspector will be restricted.

`$ node --permission index.js

Error: Access to this API has been restricted at node:internal/main/run_main_module:23:47 { code: 'ERR_ACCESS_DENIED', permission: 'FileSystemRead', resource: '/home/user/index.js' }`

Allowing access to spawning a process and creating worker threads can be done using the --allow-child-process and --allow-worker respectively.

To allow native addons when using permission model, use the --allow-addonsflag. For WASI, use the --allow-wasi flag.

Runtime API#

When enabling the Permission Model through the --permissionflag a new property permission is added to the process object. This property contains one function:

permission.has(scope[, reference])#

API call to check permissions at runtime (permission.has())

`process.permission.has('fs.write'); // true process.permission.has('fs.write', '/home/rafaelgss/protected-folder'); // true

process.permission.has('fs.read'); // true process.permission.has('fs.read', '/home/rafaelgss/protected-folder'); // false`

File System Permissions#

The Permission Model, by default, restricts access to the file system through the node:fs module. It does not guarantee that users will not be able to access the file system through other means, such as through the node:sqlite module.

To allow access to the file system, use the --allow-fs-read and--allow-fs-write flags:

$ node --permission --allow-fs-read=* --allow-fs-write=* index.js Hello world!

The valid arguments for both flags are:

Example:

Wildcards are supported too:

After passing a wildcard character (*) all subsequent characters will be ignored. For example: /home/*.js will work similar to /home/*.

When the permission model is initialized, it will automatically add a wildcard (*) if the specified directory exists. For example, if /home/test/filesexists, it will be treated as /home/test/files/*. However, if the directory does not exist, the wildcard will not be added, and access will be limited to/home/test/files. If you want to allow access to a folder that does not exist yet, make sure to explicitly include the wildcard:/my-path/folder-do-not-exist/*.

Using the Permission Model with npx#

If you're using npx to execute a Node.js script, you can enable the Permission Model by passing the --node-options flag. For example:

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

This sets the NODE_OPTIONS environment variable for all Node.js processes spawned by npx, without affecting the npx process itself.

FileSystemRead Error with npx

The above command will likely throw a FileSystemRead invalid access error because Node.js requires file system read access to locate and execute the package. To avoid this:

  1. Using a Globally Installed PackageGrant read access to the global node_modules directory by running:
    npx --node-options="--permission --allow-fs-read=$(npm prefix -g)" package-name
  2. Using the npx CacheIf you are installing the package temporarily or relying on the npx cache, grant read access to the npm cache directory:
    npx --node-options="--permission --allow-fs-read=$(npm config get cache)" package-name

Any arguments you would normally pass to node (e.g., --allow-* flags) can also be passed through the --node-options flag. This flexibility makes it easy to configure permissions as needed when using npx.

Permission Model constraints#

There are constraints you need to know before using this system:

Limitations and Known Issues#