From 9c645ef0f414f17ad80aecd31df5ce31aaa261aa Mon Sep 17 00:00:00 2001 From: theanarkh Date: Mon, 10 Jun 2024 04:44:09 +0800 Subject: [PATCH] permission,dns: add permission for dns --- doc/api/cli.md | 42 ++++++++++++++++ doc/api/permissions.md | 1 + src/cares_wrap.cc | 11 +++-- src/cares_wrap.h | 1 - src/env.cc | 5 ++ src/node_options.cc | 4 ++ src/node_options.h | 1 + src/permission/net_dns_permission.cc | 23 +++++++++ src/permission/net_dns_permission.h | 32 ++++++++++++ src/permission/permission.cc | 5 ++ src/permission/permission.h | 1 + src/permission/permission_base.h | 5 +- test/parallel/test-permission-dns.js | 74 ++++++++++++++++++++++++++++ 13 files changed, 199 insertions(+), 6 deletions(-) create mode 100644 src/permission/net_dns_permission.cc create mode 100644 src/permission/net_dns_permission.h create mode 100644 test/parallel/test-permission-dns.js diff --git a/doc/api/cli.md b/doc/api/cli.md index cf64f45bfd4d64..03a445b7ab2043 100644 --- a/doc/api/cli.md +++ b/doc/api/cli.md @@ -357,6 +357,48 @@ Error: Access to this API has been restricted } ``` +### `--allow-net-dns` + + + +> Stability: 1.1 - Active development + +When using the [Permission Model][], the process will not be able to make dns query +by default. +Attempts to do so will throw an `ERR_ACCESS_DENIED` unless the +user explicitly passes the `--allow-net-dns` flag when starting Node.js. + +Example: + +```js +const dns = require('node:dns'); +dns.lookup("localhost", () => {}); +``` + +```console +$ node --experimental-permission --allow-fs-read=\* index.js +node:dns:235 + const err = cares.getaddrinfo( + ^ + +Error: Access to this API has been restricted + at Object.lookup (node:dns:235:21) + at Object. (/home/index.js:2:5) + at Module._compile (node:internal/modules/cjs/loader:1460:14) + at Module._extensions..js (node:internal/modules/cjs/loader:1544:10) + at Module.load (node:internal/modules/cjs/loader:1275:32) + at Module._load (node:internal/modules/cjs/loader:1091:12) + at wrapModuleLoad (node:internal/modules/cjs/loader:212:19) + at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:158:5) + at node:internal/main/run_main_module:30:49 { + code: 'ERR_ACCESS_DENIED', + permission: 'NetDNS', + resource: 'lookup' +} +``` + ### `--build-snapshot`