Extension grants: what a component may reach
An extension can read files, write files and send HTTP requests only when three parties all allow it.
The person who runs the server writes grants on the extension's [[extension]] entry in nvs.toml. read and write are lists of folders, and connect is a list of hosts. An entry without grants gives the extension no files and no hosts.
The extension's manifest lists what it asks for. If it does not ask to write files, it cannot write. nvs ext inspect prints what an extension asks for.
The program that calls the extension sets the last limit. An extension never reaches more than that program may reach.
A package that calls such an extension needs a line in [grants]. The line lists fs.read for an extension that reads, and net.connect for one that connects. An extension that writes also needs fs.write. Novis code inside an extension is a package too, and it needs its own line.
Good to know: a path with .. or a symbolic link never leaves a granted folder.
Examples
The examples below run with this configuration file.
# The extension `Geo\Atlas` is the file `atlas.nvsx`, and `sha256` is the hash of that file.# `grants` lists what the extension may reach. It may read and write the folder `data/geo/`,# and connect to two hosts.[[extension]]path = "atlas.nvsx"sha256 = "8bb2081b5d76340eb1cdc2cf22b0eaa2144bb7851b83abb5662c1b369ce9694b"grants = { read = ["data/geo/"], write = ["data/geo/"], connect = ["tiles.example.com", "maps.example.com"] }
# The programs in this folder may read the folder `data/`, write the folder `data/geo/`,# and connect to `maps.example.com`.[capabilities.fs]read = ["data/"]write = ["data/geo/"]
[capabilities.net]connect = ["maps.example.com"]
# The third program may read only the folder `data/geo/roads/`.[[app]]entry = "03-a-program-with-a-narrower-grant.nvs"
[app.capabilities.fs]read = ["data/geo/roads/"]Reading a granted folder
<?nvs// Shows an extension that reads files. `nvs.toml` lets it read the folder `data/geo/`.
use Geo\Atlas;
// `cities.txt` is in `data/geo/`, so this prints three cities.echo Atlas::read('cities.txt');
// `notes.txt` is in `data/private/`. This program may read that folder, but the extension may not.// So the call throws a `RuntimeError`, and this prints `not permitted`.try { echo Atlas::read('../private/notes.txt');} catch (RuntimeError $denied) { echo $denied->message, "\n";}ViennaGrazLinznot permittedWhat the extension asks for
<?nvs// Shows that an extension may only do what `nvs.toml`, the extension and the program all allow.
use Geo\Atlas;
// Each host below is missing from one of the three grants.// So each call throws a `RuntimeError`, and no request is sent.array<string> $hosts = [ 'tiles.example.com', // This program may not connect to it. 'maps.example.com', // The extension does not ask for it. 'example.org', // `nvs.toml` does not grant it to the extension.];foreach ($hosts as string $host) { try { int $status = Atlas::status($host); echo $host, ': ', $status, "\n"; } catch (RuntimeError $denied) { echo $host, ': ', $denied->message, "\n"; }}
// `nvs.toml` lets the extension write to `data/geo/`, but the extension does not ask to write.// So this call throws a `RuntimeError`.try { Atlas::write('visited.txt', "Vienna\n");} catch (RuntimeError $denied) { echo $denied->message, "\n";}tiles.example.com: the request was deniedmaps.example.com: the request was deniedexample.org: the request was deniedthe folder is read-onlyA program with a narrower grant
<?nvs// Shows that an extension never reaches more than the program that calls it.// `nvs.toml` lets this program read only `data/geo/roads/`, so the extension reads only there.
use Geo\Atlas;
// `main.txt` is in `data/geo/roads/`, so this prints one road.echo Atlas::read('main.txt');
// `cities.txt` is in `data/geo/`. The extension may read that folder, but this program may not.// So the call throws a `RuntimeError`.try { echo Atlas::read('../cities.txt');} catch (RuntimeError $denied) { echo $denied->message, "\n";}A1 Vienna - Salzburgnot permitted