wasm-sandbox 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +94 -0
  3. package/cli.js +121 -0
  4. package/package.json +32 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2020 N-API for Rust
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,94 @@
1
+ # wasm-sandbox
2
+
3
+ Node.js WebAssembly sandbox CLI tool built on [@wasm-sandbox/runtime](https://www.npmjs.com/package/@wasm-sandbox/runtime).
4
+
5
+ > **Note:** This CLI tool does not support running a **Core WebAssembly module** directly. You can use [`wasm-tools`](https://github.com/bytecodealliance/wasm-tools) to convert a Core wasm module into a **WebAssembly Component**.
6
+
7
+ ## Installation
8
+
9
+ ```bash
10
+ npm install -g wasm-sandbox
11
+ # or use npx
12
+ npx wasm-sandbox <command>
13
+ ```
14
+
15
+ ## Usage
16
+
17
+ ### Run WebAssembly Component
18
+
19
+ ```bash
20
+ # Basic run
21
+ wasm-sandbox run example.wasm
22
+
23
+ # Pass arguments
24
+ wasm-sandbox run example.wasm arg1 arg2 arg3
25
+
26
+ # Invoke specific function
27
+ wasm-sandbox run example.wasm 1 2 --invoke add
28
+
29
+ # Set working directory
30
+ wasm-sandbox run example.wasm --work-dir ./
31
+
32
+ # Map directory
33
+ wasm-sandbox run example.wasm --map-dir ./host/path::/guest/path
34
+
35
+ # Set environment variables
36
+ wasm-sandbox run example.wasm --env FOO=BAR --env BAZ=QUX
37
+
38
+ # Limit resources
39
+ wasm-sandbox run example.wasm --wasm-timeout 10 --wasm-max-memory-size 1073741824
40
+ ```
41
+
42
+ ### Start HTTP Server
43
+
44
+ ```bash
45
+ # Basic server
46
+ wasm-sandbox serve example.wasm
47
+
48
+ # Specify port and IP
49
+ wasm-sandbox serve example.wasm --ip 127.0.0.1 --port 3000
50
+
51
+ # Configure network access
52
+ wasm-sandbox serve example.wasm --allowed-outbound-hosts https://*.github.com
53
+
54
+ # Set config variables
55
+ wasm-sandbox serve example.wasm --config-var KEY=VALUE --keyvalue-var DATA=VALUE
56
+ ```
57
+
58
+ ## Command Options
59
+
60
+ ### run Command
61
+
62
+ | Option | Description |
63
+ |--------|-------------|
64
+ | `--program-name <name>` | Override argv[0] value |
65
+ | `--invoke <function>` | Invoke specific function |
66
+ | `--work-dir <dir>` | Grant host directory access |
67
+ | `--map-dir <mapping>` | Map host directory to guest, format: `host::guest` |
68
+ | `--env <var>` | Pass environment variable, format: `NAME=VALUE` |
69
+ | `--allowed-outbound-hosts <host>` | Allowed network destinations |
70
+ | `--block-networks <network>` | Blocked IP networks |
71
+ | `--wasm-timeout <seconds>` | Maximum execution time (seconds) |
72
+ | `--wasm-max-memory-size <bytes>` | Maximum memory size (bytes) |
73
+ | `--wasm-max-stack <bytes>` | Maximum stack size (bytes) |
74
+ | `--wasm-fuel <units>` | Execution fuel units |
75
+ | `--wasm-cache-dir <dir>` | Precompiled Wasm Component cache directory |
76
+
77
+ ### serve Command
78
+
79
+ | Option | Description |
80
+ |--------|-------------|
81
+ | `-i, --ip <ip>` | Bind IP address |
82
+ | `-p, --port <port>` | Bind port |
83
+ | `--config-var <var>` | WASI config variable, format: `NAME=VALUE` |
84
+ | `--keyvalue-var <var>` | WASI key-value API preset data |
85
+
86
+ Other options are same as `run` command.
87
+
88
+ ## License
89
+
90
+ [MIT](LICENSE)
91
+
92
+ ---
93
+
94
+ **Repository**: [https://github.com/guyoung/wasm-sandbox-node](https://github.com/guyoung/wasm-sandbox-node)
package/cli.js ADDED
@@ -0,0 +1,121 @@
1
+ #!/usr/bin/env node
2
+
3
+ import { readFileSync } from 'fs'
4
+ import { fileURLToPath } from 'url'
5
+ import { dirname, join } from 'path'
6
+
7
+ import { Command } from 'commander';
8
+ import wasm_sandbox from '@wasm-sandbox/runtime';
9
+
10
+ const __dirname = dirname(fileURLToPath(import.meta.url))
11
+ const { name, version, description } = JSON.parse(readFileSync(join(__dirname, 'package.json'), 'utf8'))
12
+
13
+ const program = new Command();
14
+
15
+ program
16
+ .name(name)
17
+ .version(version)
18
+ .description("Wasm Sandbox CLI tool.\n\n Wasm Sandbox Built on the `WebAssembly Component Model` and `WASI (WebAssembly System Interface)`, it provides `Capability-based security` to ensure your host environment remains protected.\nAllows to execute WebAssembly components in a restricted environment with granular control over file system access, network requests, environment variables, and computational resources (memory, CPU fuel, and execution time).\n\n Note: Wasm Sandbox does not support running a `Core WebAssembly module` directly. You can use `wasm-tools`(https://github.com/bytecodealliance/wasm-tools) to convert a Core wasm module into a `WebAssembly Component`")
19
+
20
+ program
21
+ .command("run")
22
+ .argument("<args...>", "WebAssembly Component file and arguments.")
23
+ .option('--program-name <name>', "Override the value of `argv[0]`, typically the name of the executable of the application being run.")
24
+ .option('--invoke <function>', "The name of the function to run ")
25
+ .option('--work-dir <dir>', "Grant access of a host directory to guest root dir.\nExample: `--work-dir ./`")
26
+ .option('--map-dir <mapping...>', "Grant access of a host directory to a guest.\nExample: `--map-dir ./usr/hello::/hello`")
27
+ .option('--env <var...>', "Pass an environment variable to the program.\nExample: `--env FOO=BAR`")
28
+ .option('--allowed-outbound-hosts <host...>', "The network destinations which the component is allowed to access.\nExample: `--allowed-outbound-hosts https://*.github.net`")
29
+ .option('--block-networks <network...>', "Set of IP networks to be blocked.\nExample: `--block-networks 1.1.1.1/32 --block-networks private`")
30
+ .option('--wasm-timeout <seconds>', "Maximum execution time of wasm code before timing out (seconds).", parseInt)
31
+ .option('--wasm-max-memory-size <bytes>', "Maximum size, in bytes, that a linear memory is allowed to reach.", parseInt)
32
+ .option('--wasm-max-stack <bytes>', "Maximum stack size, in bytes, that wasm is allowed to consume before a stack overflow is reported.", parseInt)
33
+ .option('--wasm-fuel <units>', "Enable execution fuel with N units fuel, trapping after running out of fuel.", parseInt)
34
+ .option('--wasm-cache-dir <dir>', "Precompiled WebAssembly Component as `*.cwasm` files cache dir. ")
35
+
36
+ .action(async (args, opts) => {
37
+ wasm_sandbox.run({
38
+ wasmFile: args.shift(),
39
+ args: args || [],
40
+ programName: opts.programName,
41
+ invoke: opts.invoke,
42
+ workDir: opts.workDir,
43
+ mapDirs: opts.mapDir?.map(m => {
44
+ const [key, value] = m.split('::');
45
+ return { key, value: value || key };
46
+ }),
47
+ envVars: opts.env?.map(e => {
48
+ const [key, value] = e.split('=');
49
+ return { key, value };
50
+ }),
51
+ allowedOutboundHosts: opts.allowedOutboundHosts,
52
+ blockNetworks: opts.blockNetworks,
53
+ wasmTimeout: opts.wasmTimeout,
54
+ wasmMaxMemorySize: opts.wasmMaxMemorySize,
55
+ wasmMaxWasmStack: opts.wasmMaxStack,
56
+ wasmFuel: opts.wasmFuel,
57
+ wasmCacheDir: opts.wasmCacheDir
58
+ });
59
+ });
60
+
61
+ program
62
+ .command("serve")
63
+ .description("Start http server from a WebAssembly Component.")
64
+ .argument("<wasm>", "WebAssembly Component file.")
65
+ .option('-i, --ip <ip>', "Socket ip for the web server to bind to.")
66
+ .option('-p, --port <port>', "Socket port for the web server to bind to. ", parseInt)
67
+ .option('--work-dir <dir>', "Grant access of a host directory to guest root dir.\nExample: `--work-dir ./`")
68
+ .option('--map-dir <mapping...>', "Grant access of a host directory to a guest.\nExample: `--map-dir ./usr/hello::/hello`")
69
+ .option('--env <var...>', "Pass an environment variable to the program.\nExample: `--env FOO=BAR`")
70
+ .option('--allowed-outbound-hosts <host...>', "The network destinations which the component is allowed to access.\nExample: `--allowed-outbound-hosts https://*.github.net`")
71
+ .option('--block-networks <network...>', "Set of IP networks to be blocked.\nExample: `--block-networks 1.1.1.1/32 --block-networks private`")
72
+ .option('--config-var <var...>', "Pass a wasi config variable to the program.\nExample: `--config-var FOO=BAR`")
73
+ .option('--keyvalue-var <var...>', "Preset data for the In-Memory provider of WASI key-value API.\nExample: `--keyvalue-var FOO=BAR`")
74
+ .option('--wasm-timeout <seconds>', "Maximum execution time of wasm code before timing out (seconds).", parseInt)
75
+ .option('--wasm-max-memory-size <bytes>', "Maximum size, in bytes, that a linear memory is allowed to reach.", parseInt)
76
+ .option('--wasm-max-stack <bytes>', "Maximum stack size, in bytes, that wasm is allowed to consume before a stack overflow is reported.", parseInt)
77
+ .option('--wasm-fuel <units>', "Enable execution fuel with N units fuel, trapping after running out of fuel.", parseInt)
78
+ .option('--wasm-cache-dir <dir>', "Precompiled WebAssembly Component as `*.cwasm` files cache dir. ")
79
+ .action(async (wasm, opts) => {
80
+ const mapDirs = {};
81
+ opts.mapDir?.forEach(m => {
82
+ const [key, value] = m.split('::');
83
+ mapDirs[key] = value || key;
84
+ });
85
+
86
+ const envVars = {};
87
+ opts.env?.forEach(e => {
88
+ const [key, value] = e.split('=');
89
+ envVars[key] = value;
90
+ });
91
+
92
+ const configVars = opts.configVar?.map(v => {
93
+ const [key, value] = v.split('=');
94
+ return { key, value };
95
+ });
96
+
97
+ const keyvalueVars = opts.keyvalueVar?.map(v => {
98
+ const [key, value] = v.split('=');
99
+ return { key, value };
100
+ });
101
+
102
+ wasm_sandbox.serve({
103
+ wasmFile: wasm,
104
+ ip: opts.ip,
105
+ port: opts.port,
106
+ workDir: opts.workDir,
107
+ mapDirs,
108
+ envVars,
109
+ allowedOutboundHosts: opts.allowedOutboundHosts,
110
+ blockNetworks: opts.blockNetworks,
111
+ configVars,
112
+ keyvalueVars,
113
+ wasmTimeout: opts.wasmTimeout,
114
+ wasmMaxMemorySize: opts.wasmMaxMemorySize,
115
+ wasmMaxWasmStack: opts.wasmMaxStack,
116
+ wasmFuel: opts.wasmFuel,
117
+ wasmCacheDir: opts.wasmCacheDir
118
+ });
119
+ });
120
+
121
+ program.parse();
package/package.json ADDED
@@ -0,0 +1,32 @@
1
+ {
2
+ "name": "wasm-sandbox",
3
+ "version": "1.1.0",
4
+ "description": "wasm-sandbox CLI tool",
5
+ "author": "guyoung",
6
+ "homepage": "https://github.com/guyoung/wasm-sandbox-node/tree/main/cli",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git+https://github.com/guyoung/wasm-sandbox-node.git"
10
+ },
11
+ "license": "MIT",
12
+ "keywords": [
13
+ "wasm",
14
+ "webassembly",
15
+ "wasi",
16
+ "sandbox",
17
+ "cli"
18
+ ],
19
+ "files": [
20
+ "cli.js",
21
+ "README.md",
22
+ "README_zh-CN.md"
23
+ ],
24
+ "type": "module",
25
+ "bin": {
26
+ "wasm-sandbox": "cli.js"
27
+ },
28
+ "dependencies": {
29
+ "@wasm-sandbox/runtime": "^1.1.0",
30
+ "commander": "^14.0.3"
31
+ }
32
+ }