Skip to content

Commit cba1261

Browse files
committed
permission: fix env var default behavior and add manpage docs
Signed-off-by: nabeel378 <mohammadnabeeljameel@gmail.com>
1 parent b6a89cf commit cba1261

File tree

3 files changed

+55
-13
lines changed

3 files changed

+55
-13
lines changed

doc/node.1

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,25 @@ This behavior also applies to \fBchild_process.spawn()\fR, but in that case, the
135135
flags are propagated via the \fBNODE_OPTIONS\fR environment variable rather than
136136
directly through the process arguments.
137137
.
138+
.It Fl -allow-env
139+
When using the Permission Model, the process will be able to access all
140+
environment variables by default.
141+
When the \fB--allow-env\fR flag is used, the process will only be able to access
142+
the specified environment variables.
143+
.Pp
144+
The valid arguments for the \fB--allow-env\fR flag are:
145+
.Bl -bullet
146+
.It
147+
\fB*\fR - To allow access to all environment variables.
148+
.It
149+
Multiple environment variable names can be allowed using multiple
150+
\fB--allow-env\fR flags.
151+
Example \fB--allow-env=HOME --allow-env=PATH\fR
152+
.El
153+
.Bd -literal
154+
$ node --permission --allow-env=HOME --allow-fs-read=* index.js
155+
.Ed
156+
.
138157
.It Fl -allow-ffi
139158
When using the Permission Model, the process will not be able to use
140159
\fBnode:ffi\fR by default.
@@ -1146,6 +1165,8 @@ WASI - manageable through \fB--allow-wasi\fR flag
11461165
Addons - manageable through \fB--allow-addons\fR flag
11471166
.It
11481167
FFI - manageable through \fB--allow-ffi\fR flag
1168+
.It
1169+
Environment Variables - manageable through \fB--allow-env\fR flag
11491170
.El
11501171
.
11511172
.It Fl -permission-audit
@@ -1860,6 +1881,8 @@ one is included in the list below.
18601881
.It
18611882
\fB--allow-child-process\fR
18621883
.It
1884+
\fB--allow-env\fR
1885+
.It
18631886
\fB--allow-ffi\fR
18641887
.It
18651888
\fB--allow-fs-read\fR

src/env.cc

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -952,8 +952,6 @@ Environment::Environment(IsolateData* isolate_data,
952952
if (!options_->allow_env.empty()) {
953953
permission()->Apply(
954954
this, options_->allow_env, permission::PermissionScope::kEnvVar);
955-
} else {
956-
permission()->Apply(this, {}, permission::PermissionScope::kEnvVar);
957955
}
958956

959957
// Implicit allow entrypoint to kFileSystemRead
Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Flags: --permission --allow-fs-read=*
1+
// Flags: --permission --allow-fs-read=* --allow-child-process
22
'use strict';
33

44
const common = require('../common');
@@ -10,25 +10,46 @@ if (!isMainThread) {
1010

1111
const assert = require('assert');
1212

13+
// When --permission is used without --allow-env, env vars should be
14+
// freely accessible (backward compatible behavior).
1315
{
14-
assert.ok(!process.permission.has('env'));
16+
assert.ok(process.permission.has('env'));
1517
}
1618

1719
{
18-
assert.strictEqual(process.env.HOME, undefined);
19-
assert.strictEqual(process.env.PATH, undefined);
20+
// Environment variables should be readable
21+
assert.ok(typeof process.env.HOME === 'string' || process.env.HOME === undefined);
2022
}
2123

2224
{
23-
assert.throws(() => {
24-
process.env.TEST_VAR = 'value';
25-
}, common.expectsError({
26-
code: 'ERR_ACCESS_DENIED',
27-
permission: 'EnvVar',
28-
}));
25+
// Setting env vars should work
26+
process.env.__TEST_PERMISSION_ENV = 'test';
27+
assert.strictEqual(process.env.__TEST_PERMISSION_ENV, 'test');
28+
delete process.env.__TEST_PERMISSION_ENV;
2929
}
3030

3131
{
32+
// Object.keys should return env vars
3233
const keys = Object.keys(process.env);
33-
assert.strictEqual(keys.length, 0);
34+
assert.ok(keys.length > 0);
35+
}
36+
37+
// Test that restriction activates when --allow-env is explicitly used
38+
{
39+
const { spawnSync } = require('child_process');
40+
const { status, stderr } = spawnSync(process.execPath, [
41+
'--permission',
42+
'--allow-fs-read=*',
43+
'--allow-env=__NONEXISTENT_VAR__',
44+
'-e',
45+
`
46+
const assert = require('assert');
47+
assert.ok(!process.permission.has('env'));
48+
assert.strictEqual(process.env.HOME, undefined);
49+
assert.strictEqual(process.env.PATH, undefined);
50+
assert.throws(() => { process.env.X = '1'; }, { code: 'ERR_ACCESS_DENIED' });
51+
assert.strictEqual(Object.keys(process.env).length, 0);
52+
`,
53+
]);
54+
assert.strictEqual(status, 0, `child stderr: ${stderr}`);
3455
}

0 commit comments

Comments
 (0)