Skip to content

Commit 591de5d

Browse files
committed
doc: fix --allow-env docs to describe opt-in model
The documentation incorrectly described env var restriction as deny-by-default when --permission is used. In reality, env vars are unrestricted by default and only become restricted when --allow-env is explicitly passed. Update cli.md and process.md to accurately reflect this behavior. Signed-off-by: nabeel378 <[email protected]>
1 parent 5131c17 commit 591de5d

2 files changed

Lines changed: 21 additions & 11 deletions

File tree

doc/api/cli.md

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -196,29 +196,37 @@ directly through the process arguments.
196196
> Stability: 1.1 - Active development
197197
198198
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.
199+
unrestricted by default. To restrict access, the `--allow-env` flag must be
200+
explicitly passed when starting Node.js. Once `--allow-env` is specified,
201+
only the listed environment variables are accessible.
201202

202203
* Reading a restricted variable (`process.env.HOME`) silently returns
203204
`undefined`.
204205
* Writing (`process.env.FOO = 'bar'`) or deleting (`delete process.env.FOO`)
205206
a restricted variable throws `ERR_ACCESS_DENIED`.
207+
* Enumerating (`Object.keys(process.env)`) only returns allowed variables.
206208

207209
The valid arguments for the `--allow-env` flag are:
208210

209211
* `*` - To allow access to all environment variables.
210212
* Specific environment variable names can be allowed using a comma-separated
211213
list. Example: `--allow-env=HOME,PATH,NODE_ENV`
212214

213-
Example:
215+
Examples:
214216

215-
```js
216-
console.log(process.env.HOME); // undefined (silently denied)
217-
process.env.FOO = 'bar'; // ERR_ACCESS_DENIED (throws)
217+
```console
218+
$ node --permission --allow-fs-read=* -p 'process.env.HOME'
219+
/Users/user
220+
$ node --permission --allow-env=PATH --allow-fs-read=* -p 'process.env.HOME'
221+
undefined
222+
$ node --permission --allow-env=HOME --allow-fs-read=* -p 'process.env.HOME'
223+
/Users/user
218224
```
219225

226+
Attempting to write a restricted variable throws:
227+
220228
```console
221-
$ node --permission index.js
229+
$ node --permission --allow-env=HOME --allow-fs-read=* -e "process.env.FOO = 'bar'"
222230
node:internal/process/per_thread:12
223231
throw new ERR_ACCESS_DENIED('EnvVar', name);
224232
^

doc/api/process.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1560,10 +1560,12 @@ The `process.env` property returns an object containing the user environment.
15601560
See environ(7).
15611561
15621562
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.
1563+
unrestricted by default. To restrict access, the `--allow-env` flag must be
1564+
explicitly passed. Once `--allow-env` is specified, reading a restricted
1565+
variable silently returns `undefined`, while writing or deleting throws
1566+
`ERR_ACCESS_DENIED`. Use `--allow-env=*` to grant access to all variables or
1567+
`--allow-env=HOME,PATH` to grant access to specific ones. See the
1568+
[`--allow-env`][] documentation for more details.
15671569
15681570
An example of this object looks like:
15691571

0 commit comments

Comments
 (0)