2020-05-06 18:21:13 -04:00
|
|
|
## Permissions
|
|
|
|
|
2020-05-26 07:12:07 -04:00
|
|
|
Deno is secure by default. Therefore, unless you specifically enable it, a deno
|
|
|
|
module has no file, network, or environment access for example. Access to
|
2020-08-03 22:57:57 -04:00
|
|
|
security-sensitive areas or functions requires the use of permissions to be
|
2020-05-26 07:12:07 -04:00
|
|
|
granted to a deno process on the command line.
|
2020-05-06 18:21:13 -04:00
|
|
|
|
2020-05-26 07:12:07 -04:00
|
|
|
For the following example, `mod.ts` has been granted read-only access to the
|
2020-08-03 22:57:57 -04:00
|
|
|
file system. It cannot write to it, or perform any other security-sensitive
|
2020-05-26 07:12:07 -04:00
|
|
|
functions.
|
|
|
|
|
|
|
|
```shell
|
|
|
|
deno run --allow-read mod.ts
|
|
|
|
```
|
|
|
|
|
|
|
|
### Permissions list
|
|
|
|
|
|
|
|
The following permissions are available:
|
|
|
|
|
|
|
|
- **-A, --allow-all** Allow all permissions. This disables all security.
|
2021-06-10 06:49:24 -04:00
|
|
|
- **--allow-env=\<allow-env\>** Allow environment access for things like getting
|
|
|
|
and setting of environment variables. Since Deno 1.9, you can specify a
|
|
|
|
optional, comma-separated list of environment variables to provide an
|
|
|
|
allow-list of allowed environment variables.
|
2020-08-03 22:57:57 -04:00
|
|
|
- **--allow-hrtime** Allow high-resolution time measurement. High-resolution
|
2020-05-26 07:12:07 -04:00
|
|
|
time can be used in timing attacks and fingerprinting.
|
|
|
|
- **--allow-net=\<allow-net\>** Allow network access. You can specify an
|
2020-08-03 22:57:57 -04:00
|
|
|
optional, comma-separated list of domains to provide an allow-list of allowed
|
2020-05-26 07:12:07 -04:00
|
|
|
domains.
|
|
|
|
- **--allow-plugin** Allow loading plugins. Please note that --allow-plugin is
|
|
|
|
an unstable feature.
|
|
|
|
- **--allow-read=\<allow-read\>** Allow file system read access. You can specify
|
2021-05-27 03:48:05 -04:00
|
|
|
an optional, comma-separated list of directories or files to provide an
|
2020-06-13 13:09:39 -04:00
|
|
|
allow-list of allowed file system access.
|
2021-06-14 09:48:10 -04:00
|
|
|
- **--allow-run=\<allow-run\>** Allow running subprocesses. Since Deno 1.9, You
|
2021-06-10 06:49:24 -04:00
|
|
|
can specify an options, comma-separated list of subprocesses to provide an
|
|
|
|
allow-list of allowed subprocesses. Be aware that subprocesses are not run in
|
|
|
|
a sandbox and therefore do not have the same security restrictions as the deno
|
|
|
|
process. Therefore, use with caution.
|
2020-05-26 07:12:07 -04:00
|
|
|
- **--allow-write=\<allow-write\>** Allow file system write access. You can
|
2021-05-27 03:48:05 -04:00
|
|
|
specify an optional, comma-separated list of directories or files to provide
|
|
|
|
an allow-list of allowed file system access.
|
2020-05-06 18:21:13 -04:00
|
|
|
|
2020-06-13 13:09:39 -04:00
|
|
|
### Permissions allow-list
|
2020-05-06 18:21:13 -04:00
|
|
|
|
2020-05-26 07:12:07 -04:00
|
|
|
Deno also allows you to control the granularity of some permissions with
|
2020-06-13 13:09:39 -04:00
|
|
|
allow-lists.
|
2020-05-06 18:21:13 -04:00
|
|
|
|
2020-06-13 13:09:39 -04:00
|
|
|
This example restricts file system access by allow-listing only the `/usr`
|
2020-05-26 07:12:07 -04:00
|
|
|
directory, however the execution fails as the process was attempting to access a
|
|
|
|
file in the `/etc` directory:
|
2020-05-06 18:21:13 -04:00
|
|
|
|
|
|
|
```shell
|
2020-07-31 05:12:20 -04:00
|
|
|
$ deno run --allow-read=/usr https://deno.land/std@$STD_VERSION/examples/cat.ts /etc/passwd
|
2020-05-06 18:21:13 -04:00
|
|
|
error: Uncaught PermissionDenied: read access to "/etc/passwd", run again with the --allow-read flag
|
|
|
|
► $deno$/dispatch_json.ts:40:11
|
|
|
|
at DenoError ($deno$/errors.ts:20:5)
|
|
|
|
...
|
|
|
|
```
|
|
|
|
|
2020-06-13 13:09:39 -04:00
|
|
|
Try it out again with the correct permissions by allow-listing `/etc` instead:
|
2020-05-06 18:21:13 -04:00
|
|
|
|
|
|
|
```shell
|
2020-07-31 05:12:20 -04:00
|
|
|
deno run --allow-read=/etc https://deno.land/std@$STD_VERSION/examples/cat.ts /etc/passwd
|
2020-05-06 18:21:13 -04:00
|
|
|
```
|
|
|
|
|
2020-05-18 07:31:18 -04:00
|
|
|
`--allow-write` works the same as `--allow-read`.
|
2020-05-06 18:21:13 -04:00
|
|
|
|
2020-05-21 06:54:09 -04:00
|
|
|
### Network access:
|
|
|
|
|
|
|
|
_fetch.ts_:
|
2020-05-06 18:21:13 -04:00
|
|
|
|
|
|
|
```ts
|
|
|
|
const result = await fetch("https://deno.land/");
|
|
|
|
```
|
|
|
|
|
2020-08-03 22:57:57 -04:00
|
|
|
This is an example of how to allow-list hosts/urls:
|
2020-05-21 06:54:09 -04:00
|
|
|
|
|
|
|
```shell
|
2020-06-10 20:24:41 -04:00
|
|
|
deno run --allow-net=github.com,deno.land fetch.ts
|
2020-05-21 06:54:09 -04:00
|
|
|
```
|
|
|
|
|
2020-05-26 07:12:07 -04:00
|
|
|
If `fetch.ts` tries to establish network connections to any other domain, the
|
|
|
|
process will fail.
|
|
|
|
|
2020-05-21 06:54:09 -04:00
|
|
|
Allow net calls to any host/url:
|
|
|
|
|
2020-05-06 18:21:13 -04:00
|
|
|
```shell
|
2020-06-10 20:24:41 -04:00
|
|
|
deno run --allow-net fetch.ts
|
2020-05-06 18:21:13 -04:00
|
|
|
```
|
2020-11-05 20:35:17 -05:00
|
|
|
|
|
|
|
### Conference
|
|
|
|
|
|
|
|
Ryan Dahl. (September 25, 2020).
|
|
|
|
[The Deno security model](https://www.youtube.com/watch?v=r5F6dekUmdE#t=34m57).
|
|
|
|
Speakeasy JS.
|