[capabilities]
[capabilities] lists what a program may do outside its own memory.
fs.read and fs.write are for files. script.spawn starts a script, and process.exec starts a program. net.connect, net.listen and net.local are for the network. Others cover databases, LDAP directories, mail and the shared cache. file_transfer.connect lists the [file_transfer.<name>] blocks that a program may open. tls.anchors, tls.pin, tls.any_name and tls.insecure list the hosts where a TLS certificate check may skip a step.
With no [capabilities], a command-line program may use any file, connect to public hosts and loopback, and run any program. A server program may read its application folder, except nvs.toml and names that start with .. It may write only in folders from Core\IO::createTemporaryDirectory, and connect to public hosts. Both may start a script from the application folder. Every other capability needs a grant.
A grant is true for all, or a list of directories, hosts or block names. false or [] allows nothing. A capability you write replaces its default.
A call without its grant throws a RuntimeError that names the method, the capability and the argument.
Good to know: paths compare whole folder names, so data2 is not inside data. fs.read does not allow running a script.
Examples
The examples below run with this configuration file.
# These lines allow reading only the files in the `data` directory beside this file, and no# writing. Without them, a program run with `nvs run` may read and write any file.[capabilities.fs]read = ["data"]write = []Reading one directory and nothing else
<?nvs// Shows a program that may read one directory and nothing else.// `nvs.toml` grants `fs.read` for `data`, and every other file access fails.
// A relative path in quotes starts at the folder of this file.// The file is inside `data`, so this works. This prints `coffee 3.20`.echo Core\Str::trim(Core\IO::read('data/prices.txt')), "\n";
// `nvs.toml` is not inside `data`, so this throws a `RuntimeError`.try { echo Core\IO::read('nvs.toml');} catch (RuntimeError $denied) { echo "not allowed: nvs.toml\n";}
// `Core\IO::canonicalize` returns the full path of `data`.string $data = Core\IO::canonicalize('data');
// `Core\Path::join` keeps the `..`. The path is checked after `..` is removed.// It points to `nvs.toml` again, so it also fails.try { echo Core\IO::read(Core\Path::join($data, '../nvs.toml'));} catch (RuntimeError $denied) { echo "not allowed: data/../nvs.toml\n";}
// Writing needs `fs.write`. It is not granted, not even for `data`.try { Core\IO::write('data/prices.txt', "coffee 9.90\n");} catch (RuntimeError $denied) { echo "not allowed to write: data/prices.txt\n";}coffee 3.20not allowed: nvs.tomlnot allowed: data/../nvs.tomlnot allowed to write: data/prices.txtChangelog
| Version | Change |
|---|---|
0.0.6 | A [file_transfer.<name>] block names a server, and [capabilities.file_transfer] connect lists the blocks a program may open. An sftp block needs the server's host key in host_key or known_hosts_file. An ftp block allows only an anonymous sign-in, so a password is never sent unencrypted. |
capabilities
Says what a program is allowed to do. A capability is one permission, such as reading files, connecting to a host, starting a script, or using a database, a mail server or a mailbox. Each key of this block is one capability.
Some keys have a default, and the default depends on the kind of run. A command-line run, such as nvs run, reads and writes any file. Server work, such as a request, reads only the application folder. That is the folder of the file the server started from. A key with no default allows nothing until you write it.
A key you write replaces its default completely. [] allows nothing. A value can be true, or a list of the places it covers, such as the folders a program may read or the hosts it may connect to.
Good to know: a program can read its own capabilities, but it cannot change them while it runs. So nvs.toml is the one place that says what a program may do.
Only nvs.toml sets this key. A request may not change it.
A change in the file takes effect a few seconds after you save it. The server does not restart.
The examples below run with this configuration file.
# The program may read files. It may not start other scripts.[capabilities.fs]read = true
[capabilities.script]spawn = falseWhat this program is allowed to do
<?nvs// `[capabilities]` says what a program may do. A line you write replaces the default.// The `nvs.toml` beside this file sets `read = true` and `spawn = false`.
echo 'read files: ', Core\Config::get('capabilities.fs.read') ?? '(nothing)', "\n";
// The file does not write `write`, so `Core\Config::get` returns `null`.// The default still applies: a program run with `nvs run` may write any file.echo 'write files: ', Core\Config::get('capabilities.fs.write') ?? '(nothing)', "\n";
// A line can also turn a permission off. `spawn = false` means this program may not// start other scripts, so this prints `false`.echo 'run scripts: ', Core\Config::get('capabilities.script.spawn') ?? '(nothing)', "\n";
// Some permissions are lists, such as the directories a program may read.// `Core\Config::get` cannot read a list. Run `nvs config dump` to see them.
// A program can never give itself a permission.
// `Core\Config::set('capabilities.db.connect', ...)` does not compile,// because a program may not change `capabilities.db.connect`.
// This program keeps the name in a variable. The compiler does not check a variable, so `set` runs and returns `false`.string $grant = 'capabilities.db.connect';echo 'grant a db: ', Core\Config::set($grant, 'main') ? 'yes' : 'no', "\n";
// A program cannot turn off one of its own permissions either, so this prints `no` too.// Only the configuration file turns permissions on and off.string $drop = 'capabilities.fs.read';echo 'drop one: ', Core\Config::set($drop, 'false') ? 'yes' : 'no', "\n";read files: truewrite files: (nothing)run scripts: falsegrant a db: nodrop one: noChangelog
| Version | Change |
|---|---|
0.0.6 | The page of each setting says whether a request may change it with Core\Config::set. A page for a block lists the settings in it that a request may change. |
