From 29e2adc7d0bb29b2de5f68f59ba8e4e3c467ef82 Mon Sep 17 00:00:00 2001 From: theanarkh Date: Mon, 10 Jun 2024 04:44:09 +0800 Subject: [PATCH] permission,net,dgram: add permission for net --- doc/api/cli.md | 54 +++ doc/api/permissions.md | 4 + doc/node.1 | 3 + lib/dgram.js | 63 +++- lib/internal/process/pre_execution.js | 1 + node.gyp | 2 + src/env.cc | 4 + src/node_options.cc | 5 + src/node_options.h | 1 + src/permission/net_permission.cc | 237 ++++++++++++ src/permission/net_permission.h | 75 ++++ src/permission/permission.cc | 5 + src/permission/permission.h | 1 + src/permission/permission_base.h | 7 +- .../test-permission-allow-net-udp-socket.js | 348 ++++++++++++++++++ .../parallel/test-permission-allow-net-udp.js | 181 +++++++++ 16 files changed, 981 insertions(+), 10 deletions(-) create mode 100755 src/permission/net_permission.cc create mode 100755 src/permission/net_permission.h create mode 100644 test/parallel/test-permission-allow-net-udp-socket.js create mode 100644 test/parallel/test-permission-allow-net-udp.js diff --git a/doc/api/cli.md b/doc/api/cli.md index cf64f45bfd4d64..6ce6cb57302fcc 100644 --- a/doc/api/cli.md +++ b/doc/api/cli.md @@ -357,6 +357,57 @@ Error: Access to this API has been restricted } ``` +### `--allow-net-udp` + + + +> Stability: 1.1 - Active development + +When using the [Permission Model][], the process will not be able to create, +receive, and send UDP packages by default. Attempts to do so will throw an +`ERR_ACCESS_DENIED` unless the user explicitly passes the `--allow-net-udp` flag +when starting Node.js. + +The argument format is `--allow-net-udp=domain_or_ip[/netmask][:port]`. +The valid arguments are: + +* `*` - To allow all operations. +* `--allow-net-udp=nodejs.org` +* `--allow-net-udp=127.0.0.1` +* `--allow-net-udp=127.0.0.1:8888` +* `--allow-net-udp=127.0.0.1:*` +* `--allow-net-udp=*:9999` +* `--allow-net-udp=127.0.0.1/24:*` +* `--allow-net-udp=127.0.0.1/255.255.255.0:*` +* `--allow-net-udp=127.0.0.1:8080 --allow-net-udp=127.0.0.1:9090` +* `--allow-net-udp=127.0.0.1:8080,localhost:9090` + +Example: + +```js +const dgram = require('node:dgram'); +dgram.createSocket('udp4').bind(9297, '127.0.0.1') +``` + +```console +$ node --experimental-permission --allow-fs-read=./index.js index.js +node:events:498 + throw er; // Unhandled 'error' event + ^ + +Error [ERR_ACCESS_DENIED]: Access to this API has been restricted. Permission: bind to 127.0.0.1/9297 + at node:dgram:379:18 + at process.processTicksAndRejections (node:internal/process/task_queues:77:11) +Emitted 'error' event on Socket instance at: + at afterDns (node:dgram:337:12) + at node:dgram:379:9 + at process.processTicksAndRejections (node:internal/process/task_queues:77:11) { + code: 'ERR_ACCESS_DENIED' +} +``` + ### `--build-snapshot`