Skip to content

Commit e83869a

Browse files
committed
doc: policy permission docs
1 parent 895dd16 commit e83869a

1 file changed

Lines changed: 125 additions & 12 deletions

File tree

doc/api/policy.md

Lines changed: 125 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,20 @@
77
88
<!-- name=policy -->
99

10-
Node.js contains experimental support for creating policies on loading code.
10+
Node.js contains experimental support for creating policies on loading code
11+
and performing various core functions.
1112

12-
Policies are a security feature intended to allow guarantees
13-
about what code Node.js is able to load. The use of policies assumes
14-
safe practices for the policy files such as ensuring that policy
13+
Node.js supports two experimental policy mechanisms:
14+
15+
* One that performs verification checks on loaded code before it can be
16+
executed.
17+
* One that limits the system functions loaded code may perform.
18+
19+
## Integrity Policies
20+
21+
Integrity Policies are a security feature intended to allow guarantees
22+
about what code Node.js is able to load. The use of integrity policies
23+
assumes safe practices for the policy files such as ensuring that policy
1524
files cannot be overwritten by the Node.js application by using
1625
file permissions.
1726

@@ -21,12 +30,12 @@ by the running Node.js application in any way. A typical setup would be to
2130
create the policy file as a different user id than the one running Node.js
2231
and granting read permissions to the user id running Node.js.
2332

24-
## Enabling
33+
### Enabling
2534

2635
<!-- type=misc -->
2736

28-
The `--experimental-policy` flag can be used to enable features for policies
29-
when loading modules.
37+
The `--experimental-policy` flag can be used to enable the integrity policy
38+
feature when loading modules.
3039

3140
Once this has been set, all modules must conform to a policy manifest file
3241
passed to the flag:
@@ -47,9 +56,9 @@ even if the file is changed on disk.
4756
node --experimental-policy=policy.json --policy-integrity="sha384-SggXRQHwCG8g+DktYYzxkXRIkTiEYWBHqev0xnpCxYlqMBufKZHAHQM3/boDaI/0" app.js
4857
```
4958

50-
## Features
59+
### Features
5160

52-
### Error Behavior
61+
#### Error Behavior
5362

5463
When a policy check fails, Node.js by default will throw an error.
5564
It is possible to change the error behavior to one of a few possibilities
@@ -73,7 +82,7 @@ available to change the behavior:
7382
}
7483
```
7584

76-
### Integrity Checks
85+
#### Integrity Checks
7786

7887
Policy files must use integrity checks with Subresource Integrity strings
7988
compatible with the browser
@@ -115,7 +124,7 @@ body for the resource which can be useful for local development. It is not
115124
recommended in production since it would allow unexpected alteration of
116125
resources to be considered valid.
117126

118-
### Dependency Redirection
127+
#### Dependency Redirection
119128

120129
An application may need to ship patched versions of modules or to prevent
121130
modules from allowing all modules access to all other modules. Redirection
@@ -164,7 +173,7 @@ module to load any specifier without redirection. This can be useful for local
164173
development and may have some valid usage in production, but should be used
165174
only with care after auditing a module to ensure its behavior is valid.
166175

167-
#### Example: Patched Dependency
176+
##### Example: Patched Dependency
168177

169178
Redirected dependencies can provide attenuated or modified functionality as fits
170179
the application. For example, log data about timing of function durations by
@@ -184,4 +193,108 @@ module.exports = function fn(...args) {
184193
};
185194
```
186195

196+
## Permission Policies
197+
198+
Permission policies allow granting or denying permissions for running code to
199+
perform various system calls.
200+
201+
This feature is experimental and needs to be enabled by passing either or both
202+
of the `--policy-deny=` or `--policy-grant` CLI flags to Node.js.
203+
204+
Both flags take a comma-separated list of permission identifiers as described
205+
below. For example, `--policy-deny=fs,net.udp`.
206+
207+
### Permissions
208+
209+
The permission identifiers are:
210+
211+
* `*` - Wildcard for all permissions.
212+
* `special` - Special categories are denied by default if any other permission
213+
is denied.
214+
* `special.inspector` - Controls ability to use Inspector APIs (not yet
215+
active)
216+
* `special.addons` - Controls ability to load native addons / use
217+
`process.dlopen()`
218+
* `special.child_process` - Controls ability to spawn child processes
219+
* `workers` - Controls ability to spawn worker threads
220+
* `fs` - Controls all file system operations
221+
* `fs.in` - Controls file system read operations
222+
* `fs.out` - Controls file system write operations
223+
* `user` - Controls access to user data methods (e.g. `os.homedir()`,
224+
`os.userInfo()`)
225+
* `net` - Controls access to networking APIs
226+
* `net.udp` - Controls access to connectionless UDP
227+
* `net.dns` - Controls access to DNS APIs.
228+
* `net.tcp` - Controls access to all TCP APIs
229+
* `net.tcp.in` - Controls access to TCP `listen()` operations
230+
* `net.tcp.out` - Controls access to TCP `connect()` operations
231+
* `net.tls` - Controls access to TLS wrap APIs.
232+
* `net.tls.log` - Controls access to TLS keylogging
233+
* `process` - Controls access to APIs that change process state
234+
* `timing` - Controls access to timing APIs (e.g. `process.hrtime()`,
235+
`performance.now()`, `performance.mark()`, etc. Has no impact on the
236+
use of `new Date()`)
237+
* `signal` - Controls ability to send signals (e.g. `process.kill()`)
238+
* `experimental` - Controls access to experimental APIs
239+
* `experimental.wasi` - Controls access to experimental WASI apis.
240+
241+
Permission identifiers are hierarchical. For instance
242+
`--policy-deny=net` will deny access to all `net.*` identifiers.
243+
244+
### `--policy-deny` and `--policy-grant`
245+
246+
The `--policy-deny` and `--policy-grant` command-line flags specify the
247+
policy permissions for the Node.js process at startup. Upon initialization,
248+
the `--policy-deny` list will be processed first, followed by the
249+
`--policy-grant` list.
250+
251+
For instance,
252+
253+
```
254+
--policy-deny=* --policy-grant=fs.in
255+
```
256+
257+
Will deny all permissions except for `fs.in`, while:
258+
259+
```
260+
--policy-deny=fs.in --policy-grant=*
261+
```
262+
263+
Will result in all permissions being granted.
264+
265+
To disable all network capabilities other than DNS, the combination would be:
266+
267+
```
268+
--policy-deny=net --policy-grant=net.dns
269+
```
270+
271+
### `process.policy` API
272+
273+
The `process.policy` API allows the current enforced policy to be
274+
inspected at runtime and permits the ability to further restrict the
275+
current policy via the `process.policy.deny()` method. It is not
276+
possible to grant permissions via the JavaScript API.
277+
278+
#### `process.policy.granted(permission)`
279+
280+
* `permission`: {string} A permission identifier
281+
* Return: {boolean} Returns `true` if the permission is granted.
282+
283+
```js
284+
if (!process.policy.granted('fs.in'))
285+
console.log('File system read is not permitted!')
286+
```
287+
288+
#### `process.policy.deny(permissions)
289+
290+
* `permissions`: {string} A comma-separated list of permission identifiers.
291+
292+
```js
293+
process.policy.deny('fs,workers');
294+
```
295+
296+
Further restricts the current permission policy by denying the specified
297+
permissions. The input `permissions` parameter uses the same syntax as
298+
the `--policy-deny=` command-line flag.
299+
187300
[relative url string]: https://url.spec.whatwg.org/#relative-url-with-fragment-string

0 commit comments

Comments
 (0)