From 75e5612fae10a15f49340a2317adc2ccff400fbb Mon Sep 17 00:00:00 2001 From: Rafael Gonzaga Date: Sat, 1 Jun 2024 10:13:12 -0300 Subject: [PATCH] src,permission: --allow-wasi & prevent WASI exec PR-URL: https://github.com/nodejs/node/pull/53124 Reviewed-By: Marco Ippolito Reviewed-By: Antoine du Hamel --- doc/api/cli.md | 50 +++++++++++++++++++ doc/api/permissions.md | 6 ++- doc/node.1 | 3 ++ lib/internal/process/pre_execution.js | 2 + node.gyp | 2 + src/env.cc | 3 ++ src/node_options.cc | 4 ++ src/node_options.h | 1 + src/node_wasi.cc | 12 +++-- src/permission/permission.cc | 5 ++ src/permission/permission.h | 1 + src/permission/permission_base.h | 3 ++ src/permission/wasi_permission.cc | 25 ++++++++++ src/permission/wasi_permission.h | 31 ++++++++++++ .../test-permission-allow-wasi-cli.js | 22 ++++++++ .../parallel/test-permission-warning-flags.js | 1 + test/parallel/test-permission-wasi.js | 19 +++++++ 17 files changed, 183 insertions(+), 7 deletions(-) create mode 100644 src/permission/wasi_permission.cc create mode 100644 src/permission/wasi_permission.h create mode 100644 test/parallel/test-permission-allow-wasi-cli.js create mode 100644 test/parallel/test-permission-wasi.js diff --git a/doc/api/cli.md b/doc/api/cli.md index 9397b4e24a348f..b983e2e0afc24b 100644 --- a/doc/api/cli.md +++ b/doc/api/cli.md @@ -269,6 +269,53 @@ Examples can be found in the [File System Permissions][] documentation. Relative paths are NOT supported through the CLI flag. +### `--allow-wasi` + + + +> Stability: 1.1 - Active development + +When using the [Permission Model][], the process will not be capable of creating +any WASI instances by default. +For security reasons, the call will throw an `ERR_ACCESS_DENIED` unless the +user explicitly passes the flag `--allow-wasi` in the main Node.js process. + +Example: + +```js +const { WASI } = require('node:wasi'); +// Attempt to bypass the permission +new WASI({ + version: 'preview1', + // Attempt to mount the whole filesystem + preopens: { + '/': '/', + }, +}); +``` + +```console +$ node --experimental-permission --allow-fs-read=* index.js +node:wasi:99 + const wrap = new _WASI(args, env, preopens, stdio); + ^ + +Error: Access to this API has been restricted + at new WASI (node:wasi:99:18) + at Object. (/home/index.js:3:1) + at Module._compile (node:internal/modules/cjs/loader:1476:14) + at Module._extensions..js (node:internal/modules/cjs/loader:1555:10) + at Module.load (node:internal/modules/cjs/loader:1288:32) + at Module._load (node:internal/modules/cjs/loader:1104:12) + at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:191:14) + at node:internal/main/run_main_module:30:49 { + code: 'ERR_ACCESS_DENIED', + permission: 'WASI', +} +``` + ### `--allow-worker`