Skip to content

Commit 6ad4758

Browse files
src: add permission support to config file
PR-URL: #60746 Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com> Reviewed-By: Pietro Marchini <pietro.marchini94@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
1 parent a072411 commit 6ad4758

12 files changed

+245
-8
lines changed

β€Ždoc/api/permissions.mdβ€Ž

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,34 @@ does not exist, the wildcard will not be added, and access will be limited to
151151
yet, make sure to explicitly include the wildcard:
152152
`/my-path/folder-do-not-exist/*`.
153153

154+
#### Configuration file support
155+
156+
In addition to passing permission flags on the command line, they can also be
157+
declared in a Node.js configuration file when using the experimental
158+
\[`--experimental-config-file`]\[] flag. Permission options must be placed inside
159+
the `permission` top-level object.
160+
161+
Example `node.config.json`:
162+
163+
```json
164+
{
165+
"permission": {
166+
"allow-fs-read": ["./foo"],
167+
"allow-fs-write": ["./bar"],
168+
"allow-child-process": true,
169+
"allow-worker": true,
170+
"allow-net": true,
171+
"allow-addons": false
172+
}
173+
}
174+
```
175+
176+
Run with the configuration file:
177+
178+
```console
179+
$ node --permission --experimental-default-config-file app.js
180+
```
181+
154182
#### Using the Permission Model with `npx`
155183

156184
If you're using [`npx`][] to execute a Node.js script, you can enable the

β€Ždoc/node-config-schema.jsonβ€Ž

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -600,6 +600,58 @@
600600
},
601601
"type": "object"
602602
},
603+
"permission": {
604+
"type": "object",
605+
"additionalProperties": false,
606+
"properties": {
607+
"allow-addons": {
608+
"type": "boolean"
609+
},
610+
"allow-child-process": {
611+
"type": "boolean"
612+
},
613+
"allow-fs-read": {
614+
"oneOf": [
615+
{
616+
"type": "string"
617+
},
618+
{
619+
"items": {
620+
"type": "string",
621+
"minItems": 1
622+
},
623+
"type": "array"
624+
}
625+
]
626+
},
627+
"allow-fs-write": {
628+
"oneOf": [
629+
{
630+
"type": "string"
631+
},
632+
{
633+
"items": {
634+
"type": "string",
635+
"minItems": 1
636+
},
637+
"type": "array"
638+
}
639+
]
640+
},
641+
"allow-inspector": {
642+
"type": "boolean"
643+
},
644+
"allow-net": {
645+
"type": "boolean"
646+
},
647+
"allow-wasi": {
648+
"type": "boolean"
649+
},
650+
"allow-worker": {
651+
"type": "boolean"
652+
}
653+
}
654+
},
603655
"testRunner": {
604656
"type": "object",
605657
"additionalProperties": false,

β€Žsrc/node_options.ccβ€Ž

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -607,31 +607,43 @@ EnvironmentOptionsParser::EnvironmentOptionsParser() {
607607
AddOption("--allow-fs-read",
608608
"allow permissions to read the filesystem",
609609
&EnvironmentOptions::allow_fs_read,
610-
kAllowedInEnvvar);
610+
kAllowedInEnvvar,
611+
OptionNamespaces::kPermissionNamespace);
611612
AddOption("--allow-fs-write",
612613
"allow permissions to write in the filesystem",
613614
&EnvironmentOptions::allow_fs_write,
614-
kAllowedInEnvvar);
615+
kAllowedInEnvvar,
616+
OptionNamespaces::kPermissionNamespace);
615617
AddOption("--allow-addons",
616618
"allow use of addons when any permissions are set",
617619
&EnvironmentOptions::allow_addons,
618-
kAllowedInEnvvar);
620+
kAllowedInEnvvar,
621+
false,
622+
OptionNamespaces::kPermissionNamespace);
619623
AddOption("--allow-child-process",
620624
"allow use of child process when any permissions are set",
621625
&EnvironmentOptions::allow_child_process,
622-
kAllowedInEnvvar);
626+
kAllowedInEnvvar,
627+
false,
628+
OptionNamespaces::kPermissionNamespace);
623629
AddOption("--allow-inspector",
624630
"allow use of inspector when any permissions are set",
625631
&EnvironmentOptions::allow_inspector,
626-
kAllowedInEnvvar);
632+
kAllowedInEnvvar,
633+
false,
634+
OptionNamespaces::kPermissionNamespace);
627635
AddOption("--allow-wasi",
628636
"allow wasi when any permissions are set",
629637
&EnvironmentOptions::allow_wasi,
630-
kAllowedInEnvvar);
638+
kAllowedInEnvvar,
639+
false,
640+
OptionNamespaces::kPermissionNamespace);
631641
AddOption("--allow-worker",
632642
"allow worker threads when any permissions are set",
633643
&EnvironmentOptions::allow_worker_threads,
634-
kAllowedInEnvvar);
644+
kAllowedInEnvvar,
645+
false,
646+
OptionNamespaces::kPermissionNamespace);
635647
AddOption("--experimental-repl-await",
636648
"experimental await keyword support in REPL",
637649
&EnvironmentOptions::experimental_repl_await,

β€Žsrc/node_options.hβ€Ž

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,8 @@ std::vector<std::string> MapAvailableNamespaces();
414414
#define OPTION_NAMESPACE_LIST(V) \
415415
V(kNoNamespace, "") \
416416
V(kTestRunnerNamespace, "testRunner") \
417-
V(kWatchNamespace, "watch")
417+
V(kWatchNamespace, "watch") \
418+
V(kPermissionNamespace, "permission")
418419

419420
enum class OptionNamespaces {
420421
#define V(name, _) name,
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
const { spawnSync } = require('child_process');
2+
spawnSync(process.execPath, ['--version']);
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"permission": {
3+
"allow-addons": true,
4+
"allow-wasi": true
5+
}
6+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"permission": {
3+
"allow-child-process": true,
4+
"allow-worker": true
5+
}
6+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"permission": {
3+
"allow-fs-read": [
4+
"*"
5+
],
6+
"allow-fs-write": [
7+
"*"
8+
]
9+
}
10+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"permission": {
3+
"allow-net": true,
4+
"allow-inspector": true
5+
}
6+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
require('fs').readFileSync(__filename);

0 commit comments

Comments
Β (0)