Skip to content

Commit 0b6ae6d

Browse files
src: add permission support to config file
PR-URL: #60746 Reviewed-By: Rafael Gonzaga <[email protected]> Reviewed-By: Pietro Marchini <[email protected]> Reviewed-By: Colin Ihrig <[email protected]>
1 parent 6c306b6 commit 0b6ae6d

12 files changed

+248
-9
lines changed

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

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

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

158186
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
@@ -603,6 +603,58 @@
603603
},
604604
"type": "object"
605605
},
606+
"permission": {
607+
"type": "object",
608+
"additionalProperties": false,
609+
"properties": {
610+
"allow-addons": {
611+
"type": "boolean"
612+
},
613+
"allow-child-process": {
614+
"type": "boolean"
615+
},
616+
"allow-fs-read": {
617+
"oneOf": [
618+
{
619+
"type": "string"
620+
},
621+
{
622+
"items": {
623+
"type": "string",
624+
"minItems": 1
625+
},
626+
"type": "array"
627+
}
628+
]
629+
},
630+
"allow-fs-write": {
631+
"oneOf": [
632+
{
633+
"type": "string"
634+
},
635+
{
636+
"items": {
637+
"type": "string",
638+
"minItems": 1
639+
},
640+
"type": "array"
641+
}
642+
]
643+
},
644+
"allow-inspector": {
645+
"type": "boolean"
646+
},
647+
"allow-net": {
648+
"type": "boolean"
649+
},
650+
"allow-wasi": {
651+
"type": "boolean"
652+
},
653+
"allow-worker": {
654+
"type": "boolean"
655+
}
656+
}
657+
},
606658
"testRunner": {
607659
"type": "object",
608660
"additionalProperties": false,

β€Žsrc/node_options.ccβ€Ž

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -603,35 +603,49 @@ EnvironmentOptionsParser::EnvironmentOptionsParser() {
603603
AddOption("--allow-fs-read",
604604
"allow permissions to read the filesystem",
605605
&EnvironmentOptions::allow_fs_read,
606-
kAllowedInEnvvar);
606+
kAllowedInEnvvar,
607+
OptionNamespaces::kPermissionNamespace);
607608
AddOption("--allow-fs-write",
608609
"allow permissions to write in the filesystem",
609610
&EnvironmentOptions::allow_fs_write,
610-
kAllowedInEnvvar);
611+
kAllowedInEnvvar,
612+
OptionNamespaces::kPermissionNamespace);
611613
AddOption("--allow-addons",
612614
"allow use of addons when any permissions are set",
613615
&EnvironmentOptions::allow_addons,
614-
kAllowedInEnvvar);
616+
kAllowedInEnvvar,
617+
false,
618+
OptionNamespaces::kPermissionNamespace);
615619
AddOption("--allow-child-process",
616620
"allow use of child process when any permissions are set",
617621
&EnvironmentOptions::allow_child_process,
618-
kAllowedInEnvvar);
622+
kAllowedInEnvvar,
623+
false,
624+
OptionNamespaces::kPermissionNamespace);
619625
AddOption("--allow-inspector",
620626
"allow use of inspector when any permissions are set",
621627
&EnvironmentOptions::allow_inspector,
622-
kAllowedInEnvvar);
628+
kAllowedInEnvvar,
629+
false,
630+
OptionNamespaces::kPermissionNamespace);
623631
AddOption("--allow-net",
624632
"allow use of network when any permissions are set",
625633
&EnvironmentOptions::allow_net,
626-
kAllowedInEnvvar);
634+
kAllowedInEnvvar,
635+
false,
636+
OptionNamespaces::kPermissionNamespace);
627637
AddOption("--allow-wasi",
628638
"allow wasi when any permissions are set",
629639
&EnvironmentOptions::allow_wasi,
630-
kAllowedInEnvvar);
640+
kAllowedInEnvvar,
641+
false,
642+
OptionNamespaces::kPermissionNamespace);
631643
AddOption("--allow-worker",
632644
"allow worker threads when any permissions are set",
633645
&EnvironmentOptions::allow_worker_threads,
634-
kAllowedInEnvvar);
646+
kAllowedInEnvvar,
647+
false,
648+
OptionNamespaces::kPermissionNamespace);
635649
AddOption("--experimental-repl-await",
636650
"experimental await keyword support in REPL",
637651
&EnvironmentOptions::experimental_repl_await,

β€Žsrc/node_options.hβ€Ž

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,8 @@ std::vector<std::string> MapAvailableNamespaces();
416416
#define OPTION_NAMESPACE_LIST(V) \
417417
V(kNoNamespace, "") \
418418
V(kTestRunnerNamespace, "testRunner") \
419-
V(kWatchNamespace, "watch")
419+
V(kWatchNamespace, "watch") \
420+
V(kPermissionNamespace, "permission")
420421

421422
enum class OptionNamespaces {
422423
#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)