2022-09-05 13:07:45 -03:00
|
|
|
# Permissions
|
|
|
|
|
2025-01-22 21:02:09 +01:00
|
|
|
<!--introduced_in=v20.0.0-->
|
|
|
|
|
|
|
|
<!-- source_link=src/permission.cc -->
|
|
|
|
|
2022-09-05 13:07:45 -03:00
|
|
|
Permissions can be used to control what system resources the
|
|
|
|
Node.js process has access to or what actions the process can take
|
2024-08-10 17:06:25 -03:00
|
|
|
with those resources.
|
2022-09-05 13:07:45 -03:00
|
|
|
|
2023-02-23 15:11:51 -03:00
|
|
|
* [Process-based permissions](#process-based-permissions) control the Node.js
|
|
|
|
process's access to resources.
|
|
|
|
The resource can be entirely allowed or denied, or actions related to it can
|
|
|
|
be controlled. For example, file system reads can be allowed while denying
|
|
|
|
writes.
|
2024-08-17 15:03:21 +02:00
|
|
|
This feature does not protect against malicious code. According to the Node.js
|
|
|
|
[Security Policy][], Node.js trusts any code it is asked to run.
|
|
|
|
|
|
|
|
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.
|
2023-02-23 15:11:51 -03:00
|
|
|
|
2022-09-05 13:07:45 -03:00
|
|
|
If you find a potential security vulnerability, please refer to our
|
|
|
|
[Security Policy][].
|
|
|
|
|
2023-02-23 15:11:51 -03:00
|
|
|
## Process-based permissions
|
|
|
|
|
|
|
|
### Permission Model
|
|
|
|
|
2025-01-22 21:02:09 +01:00
|
|
|
<!-- YAML
|
|
|
|
added: v20.0.0
|
|
|
|
changes:
|
|
|
|
- version:
|
|
|
|
- v23.5.0
|
|
|
|
- v22.13.0
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/56201
|
|
|
|
description: This feature is no longer experimental.
|
|
|
|
-->
|
2023-02-23 15:11:51 -03:00
|
|
|
|
2025-03-23 16:58:20 +01:00
|
|
|
> Stability: 2 - Stable
|
2023-02-23 15:11:51 -03:00
|
|
|
|
|
|
|
The Node.js Permission Model is a mechanism for restricting access to specific
|
|
|
|
resources during execution.
|
2024-12-12 09:11:58 -03:00
|
|
|
The API exists behind a flag [`--permission`][] which when enabled,
|
2023-02-23 15:11:51 -03:00
|
|
|
will restrict access to all available permissions.
|
|
|
|
|
2024-12-12 09:11:58 -03:00
|
|
|
The available permissions are documented by the [`--permission`][]
|
2023-02-23 15:11:51 -03:00
|
|
|
flag.
|
|
|
|
|
2024-12-12 09:11:58 -03:00
|
|
|
When starting Node.js with `--permission`,
|
2023-05-01 18:19:48 +02:00
|
|
|
the ability to access the file system through the `fs` module, spawn processes,
|
2024-06-01 10:13:12 -03:00
|
|
|
use `node:worker_threads`, use native addons, use WASI, and enable the runtime inspector
|
2023-05-07 13:22:32 -03:00
|
|
|
will be restricted.
|
2023-02-23 15:11:51 -03:00
|
|
|
|
|
|
|
```console
|
2024-12-12 09:11:58 -03:00
|
|
|
$ node --permission index.js
|
2023-02-23 15:11:51 -03:00
|
|
|
|
|
|
|
Error: Access to this API has been restricted
|
|
|
|
at node:internal/main/run_main_module:23:47 {
|
|
|
|
code: 'ERR_ACCESS_DENIED',
|
2023-11-08 00:00:17 +00:00
|
|
|
permission: 'FileSystemRead',
|
|
|
|
resource: '/home/user/index.js'
|
2023-02-23 15:11:51 -03:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
Allowing access to spawning a process and creating worker threads can be done
|
|
|
|
using the [`--allow-child-process`][] and [`--allow-worker`][] respectively.
|
|
|
|
|
2023-12-21 17:44:11 +00:00
|
|
|
To allow native addons when using permission model, use the [`--allow-addons`][]
|
2024-06-01 10:13:12 -03:00
|
|
|
flag. For WASI, use the [`--allow-wasi`][] flag.
|
2023-12-21 17:44:11 +00:00
|
|
|
|
2023-02-23 15:11:51 -03:00
|
|
|
#### Runtime API
|
|
|
|
|
2024-12-12 09:11:58 -03:00
|
|
|
When enabling the Permission Model through the [`--permission`][]
|
2023-02-23 15:11:51 -03:00
|
|
|
flag a new property `permission` is added to the `process` object.
|
2023-04-04 14:14:04 -03:00
|
|
|
This property contains one function:
|
2023-02-23 15:11:51 -03:00
|
|
|
|
2023-05-07 19:08:21 +09:00
|
|
|
##### `permission.has(scope[, reference])`
|
2023-02-23 15:11:51 -03:00
|
|
|
|
|
|
|
API call to check permissions at runtime ([`permission.has()`][])
|
|
|
|
|
|
|
|
```js
|
|
|
|
process.permission.has('fs.write'); // true
|
|
|
|
process.permission.has('fs.write', '/home/rafaelgss/protected-folder'); // true
|
|
|
|
|
2023-04-04 14:14:04 -03:00
|
|
|
process.permission.has('fs.read'); // true
|
|
|
|
process.permission.has('fs.read', '/home/rafaelgss/protected-folder'); // false
|
2023-02-23 15:11:51 -03:00
|
|
|
```
|
|
|
|
|
|
|
|
#### File System Permissions
|
|
|
|
|
2024-08-10 17:07:06 -03:00
|
|
|
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.
|
|
|
|
|
2023-02-23 15:11:51 -03:00
|
|
|
To allow access to the file system, use the [`--allow-fs-read`][] and
|
|
|
|
[`--allow-fs-write`][] flags:
|
|
|
|
|
|
|
|
```console
|
2024-12-12 09:11:58 -03:00
|
|
|
$ node --permission --allow-fs-read=* --allow-fs-write=* index.js
|
2023-02-23 15:11:51 -03:00
|
|
|
Hello world!
|
|
|
|
```
|
|
|
|
|
|
|
|
The valid arguments for both flags are:
|
|
|
|
|
2023-04-19 19:40:46 +02:00
|
|
|
* `*` - To allow all `FileSystemRead` or `FileSystemWrite` operations,
|
|
|
|
respectively.
|
|
|
|
* Paths delimited by comma (`,`) to allow only matching `FileSystemRead` or
|
|
|
|
`FileSystemWrite` operations, respectively.
|
2023-02-23 15:11:51 -03:00
|
|
|
|
|
|
|
Example:
|
|
|
|
|
|
|
|
* `--allow-fs-read=*` - It will allow all `FileSystemRead` operations.
|
|
|
|
* `--allow-fs-write=*` - It will allow all `FileSystemWrite` operations.
|
|
|
|
* `--allow-fs-write=/tmp/` - It will allow `FileSystemWrite` access to the `/tmp/`
|
|
|
|
folder.
|
2023-08-17 20:39:04 +02:00
|
|
|
* `--allow-fs-read=/tmp/ --allow-fs-read=/home/.gitignore` - It allows `FileSystemRead` access
|
2023-02-23 15:11:51 -03:00
|
|
|
to the `/tmp/` folder **and** the `/home/.gitignore` path.
|
|
|
|
|
|
|
|
Wildcards are supported too:
|
|
|
|
|
2023-04-27 03:34:52 +09:00
|
|
|
* `--allow-fs-read=/home/test*` will allow read access to everything
|
2023-02-23 15:11:51 -03:00
|
|
|
that matches the wildcard. e.g: `/home/test/file1` or `/home/test2`
|
|
|
|
|
2024-01-08 16:26:34 -03:00
|
|
|
After passing a wildcard character (`*`) all subsequent characters will
|
|
|
|
be ignored. For example: `/home/*.js` will work similar to `/home/*`.
|
|
|
|
|
2024-07-05 03:00:07 +09:30
|
|
|
When the permission model is initialized, it will automatically add a wildcard
|
|
|
|
(\*) if the specified directory exists. For example, if `/home/test/files`
|
|
|
|
exists, 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/*`.
|
|
|
|
|
2025-01-13 06:49:45 -03:00
|
|
|
#### 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:
|
|
|
|
|
|
|
|
```bash
|
|
|
|
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 Package**
|
|
|
|
Grant read access to the global `node_modules` directory by running:
|
|
|
|
|
|
|
|
```bash
|
|
|
|
npx --node-options="--permission --allow-fs-read=$(npm prefix -g)" package-name
|
|
|
|
```
|
|
|
|
|
|
|
|
2. **Using the `npx` Cache**
|
|
|
|
If you are installing the package temporarily or relying on the `npx` cache,
|
|
|
|
grant read access to the npm cache directory:
|
|
|
|
|
|
|
|
```bash
|
|
|
|
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`.
|
|
|
|
|
2023-12-21 17:35:57 +00:00
|
|
|
#### Permission Model constraints
|
2023-05-15 19:41:29 +02:00
|
|
|
|
2023-02-23 15:11:51 -03:00
|
|
|
There are constraints you need to know before using this system:
|
|
|
|
|
2023-12-21 17:35:57 +00:00
|
|
|
* The model does not inherit to a child node process or a worker thread.
|
|
|
|
* When using the Permission Model the following features will be restricted:
|
|
|
|
* Native modules
|
|
|
|
* Child process
|
|
|
|
* Worker Threads
|
|
|
|
* Inspector protocol
|
|
|
|
* File system access
|
2024-06-01 10:13:12 -03:00
|
|
|
* WASI
|
2023-12-21 17:35:57 +00:00
|
|
|
* The Permission Model is initialized after the Node.js environment is set up.
|
|
|
|
However, certain flags such as `--env-file` or `--openssl-config` are designed
|
|
|
|
to read files before environment initialization. As a result, such flags are
|
2024-07-06 16:01:35 -03:00
|
|
|
not subject to the rules of the Permission Model. The same applies for V8
|
|
|
|
flags that can be set via runtime through `v8.setFlagsFromString`.
|
2023-12-21 17:35:57 +00:00
|
|
|
* OpenSSL engines cannot be requested at runtime when the Permission
|
|
|
|
Model is enabled, affecting the built-in crypto, https, and tls modules.
|
2024-12-03 15:15:46 -08:00
|
|
|
* Run-Time Loadable Extensions cannot be loaded when the Permission Model is
|
|
|
|
enabled, affecting the sqlite module.
|
2024-05-27 15:31:28 -03:00
|
|
|
* Using existing file descriptors via the `node:fs` module bypasses the
|
|
|
|
Permission Model.
|
2023-12-21 17:35:57 +00:00
|
|
|
|
|
|
|
#### Limitations and Known Issues
|
|
|
|
|
2023-09-29 13:31:51 +02:00
|
|
|
* Symbolic links will be followed even to locations outside of the set of paths
|
|
|
|
that access has been granted to. Relative symbolic links may allow access to
|
|
|
|
arbitrary files and directories. When starting applications with the
|
|
|
|
permission model enabled, you must ensure that no paths to which access has
|
|
|
|
been granted contain relative symbolic links.
|
2023-02-23 15:11:51 -03:00
|
|
|
|
2022-09-05 13:07:45 -03:00
|
|
|
[Security Policy]: https://github.com/nodejs/node/blob/main/SECURITY.md
|
2023-12-21 17:44:11 +00:00
|
|
|
[`--allow-addons`]: cli.md#--allow-addons
|
2023-02-23 15:11:51 -03:00
|
|
|
[`--allow-child-process`]: cli.md#--allow-child-process
|
|
|
|
[`--allow-fs-read`]: cli.md#--allow-fs-read
|
|
|
|
[`--allow-fs-write`]: cli.md#--allow-fs-write
|
2024-06-01 10:13:12 -03:00
|
|
|
[`--allow-wasi`]: cli.md#--allow-wasi
|
2023-02-23 15:11:51 -03:00
|
|
|
[`--allow-worker`]: cli.md#--allow-worker
|
2024-12-12 09:11:58 -03:00
|
|
|
[`--permission`]: cli.md#--permission
|
2025-01-13 06:49:45 -03:00
|
|
|
[`npx`]: https://docs.npmjs.com/cli/commands/npx
|
2023-02-23 15:11:51 -03:00
|
|
|
[`permission.has()`]: process.md#processpermissionhasscope-reference
|