Skip to content

Commit 31e4b5f

Browse files
committed
doc: add documentation for --allow-env flag in CLI and permissions
Signed-off-by: nabeel378 <[email protected]>
1 parent efce03f commit 31e4b5f

3 files changed

Lines changed: 52 additions & 1 deletion

File tree

doc/api/cli.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,45 @@ This behavior also applies to `child_process.spawn()`, but in that case, the
191191
flags are propagated via the `NODE_OPTIONS` environment variable rather than
192192
directly through the process arguments.
193193

194+
### `--allow-env`
195+
196+
> Stability: 1.1 - Active development
197+
198+
When using the [Permission Model][], access to environment variables is
199+
restricted by default unless the user explicitly passes the `--allow-env`
200+
flag when starting Node.js.
201+
202+
* Reading a restricted variable (`process.env.HOME`) silently returns
203+
`undefined`.
204+
* Writing (`process.env.FOO = 'bar'`) or deleting (`delete process.env.FOO`)
205+
a restricted variable throws `ERR_ACCESS_DENIED`.
206+
207+
The valid arguments for the `--allow-env` flag are:
208+
209+
* `*` - To allow access to all environment variables.
210+
* Specific environment variable names can be allowed using a comma-separated
211+
list. Example: `--allow-env=HOME,PATH,NODE_ENV`
212+
213+
Example:
214+
215+
```js
216+
console.log(process.env.HOME); // undefined (silently denied)
217+
process.env.FOO = 'bar'; // ERR_ACCESS_DENIED (throws)
218+
```
219+
220+
```console
221+
$ node --permission --allow-fs-read=* index.js
222+
node:internal/process/per_thread:12
223+
throw new ERR_ACCESS_DENIED('EnvVar', name);
224+
^
225+
226+
Error: Access to this API has been restricted
227+
at node:internal/main/run_main_module:17:47 {
228+
code: 'ERR_ACCESS_DENIED',
229+
permission: 'EnvVar'
230+
}
231+
```
232+
194233
### `--allow-ffi`
195234

196235
<!-- YAML
@@ -2224,6 +2263,7 @@ following permissions are restricted:
22242263
* Worker Threads - manageable through [`--allow-worker`][] flag
22252264
* WASI - manageable through [`--allow-wasi`][] flag
22262265
* Addons - manageable through [`--allow-addons`][] flag
2266+
* Environment Variables - manageable through [`--allow-env`][] flag
22272267
* FFI - manageable through [`--allow-ffi`](#--allow-ffi) flag
22282268

22292269
### `--permission-audit`
@@ -3671,6 +3711,7 @@ one is included in the list below.
36713711

36723712
* `--allow-addons`
36733713
* `--allow-child-process`
3714+
* `--allow-env`
36743715
* `--allow-ffi`
36753716
* `--allow-fs-read`
36763717
* `--allow-fs-write`
@@ -4309,6 +4350,7 @@ node --stack-trace-limit=12 -p -e "Error.stackTraceLimit" # prints 12
43094350
[`"type"`]: packages.md#type
43104351
[`--allow-addons`]: #--allow-addons
43114352
[`--allow-child-process`]: #--allow-child-process
4353+
[`--allow-env`]: #--allow-env
43124354
[`--allow-fs-read`]: #--allow-fs-read
43134355
[`--allow-fs-write`]: #--allow-fs-write
43144356
[`--allow-net`]: #--allow-net

doc/api/permissions.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ using the [`--allow-child-process`][] and [`--allow-worker`][] respectively.
7171
To allow network access, use [`--allow-net`][] and for allowing native addons
7272
when using permission model, use the [`--allow-addons`][]
7373
flag. For WASI, use the [`--allow-wasi`][] flag. For FFI, use the
74-
[`--allow-ffi`][] flag. The [`node:ffi`](ffi.md) module also requires the
74+
[`--allow-ffi`][] flag. For environment variables, use the
75+
[`--allow-env`][] flag. The [`node:ffi`](ffi.md) module also requires the
7576
`--experimental-ffi` flag and is only available in builds with FFI support.
7677

7778
#### Runtime API
@@ -283,6 +284,7 @@ Developers relying on --permission to sandbox untrusted code should be aware tha
283284
[Security Policy]: https://github.com/nodejs/node/blob/main/SECURITY.md
284285
[`--allow-addons`]: cli.md#--allow-addons
285286
[`--allow-child-process`]: cli.md#--allow-child-process
287+
[`--allow-env`]: cli.md#--allow-env
286288
[`--allow-ffi`]: cli.md#--allow-ffi
287289
[`--allow-fs-read`]: cli.md#--allow-fs-read
288290
[`--allow-fs-write`]: cli.md#--allow-fs-write

doc/api/process.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1559,6 +1559,12 @@ changes:
15591559
The `process.env` property returns an object containing the user environment.
15601560
See environ(7).
15611561
1562+
When the [Permission Model][] is enabled, access to environment variables is
1563+
restricted. Reading a restricted variable silently returns `undefined`, while
1564+
writing or deleting throws `ERR_ACCESS_DENIED`. Use `--allow-env` to grant
1565+
access to all variables or `--allow-env=HOME,PATH` to grant access to specific
1566+
ones. See the [`--allow-env`][] documentation for more details.
1567+
15621568
An example of this object looks like:
15631569
15641570
<!-- eslint-skip -->
@@ -4596,6 +4602,7 @@ cases:
45964602
[`'message'`]: child_process.md#event-message
45974603
[`'uncaughtException'`]: #event-uncaughtexception
45984604
[`--no-deprecation`]: cli.md#--no-deprecation
4605+
[`--allow-env`]: cli.md#--allow-env
45994606
[`--permission`]: cli.md#--permission
46004607
[`--unhandled-rejections`]: cli.md#--unhandled-rejectionsmode
46014608
[`Buffer`]: buffer.md

0 commit comments

Comments
 (0)