Skip to content

Network grants: the addresses and endpoints they reach

A network grant names the hosts that a program may connect to and the endpoints that it may listen on.

With no connect line, a program may connect to any public host. When you write net.connect, only the hosts in the list are allowed. A host with a private, link-local or unspecified address is always blocked. So a host name cannot point a request into your network.

Loopback (127.0.0.1 and ::1) is allowed from the command line and in development mode. For another internal service, add its address to internal. An entry is one IP address or a range such as 172.18.0.0/16. When you write connect, the host must also be there.

net.listen is a list of address:port endpoints, and true allows every endpoint. Two ways to write the same endpoint match. An invalid entry matches nothing.

Good to know: the shared cache needs neither grant. Its address is in [cache.shared] url, and the cache.shared capability allows the connection.

Examples

The examples below run with this configuration file.

nvs.toml
# Programs may open a server on 127.0.0.1 and connect to two loopback addresses.
# Only 127.0.0.1 is listed under `internal`, so only that address is reached.
[capabilities.net]
listen = ["127.0.0.1:0"]
connect = ["127.0.0.1", "127.0.0.2"]
internal = ["127.0.0.1"]

Reaching one internal address

<?nvs
// Shows which addresses a program may connect to and listen on.
// `nvs.toml` allows two loopback addresses, and lists only `127.0.0.1` under `internal`.
// `listen` allows `127.0.0.1:0`, so the program may open a server on a free port.
Core\Net\Listener $server = Core\Net::listen('127.0.0.1', 0);
// `127.0.0.1` is in `connect` and in `internal`, so this connection opens.
// This prints `connected`.
Core\Net\Stream $client = Core\Net::connect('127.0.0.1', $server->port(), 5s);
echo "connected\n";
$client->close();
// `127.0.0.2` is in `connect`, but it is a loopback address and not in `internal`.
// So this call throws a `RuntimeError`.
try {
Core\Net::connect('127.0.0.2', $server->port(), 5s);
} catch (RuntimeError $refused) {
echo $refused->message, "\n";
}
// `listen` allows port `0` only. Port 8080 is a different endpoint, so this also throws.
try {
Core\Net::listen('127.0.0.1', 8080);
} catch (RuntimeError $refused) {
echo $refused->message, "\n";
}
$server->close();
Output
connected
Core\Net::connect cannot connect to 127.0.0.2: it is loopback (127.0.0.0/8), which `net.internal` does not except
help: add `internal = ["127.0.0.2"]` under `[capabilities.net]` in nvs.toml
Core\Net::listen needs the capability `net.listen` for 127.0.0.1:8080, which is not granted
help: add `listen = ["127.0.0.1:8080"]` under `[capabilities.net]` in nvs.toml